Skip to content

Commit

Permalink
fix: update the command use to get package's version using CLI with Y…
Browse files Browse the repository at this point in the history
…arn 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.
  • Loading branch information
gaetanmaisse committed Apr 26, 2020
1 parent da28a4d commit cc6f706
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions lib/cli/src/latest_version.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,25 @@ function spawnVersionsWithYarn(packageName, constraint) {
* @param {Object} constraint Version range to use to constraint the returned version
* @returns {Promise<string|Array<string>>} 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`);
}
}

0 comments on commit cc6f706

Please sign in to comment.