Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config-pnpm-scopes): refactor to remove peer dependencies #3564

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
}