-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathindex.js
78 lines (67 loc) · 1.85 KB
/
index.js
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
const path = require('path');
const readYamlFile = require('read-yaml-file');
const fg = require('fast-glob');
const {readExactProjectManifest} = require('@pnpm/read-project-manifest');
module.exports = {
utils: {getProjects},
rules: {
'scope-enum': (ctx) =>
getProjects(ctx).then((packages) => [2, 'always', packages]),
},
};
function requirePackagesManifest(dir) {
return readYamlFile(path.join(dir, 'pnpm-workspace.yaml')).catch((err) => {
if (err.code === 'ENOENT') {
return null;
}
throw err;
});
}
function normalizePatterns(patterns) {
const normalizedPatterns = [];
for (const pattern of patterns) {
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json'));
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json5'));
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.yaml'));
}
return normalizedPatterns;
}
function findWorkspacePackages(cwd) {
return requirePackagesManifest(cwd)
.then((manifest) => {
const patterns = normalizePatterns(
(manifest && manifest.packages) || ['**']
);
const opts = {
cwd,
ignore: ['**/node_modules/**', '**/bower_components/**'],
};
return fg(patterns, opts);
})
.then((entries) => {
const paths = Array.from(
new Set(entries.map((entry) => path.join(cwd, entry)))
);
return Promise.all(
paths.map((manifestPath) => readExactProjectManifest(manifestPath))
);
})
.then((manifests) => {
return manifests.map((manifest) => manifest.manifest);
});
}
function getProjects(context) {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
return findWorkspacePackages(cwd).then((projects) => {
return projects
.reduce((projects, project) => {
const name = project.name;
if (name) {
projects.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
}
return projects;
}, [])
.sort();
});
}