2016-11-28 16:17:51 +00:00
|
|
|
/**
|
|
|
|
* 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');
|
2016-12-02 08:04:37 +00:00
|
|
|
const yarn = require('./yarn');
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
checkDeclaredVersion,
|
|
|
|
checkMatchingVersions,
|
|
|
|
checkReactPeerDependency,
|
|
|
|
checkGitAvailable,
|
2016-11-30 16:17:55 +00:00
|
|
|
checkNewVersionValid
|
2016-11-28 16:17:51 +00:00
|
|
|
} = require('./checks');
|
|
|
|
|
|
|
|
log.heading = 'git-upgrade';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promisify the callback-based shelljs function exec
|
2016-12-02 08:04:37 +00:00
|
|
|
* @param logOutput If true, log the stdout of the command.
|
2016-11-28 16:17:51 +00:00
|
|
|
* @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}`));
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:04:37 +00:00
|
|
|
function parseJsonFile(path, useYarn) {
|
|
|
|
const installHint = useYarn ?
|
|
|
|
'Make sure you ran "yarn" and that you are inside a React Native project.' :
|
|
|
|
'Make sure you ran "npm install" and that you are inside a React Native project.';
|
|
|
|
let fileContents;
|
|
|
|
try {
|
|
|
|
fileContents = fs.readFileSync(path, 'utf8');
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error('Cannot find "' + path + '". ' + installHint);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return JSON.parse(fileContents);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error('Cannot parse "' + path + '": ' + err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readPackageFiles(useYarn) {
|
2016-12-01 11:35:45 +00:00
|
|
|
const reactNativeNodeModulesPakPath = path.resolve(
|
2016-12-02 08:04:37 +00:00
|
|
|
process.cwd(), 'node_modules', 'react-native', 'package.json'
|
2016-11-28 16:17:51 +00:00
|
|
|
);
|
2016-12-01 11:35:45 +00:00
|
|
|
const reactNodeModulesPakPath = path.resolve(
|
2016-12-02 08:04:37 +00:00
|
|
|
process.cwd(), 'node_modules', 'react', 'package.json'
|
2016-12-01 11:35:45 +00:00
|
|
|
);
|
2016-11-28 16:17:51 +00:00
|
|
|
const pakPath = path.resolve(
|
2016-12-02 08:04:37 +00:00
|
|
|
process.cwd(), 'package.json'
|
2016-11-28 16:17:51 +00:00
|
|
|
);
|
2016-12-02 08:04:37 +00:00
|
|
|
return {
|
|
|
|
reactNativeNodeModulesPak: parseJsonFile(reactNativeNodeModulesPakPath),
|
|
|
|
reactNodeModulesPak: parseJsonFile(reactNodeModulesPakPath),
|
|
|
|
pak: parseJsonFile(pakPath)
|
2016-11-28 16:17:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2016-11-30 16:17:55 +00:00
|
|
|
* 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
|
2016-11-28 16:17:51 +00:00
|
|
|
*/
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:10:24 +00:00
|
|
|
/**
|
|
|
|
* If there's a newer version of react-native-git-upgrade in npm, suggest to the user to upgrade.
|
|
|
|
*/
|
|
|
|
async function checkForUpdates() {
|
2016-11-28 16:17:51 +00:00
|
|
|
try {
|
2016-11-30 16:17:55 +00:00
|
|
|
log.info('Check for updates');
|
2016-11-28 16:17:51 +00:00
|
|
|
const lastGitUpgradeVersion = await exec('npm view react-native-git-upgrade@latest version');
|
|
|
|
const current = require('./package').version;
|
|
|
|
const latest = semver.clean(lastGitUpgradeVersion);
|
2016-11-29 14:10:24 +00:00
|
|
|
if (semver.gt(latest, current)) {
|
2016-11-28 16:17:51 +00:00
|
|
|
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"'
|
|
|
|
);
|
|
|
|
}
|
2016-11-29 14:10:24 +00:00
|
|
|
} catch (err) {
|
|
|
|
log.warn('Check for latest version failed', err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:04:37 +00:00
|
|
|
/**
|
|
|
|
* If true, use yarn instead of the npm client to upgrade the project.
|
|
|
|
*/
|
|
|
|
function shouldUseYarn(cliArgs, projectDir) {
|
|
|
|
if (cliArgs && cliArgs.npm) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const yarnVersion = yarn.getYarnVersionIfAvailable();
|
|
|
|
if (yarnVersion && yarn.isProjectUsingYarn(projectDir)) {
|
|
|
|
log.info('Using yarn ' + yarnVersion);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:17:55 +00:00
|
|
|
/**
|
|
|
|
* @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');
|
2016-12-01 11:35:45 +00:00
|
|
|
let projectBackupCreated = false;
|
2016-11-29 14:10:24 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await checkForUpdates();
|
2016-11-28 16:17:51 +00:00
|
|
|
|
2016-12-02 08:04:37 +00:00
|
|
|
const useYarn = shouldUseYarn(cliArgs, path.resolve(process.cwd()));
|
|
|
|
|
2016-11-28 16:17:51 +00:00
|
|
|
log.info('Read package.json files');
|
2016-12-02 08:04:37 +00:00
|
|
|
const {reactNativeNodeModulesPak, reactNodeModulesPak, pak} = readPackageFiles(useYarn);
|
2016-11-30 16:17:55 +00:00
|
|
|
const appName = pak.name;
|
2016-12-01 11:35:45 +00:00
|
|
|
const currentVersion = reactNativeNodeModulesPak.version;
|
|
|
|
const currentReactVersion = reactNodeModulesPak.version;
|
2016-11-30 16:17:55 +00:00
|
|
|
const declaredVersion = pak.dependencies['react-native'];
|
|
|
|
const declaredReactVersion = pak.dependencies.react;
|
2016-11-28 16:17:51 +00:00
|
|
|
|
2016-11-30 16:17:55 +00:00
|
|
|
const verbose = cliArgs.verbose;
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Check declared version');
|
2016-11-30 16:17:55 +00:00
|
|
|
checkDeclaredVersion(declaredVersion);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Check matching versions');
|
2016-12-02 08:04:37 +00:00
|
|
|
checkMatchingVersions(currentVersion, declaredVersion, useYarn);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Check React peer dependency');
|
2016-11-30 16:17:55 +00:00
|
|
|
checkReactPeerDependency(currentVersion, declaredReactVersion);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
2016-11-30 16:17:55 +00:00
|
|
|
log.info('Check that Git is installed');
|
2016-11-28 16:17:51 +00:00
|
|
|
checkGitAvailable();
|
|
|
|
|
2016-12-01 11:35:45 +00:00
|
|
|
log.info('Get information from NPM registry');
|
|
|
|
const viewCommand = 'npm view react-native@' + (requestedVersion || 'latest') + ' peerDependencies.react version --json';
|
|
|
|
const viewOutput = await exec(viewCommand, verbose).then(JSON.parse);
|
|
|
|
const newVersion = viewOutput.version;
|
|
|
|
const newReactVersionRange = viewOutput['peerDependencies.react'];
|
2016-12-02 08:04:37 +00:00
|
|
|
// Print which versions we're upgrading to
|
|
|
|
log.info('Upgrading to React Native ' + newVersion + (newReactVersionRange ? ', React ' + newReactVersionRange : ''));
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Check new version');
|
2016-11-30 16:17:55 +00:00
|
|
|
checkNewVersionValid(newVersion, requestedVersion);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Setup temporary working directory');
|
2016-11-30 16:17:55 +00:00
|
|
|
await setupWorkingDir(tmpDir);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Configure Git environment');
|
2016-11-30 16:17:55 +00:00
|
|
|
configureGitEnv(tmpDir);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Init Git repository');
|
|
|
|
await exec('git init', verbose);
|
|
|
|
|
|
|
|
log.info('Add all files to commit');
|
|
|
|
await exec('git add .', verbose);
|
|
|
|
|
2016-11-30 16:17:55 +00:00
|
|
|
log.info('Commit current project sources');
|
2016-11-28 16:17:51 +00:00
|
|
|
await exec('git commit -m "Project snapshot"', verbose);
|
|
|
|
|
|
|
|
log.info ('Create a tag before updating sources');
|
|
|
|
await exec('git tag project-snapshot', verbose);
|
2016-11-30 16:17:55 +00:00
|
|
|
projectBackupCreated = true;
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Generate old version template');
|
2016-11-30 16:17:55 +00:00
|
|
|
await generateTemplates(generatorDir, appName, verbose);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
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');
|
2016-12-02 08:04:37 +00:00
|
|
|
let installCommand;
|
|
|
|
if (useYarn) {
|
|
|
|
installCommand = 'yarn add';
|
|
|
|
} else {
|
|
|
|
installCommand = 'npm install --save --color=always';
|
|
|
|
}
|
2016-12-01 11:35:45 +00:00
|
|
|
installCommand += ' react-native@' + newVersion;
|
2016-12-02 08:04:37 +00:00
|
|
|
if (newReactVersionRange && !semver.satisfies(currentReactVersion, newReactVersionRange)) {
|
2016-12-01 11:35:45 +00:00
|
|
|
// Install React as well to avoid unmet peer dependency
|
|
|
|
installCommand += ' react@' + newReactVersionRange;
|
|
|
|
}
|
|
|
|
await exec(installCommand, verbose);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Generate new version template');
|
2016-11-30 16:17:55 +00:00
|
|
|
await generateTemplates(generatorDir, appName, verbose);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
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');
|
2016-11-30 16:17:55 +00:00
|
|
|
const patchPath = path.resolve(tmpDir, `upgrade_${currentVersion}_${newVersion}.patch`);
|
|
|
|
fs.writeFileSync(patchPath, diffOutput);
|
2016-11-28 16:17:51 +00:00
|
|
|
|
|
|
|
log.info('Reset the 2 temporary commits');
|
|
|
|
await exec('git reset HEAD~2 --hard', verbose);
|
|
|
|
|
|
|
|
try {
|
|
|
|
log.info('Apply the patch');
|
2016-11-30 16:17:55 +00:00
|
|
|
await exec(`git apply --3way ${patchPath}`, true);
|
2016-11-28 16:17:51 +00:00
|
|
|
} catch (err) {
|
2016-11-30 16:17:55 +00:00
|
|
|
log.warn(
|
2016-12-02 08:04:37 +00:00
|
|
|
'The upgrade process succeeded but there might be conflicts to be resolved. ' +
|
|
|
|
'See above for the list of files that have merge conflicts.');
|
2016-11-28 16:17:51 +00:00
|
|
|
} finally {
|
|
|
|
log.info('Upgrade done');
|
2016-11-30 16:17:55 +00:00
|
|
|
if (cliArgs.verbose) {
|
|
|
|
log.info(`Temporary working directory: ${tmpDir}`);
|
2016-11-28 16:17:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
log.error('An error occurred during upgrade:');
|
|
|
|
log.error(err.stack);
|
2016-11-30 16:17:55 +00:00
|
|
|
if (projectBackupCreated) {
|
2016-11-28 16:17:51 +00:00
|
|
|
log.error('Restore initial sources');
|
|
|
|
await exec('git checkout project-snapshot', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
run: run,
|
|
|
|
};
|