Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements set version #7862

Merged
merged 1 commit into from Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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