@commitlint/config-lerna-scopes 21.0.2 → 21.3.0

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 (2) hide show
  1. package/index.js +22 -11
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import path from "node:path";
2
+ import { createRequire } from "node:module";
2
3
  import fs from "node:fs/promises";
3
4
  import { glob } from "node:fs/promises";
4
5
  import configWorkspaceScopes from "@commitlint/config-workspace-scopes";
5
6
 
7
+ const require = createRequire(import.meta.url);
8
+
6
9
  export default {
7
10
  utils: { getProjects },
8
11
  rules: {
@@ -24,17 +27,20 @@ function normalizePatterns(patterns) {
24
27
  return normalizedPatterns;
25
28
  }
26
29
 
30
+ async function readLernaPatterns(cwd) {
31
+ const json = await fs.readFile(path.join(cwd, "lerna.json"), {
32
+ encoding: "utf-8",
33
+ });
34
+ return JSON.parse(json)?.packages;
35
+ }
36
+
27
37
  /**
28
38
  * Find all package.json contents in the defined cwd
29
39
  * @param {string} cwd
40
+ * @param {string[]} packages
30
41
  * @returns A list of parsed package.json files as objects
31
42
  */
32
- async function findPackages(cwd) {
33
- const json = await fs.readFile(path.join(cwd, "lerna.json"), {
34
- encoding: "utf-8",
35
- });
36
-
37
- const packages = JSON.parse(json)?.packages || [];
43
+ async function findPackages(cwd, packages) {
38
44
  if (packages.length === 0) {
39
45
  return [];
40
46
  }
@@ -63,10 +69,14 @@ async function getProjects(context) {
63
69
  const ctx = context || {};
64
70
  const cwd = ctx.cwd || process.cwd();
65
71
 
66
- // try to read workspaces for backwards compatibility
67
- const workspacePackages = await configWorkspaceScopes.utils.getPackages({
68
- cwd,
69
- });
72
+ const { workspaces } = require(path.join(cwd, "package.json"));
73
+ const hasWorkspaceArray = Array.isArray(workspaces);
74
+ // Keep the legacy array fallback. For objects, Lerna's own list takes priority.
75
+ const lernaPatterns = hasWorkspaceArray ? undefined : await readLernaPatterns(cwd);
76
+ const workspacePackages =
77
+ hasWorkspaceArray || lernaPatterns == null
78
+ ? await configWorkspaceScopes.utils.getPackages({ cwd })
79
+ : [];
70
80
  // native npm/yarn workspaces detected, inform user to use new package instead
71
81
  if (workspacePackages.length > 0) {
72
82
  console.warn(
@@ -79,7 +89,8 @@ async function getProjects(context) {
79
89
  return workspacePackages;
80
90
  }
81
91
 
82
- const packages = await findPackages(cwd);
92
+ const patterns = hasWorkspaceArray ? await readLernaPatterns(cwd) : lernaPatterns;
93
+ const packages = await findPackages(cwd, patterns || []);
83
94
 
84
95
  return packages
85
96
  .reduce((pkgNames, pkg) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@commitlint/config-lerna-scopes",
3
- "version": "21.0.2",
4
- "gitHead": "806904829c497cdfedeebfbac573bd1c8ee66b2b",
3
+ "version": "21.3.0",
4
+ "gitHead": "c0bce295b8c1fbc4f692ec05fb760c8b157fb708",
5
5
  "description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
6
6
  "keywords": [
7
7
  "commitlint",
@@ -30,10 +30,10 @@
30
30
  "pkg": "pkg-check"
31
31
  },
32
32
  "dependencies": {
33
- "@commitlint/config-workspace-scopes": "^21.0.2"
33
+ "@commitlint/config-workspace-scopes": "^21.3.0"
34
34
  },
35
35
  "devDependencies": {
36
- "@commitlint/test": "^21.0.2",
36
+ "@commitlint/test": "^21.2.0",
37
37
  "@commitlint/utils": "^21.0.1"
38
38
  },
39
39
  "peerDependencies": {