mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 11:57:46 +08:00
Prettier the rest of ReactNative
Reviewed By: yungsters Differential Revision: D7974340 fbshipit-source-id: 5fe457a8a9be4bd360fc3af9acb5c1136b2be0d7
This commit is contained in:
committed by
Facebook Github Bot
parent
aba4ec0c09
commit
36fcbaa56d
29
react-native-git-upgrade/checks.js
vendored
29
react-native-git-upgrade/checks.js
vendored
@@ -3,7 +3,10 @@
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const {execSync} = require('child_process');
|
||||
@@ -12,7 +15,7 @@ const semver = require('semver');
|
||||
function checkDeclaredVersion(declaredVersion) {
|
||||
if (!declaredVersion) {
|
||||
throw new Error(
|
||||
'Your "package.json" file doesn\'t seem to have "react-native" as a dependency.'
|
||||
'Your "package.json" file doesn\'t seem to have "react-native" as a dependency.',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -20,11 +23,15 @@ function checkDeclaredVersion(declaredVersion) {
|
||||
function checkMatchingVersions(currentVersion, declaredVersion, useYarn) {
|
||||
if (!semver.satisfies(currentVersion, declaredVersion)) {
|
||||
throw new Error(
|
||||
'react-native version in "package.json" (' + declaredVersion + ') doesn\'t match ' +
|
||||
'the installed version in "node_modules" (' + currentVersion + ').\n' +
|
||||
(useYarn ?
|
||||
'Try running "yarn" to fix this.' :
|
||||
'Try running "npm install" to fix this.')
|
||||
'react-native version in "package.json" (' +
|
||||
declaredVersion +
|
||||
") doesn't match " +
|
||||
'the installed version in "node_modules" (' +
|
||||
currentVersion +
|
||||
').\n' +
|
||||
(useYarn
|
||||
? 'Try running "yarn" to fix this.'
|
||||
: 'Try running "npm install" to fix this.'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,10 +40,10 @@ function checkReactPeerDependency(currentVersion, declaredReactVersion) {
|
||||
if (semver.lt(currentVersion, '0.21.0') && !declaredReactVersion) {
|
||||
throw new Error(
|
||||
'Your "package.json" file doesn\'t seem to have "react" as a dependency.\n' +
|
||||
'"react" was changed from a dependency to a peer dependency in react-native v0.21.0.\n' +
|
||||
'Therefore, it\'s necessary to include "react" in your project\'s dependencies.\n' +
|
||||
'Please run "npm install --save react", then re-run ' +
|
||||
'"react-native upgrade".'
|
||||
'"react" was changed from a dependency to a peer dependency in react-native v0.21.0.\n' +
|
||||
'Therefore, it\'s necessary to include "react" in your project\'s dependencies.\n' +
|
||||
'Please run "npm install --save react", then re-run ' +
|
||||
'"react-native upgrade".',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -47,7 +54,7 @@ function checkGitAvailable() {
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
'"react-native-git-upgrade" requires "git" to be available in path. ' +
|
||||
'Please install Git (https://git-scm.com)"'
|
||||
'Please install Git (https://git-scm.com)"',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
7
react-native-git-upgrade/cli.js
vendored
7
react-native-git-upgrade/cli.js
vendored
@@ -3,16 +3,19 @@
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
require('babel-register')({
|
||||
presets: [
|
||||
require('babel-preset-es2015-node'),
|
||||
require('babel-preset-stage-3')
|
||||
require('babel-preset-stage-3'),
|
||||
],
|
||||
// Enable transpiling for react-native-git-upgrade AND the generator, just like the upgrade CLI command does
|
||||
only: /(react-native-git-upgrade\/(?!(node_modules)))|(local-cli\/generator)/
|
||||
only: /(react-native-git-upgrade\/(?!(node_modules)))|(local-cli\/generator)/,
|
||||
});
|
||||
|
||||
var cliEntry = require('./cliEntry');
|
||||
|
||||
139
react-native-git-upgrade/cliEntry.js
vendored
139
react-native-git-upgrade/cliEntry.js
vendored
@@ -3,7 +3,10 @@
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
@@ -36,7 +39,8 @@ log.heading = 'git-upgrade';
|
||||
*/
|
||||
function exec(command, logOutput, logger = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let stderr, stdout = '';
|
||||
let stderr,
|
||||
stdout = '';
|
||||
const child = shell.exec(command, {async: true, silent: true});
|
||||
|
||||
child.stdout.on('data', data => {
|
||||
@@ -63,22 +67,26 @@ function exec(command, logOutput, logger = null) {
|
||||
if (code === 0) {
|
||||
resolve(stdout);
|
||||
} else if (code) {
|
||||
reject(new Error(`Command '${command}' exited with code ${code}:
|
||||
reject(
|
||||
new Error(`Command '${command}' exited with code ${code}:
|
||||
stderr: ${stderr}
|
||||
stdout: ${stdout}`));
|
||||
stdout: ${stdout}`),
|
||||
);
|
||||
} else {
|
||||
reject(new Error(`Command '${command}' terminated with signal '${signal}':
|
||||
reject(
|
||||
new Error(`Command '${command}' terminated with signal '${signal}':
|
||||
stderr: ${stderr}
|
||||
stdout: ${stdout}`));
|
||||
stdout: ${stdout}`),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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.';
|
||||
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');
|
||||
@@ -94,18 +102,22 @@ function parseJsonFile(path, useYarn) {
|
||||
|
||||
function readPackageFiles(useYarn) {
|
||||
const reactNativeNodeModulesPakPath = path.resolve(
|
||||
process.cwd(), 'node_modules', 'react-native', 'package.json'
|
||||
process.cwd(),
|
||||
'node_modules',
|
||||
'react-native',
|
||||
'package.json',
|
||||
);
|
||||
const reactNodeModulesPakPath = path.resolve(
|
||||
process.cwd(), 'node_modules', 'react', 'package.json'
|
||||
);
|
||||
const pakPath = path.resolve(
|
||||
process.cwd(), 'package.json'
|
||||
process.cwd(),
|
||||
'node_modules',
|
||||
'react',
|
||||
'package.json',
|
||||
);
|
||||
const pakPath = path.resolve(process.cwd(), 'package.json');
|
||||
return {
|
||||
reactNativeNodeModulesPak: parseJsonFile(reactNativeNodeModulesPakPath),
|
||||
reactNodeModulesPak: parseJsonFile(reactNodeModulesPakPath),
|
||||
pak: parseJsonFile(pakPath)
|
||||
pak: parseJsonFile(pakPath),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -121,15 +133,16 @@ function parseInformationJsonOutput(jsonOutput, requestedVersion) {
|
||||
return {newVersion, newReactVersionRange};
|
||||
} catch (err) {
|
||||
throw new Error(
|
||||
'The specified version of React Native ' + requestedVersion + ' doesn\'t exist.\n' +
|
||||
'Re-run the react-native-git-upgrade command with an existing version,\n' +
|
||||
'for example: "react-native-git-upgrade 0.38.0",\n' +
|
||||
'or without arguments to upgrade to the latest: "react-native-git-upgrade".'
|
||||
'The specified version of React Native ' +
|
||||
requestedVersion +
|
||||
" doesn't exist.\n" +
|
||||
'Re-run the react-native-git-upgrade command with an existing version,\n' +
|
||||
'for example: "react-native-git-upgrade 0.38.0",\n' +
|
||||
'or without arguments to upgrade to the latest: "react-native-git-upgrade".',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function setupWorkingDir(tmpDir) {
|
||||
return new Promise((resolve, reject) => {
|
||||
rimraf(tmpDir, err => {
|
||||
@@ -163,7 +176,11 @@ function copyCurrentGitIgnoreFile(tmpDir) {
|
||||
*/
|
||||
try {
|
||||
const gitignorePath = path.resolve(process.cwd(), '.gitignore');
|
||||
const repoExcludePath = path.resolve(tmpDir, process.env.GIT_DIR, 'info/exclude');
|
||||
const repoExcludePath = path.resolve(
|
||||
tmpDir,
|
||||
process.env.GIT_DIR,
|
||||
'info/exclude',
|
||||
);
|
||||
const content = fs.readFileSync(gitignorePath, 'utf8');
|
||||
fs.appendFileSync(repoExcludePath, content);
|
||||
} catch (err) {
|
||||
@@ -188,7 +205,10 @@ function generateTemplates(generatorDir, appName, verbose) {
|
||||
}
|
||||
|
||||
function runCopyAndReplace(generatorDir, appName) {
|
||||
const copyProjectTemplateAndReplacePath = path.resolve(generatorDir, 'copyProjectTemplateAndReplace');
|
||||
const copyProjectTemplateAndReplacePath = path.resolve(
|
||||
generatorDir,
|
||||
'copyProjectTemplateAndReplace',
|
||||
);
|
||||
/*
|
||||
* This module is required twice during the process: for both old and new version
|
||||
* of React Native.
|
||||
@@ -201,7 +221,7 @@ function runCopyAndReplace(generatorDir, appName) {
|
||||
path.resolve(generatorDir, '..', 'templates', 'HelloWorld'),
|
||||
process.cwd(),
|
||||
appName,
|
||||
{upgrade: true, force: true}
|
||||
{upgrade: true, force: true},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -216,7 +236,9 @@ function runYeomanGenerators(generatorDir, appName, verbose) {
|
||||
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));
|
||||
return new Promise(resolve =>
|
||||
env.run(generatorArgs, {upgrade: true, force: true}, resolve),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,15 +247,17 @@ function runYeomanGenerators(generatorDir, appName, verbose) {
|
||||
async function checkForUpdates() {
|
||||
try {
|
||||
log.info('Check for updates');
|
||||
const lastGitUpgradeVersion = await exec('npm view react-native-git-upgrade@latest version');
|
||||
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"'
|
||||
`Current: ${current}\n` +
|
||||
`Latest: ${latest}\n` +
|
||||
'Please run "npm install -g react-native-git-upgrade"',
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -263,7 +287,13 @@ function shouldUseYarn(cliArgs, projectDir) {
|
||||
*/
|
||||
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');
|
||||
const generatorDir = path.resolve(
|
||||
process.cwd(),
|
||||
'node_modules',
|
||||
'react-native',
|
||||
'local-cli',
|
||||
'generator',
|
||||
);
|
||||
let projectBackupCreated = false;
|
||||
|
||||
try {
|
||||
@@ -272,7 +302,11 @@ async function run(requestedVersion, cliArgs) {
|
||||
const useYarn = shouldUseYarn(cliArgs, path.resolve(process.cwd()));
|
||||
|
||||
log.info('Read package.json files');
|
||||
const {reactNativeNodeModulesPak, reactNodeModulesPak, pak} = readPackageFiles(useYarn);
|
||||
const {
|
||||
reactNativeNodeModulesPak,
|
||||
reactNodeModulesPak,
|
||||
pak,
|
||||
} = readPackageFiles(useYarn);
|
||||
const appName = pak.name;
|
||||
const currentVersion = reactNativeNodeModulesPak.version;
|
||||
const currentReactVersion = reactNodeModulesPak.version;
|
||||
@@ -294,11 +328,19 @@ async function run(requestedVersion, cliArgs) {
|
||||
checkGitAvailable();
|
||||
|
||||
log.info('Get information from NPM registry');
|
||||
const viewCommand = 'npm view react-native@' + (requestedVersion || 'latest') + ' --json';
|
||||
const viewCommand =
|
||||
'npm view react-native@' + (requestedVersion || 'latest') + ' --json';
|
||||
const jsonOutput = await exec(viewCommand, verbose);
|
||||
const {newVersion, newReactVersionRange} = parseInformationJsonOutput(jsonOutput, requestedVersion);
|
||||
const {newVersion, newReactVersionRange} = parseInformationJsonOutput(
|
||||
jsonOutput,
|
||||
requestedVersion,
|
||||
);
|
||||
// Print which versions we're upgrading to
|
||||
log.info('Upgrading to React Native ' + newVersion + (newReactVersionRange ? ', React ' + newReactVersionRange : ''));
|
||||
log.info(
|
||||
'Upgrading to React Native ' +
|
||||
newVersion +
|
||||
(newReactVersionRange ? ', React ' + newReactVersionRange : ''),
|
||||
);
|
||||
|
||||
log.info('Setup temporary working directory');
|
||||
await setupWorkingDir(tmpDir);
|
||||
@@ -329,7 +371,10 @@ async function run(requestedVersion, cliArgs) {
|
||||
await exec('git add .', verbose);
|
||||
|
||||
log.info('Commit old version template');
|
||||
await exec('git commit -m "Old version" --allow-empty --no-verify', verbose);
|
||||
await exec(
|
||||
'git commit -m "Old version" --allow-empty --no-verify',
|
||||
verbose,
|
||||
);
|
||||
|
||||
log.info('Install the new version');
|
||||
let installCommand;
|
||||
@@ -339,7 +384,10 @@ async function run(requestedVersion, cliArgs) {
|
||||
installCommand = 'npm install --save --color=always';
|
||||
}
|
||||
installCommand += ' react-native@' + newVersion;
|
||||
if (newReactVersionRange && !semver.satisfies(currentReactVersion, newReactVersionRange)) {
|
||||
if (
|
||||
newReactVersionRange &&
|
||||
!semver.satisfies(currentReactVersion, newReactVersionRange)
|
||||
) {
|
||||
// Install React as well to avoid unmet peer dependency
|
||||
installCommand += ' react@' + newReactVersionRange;
|
||||
}
|
||||
@@ -352,13 +400,22 @@ async function run(requestedVersion, cliArgs) {
|
||||
await exec('git add .', verbose);
|
||||
|
||||
log.info('Commit new version template');
|
||||
await exec('git commit -m "New version" --allow-empty --no-verify', verbose);
|
||||
await exec(
|
||||
'git commit -m "New version" --allow-empty --no-verify',
|
||||
verbose,
|
||||
);
|
||||
|
||||
log.info('Generate the patch between the 2 versions');
|
||||
const diffOutput = await exec('git diff --binary --no-color HEAD~1 HEAD', verbose);
|
||||
const diffOutput = await exec(
|
||||
'git diff --binary --no-color HEAD~1 HEAD',
|
||||
verbose,
|
||||
);
|
||||
|
||||
log.info('Save the patch in temp directory');
|
||||
const patchPath = path.resolve(tmpDir, `upgrade_${currentVersion}_${newVersion}.patch`);
|
||||
const patchPath = path.resolve(
|
||||
tmpDir,
|
||||
`upgrade_${currentVersion}_${newVersion}.patch`,
|
||||
);
|
||||
fs.writeFileSync(patchPath, diffOutput);
|
||||
|
||||
log.info('Reset the 2 temporary commits');
|
||||
@@ -376,16 +433,16 @@ async function run(requestedVersion, cliArgs) {
|
||||
} catch (err) {
|
||||
log.warn(
|
||||
'The upgrade process succeeded but there might be conflicts to be resolved. ' +
|
||||
'See above for the list of files that have merge conflicts. ' +
|
||||
'If you don’t see the expected changes, try running:\n' +
|
||||
`git apply --reject ${patchPath}`);
|
||||
'See above for the list of files that have merge conflicts. ' +
|
||||
'If you don’t see the expected changes, try running:\n' +
|
||||
`git apply --reject ${patchPath}`,
|
||||
);
|
||||
} 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);
|
||||
|
||||
7
react-native-git-upgrade/yarn.js
vendored
7
react-native-git-upgrade/yarn.js
vendored
@@ -3,7 +3,10 @@
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const execSync = require('child_process').execSync;
|
||||
@@ -22,7 +25,9 @@ function getYarnVersionIfAvailable() {
|
||||
if (process.platform.startsWith('win')) {
|
||||
yarnVersion = (execSync('yarn --version').toString() || '').trim();
|
||||
} else {
|
||||
yarnVersion = (execSync('yarn --version 2>/dev/null').toString() || '').trim();
|
||||
yarnVersion = (
|
||||
execSync('yarn --version 2>/dev/null').toString() || ''
|
||||
).trim();
|
||||
}
|
||||
} catch (error) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user