From cc6f7060ca3e9154a7e757abc78e9cb63ba5b15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Maisse?= Date: Sun, 26 Apr 2020 17:42:32 +0200 Subject: [PATCH] fix: update the command use to get package's version using CLI with Yarn 2 Use CLI with Yarn 2 and Node 10 results in an output polluted with: `(node:87281) ExperimentalWarning: The XXX.promises API is experimental` These messages are warnings but are send to `stderr` which is causing CLI to throw an error too. With `sync` function we can check the `status` to know if command ends in error or not and process accordingly. --- lib/cli/src/latest_version.js | 40 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/cli/src/latest_version.js b/lib/cli/src/latest_version.js index aec5977f4fc3..2a803256d1fc 100644 --- a/lib/cli/src/latest_version.js +++ b/lib/cli/src/latest_version.js @@ -122,29 +122,25 @@ function spawnVersionsWithYarn(packageName, constraint) { * @param {Object} constraint Version range to use to constraint the returned version * @returns {Promise>} versions Promise resolved with a version or an array of versions */ -function spawnVersionsWithYarn2(packageName, constraint) { +async function spawnVersionsWithYarn2(packageName, constraint) { const field = constraint ? 'versions' : 'version'; - return new Promise((resolve, reject) => { - const command = spawn('yarn', ['npm', 'info', packageName, '--fields', field, '--json'], { - cwd: process.cwd(), - env: process.env, - stdio: 'pipe', - encoding: 'utf-8', - silent: true, - }); - - command.stdout.on('data', (data) => { - try { - const info = JSON.parse(data); - resolve(info[field]); - } catch (e) { - reject(new Error(`Unable to find versions of ${packageName} using yarn 2`)); - } - }); - command.stderr.on('data', (data) => { - const info = JSON.parse(data); - reject(new Error(info)); - }); + const commandResult = sync('yarn', ['npm', 'info', packageName, '--fields', field, '--json'], { + cwd: process.cwd(), + env: process.env, + stdio: 'pipe', + encoding: 'utf-8', + silent: true, }); + + if (commandResult.status !== 0) { + throw new Error(commandResult.stderr.toString()); + } + + try { + const parsedOutput = JSON.parse(commandResult.stdout.toString()); + return parsedOutput[field]; + } catch (e) { + throw new Error(`Unable to find versions of ${packageName} using yarn 2`); + } }