Skip to content

Commit 65e5699

Browse files
authored
[ENHC] Create Yarn constraints to validate node version (twentyhq#10542)
## Introduction This is PR is a suggestion ! And should be discussed With yarn `^4`, during installation won't raise an error if current dev env does not satisfies the `engines` policy. We have usually 10+ contributors support request regarding higher node version issue per week I would have preferred a very declarative integration using npm [engines](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#engines) but this does not seems to be natively supported by `yarn` We should keep in mind that this might block any machines from our CICD if they have diff node version installed ( such as running the project on a different node version could result in bugs too ) ## Implem Created a yarn [constraints](https://yarnpkg.com/features/constraints) run after each installation that checking if current node version satisfies defined engines range ( might also be done for others engines entries ) I assume we will always have the same engines policy for every packages, at least that's not a consideration from now ## Further We could refactor our package.json engines into only one using `Yarn.set` etc ## Resource - https://yarnpkg.com/configuration/yarnrc - https://yarnpkg.com/features/constraints ## Note - Not running constraints in `preInstall` hook as won't be effective on fresh install - [engine-strict](https://docs.npmjs.com/cli/v8/using-npm/config#engine-strict) is an npm-config - [devEngines](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#devengines) are npm feature too ( for instance pnpm current PR pnpm/pnpm#8153 ) ## Conclusion As always any suggestions are more than welcomed !
1 parent 6be66f2 commit 65e5699

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

.yarnrc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ enableHardenedMode: true
22

33
enableInlineHunks: true
44

5+
enableConstraintsChecks: true
6+
57
nodeLinker: node-modules
68

79
yarnPath: .yarn/releases/yarn-4.4.0.cjs

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"private": true,
23
"dependencies": {
34
"@air/react-drag-to-select": "^5.0.8",
45
"@apollo/client": "^3.7.17",
@@ -293,6 +294,7 @@
293294
"@typescript-eslint/utils": "6.21.0",
294295
"@vitejs/plugin-react-swc": "^3.5.0",
295296
"@vitest/ui": "1.4.0",
297+
"@yarnpkg/types": "^4.0.0",
296298
"chromatic": "^6.18.0",
297299
"concurrently": "^8.2.2",
298300
"cross-var": "^1.1.0",

yarn.config.cjs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @ts-check
2+
/** @type {import('@yarnpkg/types')} */
3+
const { defineConfig, Yarn } = require('@yarnpkg/types');
4+
const semver = require('semver');
5+
6+
const MONOREPO_ROOT_WORKSPACE = 'twenty';
7+
8+
module.exports = defineConfig({
9+
async constraints({ Yarn }) {
10+
const rootWorkspace = Yarn.workspace({ ident: MONOREPO_ROOT_WORKSPACE });
11+
if (!rootWorkspace) {
12+
throw new Error(
13+
`Should never occur, ${MONOREPO_ROOT_WORKSPACE} workspace not found`,
14+
);
15+
}
16+
17+
const requiredNodeVersion = rootWorkspace.manifest.engines?.node;
18+
if (!requiredNodeVersion) {
19+
throw new Error(
20+
`Should never occur, ${requiredNodeVersion} could not find node range in engines manifest`,
21+
);
22+
}
23+
24+
const currentNodeVersion = process.version;
25+
if (!semver.satisfies(currentNodeVersion, requiredNodeVersion)) {
26+
throw new Error(
27+
`Node version ${currentNodeVersion} doesn't match the required version, please use ${requiredNodeVersion}`,
28+
);
29+
}
30+
},
31+
});

yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -19925,6 +19925,15 @@ __metadata:
1992519925
languageName: node
1992619926
linkType: hard
1992719927

19928+
"@yarnpkg/types@npm:^4.0.0":
19929+
version: 4.0.0
19930+
resolution: "@yarnpkg/types@npm:4.0.0"
19931+
dependencies:
19932+
tslib: "npm:^2.4.0"
19933+
checksum: 10c0/41f67a4aa5c414c1e228f51453451fa15e0dd70c5cf2b1ae1ca142a3f018f25e4a37e60372cd0f5970c755e1804a2e31e208bff427add1cf13f899b0b9adc1e0
19934+
languageName: node
19935+
linkType: hard
19936+
1992819937
"@zapier/secret-scrubber@npm:^1.0.8":
1992919938
version: 1.1.1
1993019939
resolution: "@zapier/secret-scrubber@npm:1.1.1"
@@ -47239,6 +47248,7 @@ __metadata:
4723947248
"@vitest/ui": "npm:1.4.0"
4724047249
"@wyw-in-js/vite": "npm:^0.5.3"
4724147250
"@xyflow/react": "npm:^12.4.2"
47251+
"@yarnpkg/types": "npm:^4.0.0"
4724247252
add: "npm:^2.0.6"
4724347253
addressparser: "npm:^1.0.1"
4724447254
afterframe: "npm:^1.0.2"

0 commit comments

Comments
 (0)