@commitlint/config-nx-scopes 21.2.0 → 21.2.3
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.
- package/index.js +44 -8
- package/package.json +5 -4
- package/project-graph.js +42 -0
- package/readme.md +3 -0
package/index.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
1
4
|
import { RuleConfigSeverity } from "@commitlint/types";
|
|
2
|
-
|
|
3
|
-
|
|
5
|
+
|
|
6
|
+
const projectGraphReader = fileURLToPath(new URL("./project-graph.js", import.meta.url));
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Stdio slot the graph reader writes its payload to. Nx, its daemon and the
|
|
10
|
+
* worker processes it runs plugins in all write to stdout, so the payload gets
|
|
11
|
+
* a pipe of its own instead of sharing one with them.
|
|
12
|
+
*
|
|
13
|
+
* This is the only place the descriptor is declared: the reader is told which
|
|
14
|
+
* one to write to on its command line, so the two sides cannot drift apart.
|
|
15
|
+
*/
|
|
16
|
+
const payloadFd = 3;
|
|
4
17
|
|
|
5
18
|
export default {
|
|
6
19
|
utils: { getProjects },
|
|
@@ -16,12 +29,7 @@ function getProjects(context, selector = () => true) {
|
|
|
16
29
|
const ctx = context || {};
|
|
17
30
|
const cwd = ctx.cwd || process.cwd();
|
|
18
31
|
|
|
19
|
-
|
|
20
|
-
return Array.from(projects.entries())
|
|
21
|
-
.map(([name, project]) => ({
|
|
22
|
-
name,
|
|
23
|
-
...project,
|
|
24
|
-
}))
|
|
32
|
+
return readProjectGraph(cwd)
|
|
25
33
|
.filter((project) =>
|
|
26
34
|
selector({
|
|
27
35
|
name: project.name,
|
|
@@ -32,3 +40,31 @@ function getProjects(context, selector = () => true) {
|
|
|
32
40
|
.map((project) => project.name)
|
|
33
41
|
.map((name) => (name.charAt(0) === "@" ? name.split("/")[1] : name));
|
|
34
42
|
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Nx picks its workspace root from the cwd of the process that loads it, so the
|
|
46
|
+
* graph is read in a child process started in the directory we care about.
|
|
47
|
+
*/
|
|
48
|
+
function readProjectGraph(cwd) {
|
|
49
|
+
const { status, error, stdout, stderr, output } = spawnSync(
|
|
50
|
+
process.execPath,
|
|
51
|
+
[projectGraphReader, String(payloadFd)],
|
|
52
|
+
{
|
|
53
|
+
cwd,
|
|
54
|
+
encoding: "utf8",
|
|
55
|
+
// stdin, stdout, stderr, then the payload pipe at `payloadFd`.
|
|
56
|
+
stdio: ["ignore", "pipe", "pipe", "pipe"],
|
|
57
|
+
// A workspace with a lot of projects or a chatty plugin must not run
|
|
58
|
+
// into the default 1 MB cap, which aborts the read.
|
|
59
|
+
maxBuffer: Infinity,
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const payload = output[payloadFd];
|
|
64
|
+
if (error || status !== 0 || !payload) {
|
|
65
|
+
const reason = error ? error.message : stderr || stdout;
|
|
66
|
+
throw new Error(`Unable to read the nx project graph in "${cwd}".\n${reason}`.trim());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return JSON.parse(payload);
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commitlint/config-nx-scopes",
|
|
3
|
-
"version": "21.2.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "21.2.3",
|
|
4
|
+
"gitHead": "95d40569d2592bf9719bc27da2d51fc6b801e4ef",
|
|
5
5
|
"description": "Shareable commitlint config enforcing nx project names as scopes",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"commitlint",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"directory": "@commitlint/config-nx-scopes"
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
|
-
"index.js"
|
|
24
|
+
"index.js",
|
|
25
|
+
"project-graph.js"
|
|
25
26
|
],
|
|
26
27
|
"type": "module",
|
|
27
28
|
"main": "index.js",
|
|
@@ -30,7 +31,7 @@
|
|
|
30
31
|
"pkg": "pkg-check"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
|
-
"@commitlint/types": "^21.2.
|
|
34
|
+
"@commitlint/types": "^21.2.3"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"@commitlint/test": "^21.2.0",
|
package/project-graph.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the nx project graph and writes every project it contains to the file
|
|
3
|
+
* descriptor named by its first argument, where the parent process picks it up.
|
|
4
|
+
* The parent owns that number and passes it down, so there is nothing here to
|
|
5
|
+
* keep in step with it by hand.
|
|
6
|
+
*
|
|
7
|
+
* This runs as a child process on purpose. Nx derives the workspace root from
|
|
8
|
+
* `process.cwd()` when it is first loaded and bakes that value into its cache
|
|
9
|
+
* paths and its daemon client, so the only reliable way to read the graph of an
|
|
10
|
+
* arbitrary directory is to be a process whose cwd is that directory.
|
|
11
|
+
*
|
|
12
|
+
* The payload does not go to stdout because nx does not leave stdout alone:
|
|
13
|
+
* progress output, the daemon and plugins all write to it, and plugins run in
|
|
14
|
+
* worker processes that inherit it, so it cannot be kept clean from in here.
|
|
15
|
+
*/
|
|
16
|
+
import { writeSync } from "node:fs";
|
|
17
|
+
|
|
18
|
+
import { createProjectGraphAsync } from "nx/src/project-graph/project-graph.js";
|
|
19
|
+
|
|
20
|
+
const payloadFd = Number(process.argv[2]);
|
|
21
|
+
if (!Number.isInteger(payloadFd) || payloadFd < 3) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Expected the payload file descriptor as the first argument, got "${process.argv[2]}".`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const graph = await createProjectGraphAsync({ exitOnError: false });
|
|
28
|
+
|
|
29
|
+
const projects = Object.entries(graph.nodes ?? {}).map(([name, node]) => ({
|
|
30
|
+
name,
|
|
31
|
+
projectType: node.data?.projectType,
|
|
32
|
+
tags: node.data?.tags ?? [],
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
const payload = Buffer.from(JSON.stringify(projects));
|
|
36
|
+
for (let written = 0; written < payload.length;) {
|
|
37
|
+
written += writeSync(payloadFd, payload, written);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Nx keeps the daemon connection and its file watchers alive, so ask for the
|
|
41
|
+
// process to end now that the payload is out.
|
|
42
|
+
process.exit(0);
|
package/readme.md
CHANGED
|
@@ -5,6 +5,9 @@ Lint your nx project commits
|
|
|
5
5
|
Shareable `commitlint` config enforcing nx project and workspace names as scopes.
|
|
6
6
|
Use with [@commitlint/cli](../cli) and [@commitlint/prompt-cli](../prompt-cli).
|
|
7
7
|
|
|
8
|
+
Scopes are read from the nx project graph, so a project contributed by an nx plugin is
|
|
9
|
+
a valid scope even though it has no `project.json` or `package.json` of its own.
|
|
10
|
+
|
|
8
11
|
## Getting started
|
|
9
12
|
|
|
10
13
|
```
|