Skip to content

Commit

Permalink
fix(config-pnpm-scopes): refactor to remove peer dependencies (#3564)
Browse files Browse the repository at this point in the history
Fixes #3556
  • Loading branch information
dangreen authored Mar 21, 2023
1 parent 4c40a18 commit f1f3bd5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2,285 deletions.
68 changes: 60 additions & 8 deletions @commitlint/config-pnpm-scopes/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const {findWorkspacePackages} = require('@pnpm/find-workspace-packages');
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},
Expand All @@ -8,19 +11,68 @@ module.exports = {
},
};

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.manifest.name;
return projects
.reduce((projects, project) => {
const name = project.name;

if (name && project.dir !== cwd) {
projects.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
}
if (name) {
projects.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
}

return projects;
}, []);
return projects;
}, [])
.sort();
});
}
21 changes: 6 additions & 15 deletions @commitlint/config-pnpm-scopes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,16 @@
"url": "https://github.com/conventional-changelog/commitlint/issues"
},
"homepage": "https://commitlint.js.org/",
"peerDependencies": {
"@pnpm/find-workspace-packages": "^5.0.0",
"@pnpm/logger": "^5.0.0"
},
"peerDependenciesMeta": {
"@pnpm/find-workspace-packages": {
"optional": true
},
"@pnpm/logger": {
"optional": true
}
},
"engines": {
"node": ">=v14"
},
"dependencies": {
"@pnpm/read-project-manifest": "^4.1.4",
"fast-glob": "^3.2.12",
"read-yaml-file": "^2.1.0"
},
"devDependencies": {
"@commitlint/test": "^17.4.2",
"@commitlint/utils": "^17.4.0",
"@pnpm/find-workspace-packages": "^5.0.0",
"@pnpm/logger": "^5.0.0"
"@commitlint/utils": "^17.4.0"
}
}
Loading

0 comments on commit f1f3bd5

Please sign in to comment.