Skip to content

Commit

Permalink
Implements set version (yarnpkg#7862)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis authored and VincentBailly committed Jun 10, 2020
1 parent 5f013bf commit b18ed7e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/cli/commands/init.js
Expand Up @@ -19,6 +19,7 @@ export function setFlags(commander: Object) {
commander.option('-y, --yes', 'use default options');
commander.option('-p, --private', 'use default options and private true');
commander.option('-i, --install <value>', 'install a specific Yarn release');
commander.option('-2', 'generates the project using Yarn 2');
}

export function hasWrapper(commander: Object, args: Array<string>): boolean {
Expand All @@ -28,12 +29,14 @@ export function hasWrapper(commander: Object, args: Array<string>): boolean {
export const shouldRunInCurrentCwd = true;

export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
if (flags.install) {
const installVersion = flags[`2`] ? `berry` : flags.install;

if (installVersion) {
const lockfilePath = path.resolve(config.cwd, 'yarn.lock');
if (!await fs.exists(lockfilePath)) {
await fs.writeFile(lockfilePath, '');
}
await child.spawn(NODE_BIN_PATH, [process.argv[1], 'policies', 'set-version', flags.install], {
await child.spawn(NODE_BIN_PATH, [process.argv[1], 'policies', 'set-version', installVersion], {
stdio: 'inherit',
cwd: config.cwd,
});
Expand Down
23 changes: 18 additions & 5 deletions src/cli/commands/policies.js
Expand Up @@ -114,13 +114,15 @@ const {run, setFlags, examples} = buildSubCommands('policies', {

let bundleUrl;
let bundleVersion;
let isV2 = false;

if (range === 'nightly' || range === 'nightlies') {
bundleUrl = 'https://nightly.yarnpkg.com/latest.js';
bundleVersion = 'nightly';
} else if (range === 'berry' || range === 'v2' || range === '2') {
bundleUrl = 'https://github.com/yarnpkg/berry/raw/master/packages/berry-cli/bin/berry.js';
bundleVersion = 'berry';
isV2 = true;
} else {
const releases = await fetchReleases(config, {
includePrereleases: allowRc,
Expand All @@ -145,18 +147,29 @@ const {run, setFlags, examples} = buildSubCommands('policies', {
reporter.log(`Downloading ${chalk.green(bundleUrl)}...`);

const bundle = await fetchBundle(config, bundleUrl);
const rc = getRcConfigForFolder(config.lockfileFolder);

const yarnPath = path.resolve(config.lockfileFolder, `.yarn/releases/yarn-${bundleVersion}.js`);
reporter.log(`Saving it into ${chalk.magenta(yarnPath)}...`);
await fs.mkdirp(path.dirname(yarnPath));
await fs.writeFile(yarnPath, bundle);
await fs.chmod(yarnPath, 0o755);

const rcPath = `${config.lockfileFolder}/.yarnrc`;
reporter.log(`Updating ${chalk.magenta(rcPath)}...`);
rc['yarn-path'] = path.relative(config.lockfileFolder, yarnPath);
await fs.writeFilePreservingEol(rcPath, `${stringify(rc)}\n`);
const targetPath = path.relative(config.lockfileFolder, yarnPath).replace(/\\/g, '/');

if (isV2) {
const rcPath = `${config.lockfileFolder}/.yarnrc.yml`;
reporter.log(`Updating ${chalk.magenta(rcPath)}...`);

await fs.writeFilePreservingEol(rcPath, `yarnPath: ${JSON.stringify(yarnPath)}\n`);
} else {
const rcPath = `${config.lockfileFolder}/.yarnrc`;
reporter.log(`Updating ${chalk.magenta(rcPath)}...`);

const rc = getRcConfigForFolder(config.lockfileFolder);
rc['yarn-path'] = targetPath;

await fs.writeFilePreservingEol(rcPath, `${stringify(rc)}\n`);
}

reporter.log(`Done!`);
},
Expand Down
6 changes: 5 additions & 1 deletion src/cli/index.js
Expand Up @@ -187,7 +187,11 @@ export async function main({
commandName = 'install';
isKnownCommand = true;
}

if (commandName === ('set': string) && args[0] === 'version') {
commandName = ('policies': string);
args.splice(0, 1, 'set-version');
isKnownCommand = true;
}
if (!isKnownCommand) {
// if command is not recognized, then set default to `run`
args.unshift(commandName);
Expand Down

0 comments on commit b18ed7e

Please sign in to comment.