Skip to content

Commit

Permalink
Merge pull request #3297 from storybooks/cli-helpful-errors
Browse files Browse the repository at this point in the history
CLI: add error handling for latest_version helper
  • Loading branch information
Hypnosphi authored Mar 27, 2018
2 parents 2994fc1 + 502a9e4 commit bdbb171
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
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

0 comments on commit bdbb171

Please sign in to comment.