Skip to content

Commit 11872ca

Browse files
authored
Adds support for --install in yarn init (#7723)
* Adds support for --install in yarn init * Updates the CHANGELOG
1 parent 98d51d8 commit 11872ca

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
44

55
## Master
66

7+
- Implements `yarn init --install berry`
8+
9+
[#7723](https://github.com/yarnpkg/yarn/pull/7723) - [**Maël Nison**](https://twitter.com/arcanis)
10+
11+
## 1.19.2
12+
713
- Folders like `.cache` won't be pruned from the `node_modules` after each install.
814

915
[#7699](https://github.com/yarnpkg/yarn/pull/7699) - [**Maël Nison**](https://twitter.com/arcanis)

src/cli/commands/init.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import GitHubResolver from '../../resolvers/exotics/github-resolver.js';
88
import * as child from '../../util/child.js';
99
import * as fs from '../../util/fs.js';
1010
import * as validate from '../../util/normalize-manifest/validate.js';
11+
import {NODE_BIN_PATH} from '../../constants';
1112

1213
const objectPath = require('object-path');
1314
const path = require('path');
@@ -17,6 +18,7 @@ export function setFlags(commander: Object) {
1718
commander.description('Interactively creates or updates a package.json file.');
1819
commander.option('-y, --yes', 'use default options');
1920
commander.option('-p, --private', 'use default options and private true');
21+
commander.option('-i, --install <value>', 'install a specific Yarn release');
2022
}
2123

2224
export function hasWrapper(commander: Object, args: Array<string>): boolean {
@@ -26,6 +28,26 @@ export function hasWrapper(commander: Object, args: Array<string>): boolean {
2628
export const shouldRunInCurrentCwd = true;
2729

2830
export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
31+
if (flags.install) {
32+
const lockfilePath = path.resolve(config.cwd, 'yarn.lock');
33+
if (!await fs.exists(lockfilePath)) {
34+
await fs.writeFile(lockfilePath, '');
35+
}
36+
await child.spawn(NODE_BIN_PATH, [process.argv[1], 'policies', 'set-version', flags.install], {
37+
stdio: 'inherit',
38+
cwd: config.cwd,
39+
});
40+
await child.spawn(
41+
NODE_BIN_PATH,
42+
[process.argv[1], 'init', ...(flags.yes ? ['-y'] : []), ...(flags.private ? ['-p'] : [])],
43+
{
44+
stdio: 'inherit',
45+
cwd: config.cwd,
46+
},
47+
);
48+
return;
49+
}
50+
2951
const manifests = await config.getRootManifests();
3052

3153
let repository = {};

src/cli/commands/policies.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import type {Reporter} from '../../reporters/index.js';
44
import type Config from '../../config.js';
55
import buildSubCommands from './_build-sub-commands.js';
6-
import {getRcConfigForCwd} from '../../rc.js';
6+
import {getRcConfigForFolder} from '../../rc.js';
77
import * as fs from '../../util/fs.js';
88
import {stringify} from '../../lockfile';
99

@@ -145,7 +145,7 @@ const {run, setFlags, examples} = buildSubCommands('policies', {
145145
reporter.log(`Downloading ${chalk.green(bundleUrl)}...`);
146146

147147
const bundle = await fetchBundle(config, bundleUrl);
148-
const rc = getRcConfigForCwd(config.lockfileFolder, []);
148+
const rc = getRcConfigForFolder(config.lockfileFolder);
149149

150150
const yarnPath = path.resolve(config.lockfileFolder, `.yarn/releases/yarn-${bundleVersion}.js`);
151151
reporter.log(`Saving it into ${chalk.magenta(yarnPath)}...`);

src/rc.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import {readFileSync} from 'fs';
3+
import {existsSync, readFileSync} from 'fs';
44
import {dirname, resolve} from 'path';
55

66
import commander from 'commander';
@@ -35,13 +35,23 @@ export function getRcConfigForCwd(cwd: string, args: Array<string>): {[key: stri
3535
const value = args[index + 1];
3636

3737
if (value && value.charAt(0) !== '-') {
38-
Object.assign(config, loadRcFile(readFileSync(value).toString(), value));
38+
Object.assign(config, loadRcFile(readFileSync(value, 'utf8'), value));
3939
}
4040
}
4141

4242
return config;
4343
}
4444

45+
export function getRcConfigForFolder(cwd: string): {[key: string]: string} {
46+
const filePath = resolve(cwd, '.yarnrc');
47+
if (!existsSync(filePath)) {
48+
return {};
49+
}
50+
51+
const fileText = readFileSync(filePath, 'utf8');
52+
return loadRcFile(fileText, filePath);
53+
}
54+
4555
function loadRcFile(fileText: string, filePath: string): {[key: string]: string} {
4656
let {object: values} = parse(fileText, 'yarnrc');
4757

0 commit comments

Comments
 (0)