From b18ed7ed89b82fb1f13d620a461c92fdbde2fcb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Fri, 31 Jan 2020 17:45:12 +0100 Subject: [PATCH] Implements set version (#7862) --- src/cli/commands/init.js | 7 +++++-- src/cli/commands/policies.js | 23 ++++++++++++++++++----- src/cli/index.js | 6 +++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index eebba06d06..0576ba0942 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -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 ', 'install a specific Yarn release'); + commander.option('-2', 'generates the project using Yarn 2'); } export function hasWrapper(commander: Object, args: Array): boolean { @@ -28,12 +29,14 @@ export function hasWrapper(commander: Object, args: Array): boolean { export const shouldRunInCurrentCwd = true; export async function run(config: Config, reporter: Reporter, flags: Object, args: Array): Promise { - 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, }); diff --git a/src/cli/commands/policies.js b/src/cli/commands/policies.js index 62b8617f30..250ab8bead 100644 --- a/src/cli/commands/policies.js +++ b/src/cli/commands/policies.js @@ -114,6 +114,7 @@ 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'; @@ -121,6 +122,7 @@ const {run, setFlags, examples} = buildSubCommands('policies', { } 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, @@ -145,7 +147,6 @@ 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)}...`); @@ -153,10 +154,22 @@ const {run, setFlags, examples} = buildSubCommands('policies', { 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!`); }, diff --git a/src/cli/index.js b/src/cli/index.js index f86454a57a..5a66fc03a4 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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);