Skip to content

Commit be559d9

Browse files
Martin KonicekFacebook Github Bot
authored andcommitted
Small wording and naming changes
Summary: Just some small changes that made the core easier for me to understand as I went through it. **Test plan (required)** $ cd react-native # "Install globally" without using Sinopia :) $ cp -r react-native-git-upgrade /usr/local/lib/node_modules/ $ cd ~ $ react-native init Zero29App --version=0.29.2 $ cd Zero29App Made a small change in `MainApplication.java` to introduce a conflict with the new template. $ git init && git add . && git commit -m "Initial commit" $ react-native-git-upgrade Worked, printed the conflicting file. Output: http://pastie.org/10972793 $ git reset --hard $ react-native-git-upgrade --verbose Worked, printed the conflicting file. Output: http://pastie.org/10972796 In both cases (with and without --verbose) the output of `git st` was http://pastie.org/10972795 Closes facebook#11197 Differential Revision: D4251008 Pulled By: mkonicek fbshipit-source-id: c5bbbeaf996cb5cb46cccc12fd1da7634cc23520
1 parent 89a380a commit be559d9

File tree

3 files changed

+58
-50
lines changed

3 files changed

+58
-50
lines changed

react-native-git-upgrade/checks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ function checkGitAvailable() {
5252
}
5353
}
5454

55-
function checkNewVersion(newVersion, requiredVersion) {
56-
if (!semver.valid(newVersion) && requiredVersion) {
55+
function checkNewVersionValid(newVersion, requestedVersion) {
56+
if (!semver.valid(newVersion) && requestedVersion) {
5757
throw new Error(
58-
'The specified version of React Native ' + requiredVersion + ' doesn\'t exist.\n' +
58+
'The specified version of React Native ' + requestedVersion + ' doesn\'t exist.\n' +
5959
'Re-run the react-native-git-upgrade command with an existing version,\n' +
6060
'for example: "react-native-git-upgrade 0.38.0",\n' +
6161
'or without arguments to upgrade to the latest: "react-native-git-upgrade".'
@@ -68,5 +68,5 @@ module.exports = {
6868
checkMatchingVersions,
6969
checkReactPeerDependency,
7070
checkGitAvailable,
71-
checkNewVersion,
71+
checkNewVersionValid,
7272
};

react-native-git-upgrade/cliEntry.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const {
2424
checkMatchingVersions,
2525
checkReactPeerDependency,
2626
checkGitAvailable,
27-
checkNewVersion
27+
checkNewVersionValid
2828
} = require('./checks');
2929

3030
log.heading = 'git-upgrade';
@@ -62,8 +62,13 @@ stdout: ${stdout}`));
6262
})
6363
}
6464

65+
/**
66+
+ * Returns two objects:
67+
+ * - Parsed node_modules/react-native/package.json
68+
+ * - Parsed package.json
69+
+ */
6570
function readPackageFiles() {
66-
const rnPakPath = path.resolve(
71+
const nodeModulesPakPath = path.resolve(
6772
process.cwd(),
6873
'node_modules',
6974
'react-native',
@@ -76,14 +81,14 @@ function readPackageFiles() {
7681
);
7782

7883
try {
79-
const rnPak = JSON.parse(fs.readFileSync(rnPakPath, 'utf8'));
84+
const nodeModulesPak = JSON.parse(fs.readFileSync(nodeModulesPakPath, 'utf8'));
8085
const pak = JSON.parse(fs.readFileSync(pakPath, 'utf8'));
8186

82-
return {rnPak, pak};
87+
return {nodeModulesPak, pak};
8388
} catch (err) {
8489
throw new Error(
85-
'Unable to find "' + pakPath + '" or "' + rnPakPath + '". Make sure that you have run ' +
86-
'"npm install" and that you are inside a React Native project.'
90+
'Unable to find "' + pakPath + '" or "' + nodeModulesPakPath + '". ' +
91+
'Make sure you ran "npm install" and that you are inside a React Native project.'
8792
)
8893
}
8994
}
@@ -106,8 +111,8 @@ function configureGitEnv(tmpDir) {
106111
/*
107112
* The workflow inits a temporary Git repository. We don't want to interfere
108113
* with an existing user's Git repository.
109-
* Thanks to Git env vars, we could address an different directory from the
110-
* default ".git". See https://git-scm.com/book/tr/v2/Git-Internals-Environment-Variables
114+
* Thanks to Git env vars, we can make Git use a different directory for its ".git" folder.
115+
* See https://git-scm.com/book/tr/v2/Git-Internals-Environment-Variables
111116
*/
112117
process.env.GIT_DIR = path.resolve(tmpDir, '.gitrn');
113118
process.env.GIT_WORK_TREE = '.';
@@ -161,7 +166,7 @@ function runYeomanGenerators(generatorDir, appName, verbose) {
161166
*/
162167
async function checkForUpdates() {
163168
try {
164-
log.info('Check for react-native-git-upgrade updates');
169+
log.info('Check for updates');
165170
const lastGitUpgradeVersion = await exec('npm view react-native-git-upgrade@latest version');
166171
const current = require('./package').version;
167172
const latest = semver.clean(lastGitUpgradeVersion);
@@ -178,66 +183,70 @@ async function checkForUpdates() {
178183
}
179184
}
180185

181-
async function run(requiredVersion, cliArgs) {
182-
const context = {
183-
tmpDir: path.resolve(os.tmpdir(), 'react-native-git-upgrade'),
184-
generatorDir: path.resolve(process.cwd(), 'node_modules', 'react-native', 'local-cli', 'generator'),
185-
requiredVersion,
186-
cliArgs,
187-
};
186+
/**
187+
* @param requestedVersion The version argument, e.g. 'react-native-git-upgrade 0.38'.
188+
* `undefined` if no argument passed.
189+
* @param cliArgs Additional arguments parsed using minimist.
190+
*/
191+
async function run(requestedVersion, cliArgs) {
192+
const tmpDir = path.resolve(os.tmpdir(), 'react-native-git-upgrade');
193+
const generatorDir = path.resolve(process.cwd(), 'node_modules', 'react-native', 'local-cli', 'generator');
188194

189195
try {
196+
let projectBackupCreated = false;
197+
190198
await checkForUpdates();
191199

192200
log.info('Read package.json files');
193-
const {rnPak, pak} = readPackageFiles();
194-
context.appName = pak.name;
195-
context.currentVersion = rnPak.version;
196-
context.declaredVersion = pak.dependencies['react-native'];
197-
context.declaredReactVersion = pak.dependencies.react;
201+
const {nodeModulesPak, pak} = readPackageFiles();
202+
const appName = pak.name;
203+
const currentVersion = nodeModulesPak.version;
204+
const declaredVersion = pak.dependencies['react-native'];
205+
const declaredReactVersion = pak.dependencies.react;
198206

199-
const verbose = context.cliArgs.verbose;
207+
const verbose = cliArgs.verbose;
200208

201209
log.info('Check declared version');
202-
checkDeclaredVersion(context.declaredVersion);
210+
checkDeclaredVersion(declaredVersion);
203211

204212
log.info('Check matching versions');
205-
checkMatchingVersions(context.currentVersion, context.declaredVersion);
213+
checkMatchingVersions(currentVersion, declaredVersion);
206214

207215
log.info('Check React peer dependency');
208-
checkReactPeerDependency(context.currentVersion, context.declaredReactVersion);
216+
checkReactPeerDependency(currentVersion, declaredReactVersion);
209217

210-
log.info('Check Git installation');
218+
log.info('Check that Git is installed');
211219
checkGitAvailable();
212220

213221
log.info('Get react-native version from NPM registry');
214-
const versionOutput = await exec('npm view react-native@' + (context.requiredVersion || 'latest') + ' version', verbose);
215-
context.newVersion = semver.clean(versionOutput);
222+
const versionOutput = await exec('npm view react-native@' + (requestedVersion || 'latest') + ' version', verbose);
223+
const newVersion = semver.clean(versionOutput);
224+
log.info('Upgrading to React Native ' + newVersion);
216225

217226
log.info('Check new version');
218-
checkNewVersion(context.newVersion, context.requiredVersion);
227+
checkNewVersionValid(newVersion, requestedVersion);
219228

220229
log.info('Setup temporary working directory');
221-
await setupWorkingDir(context.tmpDir);
230+
await setupWorkingDir(tmpDir);
222231

223232
log.info('Configure Git environment');
224-
configureGitEnv(context.tmpDir);
233+
configureGitEnv(tmpDir);
225234

226235
log.info('Init Git repository');
227236
await exec('git init', verbose);
228237

229238
log.info('Add all files to commit');
230239
await exec('git add .', verbose);
231240

232-
log.info('Commit pristine sources');
241+
log.info('Commit current project sources');
233242
await exec('git commit -m "Project snapshot"', verbose);
234243

235244
log.info ('Create a tag before updating sources');
236245
await exec('git tag project-snapshot', verbose);
237-
context.sourcesUpdated = true;
246+
projectBackupCreated = true;
238247

239248
log.info('Generate old version template');
240-
await generateTemplates(context.generatorDir, context.appName, verbose);
249+
await generateTemplates(generatorDir, appName, verbose);
241250

242251
log.info('Add updated files to commit');
243252
await exec('git add .', verbose);
@@ -246,10 +255,10 @@ async function run(requiredVersion, cliArgs) {
246255
await exec('git commit -m "Old version" --allow-empty', verbose);
247256

248257
log.info('Install the new version');
249-
await exec('npm install --save react-native@' + context.newVersion + ' --color=always', verbose);
258+
await exec('npm install --save react-native@' + newVersion + ' --color=always', verbose);
250259

251260
log.info('Generate new version template');
252-
await generateTemplates(context.generatorDir, context.appName, verbose);
261+
await generateTemplates(generatorDir, appName, verbose);
253262

254263
log.info('Add updated files to commit');
255264
await exec('git add .', verbose);
@@ -261,28 +270,30 @@ async function run(requiredVersion, cliArgs) {
261270
const diffOutput = await exec('git diff HEAD~1 HEAD', verbose);
262271

263272
log.info('Save the patch in temp directory');
264-
context.patchPath = path.resolve(context.tmpDir, `upgrade_${context.currentVersion}_${context.newVersion}.patch`);
265-
fs.writeFileSync(context.patchPath, diffOutput);
273+
const patchPath = path.resolve(tmpDir, `upgrade_${currentVersion}_${newVersion}.patch`);
274+
fs.writeFileSync(patchPath, diffOutput);
266275

267276
log.info('Reset the 2 temporary commits');
268277
await exec('git reset HEAD~2 --hard', verbose);
269278

270279
try {
271280
log.info('Apply the patch');
272-
await exec(`git apply --3way ${context.patchPath}`, true);
281+
await exec(`git apply --3way ${patchPath}`, true);
273282
} catch (err) {
274-
log.warn('The upgrade process succeeded but you may have conflicts to solve');
283+
log.warn(
284+
'The upgrade process succeeded but there might be conflicts to be resolved.\n' +
285+
'See above for the list of files that had merge conflicts.');
275286
} finally {
276287
log.info('Upgrade done');
277-
if (context.cliArgs.verbose) {
278-
log.info(`Temporary working directory: ${context.tmpDir}`);
288+
if (cliArgs.verbose) {
289+
log.info(`Temporary working directory: ${tmpDir}`);
279290
}
280291
}
281292

282293
} catch (err) {
283294
log.error('An error occurred during upgrade:');
284295
log.error(err.stack);
285-
if (context.sourcesUpdated) {
296+
if (projectBackupCreated) {
286297
log.error('Restore initial sources');
287298
await exec('git checkout project-snapshot', true);
288299
}

react-native-git-upgrade/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
*/
1111

1212
var argv = require('minimist')(process.argv.slice(2));
13-
1413
var cli = require('./cli');
1514

16-
1715
if (argv._.length === 0 && (argv.h || argv.help)) {
1816
console.log([
1917
'',
@@ -40,6 +38,5 @@ if (argv._.length === 0 && (argv.v || argv.version)) {
4038
process.exit(0);
4139
}
4240

43-
4441
cli.run(argv._[0], argv)
4542
.catch(console.error);

0 commit comments

Comments
 (0)