From 362e73c2d4f664c592496fd9d5c835cba5ff509d Mon Sep 17 00:00:00 2001 From: Hypnosphi Date: Tue, 27 Mar 2018 03:36:17 +0300 Subject: [PATCH] CLI: add error handling for latest_version helper --- lib/cli/lib/latest_version.js | 45 ++++++++++++++++++++++++++--------- lib/cli/test/run_tests.sh | 4 ++-- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/cli/lib/latest_version.js b/lib/cli/lib/latest_version.js index d2a6860be101..ad6d8f7b3240 100644 --- a/lib/cli/lib/latest_version.js +++ b/lib/cli/lib/latest_version.js @@ -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)); + }); + }); } diff --git a/lib/cli/test/run_tests.sh b/lib/cli/test/run_tests.sh index 59e8c600d9ed..da1bf917d582 100755 --- a/lib/cli/test/run_tests.sh +++ b/lib/cli/test/run_tests.sh @@ -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