Skip to content

Commit f381c5f

Browse files
authored
Implements set version (#7862)
1 parent d2c3a48 commit f381c5f

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/cli/commands/init.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function setFlags(commander: Object) {
1919
commander.option('-y, --yes', 'use default options');
2020
commander.option('-p, --private', 'use default options and private true');
2121
commander.option('-i, --install <value>', 'install a specific Yarn release');
22+
commander.option('-2', 'generates the project using Yarn 2');
2223
}
2324

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

3031
export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
31-
if (flags.install) {
32+
const installVersion = flags[`2`] ? `berry` : flags.install;
33+
34+
if (installVersion) {
3235
const lockfilePath = path.resolve(config.cwd, 'yarn.lock');
3336
if (!await fs.exists(lockfilePath)) {
3437
await fs.writeFile(lockfilePath, '');
3538
}
36-
await child.spawn(NODE_BIN_PATH, [process.argv[1], 'policies', 'set-version', flags.install], {
39+
await child.spawn(NODE_BIN_PATH, [process.argv[1], 'policies', 'set-version', installVersion], {
3740
stdio: 'inherit',
3841
cwd: config.cwd,
3942
});

src/cli/commands/policies.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ const {run, setFlags, examples} = buildSubCommands('policies', {
114114

115115
let bundleUrl;
116116
let bundleVersion;
117+
let isV2 = false;
117118

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

147149
const bundle = await fetchBundle(config, bundleUrl);
148-
const rc = getRcConfigForFolder(config.lockfileFolder);
149150

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

156-
const rcPath = `${config.lockfileFolder}/.yarnrc`;
157-
reporter.log(`Updating ${chalk.magenta(rcPath)}...`);
158-
rc['yarn-path'] = path.relative(config.lockfileFolder, yarnPath);
159-
await fs.writeFilePreservingEol(rcPath, `${stringify(rc)}\n`);
157+
const targetPath = path.relative(config.lockfileFolder, yarnPath).replace(/\\/g, '/');
158+
159+
if (isV2) {
160+
const rcPath = `${config.lockfileFolder}/.yarnrc.yml`;
161+
reporter.log(`Updating ${chalk.magenta(rcPath)}...`);
162+
163+
await fs.writeFilePreservingEol(rcPath, `yarnPath: ${JSON.stringify(yarnPath)}\n`);
164+
} else {
165+
const rcPath = `${config.lockfileFolder}/.yarnrc`;
166+
reporter.log(`Updating ${chalk.magenta(rcPath)}...`);
167+
168+
const rc = getRcConfigForFolder(config.lockfileFolder);
169+
rc['yarn-path'] = targetPath;
170+
171+
await fs.writeFilePreservingEol(rcPath, `${stringify(rc)}\n`);
172+
}
160173

161174
reporter.log(`Done!`);
162175
},

src/cli/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ export async function main({
187187
commandName = 'install';
188188
isKnownCommand = true;
189189
}
190-
190+
if (commandName === ('set': string) && args[0] === 'version') {
191+
commandName = ('policies': string);
192+
args.splice(0, 1, 'set-version');
193+
isKnownCommand = true;
194+
}
191195
if (!isKnownCommand) {
192196
// if command is not recognized, then set default to `run`
193197
args.unshift(commandName);

0 commit comments

Comments
 (0)