Skip to content

Commit

Permalink
feat: configure and use yarn 4
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Oct 27, 2023
1 parent d331dda commit da7fe4e
Show file tree
Hide file tree
Showing 6 changed files with 4,175 additions and 5,073 deletions.
28 changes: 0 additions & 28 deletions .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs

This file was deleted.

874 changes: 0 additions & 874 deletions .yarn/releases/yarn-3.6.4.cjs

This file was deleted.

11 changes: 7 additions & 4 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
compressionLevel: mixed

defaultSemverRangePrefix: ^

enableGlobalCache: true

enableMessageNames: false

nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"

supportedArchitectures:
cpu:
- x64
- arm64
os:
- linux
- darwin
yarnPath: .yarn/releases/yarn-3.6.4.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"engines": {
"node": ">=18.12.0"
},
"packageManager": "yarn@3.6.4",
"packageManager": "yarn@4.0.0",
"workspaces": [
"packages/*",
"@pob/*",
Expand Down
44 changes: 33 additions & 11 deletions packages/pob/lib/generators/core/yarn/CoreYarnGenerator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from 'node:fs';
import sortObject from '@pob/sort-object';
import yml from 'js-yaml';
import { lt } from 'semver';
import Generator from 'yeoman-generator';
import ensureJsonFileFormatted from '../../../utils/ensureJsonFileFormatted.js';
import inMonorepo from '../../../utils/inMonorepo.js';
Expand Down Expand Up @@ -45,9 +47,10 @@ export default class CoreYarnGenerator extends Generator {
// yarn 2 not yet installed
// https://yarnpkg.com/getting-started/install
this.spawnSync('yarn', ['set', 'version', 'stable']);
} else {
this.spawnSync('yarn', ['set', 'version', 'stable']);
ensureJsonFileFormatted(this.destinationPath('package.json'));
} else {
// disabled now that corepack is supposed to set the version used
// this.spawnSync('yarn', ['set', 'version', 'stable']);)
}
}
}
Expand Down Expand Up @@ -94,7 +97,6 @@ export default class CoreYarnGenerator extends Generator {
};

const postinstallDevPluginName = '@yarnpkg/plugin-postinstall-dev';
const workspacesPluginName = '@yarnpkg/plugin-workspace-tools';
const versionPluginName = '@yarnpkg/plugin-conventional-version';

if (!inMonorepo && !pkg.private) {
Expand All @@ -107,7 +109,6 @@ export default class CoreYarnGenerator extends Generator {
}

if (pkg.workspaces) {
installPluginIfNotInstalled(workspacesPluginName);
if (!pkg.devDependencies?.['@pob/lerna-light']) {
installPluginIfNotInstalled(
versionPluginName,
Expand All @@ -123,22 +124,43 @@ export default class CoreYarnGenerator extends Generator {
);
}

if (
!pkg.packageManager ||
!pkg.packageManager.startsWith('yarn@') ||
lt(pkg.packageManager.slice('yarn@'.length), '4.0.0')
) {
pkg.packageManager = 'yarn@4.0.0';
}

// must be done after plugins installed
const configString = this.fs.read('.yarnrc.yml');
const config = yml.load(configString, {
schema: yml.FAILSAFE_SCHEMA,
json: true,
});
if (this.options.disableYarnGitCache) {
config.compressionLevel = 'mixed'; // optimized for size
config.enableGlobalCache = true;
} else {
config.compressionLevel = 0; // optimized for github config
config.enableGlobalCache = true;
// https://yarnpkg.dev/releases/3-1/
// make sure all supported architectures are in yarn cache
config.supportedArchitectures = {
cpu: ['x64', 'arm64'],
os: ['linux', 'darwin'],
};
}
config.defaultSemverRangePrefix = this.options.type === 'app' ? '' : '^';
config.enableMessageNames = false;
config.nodeLinker = this.options.yarnNodeLinker;
// https://yarnpkg.dev/releases/3-1/
// make sure all supported architectures are in yarn cache
config.supportedArchitectures = {
cpu: ['x64', 'arm64'],
os: ['linux', 'darwin'],
};
writeAndFormat(this.fs, '.yarnrc.yml', yml.dump(config, {}));

if (config.yarnPath) {
this.fs.delete(config.yarnPath);
delete config.yarnPath;
}

writeAndFormat(this.fs, '.yarnrc.yml', yml.dump(sortObject(config), {}));
} else {
this.fs.delete('.yarn');
this.fs.delete('.yarnrc.yml');
Expand Down

0 comments on commit da7fe4e

Please sign in to comment.