Skip to content

Commit

Permalink
Merge pull request #10550 from storybookjs/fix-cli-for-yarn-2-node-10
Browse files Browse the repository at this point in the history
CLI: Fix cli when working with Yarn 2 and Node 10
  • Loading branch information
shilman authored Apr 30, 2020
2 parents f081d47 + 4f561dc commit 613a539
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions lib/cli/src/latest_version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { spawn, sync } from 'cross-spawn';
import { satisfies } from 'semver';

/**
* Get latest version of the package available on npmjs.com.
* Get the latest version of the package available on npmjs.com.
* If constraint is set then it returns a version satisfying it, otherwise the latest version available is returned.
*
* @param {Object} npmOptions Object containing a `useYarn: boolean` attribute
* @param {string} packageName Name of the package
* @param {Object} constraint Version range to use to constraint the returned version
* @return {Promise<string>} Promise resolved with a version
*/
export function latestVersion(npmOptions, packageName, constraint) {
let getPackageVersions;
export async function latestVersion(npmOptions, packageName, constraint) {
let versions;

// TODO: Refactor things to hide the package manager details:
// Create a `PackageManager` interface that expose some functions like `version`, `add` etc
Expand All @@ -23,19 +23,19 @@ export function latestVersion(npmOptions, packageName, constraint) {
.replace(/"/g, '');

if (/^1\.+/.test(yarnVersion)) {
getPackageVersions = spawnVersionsWithYarn(packageName, constraint);
versions = await spawnVersionsWithYarn(packageName, constraint);
} else {
getPackageVersions = spawnVersionsWithYarn2(packageName, constraint);
versions = await spawnVersionsWithYarn2(packageName, constraint);
}
} else {
getPackageVersions = spawnVersionsWithNpm(packageName, constraint);
versions = await spawnVersionsWithNpm(packageName, constraint);
}

return getPackageVersions.then((versions) => {
if (!constraint) return versions;
if (!constraint) {
return versions;
}

return versions.reverse().find((version) => satisfies(version, constraint));
});
return versions.reverse().find((version) => satisfies(version, constraint));
}

/**
Expand Down 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 613a539

Please sign in to comment.