@commitlint/config-lerna-scopes 19.7.0 → 19.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +74 -55
  2. package/package.json +8 -11
  3. package/readme.md +4 -1
package/index.js CHANGED
@@ -1,70 +1,89 @@
1
- import {createRequire} from 'module';
2
- import Path from 'path';
3
-
4
- import {globSync} from 'glob';
5
- import importFrom from 'import-from';
6
- import semver from 'semver';
7
-
8
- const require = createRequire(import.meta.url);
1
+ import path from "node:path";
2
+ import fs from "node:fs/promises";
3
+ import fg from "fast-glob";
4
+ import configWorkspaceScopes from "@commitlint/config-workspace-scopes";
9
5
 
10
6
  export default {
11
- utils: {getPackages},
7
+ utils: { getProjects },
12
8
  rules: {
13
- 'scope-enum': (ctx) =>
14
- getPackages(ctx).then((packages) => [2, 'always', packages]),
9
+ "scope-enum": (ctx) =>
10
+ getProjects(ctx).then((packages) => [2, "always", packages]),
15
11
  },
16
12
  };
17
13
 
18
- function getPackages(context) {
19
- return Promise.resolve()
20
- .then(() => {
21
- const ctx = context || {};
22
- const cwd = ctx.cwd || process.cwd();
23
-
24
- const {workspaces} = require(Path.join(cwd, 'package.json'));
25
- if (Array.isArray(workspaces) && workspaces.length) {
26
- // use yarn workspaces
14
+ /**
15
+ * Turn glob paths with potential 'package.json' ending always into paths
16
+ * with a package.json ending to find monorepo packages
17
+ * @param {string[]} patterns
18
+ * @returns A list of glob paths to resolve package.json files
19
+ */
20
+ function normalizePatterns(patterns) {
21
+ const normalizedPatterns = [];
22
+ for (const pattern of patterns) {
23
+ normalizedPatterns.push(pattern.replace(/\/?$/, "/package.json"));
24
+ }
25
+ return normalizedPatterns;
26
+ }
27
27
 
28
- const wsGlobs = workspaces.flatMap((ws) => {
29
- const path = Path.posix.join(ws, 'package.json');
30
- return globSync(path, {cwd, ignore: ['**/node_modules/**']});
31
- });
28
+ /**
29
+ * Find all package.json contents in the defined cwd
30
+ * @param {string} cwd
31
+ * @returns A list of parsed package.json files as objects
32
+ */
33
+ async function findPackages(cwd) {
34
+ const json = await fs.readFile(path.join(cwd, "lerna.json"), {
35
+ encoding: "utf-8",
36
+ });
32
37
 
33
- return wsGlobs.map((pJson) => require(Path.join(cwd, pJson)));
34
- }
38
+ const packages = JSON.parse(json)?.packages || [];
39
+ if (packages.length === 0) {
40
+ return [];
41
+ }
35
42
 
36
- const lernaVersion = getLernaVersion(cwd);
37
- if (semver.lt(lernaVersion, '3.0.0')) {
38
- const Repository = importFrom(cwd, 'lerna/lib/Repository');
39
- const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
43
+ const patterns = normalizePatterns(packages);
44
+ const entries = await fg(patterns, {
45
+ cwd,
46
+ ignore: ["**/node_modules/**", "**/bower_components/**"],
47
+ });
40
48
 
41
- const repository = new Repository(cwd);
42
- return PackageUtilities.getPackages({
43
- packageConfigs: repository.packageConfigs,
44
- rootPath: cwd,
45
- });
46
- }
49
+ const pkgJsons = await Promise.all(
50
+ Array.from(new Set(entries.map((entry) => path.join(cwd, entry)))).map(
51
+ (pkgPath) => fs.readFile(pkgPath, { encoding: "utf-8" }),
52
+ ),
53
+ );
47
54
 
48
- const {getPackages} = importFrom(cwd, '@lerna/project');
49
- return getPackages(cwd);
50
- })
51
- .then((packages) => {
52
- return packages
53
- .map((pkg) => pkg.name)
54
- .filter(Boolean)
55
- .map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
56
- });
55
+ return pkgJsons.map((pkgJson) => JSON.parse(pkgJson) || {});
57
56
  }
58
57
 
59
- function getLernaVersion(cwd) {
60
- const moduleEntrypoint = require.resolve('lerna', {
61
- paths: [cwd],
58
+ async function getProjects(context) {
59
+ const ctx = context || {};
60
+ const cwd = ctx.cwd || process.cwd();
61
+
62
+ // try to read workspaces for backwards compatibility
63
+ const workspacePackages = await configWorkspaceScopes.utils.getPackages({
64
+ cwd,
62
65
  });
63
- const moduleDir = Path.join(
64
- moduleEntrypoint.slice(0, moduleEntrypoint.lastIndexOf('node_modules')),
65
- 'node_modules',
66
- 'lerna'
67
- );
68
- const modulePackageJson = Path.join(moduleDir, 'package.json');
69
- return require(modulePackageJson).version;
66
+ // native npm/yarn workspaces detected, inform user to use new package instead
67
+ if (workspacePackages.length > 0) {
68
+ console.warn(
69
+ [
70
+ `It seems that you are using npm/yarn workspaces instead of lernas "packages" declaration.`,
71
+ `Support for workspaces will be removed in a future major version of this package.`,
72
+ `Please make sure to transition to "@commitlint/config-workspace-scopes" in the near future.`,
73
+ ].join("\n"),
74
+ );
75
+ return workspacePackages;
76
+ }
77
+
78
+ const packages = await findPackages(cwd);
79
+
80
+ return packages
81
+ .reduce((pkgNames, pkg) => {
82
+ const name = pkg.name;
83
+ if (name) {
84
+ pkgNames.push(name.charAt(0) === "@" ? name.split("/")[1] : name);
85
+ }
86
+ return pkgNames;
87
+ }, [])
88
+ .sort();
70
89
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@commitlint/config-lerna-scopes",
3
3
  "type": "module",
4
- "version": "19.7.0",
4
+ "version": "19.8.1",
5
5
  "description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -22,14 +22,14 @@
22
22
  "commitlint-config",
23
23
  "lerna"
24
24
  ],
25
- "author": "Mario Nebl <hello@herebecode.com>",
25
+ "author": "Jan Biasi (https://github.com/janbiasi)",
26
26
  "license": "MIT",
27
27
  "bugs": {
28
28
  "url": "https://github.com/conventional-changelog/commitlint/issues"
29
29
  },
30
30
  "homepage": "https://commitlint.js.org/",
31
31
  "peerDependencies": {
32
- "lerna": ">=5 <9"
32
+ "lerna": "*"
33
33
  },
34
34
  "peerDependenciesMeta": {
35
35
  "lerna": {
@@ -40,15 +40,12 @@
40
40
  "node": ">=v18"
41
41
  },
42
42
  "dependencies": {
43
- "@lerna/project": "^6.0.0",
44
- "glob": "^10.3.10",
45
- "import-from": "^4.0.0",
46
- "semver": "^7.6.0"
43
+ "@commitlint/config-workspace-scopes": "^19.8.1",
44
+ "fast-glob": "^3.3.3"
47
45
  },
48
46
  "devDependencies": {
49
- "@commitlint/test": "^19.5.0",
50
- "@commitlint/utils": "^19.5.0",
51
- "@types/glob": "^8.1.0"
47
+ "@commitlint/test": "^19.8.1",
48
+ "@commitlint/utils": "^19.8.1"
52
49
  },
53
- "gitHead": "fff6270245d1c5f7b8189660f84ec4f38c46d12c"
50
+ "gitHead": "3c302008cabeb0b08cd246b2417a51a9d745a918"
54
51
  }
package/readme.md CHANGED
@@ -2,9 +2,12 @@
2
2
 
3
3
  Lint your lerna project commits
4
4
 
5
- Shareable `commitlint` config enforcing lerna package and workspace names as scopes.
5
+ Shareable `commitlint` config enforcing lerna package scopes.
6
6
  Use with [@commitlint/cli](../cli) and [@commitlint/prompt-cli](../prompt-cli).
7
7
 
8
+ > [!IMPORTANT]
9
+ > If you are using NPM or Yarn workspaces, please use the [@commitlint/config-workspace-scopes](../config-workspace-scopes/) package instead. Support for workspaces will be removed from this package in a future major version.
10
+
8
11
  ## Getting started
9
12
 
10
13
  ```sh