Skip to content

Commit

Permalink
Adds support for --install in yarn init (yarnpkg#7723)
Browse files Browse the repository at this point in the history
* Adds support for --install in yarn init

* Updates the CHANGELOG
  • Loading branch information
arcanis authored and VincentBailly committed Jun 10, 2020
1 parent cdf5d24 commit 6d7696e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,12 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa

## Master

- Implements `yarn init --install berry`

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

## 1.19.2

- Folders like `.cache` won't be pruned from the `node_modules` after each install.

[#7699](https://github.com/yarnpkg/yarn/pull/7699) - [**Maël Nison**](https://twitter.com/arcanis)
Expand Down
22 changes: 22 additions & 0 deletions src/cli/commands/init.js
Expand Up @@ -8,6 +8,7 @@ import GitHubResolver from '../../resolvers/exotics/github-resolver.js';
import * as child from '../../util/child.js';
import * as fs from '../../util/fs.js';
import * as validate from '../../util/normalize-manifest/validate.js';
import {NODE_BIN_PATH} from '../../constants';

const objectPath = require('object-path');
const path = require('path');
Expand All @@ -17,6 +18,7 @@ export function setFlags(commander: Object) {
commander.description('Interactively creates or updates a package.json file.');
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');
}

export function hasWrapper(commander: Object, args: Array<string>): boolean {
Expand All @@ -26,6 +28,26 @@ 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 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], {
stdio: 'inherit',
cwd: config.cwd,
});
await child.spawn(
NODE_BIN_PATH,
[process.argv[1], 'init', ...(flags.yes ? ['-y'] : []), ...(flags.private ? ['-p'] : [])],
{
stdio: 'inherit',
cwd: config.cwd,
},
);
return;
}

const manifests = await config.getRootManifests();

let repository = {};
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/policies.js
Expand Up @@ -3,7 +3,7 @@
import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import buildSubCommands from './_build-sub-commands.js';
import {getRcConfigForCwd} from '../../rc.js';
import {getRcConfigForFolder} from '../../rc.js';
import * as fs from '../../util/fs.js';
import {stringify} from '../../lockfile';

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

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

const yarnPath = path.resolve(config.lockfileFolder, `.yarn/releases/yarn-${bundleVersion}.js`);
reporter.log(`Saving it into ${chalk.magenta(yarnPath)}...`);
Expand Down
14 changes: 12 additions & 2 deletions src/rc.js
@@ -1,6 +1,6 @@
/* @flow */

import {readFileSync} from 'fs';
import {existsSync, readFileSync} from 'fs';
import {dirname, resolve} from 'path';

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

if (value && value.charAt(0) !== '-') {
Object.assign(config, loadRcFile(readFileSync(value).toString(), value));
Object.assign(config, loadRcFile(readFileSync(value, 'utf8'), value));
}
}

return config;
}

export function getRcConfigForFolder(cwd: string): {[key: string]: string} {
const filePath = resolve(cwd, '.yarnrc');
if (!existsSync(filePath)) {
return {};
}

const fileText = readFileSync(filePath, 'utf8');
return loadRcFile(fileText, filePath);
}

function loadRcFile(fileText: string, filePath: string): {[key: string]: string} {
let {object: values} = parse(fileText, 'yarnrc');

Expand Down

0 comments on commit 6d7696e

Please sign in to comment.