Martin Konicek be559d9760 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 https://github.com/facebook/react-native/pull/11197

Differential Revision: D4251008

Pulled By: mkonicek

fbshipit-source-id: c5bbbeaf996cb5cb46cccc12fd1da7634cc23520
2016-11-30 08:28:33 -08:00

306 lines
9.3 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const shell = require('shelljs');
const Promise = require('promise');
const yeoman = require('yeoman-environment');
const TerminalAdapter = require('yeoman-environment/lib/adapter');
const log = require('npmlog');
const rimraf = require('rimraf');
const semver = require('semver');
const {
checkDeclaredVersion,
checkMatchingVersions,
checkReactPeerDependency,
checkGitAvailable,
checkNewVersionValid
} = require('./checks');
log.heading = 'git-upgrade';
/**
* Promisify the callback-based shelljs function exec
* @param command
* @param logOutput
* @returns {Promise}
*/
function exec(command, logOutput) {
return new Promise((resolve, reject) => {
let stderr, stdout = '';
const child = shell.exec(command, {async: true, silent: true});
child.stdout.on('data', data => {
stdout += data;
if (logOutput) {
process.stdout.write(data);
}
});
child.stderr.on('data', data => {
stderr += data;
process.stderr.write(data);
});
child.on('exit', code => {
(code === 0)
? resolve(stdout)
: reject(new Error(`Command '${command}' exited with code ${code}:
stderr: ${stderr}
stdout: ${stdout}`));
});
})
}
/**
+ * Returns two objects:
+ * - Parsed node_modules/react-native/package.json
+ * - Parsed package.json
+ */
function readPackageFiles() {
const nodeModulesPakPath = path.resolve(
process.cwd(),
'node_modules',
'react-native',
'package.json'
);
const pakPath = path.resolve(
process.cwd(),
'package.json'
);
try {
const nodeModulesPak = JSON.parse(fs.readFileSync(nodeModulesPakPath, 'utf8'));
const pak = JSON.parse(fs.readFileSync(pakPath, 'utf8'));
return {nodeModulesPak, pak};
} catch (err) {
throw new Error(
'Unable to find "' + pakPath + '" or "' + nodeModulesPakPath + '". ' +
'Make sure you ran "npm install" and that you are inside a React Native project.'
)
}
}
function setupWorkingDir(tmpDir) {
return new Promise((resolve, reject) => {
rimraf(tmpDir, err => {
if (err) {
reject(err);
} else {
fs.mkdirSync(tmpDir);
resolve();
}
});
});
}
function configureGitEnv(tmpDir) {
/*
* The workflow inits a temporary Git repository. We don't want to interfere
* with an existing user's Git repository.
* Thanks to Git env vars, we can make Git use a different directory for its ".git" folder.
* See https://git-scm.com/book/tr/v2/Git-Internals-Environment-Variables
*/
process.env.GIT_DIR = path.resolve(tmpDir, '.gitrn');
process.env.GIT_WORK_TREE = '.';
}
function generateTemplates(generatorDir, appName, verbose) {
try {
const yeomanGeneratorEntryPoint = path.resolve(generatorDir, 'index.js');
// Try requiring the index.js (entry-point of Yeoman generators)
fs.accessSync(yeomanGeneratorEntryPoint);
return runYeomanGenerators(generatorDir, appName, verbose);
} catch(err) {
return runCopyAndReplace(generatorDir, appName);
}
}
function runCopyAndReplace(generatorDir, appName) {
const copyProjectTemplateAndReplacePath = path.resolve(generatorDir, 'copyProjectTemplateAndReplace');
/*
* This module is required twice during the process: for both old and new version
* of React Native.
* This file could have changed between these 2 versions. When generating the new template,
* we don't want to load the old version of the generator from the cache
*/
delete require.cache[copyProjectTemplateAndReplacePath];
const copyProjectTemplateAndReplace = require(copyProjectTemplateAndReplacePath);
copyProjectTemplateAndReplace(
path.resolve(generatorDir, '..', 'templates', 'HelloWorld'),
process.cwd(),
appName,
{upgrade: true, force: true}
);
}
function runYeomanGenerators(generatorDir, appName, verbose) {
if (!verbose) {
// Yeoman output needs monkey-patching (no silent option)
TerminalAdapter.prototype.log = () => {};
TerminalAdapter.prototype.log.force = () => {};
TerminalAdapter.prototype.log.create = () => {};
}
const env = yeoman.createEnv();
env.register(generatorDir, 'react:app');
const generatorArgs = ['react:app', appName];
return new Promise((resolve) => env.run(generatorArgs, {upgrade: true, force: true}, resolve));
}
/**
* If there's a newer version of react-native-git-upgrade in npm, suggest to the user to upgrade.
*/
async function checkForUpdates() {
try {
log.info('Check for updates');
const lastGitUpgradeVersion = await exec('npm view react-native-git-upgrade@latest version');
const current = require('./package').version;
const latest = semver.clean(lastGitUpgradeVersion);
if (semver.gt(latest, current)) {
log.warn(
'A more recent version of "react-native-git-upgrade" has been found.\n' +
`Current: ${current}\n` +
`Latest: ${latest}\n` +
'Please run "npm install -g react-native-git-upgrade"'
);
}
} catch (err) {
log.warn('Check for latest version failed', err.message);
}
}
/**
* @param requestedVersion The version argument, e.g. 'react-native-git-upgrade 0.38'.
* `undefined` if no argument passed.
* @param cliArgs Additional arguments parsed using minimist.
*/
async function run(requestedVersion, cliArgs) {
const tmpDir = path.resolve(os.tmpdir(), 'react-native-git-upgrade');
const generatorDir = path.resolve(process.cwd(), 'node_modules', 'react-native', 'local-cli', 'generator');
try {
let projectBackupCreated = false;
await checkForUpdates();
log.info('Read package.json files');
const {nodeModulesPak, pak} = readPackageFiles();
const appName = pak.name;
const currentVersion = nodeModulesPak.version;
const declaredVersion = pak.dependencies['react-native'];
const declaredReactVersion = pak.dependencies.react;
const verbose = cliArgs.verbose;
log.info('Check declared version');
checkDeclaredVersion(declaredVersion);
log.info('Check matching versions');
checkMatchingVersions(currentVersion, declaredVersion);
log.info('Check React peer dependency');
checkReactPeerDependency(currentVersion, declaredReactVersion);
log.info('Check that Git is installed');
checkGitAvailable();
log.info('Get react-native version from NPM registry');
const versionOutput = await exec('npm view react-native@' + (requestedVersion || 'latest') + ' version', verbose);
const newVersion = semver.clean(versionOutput);
log.info('Upgrading to React Native ' + newVersion);
log.info('Check new version');
checkNewVersionValid(newVersion, requestedVersion);
log.info('Setup temporary working directory');
await setupWorkingDir(tmpDir);
log.info('Configure Git environment');
configureGitEnv(tmpDir);
log.info('Init Git repository');
await exec('git init', verbose);
log.info('Add all files to commit');
await exec('git add .', verbose);
log.info('Commit current project sources');
await exec('git commit -m "Project snapshot"', verbose);
log.info ('Create a tag before updating sources');
await exec('git tag project-snapshot', verbose);
projectBackupCreated = true;
log.info('Generate old version template');
await generateTemplates(generatorDir, appName, verbose);
log.info('Add updated files to commit');
await exec('git add .', verbose);
log.info('Commit old version template');
await exec('git commit -m "Old version" --allow-empty', verbose);
log.info('Install the new version');
await exec('npm install --save react-native@' + newVersion + ' --color=always', verbose);
log.info('Generate new version template');
await generateTemplates(generatorDir, appName, verbose);
log.info('Add updated files to commit');
await exec('git add .', verbose);
log.info('Commit new version template');
await exec('git commit -m "New version" --allow-empty', verbose);
log.info('Generate the patch between the 2 versions');
const diffOutput = await exec('git diff HEAD~1 HEAD', verbose);
log.info('Save the patch in temp directory');
const patchPath = path.resolve(tmpDir, `upgrade_${currentVersion}_${newVersion}.patch`);
fs.writeFileSync(patchPath, diffOutput);
log.info('Reset the 2 temporary commits');
await exec('git reset HEAD~2 --hard', verbose);
try {
log.info('Apply the patch');
await exec(`git apply --3way ${patchPath}`, true);
} catch (err) {
log.warn(
'The upgrade process succeeded but there might be conflicts to be resolved.\n' +
'See above for the list of files that had merge conflicts.');
} finally {
log.info('Upgrade done');
if (cliArgs.verbose) {
log.info(`Temporary working directory: ${tmpDir}`);
}
}
} catch (err) {
log.error('An error occurred during upgrade:');
log.error(err.stack);
if (projectBackupCreated) {
log.error('Restore initial sources');
await exec('git checkout project-snapshot', true);
}
}
}
module.exports = {
run: run,
};