Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: add error handling for latest_version helper #3297

Merged
merged 2 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions lib/cli/lib/latest_version.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import { sync as spawnSync } from 'cross-spawn';
import { spawn } from 'cross-spawn';
import hasYarn from './has_yarn';

const packageManager = hasYarn() ? 'yarn' : 'npm';

export default async function latestVersion(packageName) {
const result = spawnSync(packageManager, ['info', packageName, '--json'], {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
encoding: 'utf-8',
silent: true,
});
export default function latestVersion(packageName) {
return new Promise((resolve, reject) => {
const command = spawn(packageManager, ['info', packageName, 'version', '--json', '--silent'], {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
encoding: 'utf-8',
silent: true,
});

command.stdout.on('data', data => {
try {
const info = JSON.parse(data);
if (info.error) {
// npm error
reject(new Error(info.error.summary));
} else if (info.type === 'inspect') {
// yarn success
resolve(info.data);
} else {
// npm success
resolve(info);
}
} catch (e) {
// yarn info output
}
});

const info = JSON.parse(result.output[1].toString());
return info.data.version;
command.stderr.on('data', data => {
// yarn error
const info = JSON.parse(data);
reject(new Error(info.data));
});
});
}
4 changes: 2 additions & 2 deletions lib/cli/test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ cd ..
if [ $update -eq 1 ]
then
# copy `run` directory contents to `snapshots`, skipping irrelevant files
rsync -r --exclude={node_modules**,.DS_Store,*.md} run/ snapshots
rsync -r --exclude={node_modules**,.DS_Store,*.md,yarn-error.log} run/ snapshots
else if [ $skip -eq 0 ]
then
# check if there is any difference between `run` and `snapshots` directories,
# skipping irrelevant files
declare diff=`diff -r -x node_modules** -x .DS_Store -x *.md run snapshots`
declare diff=`diff -r -x node_modules** -x .DS_Store -x *.md -x yarn-error.log run snapshots`
if [[ $diff ]]
then
# if there is some diff, output it to stderr along with a clarifying message
Expand Down