Skip to content

fix(config-lerna-scopes): remove deprecated @lerna/project dependency #4284

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion @commitlint/config-lerna-scopes/fixtures/basic/lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"lerna": "4",
"version": "1.0.0",
"packages": ["packages/*"]
}
6 changes: 1 addition & 5 deletions @commitlint/config-lerna-scopes/fixtures/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"name": "basic",
"version": "1.0.0",
"devDependencies": {
"lerna": "^4.0.0",
"@lerna/project": "^4.0.0"
}
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "a",
"name": "@commitlint-lerna-scopes/basic-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "b",
"name": "@commitlint-lerna-scopes/basic-b",
"version": "1.0.0"
}
1 change: 0 additions & 1 deletion @commitlint/config-lerna-scopes/fixtures/empty/lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"lerna": "4",
"version": "1.0.0",
"packages": ["packages/*"]
}
6 changes: 1 addition & 5 deletions @commitlint/config-lerna-scopes/fixtures/empty/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"name": "empty",
"version": "1.0.0",
"devDependencies": {
"lerna": "^4.0.0",
"@lerna/project": "^4.0.0"
}
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"lerna": "4",
"version": "1.0.0",
"packages": ["packages/*"]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"name": "basic",
"version": "1.0.0",
"devDependencies": {
"lerna": "^4.0.0",
"@lerna/project": "^4.0.0"
}
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "a",
"name": "@commitlint-lerna-scopes/modules-a",
"version": "1.0.0"
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 4 additions & 0 deletions @commitlint/config-lerna-scopes/fixtures/nested/lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "1.0.0",
"packages": ["packages/**"]
}
4 changes: 4 additions & 0 deletions @commitlint/config-lerna-scopes/fixtures/nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "nested-workspaces",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@commitlint-lerna-scopes/nested-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@commitlint-lerna-scopes/nested-b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@commitlint-lerna-scopes/nested-c",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"name": "@packages/a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@commitlint-lerna-scopes/no-packages-declaration",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@packages/a",
"name": "@commitlint-lerna-scopes/scoped-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@packages/b",
"name": "@commitlint-lerna-scopes/scoped-b",
"version": "1.0.0"
}
3 changes: 1 addition & 2 deletions @commitlint/config-lerna-scopes/fixtures/scoped/lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"lerna": "4",
"version": "1.0.0",
"packages": ["@packages/*"]
"packages": ["@packages/**"]
}
6 changes: 1 addition & 5 deletions @commitlint/config-lerna-scopes/fixtures/scoped/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"name": "scoped",
"version": "1.0.0",
"devDependencies": {
"lerna": "^4.0.0",
"@lerna/project": "^4.0.0"
}
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"name": "@packages/b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@commitlint-lerna-scopes/transition-to-workspace-scopes",
"version": "1.0.0",
"workspaces": ["./packages/*"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@commitlint-lerna-scopes/workspace-package",
"version": "1.0.0"
}
11 changes: 0 additions & 11 deletions @commitlint/config-lerna-scopes/fixtures/yarn/package.json

This file was deleted.

127 changes: 73 additions & 54 deletions @commitlint/config-lerna-scopes/index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,89 @@
import {createRequire} from 'node:module';
import Path from 'node:path';

import {globSync} from 'glob';
import importFrom from 'import-from';
import semver from 'semver';

const require = createRequire(import.meta.url);
import path from 'node:path';
import fs from 'node:fs/promises';
import fg from 'fast-glob';
import configWorkspaceScopes from '@commitlint/config-workspace-scopes';

export default {
utils: {getPackages},
utils: {getProjects},
rules: {
'scope-enum': (ctx) =>
getPackages(ctx).then((packages) => [2, 'always', packages]),
getProjects(ctx).then((packages) => [2, 'always', packages]),
},
};

function getPackages(context) {
return Promise.resolve()
.then(() => {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();

const {workspaces} = require(Path.join(cwd, 'package.json'));
if (Array.isArray(workspaces) && workspaces.length) {
// use yarn workspaces
/**
* Turn glob paths with potential 'package.json' ending always into paths
* with a package.json ending to find monorepo packages
* @param {string[]} patterns
* @returns A list of glob paths to resolve package.json files
*/
function normalizePatterns(patterns) {
const normalizedPatterns = [];
for (const pattern of patterns) {
normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json'));
}
return normalizedPatterns;
}

const wsGlobs = workspaces.flatMap((ws) => {
const path = Path.posix.join(ws, 'package.json');
return globSync(path, {cwd, ignore: ['**/node_modules/**']});
});
/**
* Find all package.json contents in the defined cwd
* @param {string} cwd
* @returns A list of parsed package.json files as objects
*/
async function findPackages(cwd) {
const json = await fs.readFile(path.join(cwd, 'lerna.json'), {
encoding: 'utf-8',
});

return wsGlobs.map((pJson) => require(Path.join(cwd, pJson)));
}
const packages = JSON.parse(json)?.packages || [];
if (packages.length === 0) {
return [];
}

const lernaVersion = getLernaVersion(cwd);
if (semver.lt(lernaVersion, '3.0.0')) {
const Repository = importFrom(cwd, 'lerna/lib/Repository');
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
const patterns = normalizePatterns(packages);
const entries = await fg(patterns, {
cwd,
ignore: ['**/node_modules/**', '**/bower_components/**'],
});

const repository = new Repository(cwd);
return PackageUtilities.getPackages({
packageConfigs: repository.packageConfigs,
rootPath: cwd,
});
}
const pkgJsons = await Promise.all(
Array.from(new Set(entries.map((entry) => path.join(cwd, entry)))).map(
(pkgPath) => fs.readFile(pkgPath, {encoding: 'utf-8'})
)
);

const {getPackages} = importFrom(cwd, '@lerna/project');
return getPackages(cwd);
})
.then((packages) => {
return packages
.map((pkg) => pkg.name)
.filter(Boolean)
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
});
return pkgJsons.map((pkgJson) => JSON.parse(pkgJson) || {});
}

function getLernaVersion(cwd) {
const moduleEntrypoint = require.resolve('lerna', {
paths: [cwd],
async function getProjects(context) {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();

// try to read workspaces for backwards compatibility
const workspacePackages = await configWorkspaceScopes.utils.getPackages({
cwd,
});
const moduleDir = Path.join(
moduleEntrypoint.slice(0, moduleEntrypoint.lastIndexOf('node_modules')),
'node_modules',
'lerna'
);
const modulePackageJson = Path.join(moduleDir, 'package.json');
return require(modulePackageJson).version;
// native npm/yarn workspaces detected, inform user to use new package instead
if (workspacePackages.length > 0) {
console.warn(
[
`It seems that you are using npm/yarn workspaces instead of lernas "packages" declaration.`,
`Support for workspaces will be removed in a future major version of this package.`,
`Please make sure to transition to "@commitlint/config-workspace-scopes" in the near future.`,
].join('\n')
);
return workspacePackages;
}

const packages = await findPackages(cwd);

return packages
.reduce((pkgNames, pkg) => {
const name = pkg.name;
if (name) {
pkgNames.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
}
return pkgNames;
}, [])
.sort();
}
Loading
Loading