Skip to content

Commit

Permalink
feat: rearrange the output a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Feb 18, 2019
1 parent 368b96f commit 577727c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 60 deletions.
19 changes: 14 additions & 5 deletions packages/cli/src/upgrade/__tests__/upgrade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ test('fetches empty patch and installs deps', async () => {
expect(flushOutput()).toMatchInlineSnapshot(`
"info Fetching diff between v0.57.8 and v0.58.4...
info Diff has no changes to apply, proceeding further
warn Continuing after failure. Most of the files are upgraded but you will need to deal with some conflicts manually
info Installing react-native@0.58.4 and its peer dependencies...
$ execa npm info react-native@0.58.4 peerDependencies --json
$ yarn add react-native@0.58.4 react@16.6.3
Expand Down Expand Up @@ -172,7 +173,14 @@ test('cleans up if patching fails,', async () => {
return Promise.resolve({ stdout: '' });
});

await upgrade.func([newVersion], ctx, opts);
try {
await upgrade.func([newVersion], ctx, opts);
} catch (error) {
expect(error.message).toBe(
'Upgrade failed. Please see the messages above for details'
);
}

expect(flushOutput()).toMatchInlineSnapshot(`
"info Fetching diff between v0.57.8 and v0.58.4...
[fs] write tmp-upgrade-rn.patch
Expand All @@ -181,12 +189,12 @@ $ execa git fetch --no-tags tmp-rn-diff-purge
$ execa git apply --check tmp-upgrade-rn.patch --exclude=package.json -p2 --3way
info Applying diff (excluding: package.json, .flowconfig)...
$ execa git apply tmp-upgrade-rn.patch --exclude=package.json --exclude=.flowconfig -p2 --3way
error: .flowconfig: does not exist in index

error Automatically applying diff failed. Please run \\"git diff\\", review the conflicts and resolve them
error: .flowconfig: does not exist in index
error Automatically applying diff failed
info Here's the diff we tried to apply: https://github.com/pvinis/rn-diff-purge/compare/version/0.57.8...version/0.58.4
info You may find release notes helpful: https://github.com/facebook/react-native/releases/tag/v0.58.4
[fs] unlink tmp-upgrade-rn.patch
warn Continuing after failure. Most of the files are upgraded but you will need to deal with some conflicts manually
info Installing react-native@0.58.4 and its peer dependencies...
$ execa npm info react-native@0.58.4 peerDependencies --json
$ yarn add react-native@0.58.4 react@16.6.3
Expand All @@ -195,6 +203,7 @@ $ execa git add yarn.lock
$ execa git add package-lock.json
info Running \\"git status\\" to check what changed...
$ execa git status
$ execa git remote remove tmp-rn-diff-purge"
$ execa git remote remove tmp-rn-diff-purge
warn Please run \\"git diff\\" to review the conflicts and resolve them"
`);
});
125 changes: 70 additions & 55 deletions packages/cli/src/upgrade/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ const getVersionToUpgradeTo = async (argv, currentVersion, projectDir) => {
return newVersion;
};

const installDeps = async (newVersion, projectDir) => {
const installDeps = async (newVersion, projectDir, patchSuccess) => {
if (!patchSuccess) {
logger.warn(
'Continuing after failure. Most of the files are upgraded but you will need to deal with some conflicts manually'
);
}
logger.info(
`Installing react-native@${newVersion} and its peer dependencies...`
);
Expand All @@ -114,7 +119,7 @@ const installDeps = async (newVersion, projectDir) => {
`react-native@${newVersion}`,
...Object.keys(peerDeps).map(module => `${module}@${peerDeps[module]}`),
];
await pm.install(deps);
await pm.install(deps, { silent: true });
await execa('git', ['add', 'package.json']);
try {
await execa('git', ['add', 'yarn.lock']);
Expand All @@ -128,6 +133,54 @@ const installDeps = async (newVersion, projectDir) => {
}
};

const applyPatch = async (
currentVersion: string,
newVersion: string,
tmpPatchFile: string
) => {
let filesToExclude = ['package.json'];
try {
try {
const excludes = filesToExclude.map(e => `--exclude=${e}`);
await execa('git', [
'apply',
'--check',
tmpPatchFile,
...excludes,
'-p2',
'--3way',
]);
logger.info(`Applying diff...`);
} catch (error) {
filesToExclude = [
...filesToExclude,
...error.stderr
.split('\n')
.filter(x => x.includes('does not exist in index'))
.map(x => x.replace(/^error: (.*): does not exist in index$/, '$1')),
].filter(Boolean);

logger.info(`Applying diff (excluding: ${filesToExclude.join(', ')})...`);
} finally {
const excludes = filesToExclude.map(e => `--exclude=${e}`);
await execa('git', ['apply', tmpPatchFile, ...excludes, '-p2', '--3way']);
}
} catch (error) {
if (error.stderr) {
logger.log(`${chalk.dim(error.stderr.trim())}`);
}
logger.error('Automatically applying diff failed');
logger.info(
`Here's the diff we tried to apply: ${rnDiffPurgeUrl}/compare/version/${currentVersion}...version/${newVersion}`
);
logger.info(
`You may find release notes helpful: https://github.com/facebook/react-native/releases/tag/v${newVersion}`
);
return false;
}
return true;
};

/**
* Upgrade application to a new version of React Native.
*/
Expand Down Expand Up @@ -169,62 +222,14 @@ async function upgrade(argv: Array<string>, ctx: ContextT, args: FlagsT) {
return;
}

try {
let filesToExclude = ['package.json'];
let patchSuccess;

try {
fs.writeFileSync(tmpPatchFile, patch);
await execa('git', ['remote', 'add', tmpRemote, rnDiffGitAddress]);
await execa('git', ['fetch', '--no-tags', tmpRemote]);

try {
try {
const excludes = filesToExclude.map(e => `--exclude=${e}`);
await execa('git', [
'apply',
'--check',
tmpPatchFile,
...excludes,
'-p2',
'--3way',
]);
logger.info(`Applying diff...`);
} catch (error) {
filesToExclude = [
...filesToExclude,
...error.stderr
.split('\n')
.filter(x => x.includes('does not exist in index'))
.map(x =>
x.replace(/^error: (.*): does not exist in index$/, '$1')
),
].filter(Boolean);

logger.info(
`Applying diff (excluding: ${filesToExclude.join(', ')})...`
);
} finally {
const excludes = filesToExclude.map(e => `--exclude=${e}`);
await execa('git', [
'apply',
tmpPatchFile,
...excludes,
'-p2',
'--3way',
]);
}
} catch (error) {
if (error.stderr) {
logger.log(chalk.dim(error.stderr));
}
logger.error(
'Automatically applying diff failed. Please run "git diff", review the conflicts and resolve them'
);
logger.info(
`Here's the diff we tried to apply: ${rnDiffPurgeUrl}/compare/version/${currentVersion}...version/${newVersion}`
);
logger.info(
`You may find release notes helpful: https://github.com/facebook/react-native/releases/tag/v${newVersion}`
);
patchSuccess = await applyPatch(currentVersion, newVersion, tmpPatchFile);
if (!patchSuccess) {
return;
}
} catch (error) {
Expand All @@ -235,10 +240,20 @@ async function upgrade(argv: Array<string>, ctx: ContextT, args: FlagsT) {
} catch (e) {
// ignore
}
await installDeps(newVersion, projectDir);
await installDeps(newVersion, projectDir, patchSuccess);
logger.info('Running "git status" to check what changed...');
await execa('git', ['status'], { stdio: 'inherit' });
await execa('git', ['remote', 'remove', tmpRemote]);

if (!patchSuccess) {
logger.warn(
'Please run "git diff" to review the conflicts and resolve them'
);
// eslint-disable-next-line no-unsafe-finally
throw new Error(
'Upgrade failed. Please see the messages above for details'
);
}
}

logger.success(
Expand Down

0 comments on commit 577727c

Please sign in to comment.