Skip to content

Commit

Permalink
fix: init uses yarn assuming version > 1.x
Browse files Browse the repository at this point in the history
Checks that we're using yarn > 1.x when calling `yarn set` which isn't
supported [1] but is for later versions [2]. Update tests to support
environment change differences between yarn 1.x & 3.x.

[1] https://classic.yarnpkg.com/en/docs/cli/set
[2] https://yarnpkg.com/cli/set/version
  • Loading branch information
blakef committed Mar 18, 2024
1 parent b868db6 commit 4f84ea8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 16 additions & 3 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import fs from 'fs';
import path from 'path';
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {execSync} from 'child_process';
import semver from 'semver';
import slash from 'slash';

const DIR = getTempDirectory('command-init');
const PROJECT_NAME = 'TestInit';

const yarnVersion = semver.parse(

Check failure on line 11 in __e2e__/init.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎··execSync('yarn·--version').toString().trim()⏎` with `execSync('yarn·--version').toString().trim()`
execSync('yarn --version').toString().trim()
)!;

const yarnConfigFiles: string[] = [];
if (yarnVersion.major >= 3) {
yarnConfigFiles.push(

Check failure on line 17 in __e2e__/init.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎····'.yarnrc.yml',⏎····'.yarn',⏎··` with `'.yarnrc.yml',·'.yarn'`
'.yarnrc.yml',
'.yarn',
);
}

function createCustomTemplateFiles() {
writeFiles(DIR, {
'custom/template/template.config.js': `module.exports = {
Expand All @@ -22,8 +36,7 @@ function createCustomTemplateFiles() {

const customTemplateCopiedFiles = [
'.git',
'.yarn',
'.yarnrc.yml',
...yarnConfigFiles,
'dir',
'file',
'node_modules',
Expand Down Expand Up @@ -178,7 +191,7 @@ test('init uses npm as the package manager with --npm', () => {
// Remove yarn specific files and node_modules
const filteredFiles = customTemplateCopiedFiles.filter(
(file) =>

Check failure on line 193 in __e2e__/init.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎·····`
!['yarn.lock', 'node_modules', '.yarnrc.yml', '.yarn'].includes(file),
!['yarn.lock', 'node_modules', ...yarnConfigFiles].includes(file),
);

// Add package-lock.json
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const bumpYarnVersion = async (silent: boolean, root: string) => {
try {
let yarnVersion = semver.parse(getYarnVersionIfAvailable());

if (yarnVersion) {
// `yarn set` is unsupported in 1.x
if (yarnVersion && yarnVersion.major > 1) {
await executeCommand('yarn', ['set', 'version', YARN_VERSION], {
root,
silent,
Expand Down

0 comments on commit 4f84ea8

Please sign in to comment.