-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy path.pnpmfile.cjs
60 lines (48 loc) · 1.75 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
//
// Prompt:
// Use Nodejs to read all package names in the `name` field of `packages/*/package.json`,
// cannot use any third-party packages except for the built-in libraries of Nodejs.
//
const fs = require('node:fs')
const path = require('node:path')
const packagesDir = path.join(__dirname, 'packages')
// Use readdirSync to read the directories in the 'packages' folder
const packageDirs = fs.readdirSync(packagesDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
const packages = packageDirs.map(dir => {
const packageJSONPath = path.join(packagesDir, dir, 'package.json')
// Check if the package.json file exists
if (fs.existsSync(packageJSONPath)) {
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf8'))
return packageJSON
}
return null
}).filter(Boolean)
const packageMap = new Map(packages.map(packageJSON => [packageJSON.name, packageJSON]))
// https://pnpm.io/pnpmfile
module.exports = {
hooks: {
readPackage(packageJSON) {
/**
* only lock workspace package version in our monorepo examples/
*/
if (!packageJSON.name.startsWith('@example/')) {
return packageJSON
}
packageMap.forEach(({ version }, packageName) => {
// the `workspace:*` will match beta version in pnpm
if (packageJSON.dependencies?.[packageName]) {
packageJSON.dependencies[packageName] = `workspace:*`
}
if (packageJSON.devDependencies?.[packageName]) {
packageJSON.devDependencies[packageName] = `workspace:*`
}
if (packageJSON.peerDependencies?.[packageName]) {
packageJSON.devDependencies[packageName] = `workspace:*`
}
})
return packageJSON
},
},
}