-
Notifications
You must be signed in to change notification settings - Fork 25
/
.pnpmfile.cjs
99 lines (92 loc) · 2.85 KB
/
.pnpmfile.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const ALLOWED_DEPENDENCIES = {
'@kadena/js-monorepo': [
'@changesets/cli',
'@kadena-dev/markdown',
'buffer',
'jiti',
'knip',
'only-allow',
'prettier-plugin-organize-imports',
'prettier-plugin-packagejson',
'prettier',
'syncpack',
'turbo',
],
};
/**
* @type {Record<string, Record<string,string>>}
*
* @example {
* '@rushstack/eslint-config': { // package to modify a dependency for
* '@typescript-eslint/eslint-plugin': '~6.19.0', // desired version
* }
* }
*/
const RESOLUTIONS = {
'@rushstack/eslint-config': {
'@typescript-eslint/eslint-plugin': '6.21.0',
'@typescript-eslint/utils': '6.21.0',
'@typescript-eslint/parser': '6.21.0',
'@typescript-eslint/typescript-estree': '6.21.0',
},
};
function readPackage(pkg, context) {
// if it's in one of the two constants
if (
Object.keys({ ...RESOLUTIONS, ...ALLOWED_DEPENDENCIES }).includes(pkg.name)
) {
// if it's part of the packages to check for allowed dependencies
if (Object.keys(ALLOWED_DEPENDENCIES).includes(pkg.name)) {
context.log(`Checking allowed dependencies for ${pkg.name}`);
const allDependencies = Object.keys({
...(pkg.dependencies ?? {}),
...(pkg.devDependencies ?? {}),
});
allDependencies.forEach((dep) => {
if (!ALLOWED_DEPENDENCIES[pkg.name].includes(dep)) {
context.log(`Root dependency not allowed: ${dep}`);
throw new Error(`Root dependency not allowed: ${dep}`);
}
});
}
// if it's part of the packages to MODIFY for resolutions
if (Object.keys(RESOLUTIONS).includes(pkg.name)) {
context.log(`Setting resolutions for ${pkg.name}`);
const resolutions = RESOLUTIONS[pkg.name];
// if the package doesn't have any dependencies to modify
if (
!hasCommonElement(
Object.keys({ ...pkg.dependencies, ...pkg.devDependencies }),
Object.keys(resolutions),
)
) {
throw new Error(`No dependencies to modify for ${pkg.name}`);
}
// when the package has dependencies to modify
Object.entries(resolutions).forEach(([dep, version]) => {
// it could be either dependencies or devDependencies
if (pkg.dependencies?.[dep]) {
context.log(
` - ${dep}@${pkg.dependencies[dep]} => ${dep}@${version}`,
);
pkg.dependencies[dep] = version;
} // package will never be in both dependencies and devDependencies
else if (pkg.devDependencies?.[dep]) {
context.log(
` - ${dep}@${pkg.devDependencies[dep]} => ${dep}@${version}`,
);
pkg.devDependencies[dep] = version;
}
});
}
}
return pkg;
}
function hasCommonElement(arr1, arr2) {
return arr1.some((element) => arr2.includes(element));
}
module.exports = {
hooks: {
readPackage,
},
};