Skip to content

Commit f1f3bd5

Browse files
authored
fix(config-pnpm-scopes): refactor to remove peer dependencies (#3564)
Fixes #3556
1 parent 4c40a18 commit f1f3bd5

File tree

3 files changed

+126
-2285
lines changed

3 files changed

+126
-2285
lines changed
+60-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const {findWorkspacePackages} = require('@pnpm/find-workspace-packages');
1+
const path = require('path');
2+
const readYamlFile = require('read-yaml-file');
3+
const fg = require('fast-glob');
4+
const {readExactProjectManifest} = require('@pnpm/read-project-manifest');
25

36
module.exports = {
47
utils: {getProjects},
@@ -8,19 +11,68 @@ module.exports = {
811
},
912
};
1013

14+
function requirePackagesManifest(dir) {
15+
return readYamlFile(path.join(dir, 'pnpm-workspace.yaml')).catch((err) => {
16+
if (err.code === 'ENOENT') {
17+
return null;
18+
}
19+
20+
throw err;
21+
});
22+
}
23+
24+
function normalizePatterns(patterns) {
25+
const normalizedPatterns = [];
26+
for (const pattern of patterns) {
27+
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json'));
28+
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json5'));
29+
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.yaml'));
30+
}
31+
return normalizedPatterns;
32+
}
33+
34+
function findWorkspacePackages(cwd) {
35+
return requirePackagesManifest(cwd)
36+
.then((manifest) => {
37+
const patterns = normalizePatterns(
38+
(manifest && manifest.packages) || ['**']
39+
);
40+
const opts = {
41+
cwd,
42+
ignore: ['**/node_modules/**', '**/bower_components/**'],
43+
};
44+
45+
return fg(patterns, opts);
46+
})
47+
.then((entries) => {
48+
const paths = Array.from(
49+
new Set(entries.map((entry) => path.join(cwd, entry)))
50+
);
51+
52+
return Promise.all(
53+
paths.map((manifestPath) => readExactProjectManifest(manifestPath))
54+
);
55+
})
56+
.then((manifests) => {
57+
return manifests.map((manifest) => manifest.manifest);
58+
});
59+
}
60+
1161
function getProjects(context) {
1262
const ctx = context || {};
1363
const cwd = ctx.cwd || process.cwd();
1464

1565
return findWorkspacePackages(cwd).then((projects) => {
16-
return projects.reduce((projects, project) => {
17-
const name = project.manifest.name;
66+
return projects
67+
.reduce((projects, project) => {
68+
const name = project.name;
1869

19-
if (name && project.dir !== cwd) {
20-
projects.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
21-
}
70+
if (name) {
71+
projects.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
72+
}
2273

23-
return projects;
24-
}, []);
74+
return projects;
75+
}, [])
76+
.sort();
2577
});
2678
}

@commitlint/config-pnpm-scopes/package.json

+6-15
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,16 @@
2626
"url": "https://github.com/conventional-changelog/commitlint/issues"
2727
},
2828
"homepage": "https://commitlint.js.org/",
29-
"peerDependencies": {
30-
"@pnpm/find-workspace-packages": "^5.0.0",
31-
"@pnpm/logger": "^5.0.0"
32-
},
33-
"peerDependenciesMeta": {
34-
"@pnpm/find-workspace-packages": {
35-
"optional": true
36-
},
37-
"@pnpm/logger": {
38-
"optional": true
39-
}
40-
},
4129
"engines": {
4230
"node": ">=v14"
4331
},
32+
"dependencies": {
33+
"@pnpm/read-project-manifest": "^4.1.4",
34+
"fast-glob": "^3.2.12",
35+
"read-yaml-file": "^2.1.0"
36+
},
4437
"devDependencies": {
4538
"@commitlint/test": "^17.4.2",
46-
"@commitlint/utils": "^17.4.0",
47-
"@pnpm/find-workspace-packages": "^5.0.0",
48-
"@pnpm/logger": "^5.0.0"
39+
"@commitlint/utils": "^17.4.0"
4940
}
5041
}

0 commit comments

Comments
 (0)