๐Ÿ“ฆ facebook / react-native

๐Ÿ“„ set-rn-artifacts-version.js ยท 173 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 * @format
 */

/*::
import type {BuildType, Version} from './utils/version-utils';
*/

const {getNpmInfo} = require('../releases/utils/npm-utils');
const {REPO_ROOT} = require('../shared/consts');
const {parseVersion, validateBuildType} = require('./utils/version-utils');
const {promises: fs} = require('fs');
const path = require('path');
const {parseArgs} = require('util');

const GRADLE_FILE_PATH = path.join(
  REPO_ROOT,
  'packages/react-native/ReactAndroid/gradle.properties',
);

const config = {
  options: {
    'build-type': {
      type: 'string',
      short: 'b',
    },
    'to-version': {
      type: 'string',
      short: 'v',
    },
    help: {type: 'boolean'},
  },
};

/**
 * @deprecated This script entry point is deprecated. Please use `set-version`
 * instead.
 */
async function main() {
  const {
    values: {help, 'build-type': buildType, 'to-version': toVersion},
    /* $FlowFixMe[incompatible-type] Natural Inference rollout. See
     * https://fburl.com/workplace/6291gfvu */
  } = parseArgs(config);

  if (help) {
    console.log(`
  Usage: node ./scripts/releases/set-rn-artifacts-version.js [OPTIONS]

  Updates relevant native files in the react-native package to materialize
  the given release version. This does not update package.json.

  Options:
    --build-type       One of ['dry-run', 'nightly', 'release'].
    --to-version       The new version string.
    `);
    return;
  }

  if (!validateBuildType(buildType)) {
    throw new Error(`Unsupported build type: ${buildType}`);
  }

  await updateReactNativeArtifacts(
    toVersion ?? getNpmInfo(buildType).version,
    buildType,
  );
}

async function updateReactNativeArtifacts(
  version /*: string */,
  buildType /*: ?BuildType */,
) {
  const versionInfo = parseVersion(version, buildType);

  await updateSourceFiles(versionInfo);
  await updateTestFiles(versionInfo);
  await updateGradleFile(versionInfo.version);
}

function updateSourceFiles(
  versionInfo /*: Version */,
) /*: Promise<Array<void>>*/ {
  const templateData = {version: versionInfo};

  return Promise.all([
    fs.writeFile(
      path.join(
        REPO_ROOT,
        'packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.kt',
      ),
      require('./templates/ReactNativeVersion.kt-template')(templateData),
    ),
    fs.writeFile(
      path.join(REPO_ROOT, 'packages/react-native/React/Base/RCTVersion.m'),
      require('./templates/RCTVersion.m-template')(templateData),
    ),
    fs.writeFile(
      path.join(
        REPO_ROOT,
        'packages/react-native/ReactCommon/cxxreact/ReactNativeVersion.h',
      ),
      require('./templates/ReactNativeVersion.h-template')(templateData),
    ),
    fs.writeFile(
      path.join(
        REPO_ROOT,
        'packages/react-native/Libraries/Core/ReactNativeVersion.js',
      ),
      require('./templates/ReactNativeVersion.js-template')(templateData),
    ),
  ]);
}

function updateTestFiles(
  versionInfo /*: Version */,
) /*: Promise<Array<void>>*/ {
  const oldVersion = /"\d+\.\d+\.\d+(-rc\.\d+)?\\/g;
  const newVersion = `"${versionInfo.version}\\`;

  const snapshotTestPath = path.join(
    __dirname,
    '..',
    '..',
    'packages',
    'react-native',
    'scripts',
    'codegen',
    '__tests__',
    '__snapshots__',
    'generate-artifacts-executor-test.js.snap',
  );

  const promise /*: Promise<void> */ = new Promise(async (resolve, reject) => {
    try {
      let snapshot = String(await fs.readFile(snapshotTestPath, 'utf8')).trim();
      // Replace all occurrences of the old version pattern with the new version
      snapshot = snapshot.replaceAll(oldVersion, newVersion);
      await fs.writeFile(snapshotTestPath, snapshot, {encoding: 'utf8'});
      resolve();
    } catch (error) {
      reject(error);
    }
  });

  return Promise.all([promise]);
}

async function updateGradleFile(version /*: string */) /*: Promise<void> */ {
  const contents = await fs.readFile(GRADLE_FILE_PATH, 'utf-8');

  return fs.writeFile(
    GRADLE_FILE_PATH,
    contents.replace(/^VERSION_NAME=.*/, `VERSION_NAME=${version}`),
  );
}

module.exports = {
  updateReactNativeArtifacts,
  updateGradleFile,
  updateSourceFiles,
};

if (require.main === module) {
  void main();
}