@williamthorsen/nmr-core 0.15.1 → 0.16.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.
- package/.meta/changelog.json +105 -0
- package/CHANGELOG.md +39 -0
- package/README.md +1 -7
- package/dist/esm/cli.d.ts +8 -0
- package/dist/esm/cli.js +10 -0
- package/dist/esm/{UserError.d.ts → errors/UserError.d.ts} +2 -2
- package/dist/esm/{isAnticipatedFailure.d.ts → errors/isAnticipatedFailure.d.ts} +1 -1
- package/dist/esm/files/report-write-outcomes.d.ts +14 -0
- package/dist/esm/files/report-write-outcomes.js +69 -0
- package/dist/esm/{hashWorkingTree.js → git/hashWorkingTree.js} +1 -1
- package/dist/esm/{runGit.d.ts → git/runGit.d.ts} +4 -3
- package/dist/esm/{runGit.js → git/runGit.js} +4 -4
- package/dist/esm/index.d.ts +17 -15
- package/dist/esm/index.js +12 -10
- package/dist/esm/portable/isRecord.d.ts +2 -0
- package/dist/esm/portable/isRecord.js +3 -0
- package/dist/esm/terminal.d.ts +11 -18
- package/dist/esm/terminal.js +7 -75
- package/dist/esm/{workspace.js → workspace/workspace.js} +6 -8
- package/package.json +12 -6
- package/dist/esm/parseArgs.d.ts +0 -48
- package/dist/esm/parseArgs.js +0 -90
- /package/dist/esm/{cache-store.d.ts → cache/cache-store.d.ts} +0 -0
- /package/dist/esm/{cache-store.js → cache/cache-store.js} +0 -0
- /package/dist/esm/{UserError.js → errors/UserError.js} +0 -0
- /package/dist/esm/{hasErrnoCode.d.ts → errors/hasErrnoCode.d.ts} +0 -0
- /package/dist/esm/{hasErrnoCode.js → errors/hasErrnoCode.js} +0 -0
- /package/dist/esm/{isAnticipatedFailure.js → errors/isAnticipatedFailure.js} +0 -0
- /package/dist/esm/{updateManagedFile.d.ts → files/updateManagedFile.d.ts} +0 -0
- /package/dist/esm/{updateManagedFile.js → files/updateManagedFile.js} +0 -0
- /package/dist/esm/{writeFileWithCheck.d.ts → files/writeFileWithCheck.d.ts} +0 -0
- /package/dist/esm/{writeFileWithCheck.js → files/writeFileWithCheck.js} +0 -0
- /package/dist/esm/{collectChangedPaths.d.ts → git/collectChangedPaths.d.ts} +0 -0
- /package/dist/esm/{collectChangedPaths.js → git/collectChangedPaths.js} +0 -0
- /package/dist/esm/{gitObjects.d.ts → git/gitObjects.d.ts} +0 -0
- /package/dist/esm/{gitObjects.js → git/gitObjects.js} +0 -0
- /package/dist/esm/{gitOutputLimit.d.ts → git/gitOutputLimit.d.ts} +0 -0
- /package/dist/esm/{gitOutputLimit.js → git/gitOutputLimit.js} +0 -0
- /package/dist/esm/{hashWorkingTree.d.ts → git/hashWorkingTree.d.ts} +0 -0
- /package/dist/esm/{helpers → workspace}/workspace-patterns.d.ts +0 -0
- /package/dist/esm/{helpers → workspace}/workspace-patterns.js +0 -0
- /package/dist/esm/{workspace.d.ts → workspace/workspace.d.ts} +0 -0
package/.meta/changelog.json
CHANGED
|
@@ -1,4 +1,109 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "0.16.1",
|
|
4
|
+
"date": "2026-11-10",
|
|
5
|
+
"sections": [
|
|
6
|
+
{
|
|
7
|
+
"title": "♻️ Refactoring",
|
|
8
|
+
"audience": "dev",
|
|
9
|
+
"items": [
|
|
10
|
+
{
|
|
11
|
+
"description": "Replaces the duplicate `resolveStyles` modules in `release-kit` and `v11y-check` with `resolveCliStyles` from `@williamthorsen/nmr-core/cli`, which each package's `route.ts` calls with its own output-style environment variable. (#1248)",
|
|
12
|
+
"hash": "c5f4ac68360ce550a7e53006a2a633b79f179f5f",
|
|
13
|
+
"entry": 1
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"version": "0.16.0",
|
|
21
|
+
"date": "2026-11-08",
|
|
22
|
+
"sections": [
|
|
23
|
+
{
|
|
24
|
+
"title": "🎉 Features",
|
|
25
|
+
"audience": "all",
|
|
26
|
+
"items": [
|
|
27
|
+
{
|
|
28
|
+
"description": "Prefix handled error lines with the program's bin (#1190)",
|
|
29
|
+
"body": "Changes the handled error lines that nmr, release-kit, and v11y-check write from `Error: <message>` to `<bin>: <message>`, so that a line in combined output names the program that wrote it. It covers usage errors as well, by upgrading `@williamthorsen/toolbelt.cli` to 0.8.0, whose router writes the same prefix.",
|
|
30
|
+
"hash": "b7634734032c508e45388b7752b7b6905071273e"
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"title": "🐛 Bug fixes",
|
|
36
|
+
"audience": "all",
|
|
37
|
+
"items": [
|
|
38
|
+
{
|
|
39
|
+
"description": "Apply the git output limit to nmr's buffered git calls (#1188)",
|
|
40
|
+
"body": "Fixes the issue that several of nmr's git calls ran with Node's default 1 MiB output buffer instead of nmr-core's 64 MiB git output limit, which made them fail with `ENOBUFS` once a repository's file listing grew large enough. Applies the limit to every buffered git call that nmr makes outside test code, including the call made on the host during a delegated run.",
|
|
41
|
+
"hash": "ce72ce80b1af8e4dd30227a013a77fb680d8c006"
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"title": "🏗️ Internal features",
|
|
47
|
+
"audience": "dev",
|
|
48
|
+
"items": [
|
|
49
|
+
{
|
|
50
|
+
"description": "Adds the `@williamthorsen/nmr-core/cli` subpath, which re-exports the parser, help renderer, and router of `@williamthorsen/toolbelt.cli/candidate` 0.7.1 under toolbelt's own names, separate from the nmr-core root entry, whose own `parseArgs`, `ParseError`, and `FlagDefinition` use the same names. (#1184)",
|
|
51
|
+
"hash": "9ab86d20562bb131b185a5c1a7ac566734f88a1e",
|
|
52
|
+
"entry": 1
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"description": "Removes `parseArgs`, `parseArgsOrExit`, `ParseError`, and their types from the root entry of `@williamthorsen/nmr-core`. (#1187)",
|
|
56
|
+
"migration": "Import `parseArgs` and `ParseError` from `@williamthorsen/nmr-core/cli` and replace `parseArgsOrExit` with that entry's `runCli` and `UsageError`, noting that its `parseArgs` takes the schema wrapped as `{ flags: schema }` and returns the remaining arguments as `rest` rather than `positionals`.",
|
|
57
|
+
"hash": "da4f5491167e75cf8da00495e60b41d74030b4b6",
|
|
58
|
+
"entry": 7
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"title": "♻️ Refactoring",
|
|
64
|
+
"audience": "dev",
|
|
65
|
+
"items": [
|
|
66
|
+
{
|
|
67
|
+
"description": "Replaces nmr's `isObject`, release-kit's `isRecord`, and the private copies in nmr-core's `workspace.ts` and an nmr test with one `isRecord` guard exported from the root entry of `@williamthorsen/nmr-core`. (#1194)",
|
|
68
|
+
"hash": "6c66148170145ccf5628c95d27e7783ef26d97e9",
|
|
69
|
+
"entry": 1
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"description": "Groups `nmr-core`'s modules into role directories and moves `reportWriteResult` and `reportTemplateUpdate` from `terminal.ts` into `files/report-write-outcomes.ts`, so that `terminal.ts` no longer imports the file-writing modules. (#1196)",
|
|
73
|
+
"hash": "bddeaf8cdad251c681808688be3d459c60fc2bdb",
|
|
74
|
+
"entry": 1
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"title": "🧪 Tests",
|
|
80
|
+
"audience": "dev",
|
|
81
|
+
"items": [
|
|
82
|
+
{
|
|
83
|
+
"description": "Adds `collectChangedPaths.unit.test.ts`, which covers each record type that `collectChangedPaths` accepts, including paths that contain spaces and both paths of a rename, and each record that it refuses: a submodule record, an unrecognized record type, a truncated record, an empty path, and a rename without an original path. (#1189)",
|
|
84
|
+
"hash": "735c147c11f711ed5dbc62d7a2b340c524783cc5",
|
|
85
|
+
"entry": 1
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"description": "Deletes the `hashWorkingTree` tests for a modified file, a staged change, an untracked file, a deleted file, a repointed symlink, a mode change, and a path containing spaces, each of which repeated a `REFERENCE_CASES` entry that compares the id with git's own tree id. (#1195)",
|
|
89
|
+
"hash": "a7e9d524fdfc97334e67759e565ca624567fd627",
|
|
90
|
+
"entry": 1
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
"title": "⚙️ Tooling",
|
|
96
|
+
"audience": "dev",
|
|
97
|
+
"items": [
|
|
98
|
+
{
|
|
99
|
+
"description": "Adds `\"prepublishOnly\": \"nmr build\"` to `@williamthorsen/nmr-core`, the one publishable package in this repo that the new check reported. (#1192)",
|
|
100
|
+
"hash": "26b1795ea3f0fa43ca5e3d6e294acfa72f4bb68d",
|
|
101
|
+
"entry": 3
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
},
|
|
2
107
|
{
|
|
3
108
|
"version": "0.15.1",
|
|
4
109
|
"date": "2026-11-06",
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## 0.16.1 — 2026-11-10
|
|
6
|
+
|
|
7
|
+
### ♻️ Refactoring
|
|
8
|
+
|
|
9
|
+
- Replaces the duplicate `resolveStyles` modules in `release-kit` and `v11y-check` with `resolveCliStyles` from `@williamthorsen/nmr-core/cli`, which each package's `route.ts` calls with its own output-style environment variable. (#1248)
|
|
10
|
+
|
|
11
|
+
## 0.16.0 — 2026-11-08
|
|
12
|
+
|
|
13
|
+
### 🎉 Features
|
|
14
|
+
|
|
15
|
+
- Prefix handled error lines with the program's bin (#1190)
|
|
16
|
+
|
|
17
|
+
Changes the handled error lines that nmr, release-kit, and v11y-check write from `Error: <message>` to `<bin>: <message>`, so that a line in combined output names the program that wrote it. It covers usage errors as well, by upgrading `@williamthorsen/toolbelt.cli` to 0.8.0, whose router writes the same prefix.
|
|
18
|
+
|
|
19
|
+
### 🐛 Bug fixes
|
|
20
|
+
|
|
21
|
+
- Apply the git output limit to nmr's buffered git calls (#1188)
|
|
22
|
+
|
|
23
|
+
Fixes the issue that several of nmr's git calls ran with Node's default 1 MiB output buffer instead of nmr-core's 64 MiB git output limit, which made them fail with `ENOBUFS` once a repository's file listing grew large enough. Applies the limit to every buffered git call that nmr makes outside test code, including the call made on the host during a delegated run.
|
|
24
|
+
|
|
25
|
+
### 🏗️ Internal features
|
|
26
|
+
|
|
27
|
+
- Adds the `@williamthorsen/nmr-core/cli` subpath, which re-exports the parser, help renderer, and router of `@williamthorsen/toolbelt.cli/candidate` 0.7.1 under toolbelt's own names, separate from the nmr-core root entry, whose own `parseArgs`, `ParseError`, and `FlagDefinition` use the same names. (#1184)
|
|
28
|
+
- Removes `parseArgs`, `parseArgsOrExit`, `ParseError`, and their types from the root entry of `@williamthorsen/nmr-core`. (#1187)
|
|
29
|
+
|
|
30
|
+
### ♻️ Refactoring
|
|
31
|
+
|
|
32
|
+
- Replaces nmr's `isObject`, release-kit's `isRecord`, and the private copies in nmr-core's `workspace.ts` and an nmr test with one `isRecord` guard exported from the root entry of `@williamthorsen/nmr-core`. (#1194)
|
|
33
|
+
- Groups `nmr-core`'s modules into role directories and moves `reportWriteResult` and `reportTemplateUpdate` from `terminal.ts` into `files/report-write-outcomes.ts`, so that `terminal.ts` no longer imports the file-writing modules. (#1196)
|
|
34
|
+
|
|
35
|
+
### 🧪 Tests
|
|
36
|
+
|
|
37
|
+
- Adds `collectChangedPaths.unit.test.ts`, which covers each record type that `collectChangedPaths` accepts, including paths that contain spaces and both paths of a rename, and each record that it refuses: a submodule record, an unrecognized record type, a truncated record, an empty path, and a rename without an original path. (#1189)
|
|
38
|
+
- Deletes the `hashWorkingTree` tests for a modified file, a staged change, an untracked file, a deleted file, a repointed symlink, a mode change, and a path containing spaces, each of which repeated a `REFERENCE_CASES` entry that compares the id with git's own tree id. (#1195)
|
|
39
|
+
|
|
40
|
+
### ⚙️ Tooling
|
|
41
|
+
|
|
42
|
+
- Adds `"prepublishOnly": "nmr build"` to `@williamthorsen/nmr-core`, the one publishable package in this repo that the new check reported. (#1192)
|
|
43
|
+
|
|
5
44
|
## 0.15.1 — 2026-11-06
|
|
6
45
|
|
|
7
46
|
### 🐛 Bug fixes
|
package/README.md
CHANGED
|
@@ -6,13 +6,7 @@ Shared utilities for node-monorepo-tools packages.
|
|
|
6
6
|
|
|
7
7
|
This package serves as the shared library foundation for the monorepo. For the nmr CLI tool, see [`@williamthorsen/nmr`](../nmr).
|
|
8
8
|
|
|
9
|
-
<!-- section:release-notes -->
|
|
10
|
-
## Release notes — v0.15.1 (2026-11-06)
|
|
11
|
-
|
|
12
|
-
### 🐛 Bug fixes
|
|
13
|
-
|
|
14
|
-
- Fixes the issue that `nmr` and `v11y-check` printed a Node system error, such as `EACCES` or `ENOENT`, as an internal error with a stack trace rather than as `Error: <message>`. (#1158)
|
|
15
|
-
<!-- /section:release-notes -->
|
|
9
|
+
<!-- section:release-notes --><!-- /section:release-notes -->
|
|
16
10
|
|
|
17
11
|
## Installation
|
|
18
12
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ResolveStreamStylesOptions, type StreamStyles } from './terminal.js';
|
|
2
|
+
export type { Command, CommandInput, CommandNode, FlagDefinition, FlagSchema, Group, OperandDefinition, ParsedFlags, ParsedOperands, ParseErrorKind, ParseOptions, ParseResult, ParseSpec, PassthroughInput, RenderHelpOptions, RunCliOptions, Writer, } from '@williamthorsen/toolbelt.cli/candidate';
|
|
3
|
+
export { createCli, defineCommand, defineGroup, parseArgs, ParseError, readStreamText, renderHelp, runCli, UsageError, } from '@williamthorsen/toolbelt.cli/candidate';
|
|
4
|
+
/** The process state from which a CLI resolves its styles, as a router passes it to each command. */
|
|
5
|
+
export type CliStyleContext = Omit<ResolveStreamStylesOptions, 'argv' | 'envVar' | 'flag'>;
|
|
6
|
+
export type ResolveCliStylesOptions = CliStyleContext & Pick<ResolveStreamStylesOptions, 'envVar'>;
|
|
7
|
+
/** Resolves the style of each output stream, throwing a `UsageError` when `envVar` doesn't name a setting. */
|
|
8
|
+
export declare function resolveCliStyles(options: ResolveCliStylesOptions): StreamStyles;
|
package/dist/esm/cli.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { UsageError } from '@williamthorsen/toolbelt.cli/candidate';
|
|
2
|
+
import { describeInvalidOutputStyle, resolveStreamStyles, } from "./terminal.js";
|
|
3
|
+
export { createCli, defineCommand, defineGroup, parseArgs, ParseError, readStreamText, renderHelp, runCli, UsageError, } from '@williamthorsen/toolbelt.cli/candidate';
|
|
4
|
+
export function resolveCliStyles(options) {
|
|
5
|
+
const { invalid, styles } = resolveStreamStyles(options);
|
|
6
|
+
if (invalid !== undefined) {
|
|
7
|
+
throw new UsageError(describeInvalidOutputStyle(invalid));
|
|
8
|
+
}
|
|
9
|
+
return styles;
|
|
10
|
+
}
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* A failure that the tool anticipated -- in the user's input, config, or environment, including a failed external
|
|
3
3
|
* command -- rather than a fault in the tool.
|
|
4
4
|
*
|
|
5
|
-
* A CLI's last-resort handler prints one of these, or a Node system error, as
|
|
6
|
-
* internal error.
|
|
5
|
+
* A CLI's last-resort handler prints one of these, or a Node system error, as `<bin>: <message>`, and anything else as
|
|
6
|
+
* an internal error.
|
|
7
7
|
*/
|
|
8
8
|
export declare class UserError extends Error {
|
|
9
9
|
constructor(message: string, options?: ErrorOptions);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Reports whether a thrown value is a failure that a CLI reports as
|
|
2
|
+
* Reports whether a thrown value is a failure that a CLI reports as `<bin>: <message>` rather than as an internal
|
|
3
3
|
* error: a `UserError`, or a Node system error such as `EACCES` or `ENOENT`, which comes from the environment.
|
|
4
4
|
*/
|
|
5
5
|
export declare function isAnticipatedFailure(error: unknown): error is Error;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type StreamStyles } from '../terminal.js';
|
|
2
|
+
import type { ManagedFileResult } from './updateManagedFile.js';
|
|
3
|
+
import type { WriteResult } from './writeFileWithCheck.js';
|
|
4
|
+
/**
|
|
5
|
+
* Prints a status line for a managed-file result, followed by its diff indented beneath it: a failure to stderr,
|
|
6
|
+
* every other outcome to stdout.
|
|
7
|
+
*/
|
|
8
|
+
export declare function reportTemplateUpdate(result: ManagedFileResult, isDryRun: boolean, styles?: StreamStyles): void;
|
|
9
|
+
/**
|
|
10
|
+
* Prints a terminal message for a write result based on its outcome: a failure to stderr, every
|
|
11
|
+
* other outcome to stdout. Each message takes its own stream's style from `styles`; without
|
|
12
|
+
* `styles`, it takes the style detected for that stream.
|
|
13
|
+
*/
|
|
14
|
+
export declare function reportWriteResult(result: WriteResult, isDryRun: boolean, styles?: StreamStyles): void;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { printError, printSkip, printSuccess } from "../terminal.js";
|
|
2
|
+
export function reportTemplateUpdate(result, isDryRun, styles) {
|
|
3
|
+
const style = styles?.stdout;
|
|
4
|
+
switch (result.outcome) {
|
|
5
|
+
case 'created':
|
|
6
|
+
printSuccess(isDryRun ? `[dry-run] Would create ${result.filePath}` : `Created ${result.filePath}`, style);
|
|
7
|
+
break;
|
|
8
|
+
case 'updated':
|
|
9
|
+
printSuccess(isDryRun ? `[dry-run] Would update ${result.filePath}` : `Updated ${result.filePath}`, style);
|
|
10
|
+
if (result.diff !== undefined && result.diff !== '') {
|
|
11
|
+
console.info(indentLines(result.diff, ' '.repeat(4)));
|
|
12
|
+
}
|
|
13
|
+
break;
|
|
14
|
+
case 'up-to-date':
|
|
15
|
+
printSuccess(`${result.filePath} (up to date)`, style);
|
|
16
|
+
break;
|
|
17
|
+
case 'failed': {
|
|
18
|
+
const detail = result.error === undefined ? '' : `: ${result.error}`;
|
|
19
|
+
printError(`Failed to update ${result.filePath}${detail}`, styles?.stderr);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function reportWriteResult(result, isDryRun, styles) {
|
|
25
|
+
const style = styles?.stdout;
|
|
26
|
+
switch (result.outcome) {
|
|
27
|
+
case 'created':
|
|
28
|
+
if (isDryRun) {
|
|
29
|
+
printSuccess(`[dry-run] Would create ${result.filePath}`, style);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
printSuccess(`Created ${result.filePath}`, style);
|
|
33
|
+
}
|
|
34
|
+
break;
|
|
35
|
+
case 'overwritten':
|
|
36
|
+
if (isDryRun) {
|
|
37
|
+
printSuccess(`[dry-run] Would overwrite ${result.filePath}`, style);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
printSuccess(`Overwrote ${result.filePath}`, style);
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
case 'up-to-date':
|
|
44
|
+
printSuccess(`${result.filePath} (up to date)`, style);
|
|
45
|
+
break;
|
|
46
|
+
case 'skipped':
|
|
47
|
+
if (result.error) {
|
|
48
|
+
printSkip(`${result.filePath} (could not read for comparison: ${result.error})`, style);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
printSkip(`${result.filePath} (already exists)`, style);
|
|
52
|
+
}
|
|
53
|
+
break;
|
|
54
|
+
case 'failed':
|
|
55
|
+
if (result.error) {
|
|
56
|
+
printError(`Failed to write ${result.filePath}: ${result.error}`, styles?.stderr);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
printError(`Failed to write ${result.filePath}`, styles?.stderr);
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function indentLines(text, indent) {
|
|
65
|
+
return text
|
|
66
|
+
.split('\n')
|
|
67
|
+
.map((line) => `${indent}${line}`)
|
|
68
|
+
.join('\n');
|
|
69
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { existsSync, lstatSync, readlinkSync } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { describeError } from '@williamthorsen/toolbelt.errors';
|
|
4
|
+
import { hasErrnoCode } from "../errors/hasErrnoCode.js";
|
|
4
5
|
import { collectChangedPaths } from "./collectChangedPaths.js";
|
|
5
6
|
import { hashGitObject, parseObjectFormat, parseTreeObject, serializeTreeObject, TREE_MODE, } from "./gitObjects.js";
|
|
6
|
-
import { hasErrnoCode } from "./hasErrnoCode.js";
|
|
7
7
|
import { runGit, runGitForBytes } from "./runGit.js";
|
|
8
8
|
export function hashWorkingTree(cwd) {
|
|
9
9
|
const toplevelResult = runGit(['rev-parse', '--show-toplevel'], cwd);
|
|
@@ -10,9 +10,10 @@ export type GitResult<T> = {
|
|
|
10
10
|
* Runs git and returns its stdout as text, or the reason it failed. A failure must never read as empty output: An
|
|
11
11
|
* unreported change would leave a caller certifying content that is no longer there.
|
|
12
12
|
*/
|
|
13
|
-
export declare function runGit(args: string[], cwd: string, input?: string): GitResult<string>;
|
|
13
|
+
export declare function runGit(args: readonly string[], cwd: string, input?: string, env?: NodeJS.ProcessEnv): GitResult<string>;
|
|
14
14
|
/**
|
|
15
15
|
* Runs git without a shell, so that a shell never interprets any part of a path or revision, and returns its raw
|
|
16
|
-
* stdout.
|
|
16
|
+
* stdout. Git runs in `env`, or in `process.env` when `env` is absent. `GIT_OPTIONAL_LOCKS=0` stops `git status`
|
|
17
|
+
* from writing its refreshed stat data back to the index.
|
|
17
18
|
*/
|
|
18
|
-
export declare function runGitForBytes(args: string[], cwd: string, input?: Buffer): GitResult<Buffer>;
|
|
19
|
+
export declare function runGitForBytes(args: readonly string[], cwd: string, input?: Buffer, env?: NodeJS.ProcessEnv): GitResult<Buffer>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
2
|
import { GIT_OUTPUT_LIMIT_BYTES } from "./gitOutputLimit.js";
|
|
3
|
-
export function runGit(args, cwd, input) {
|
|
4
|
-
const result = runGitForBytes(args, cwd, input === undefined ? undefined : Buffer.from(input, 'utf8'));
|
|
3
|
+
export function runGit(args, cwd, input, env) {
|
|
4
|
+
const result = runGitForBytes(args, cwd, input === undefined ? undefined : Buffer.from(input, 'utf8'), env);
|
|
5
5
|
return result.ok ? { ok: true, stdout: result.stdout.toString('utf8') } : result;
|
|
6
6
|
}
|
|
7
|
-
export function runGitForBytes(args, cwd, input) {
|
|
7
|
+
export function runGitForBytes(args, cwd, input, env) {
|
|
8
8
|
const result = spawnSync('git', args, {
|
|
9
9
|
cwd,
|
|
10
|
-
env: { ...process.env, GIT_OPTIONAL_LOCKS: '0' },
|
|
10
|
+
env: { ...(env ?? process.env), GIT_OPTIONAL_LOCKS: '0' },
|
|
11
11
|
input,
|
|
12
12
|
maxBuffer: GIT_OUTPUT_LIMIT_BYTES,
|
|
13
13
|
});
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
export declare const PACKAGE_NAME = "@williamthorsen/nmr-core";
|
|
2
|
-
export type { CacheDirRef, CacheEntryRef } from './cache-store.js';
|
|
3
|
-
export { readCacheEntry, readGuardedCacheEntry, readJsonCacheEntry, removeCacheDir, removeCacheEntry, resolveCacheDir, resolveCacheEntryPath, writeCacheEntry, writeGuardedCacheEntry, } from './cache-store.js';
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
9
|
-
export
|
|
10
|
-
export {
|
|
2
|
+
export type { CacheDirRef, CacheEntryRef } from './cache/cache-store.js';
|
|
3
|
+
export { readCacheEntry, readGuardedCacheEntry, readJsonCacheEntry, removeCacheDir, removeCacheEntry, resolveCacheDir, resolveCacheEntryPath, writeCacheEntry, writeGuardedCacheEntry, } from './cache/cache-store.js';
|
|
4
|
+
export { hasErrnoCode } from './errors/hasErrnoCode.js';
|
|
5
|
+
export { isAnticipatedFailure } from './errors/isAnticipatedFailure.js';
|
|
6
|
+
export { UserError } from './errors/UserError.js';
|
|
7
|
+
export { reportTemplateUpdate, reportWriteResult } from './files/report-write-outcomes.js';
|
|
8
|
+
export type { ManagedFileOutcome, ManagedFileResult } from './files/updateManagedFile.js';
|
|
9
|
+
export { updateManagedFile } from './files/updateManagedFile.js';
|
|
10
|
+
export type { WriteOutcome, WriteResult } from './files/writeFileWithCheck.js';
|
|
11
|
+
export { writeFileWithCheck } from './files/writeFileWithCheck.js';
|
|
12
|
+
export { GIT_OUTPUT_LIMIT_BYTES } from './git/gitOutputLimit.js';
|
|
13
|
+
export type { WorkingTreeHashResult } from './git/hashWorkingTree.js';
|
|
14
|
+
export { hashWorkingTree, readHeadSha } from './git/hashWorkingTree.js';
|
|
15
|
+
export type { GitResult } from './git/runGit.js';
|
|
16
|
+
export { runGit } from './git/runGit.js';
|
|
17
|
+
export { isRecord } from './portable/isRecord.js';
|
|
11
18
|
export type { GlyphSet, InvalidOutputStyle, OutputStyle, ResolveStreamStylesOptions, StatusGlyphName, StreamStyleResolution, StreamStyles, TruncateToWidthOptions, } from './terminal.js';
|
|
12
|
-
export { defineGlyphSet, describeInvalidOutputStyle, formatErrorLine, formatGlyphLine, formatStatusLine, measureGlyphColumn, measureWidth, printError, printSkip, printStep, printSuccess, reportCrash, reportError, reportFailure,
|
|
19
|
+
export { defineGlyphSet, describeInvalidOutputStyle, formatErrorLine, formatGlyphLine, formatStatusLine, measureGlyphColumn, measureWidth, printError, printSkip, printStep, printSuccess, reportCrash, reportError, reportFailure, resolveStreamStyles, STATUS_GLYPHS, truncateToWidth, wrapToWidth, } from './terminal.js';
|
|
13
20
|
export { startTimer } from './timer.js';
|
|
14
|
-
export type { ManagedFileOutcome, ManagedFileResult } from './updateManagedFile.js';
|
|
15
|
-
export { updateManagedFile } from './updateManagedFile.js';
|
|
16
|
-
export { UserError } from './UserError.js';
|
|
17
|
-
export type { WriteOutcome, WriteResult } from './writeFileWithCheck.js';
|
|
18
|
-
export { writeFileWithCheck } from './writeFileWithCheck.js';
|
|
19
21
|
export { findPackageRoot, resolveSelfVersion } from '@williamthorsen/toolbelt.packaging/candidate';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
export const PACKAGE_NAME = '@williamthorsen/nmr-core';
|
|
2
|
-
export { readCacheEntry, readGuardedCacheEntry, readJsonCacheEntry, removeCacheDir, removeCacheEntry, resolveCacheDir, resolveCacheEntryPath, writeCacheEntry, writeGuardedCacheEntry, } from "./cache-store.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
2
|
+
export { readCacheEntry, readGuardedCacheEntry, readJsonCacheEntry, removeCacheDir, removeCacheEntry, resolveCacheDir, resolveCacheEntryPath, writeCacheEntry, writeGuardedCacheEntry, } from "./cache/cache-store.js";
|
|
3
|
+
export { hasErrnoCode } from "./errors/hasErrnoCode.js";
|
|
4
|
+
export { isAnticipatedFailure } from "./errors/isAnticipatedFailure.js";
|
|
5
|
+
export { UserError } from "./errors/UserError.js";
|
|
6
|
+
export { reportTemplateUpdate, reportWriteResult } from "./files/report-write-outcomes.js";
|
|
7
|
+
export { updateManagedFile } from "./files/updateManagedFile.js";
|
|
8
|
+
export { writeFileWithCheck } from "./files/writeFileWithCheck.js";
|
|
9
|
+
export { GIT_OUTPUT_LIMIT_BYTES } from "./git/gitOutputLimit.js";
|
|
10
|
+
export { hashWorkingTree, readHeadSha } from "./git/hashWorkingTree.js";
|
|
11
|
+
export { runGit } from "./git/runGit.js";
|
|
12
|
+
export { isRecord } from "./portable/isRecord.js";
|
|
13
|
+
export { defineGlyphSet, describeInvalidOutputStyle, formatErrorLine, formatGlyphLine, formatStatusLine, measureGlyphColumn, measureWidth, printError, printSkip, printStep, printSuccess, reportCrash, reportError, reportFailure, resolveStreamStyles, STATUS_GLYPHS, truncateToWidth, wrapToWidth, } from "./terminal.js";
|
|
9
14
|
export { startTimer } from "./timer.js";
|
|
10
|
-
export { updateManagedFile } from "./updateManagedFile.js";
|
|
11
|
-
export { UserError } from "./UserError.js";
|
|
12
|
-
export { writeFileWithCheck } from "./writeFileWithCheck.js";
|
|
13
15
|
export { findPackageRoot, resolveSelfVersion } from '@williamthorsen/toolbelt.packaging/candidate';
|
package/dist/esm/terminal.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import type { Writable } from 'node:stream';
|
|
2
2
|
import { type GlyphSet, type InvalidOutputStyle, type OutputStyle, type StatusGlyphName } from '@williamthorsen/toolbelt.terminal/candidate';
|
|
3
|
-
import type { ManagedFileResult } from './updateManagedFile.js';
|
|
4
|
-
import type { WriteResult } from './writeFileWithCheck.js';
|
|
5
3
|
export type { GlyphSet, InvalidOutputStyle, OutputStyle, StatusGlyphName, TruncateToWidthOptions, } from '@williamthorsen/toolbelt.terminal/candidate';
|
|
6
4
|
export { defineGlyphSet, describeInvalidOutputStyle, measureGlyphColumn, measureWidth, STATUS_GLYPHS, truncateToWidth, wrapToWidth, } from '@williamthorsen/toolbelt.terminal/candidate';
|
|
7
5
|
export interface ResolveStreamStylesOptions {
|
|
@@ -24,8 +22,11 @@ export interface StreamStyles {
|
|
|
24
22
|
readonly stderr: OutputStyle;
|
|
25
23
|
readonly stdout: OutputStyle;
|
|
26
24
|
}
|
|
27
|
-
/**
|
|
28
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Renders the canonical `<bin>: <message>` error line, without a trailing newline, for a caller that needs it as a
|
|
27
|
+
* value.
|
|
28
|
+
*/
|
|
29
|
+
export declare function formatErrorLine(bin: string, message: string): string;
|
|
29
30
|
/**
|
|
30
31
|
* Renders a glyph from `glyphs` and a message, omitting the separating space when the glyph's variant is
|
|
31
32
|
* empty, which is what a set declares for a name whose message always opens on a word.
|
|
@@ -46,24 +47,16 @@ export declare function printSuccess(message: string, style?: OutputStyle): void
|
|
|
46
47
|
* frames. A handled error goes through `reportError` instead, so that a reader can tell the two apart.
|
|
47
48
|
*/
|
|
48
49
|
export declare function reportCrash(bin: string, error: unknown, stream?: Writable): void;
|
|
49
|
-
/** Writes the canonical `Error: <message>` line to `stream`, which an in-process CLI sets to its own `Writable`. */
|
|
50
|
-
export declare function reportError(message: string, stream?: Writable): void;
|
|
51
|
-
/**
|
|
52
|
-
* Writes a failure that reached a CLI's last-resort handler: an anticipated failure (see `isAnticipatedFailure`) as
|
|
53
|
-
* `Error: <message>`, and anything else as an internal error through `reportCrash`.
|
|
54
|
-
*/
|
|
55
|
-
export declare function reportFailure(bin: string, error: unknown, stream?: Writable): void;
|
|
56
50
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
51
|
+
* Writes the canonical `<bin>: <message>` error line to `stream`, which an in-process CLI sets to its own
|
|
52
|
+
* `Writable`.
|
|
59
53
|
*/
|
|
60
|
-
export declare function
|
|
54
|
+
export declare function reportError(bin: string, message: string, stream?: Writable): void;
|
|
61
55
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* `styles`, it takes the style detected for that stream.
|
|
56
|
+
* Writes a failure that reached a CLI's last-resort handler: an anticipated failure (see `isAnticipatedFailure`) as
|
|
57
|
+
* `<bin>: <message>`, and anything else as an internal error through `reportCrash`.
|
|
65
58
|
*/
|
|
66
|
-
export declare function
|
|
59
|
+
export declare function reportFailure(bin: string, error: unknown, stream?: Writable): void;
|
|
67
60
|
/**
|
|
68
61
|
* Resolves the style of each output stream: the flag when one is named, else the named environment
|
|
69
62
|
* variable, else detection from `CI`, the stream's terminal state, and `TERM`. Never throws; a value
|
package/dist/esm/terminal.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
2
|
import { describeError, isError } from '@williamthorsen/toolbelt.errors';
|
|
3
3
|
import { detectOutputStyle, measureGlyphColumn, resolveOutputStyle, STATUS_GLYPHS, } from '@williamthorsen/toolbelt.terminal/candidate';
|
|
4
|
-
import { isAnticipatedFailure } from "./isAnticipatedFailure.js";
|
|
4
|
+
import { isAnticipatedFailure } from "./errors/isAnticipatedFailure.js";
|
|
5
5
|
export { defineGlyphSet, describeInvalidOutputStyle, measureGlyphColumn, measureWidth, STATUS_GLYPHS, truncateToWidth, wrapToWidth, } from '@williamthorsen/toolbelt.terminal/candidate';
|
|
6
|
-
export function formatErrorLine(message) {
|
|
7
|
-
return
|
|
6
|
+
export function formatErrorLine(bin, message) {
|
|
7
|
+
return `${bin}: ${message}`;
|
|
8
8
|
}
|
|
9
9
|
export function formatGlyphLine(glyphs, style, name, message) {
|
|
10
10
|
const { text } = glyphs[style][name];
|
|
@@ -30,81 +30,19 @@ export function printSuccess(message, style = detectStreamStyle(process.stdout))
|
|
|
30
30
|
}
|
|
31
31
|
export function reportCrash(bin, error, stream = process.stderr) {
|
|
32
32
|
const frames = isError(error) && error.stack !== undefined ? extractStackFrames(error.stack) : '';
|
|
33
|
-
const header = `${bin
|
|
33
|
+
const header = `${formatErrorLine(bin, `internal error: ${describeError(error)}`)}\n`;
|
|
34
34
|
stream.write(frames === '' ? header : `${header}${frames}\n`);
|
|
35
35
|
}
|
|
36
|
-
export function reportError(message, stream = process.stderr) {
|
|
37
|
-
stream.write(`${formatErrorLine(message)}\n`);
|
|
36
|
+
export function reportError(bin, message, stream = process.stderr) {
|
|
37
|
+
stream.write(`${formatErrorLine(bin, message)}\n`);
|
|
38
38
|
}
|
|
39
39
|
export function reportFailure(bin, error, stream = process.stderr) {
|
|
40
40
|
if (isAnticipatedFailure(error)) {
|
|
41
|
-
reportError(describeError(error), stream);
|
|
41
|
+
reportError(bin, describeError(error), stream);
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
reportCrash(bin, error, stream);
|
|
45
45
|
}
|
|
46
|
-
export function reportTemplateUpdate(result, isDryRun, styles) {
|
|
47
|
-
const style = styles?.stdout;
|
|
48
|
-
switch (result.outcome) {
|
|
49
|
-
case 'created':
|
|
50
|
-
printSuccess(isDryRun ? `[dry-run] Would create ${result.filePath}` : `Created ${result.filePath}`, style);
|
|
51
|
-
break;
|
|
52
|
-
case 'updated':
|
|
53
|
-
printSuccess(isDryRun ? `[dry-run] Would update ${result.filePath}` : `Updated ${result.filePath}`, style);
|
|
54
|
-
if (result.diff !== undefined && result.diff !== '') {
|
|
55
|
-
console.info(indentLines(result.diff, ' '.repeat(4)));
|
|
56
|
-
}
|
|
57
|
-
break;
|
|
58
|
-
case 'up-to-date':
|
|
59
|
-
printSuccess(`${result.filePath} (up to date)`, style);
|
|
60
|
-
break;
|
|
61
|
-
case 'failed': {
|
|
62
|
-
const detail = result.error === undefined ? '' : `: ${result.error}`;
|
|
63
|
-
printError(`Failed to update ${result.filePath}${detail}`, styles?.stderr);
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
export function reportWriteResult(result, isDryRun, styles) {
|
|
69
|
-
const style = styles?.stdout;
|
|
70
|
-
switch (result.outcome) {
|
|
71
|
-
case 'created':
|
|
72
|
-
if (isDryRun) {
|
|
73
|
-
printSuccess(`[dry-run] Would create ${result.filePath}`, style);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
printSuccess(`Created ${result.filePath}`, style);
|
|
77
|
-
}
|
|
78
|
-
break;
|
|
79
|
-
case 'overwritten':
|
|
80
|
-
if (isDryRun) {
|
|
81
|
-
printSuccess(`[dry-run] Would overwrite ${result.filePath}`, style);
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
printSuccess(`Overwrote ${result.filePath}`, style);
|
|
85
|
-
}
|
|
86
|
-
break;
|
|
87
|
-
case 'up-to-date':
|
|
88
|
-
printSuccess(`${result.filePath} (up to date)`, style);
|
|
89
|
-
break;
|
|
90
|
-
case 'skipped':
|
|
91
|
-
if (result.error) {
|
|
92
|
-
printSkip(`${result.filePath} (could not read for comparison: ${result.error})`, style);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
printSkip(`${result.filePath} (already exists)`, style);
|
|
96
|
-
}
|
|
97
|
-
break;
|
|
98
|
-
case 'failed':
|
|
99
|
-
if (result.error) {
|
|
100
|
-
printError(`Failed to write ${result.filePath}: ${result.error}`, styles?.stderr);
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
printError(`Failed to write ${result.filePath}`, styles?.stderr);
|
|
104
|
-
}
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
46
|
export function resolveStreamStyles(options) {
|
|
109
47
|
const { argv = [], env, envVar, flag, stderrIsTty, stdoutIsTty } = options;
|
|
110
48
|
const stderrResolution = resolveOutputStyle({ argv, env, envVar, flag, isTty: stderrIsTty });
|
|
@@ -120,9 +58,3 @@ function extractStackFrames(stack) {
|
|
|
120
58
|
const firstFrameIndex = lines.findIndex((line) => /^\s+at /.test(line));
|
|
121
59
|
return firstFrameIndex === -1 ? '' : lines.slice(firstFrameIndex).join('\n');
|
|
122
60
|
}
|
|
123
|
-
function indentLines(text, indent) {
|
|
124
|
-
return text
|
|
125
|
-
.split('\n')
|
|
126
|
-
.map((line) => `${indent}${line}`)
|
|
127
|
-
.join('\n');
|
|
128
|
-
}
|
|
@@ -2,7 +2,8 @@ import { existsSync, readFileSync } from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { findDirectoryChainMatch } from '@williamthorsen/toolbelt.filesystem';
|
|
4
4
|
import { parse } from 'yaml';
|
|
5
|
-
import {
|
|
5
|
+
import { isRecord } from "../portable/isRecord.js";
|
|
6
|
+
import { matchPackageDirs, resolvePackageDirs, splitWorkspacePatterns } from "./workspace-patterns.js";
|
|
6
7
|
const PACKAGE_MANIFEST_FILENAME = 'package.json';
|
|
7
8
|
const WORKSPACE_MANIFEST_FILENAME = 'pnpm-workspace.yaml';
|
|
8
9
|
export function findMonorepoRoot(startDir) {
|
|
@@ -13,11 +14,11 @@ export function isMonorepoRoot(dir) {
|
|
|
13
14
|
}
|
|
14
15
|
export function readWorkspaceOverrides(monorepoRoot) {
|
|
15
16
|
const manifestReadResult = readWorkspaceManifest(monorepoRoot);
|
|
16
|
-
if (manifestReadResult.kind !== 'parsed' || !
|
|
17
|
+
if (manifestReadResult.kind !== 'parsed' || !isRecord(manifestReadResult.value)) {
|
|
17
18
|
return undefined;
|
|
18
19
|
}
|
|
19
20
|
const overrides = manifestReadResult.value['overrides'];
|
|
20
|
-
return
|
|
21
|
+
return isRecord(overrides) ? readStringValues(overrides) : undefined;
|
|
21
22
|
}
|
|
22
23
|
export function readWorkspacePackageNames(packageDirs) {
|
|
23
24
|
const names = [];
|
|
@@ -29,7 +30,7 @@ export function readWorkspacePackageNames(packageDirs) {
|
|
|
29
30
|
catch {
|
|
30
31
|
continue;
|
|
31
32
|
}
|
|
32
|
-
if (
|
|
33
|
+
if (isRecord(parsedManifest) && typeof parsedManifest['name'] === 'string') {
|
|
33
34
|
names.push(parsedManifest['name']);
|
|
34
35
|
}
|
|
35
36
|
}
|
|
@@ -66,7 +67,7 @@ function diagnoseEmptyCause(monorepoRoot, patterns) {
|
|
|
66
67
|
return 'no-package';
|
|
67
68
|
}
|
|
68
69
|
function readDeclaredPackages(parsedManifest) {
|
|
69
|
-
if (!
|
|
70
|
+
if (!isRecord(parsedManifest))
|
|
70
71
|
return { kind: 'absent' };
|
|
71
72
|
const packages = parsedManifest['packages'];
|
|
72
73
|
if (packages === undefined || packages === null)
|
|
@@ -77,9 +78,6 @@ function readDeclaredPackages(parsedManifest) {
|
|
|
77
78
|
return { kind: 'unreadable' };
|
|
78
79
|
return packages.length === 0 ? { kind: 'absent' } : { kind: 'patterns', patterns: packages };
|
|
79
80
|
}
|
|
80
|
-
function isObject(value) {
|
|
81
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
82
|
-
}
|
|
83
81
|
function readStringValues(record) {
|
|
84
82
|
const result = {};
|
|
85
83
|
for (const [key, value] of Object.entries(record)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@williamthorsen/nmr-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Shared utilities for node-monorepo-tools packages",
|
|
6
6
|
"keywords": [
|
|
@@ -27,10 +27,15 @@
|
|
|
27
27
|
"types": "./dist/esm/index.d.ts",
|
|
28
28
|
"default": "./dist/esm/index.js"
|
|
29
29
|
},
|
|
30
|
+
"./cli": {
|
|
31
|
+
"nmr-source": "./src/cli.ts",
|
|
32
|
+
"types": "./dist/esm/cli.d.ts",
|
|
33
|
+
"default": "./dist/esm/cli.js"
|
|
34
|
+
},
|
|
30
35
|
"./workspace": {
|
|
31
|
-
"nmr-source": "./src/workspace.ts",
|
|
32
|
-
"types": "./dist/esm/workspace.d.ts",
|
|
33
|
-
"default": "./dist/esm/workspace.js"
|
|
36
|
+
"nmr-source": "./src/workspace/workspace.ts",
|
|
37
|
+
"types": "./dist/esm/workspace/workspace.d.ts",
|
|
38
|
+
"default": "./dist/esm/workspace/workspace.js"
|
|
34
39
|
}
|
|
35
40
|
},
|
|
36
41
|
"files": [
|
|
@@ -39,7 +44,8 @@
|
|
|
39
44
|
"dist"
|
|
40
45
|
],
|
|
41
46
|
"dependencies": {
|
|
42
|
-
"@williamthorsen/toolbelt.
|
|
47
|
+
"@williamthorsen/toolbelt.cli": "0.8.0",
|
|
48
|
+
"@williamthorsen/toolbelt.datetime": "4.1.1",
|
|
43
49
|
"@williamthorsen/toolbelt.errors": "0.9.0",
|
|
44
50
|
"@williamthorsen/toolbelt.filesystem": "0.10.0",
|
|
45
51
|
"@williamthorsen/toolbelt.packaging": "0.7.0",
|
|
@@ -49,7 +55,7 @@
|
|
|
49
55
|
},
|
|
50
56
|
"devDependencies": {
|
|
51
57
|
"@williamthorsen/toolbelt.testing": "0.8.0",
|
|
52
|
-
"@williamthorsen/toolbelt.vitest": "0.13.
|
|
58
|
+
"@williamthorsen/toolbelt.vitest": "0.13.2",
|
|
53
59
|
"vitest": "5.1.0"
|
|
54
60
|
},
|
|
55
61
|
"engines": {
|
package/dist/esm/parseArgs.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { UserError } from './UserError.js';
|
|
2
|
-
/** Schema entry describing a single CLI flag. */
|
|
3
|
-
export interface FlagDefinition {
|
|
4
|
-
long: string;
|
|
5
|
-
type: 'boolean' | 'string';
|
|
6
|
-
short?: string;
|
|
7
|
-
}
|
|
8
|
-
/** Map of camelCase property names to their flag definitions. */
|
|
9
|
-
export type FlagSchema = Record<string, FlagDefinition>;
|
|
10
|
-
/** Infers the result type from a flag schema: booleans become `boolean`, strings become `string | undefined`. */
|
|
11
|
-
export type ParsedFlags<S extends FlagSchema> = {
|
|
12
|
-
[K in keyof S]: S[K]['type'] extends 'boolean' ? boolean : string | undefined;
|
|
13
|
-
};
|
|
14
|
-
/** Return type of `parseArgs`: typed flags plus collected positionals. */
|
|
15
|
-
export interface ParsedArgs<S extends FlagSchema> {
|
|
16
|
-
flags: ParsedFlags<S>;
|
|
17
|
-
positionals: string[];
|
|
18
|
-
}
|
|
19
|
-
/** Options controlling how `parseArgs` handles input beyond the flag schema. */
|
|
20
|
-
export interface ParseArgsOptions {
|
|
21
|
-
/** When true, positionals are collected; otherwise an unexpected positional throws. Defaults to false. */
|
|
22
|
-
allowPositionals?: boolean;
|
|
23
|
-
}
|
|
24
|
-
/** Discriminates the failure modes that `parseArgs` reports. */
|
|
25
|
-
export type ParseErrorKind = 'unknown-flag' | 'missing-value' | 'unexpected-value' | 'unexpected-positional';
|
|
26
|
-
/**
|
|
27
|
-
* Error thrown by `parseArgs` on invalid input.
|
|
28
|
-
*
|
|
29
|
-
* Carries the failure `kind` and the offending token in `flag`: a flag as the user typed it, or the
|
|
30
|
-
* positional value for `'unexpected-positional'`. Its `message` is composed from those fields, so error
|
|
31
|
-
* wording is uniform across every kind.
|
|
32
|
-
*/
|
|
33
|
-
export declare class ParseError extends UserError {
|
|
34
|
-
readonly kind: ParseErrorKind;
|
|
35
|
-
readonly flag: string;
|
|
36
|
-
constructor(kind: ParseErrorKind, flag: string);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Parses a pre-sliced argv array against a flag schema.
|
|
40
|
-
*
|
|
41
|
-
* Delegates tokenizing to `node:util.parseArgs` (non-strict, with tokens) and validates the token
|
|
42
|
-
* stream against the schema. Throws `ParseError` on an unknown flag, a missing string-flag value, a
|
|
43
|
-
* value supplied to a boolean flag, or (unless `options.allowPositionals` is set) an unexpected
|
|
44
|
-
* positional argument. Does not write output or exit.
|
|
45
|
-
*/
|
|
46
|
-
export declare function parseArgs<S extends FlagSchema>(argv: string[], schema: S, options?: ParseArgsOptions): ParsedArgs<S>;
|
|
47
|
-
/** Parses argv, or prints a usage error to stderr and exits non-zero. */
|
|
48
|
-
export declare function parseArgsOrExit<S extends FlagSchema>(argv: string[], schema: S, options?: ParseArgsOptions): ParsedArgs<S>;
|
package/dist/esm/parseArgs.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import process from 'node:process';
|
|
2
|
-
import { parseArgs as nodeParseArgs } from 'node:util';
|
|
3
|
-
import { reportError } from "./terminal.js";
|
|
4
|
-
import { UserError } from "./UserError.js";
|
|
5
|
-
const initialFlagValues = { boolean: false, string: undefined };
|
|
6
|
-
export class ParseError extends UserError {
|
|
7
|
-
kind;
|
|
8
|
-
flag;
|
|
9
|
-
constructor(kind, flag) {
|
|
10
|
-
super(formatParseErrorMessage(kind, flag));
|
|
11
|
-
this.name = 'ParseError';
|
|
12
|
-
this.kind = kind;
|
|
13
|
-
this.flag = flag;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export function parseArgs(argv, schema, options = {}) {
|
|
17
|
-
const nodeOptions = {};
|
|
18
|
-
const entriesByName = new Map();
|
|
19
|
-
const flags = {};
|
|
20
|
-
for (const [key, definition] of Object.entries(schema)) {
|
|
21
|
-
const name = definition.long.replace(/^--/, '');
|
|
22
|
-
nodeOptions[name] =
|
|
23
|
-
definition.short === undefined
|
|
24
|
-
? { type: definition.type }
|
|
25
|
-
: { type: definition.type, short: definition.short.replace(/^-/, '') };
|
|
26
|
-
entriesByName.set(name, { key, definition });
|
|
27
|
-
flags[key] = initialFlagValues[definition.type];
|
|
28
|
-
}
|
|
29
|
-
const { tokens } = nodeParseArgs({
|
|
30
|
-
args: argv,
|
|
31
|
-
options: nodeOptions,
|
|
32
|
-
strict: false,
|
|
33
|
-
allowPositionals: true,
|
|
34
|
-
tokens: true,
|
|
35
|
-
});
|
|
36
|
-
const positionals = [];
|
|
37
|
-
for (const token of tokens) {
|
|
38
|
-
if (token.kind === 'positional') {
|
|
39
|
-
if (options.allowPositionals !== true) {
|
|
40
|
-
throw new ParseError('unexpected-positional', token.value);
|
|
41
|
-
}
|
|
42
|
-
positionals.push(token.value);
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (token.kind !== 'option') {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
const entry = entriesByName.get(token.name);
|
|
49
|
-
if (entry === undefined) {
|
|
50
|
-
throw new ParseError('unknown-flag', token.rawName);
|
|
51
|
-
}
|
|
52
|
-
if (entry.definition.type === 'boolean') {
|
|
53
|
-
if (token.value !== undefined) {
|
|
54
|
-
throw new ParseError('unexpected-value', token.rawName);
|
|
55
|
-
}
|
|
56
|
-
flags[entry.key] = true;
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const value = token.value;
|
|
60
|
-
if (value === undefined || value === '' || (!token.inlineValue && value.startsWith('-') && value !== '-')) {
|
|
61
|
-
throw new ParseError('missing-value', token.rawName);
|
|
62
|
-
}
|
|
63
|
-
flags[entry.key] = value;
|
|
64
|
-
}
|
|
65
|
-
return { flags, positionals };
|
|
66
|
-
}
|
|
67
|
-
export function parseArgsOrExit(argv, schema, options = {}) {
|
|
68
|
-
try {
|
|
69
|
-
return parseArgs(argv, schema, options);
|
|
70
|
-
}
|
|
71
|
-
catch (error) {
|
|
72
|
-
if (error instanceof ParseError) {
|
|
73
|
-
reportError(error.message);
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
function formatParseErrorMessage(kind, flag) {
|
|
80
|
-
switch (kind) {
|
|
81
|
-
case 'unknown-flag':
|
|
82
|
-
return `Unknown option: ${flag}`;
|
|
83
|
-
case 'missing-value':
|
|
84
|
-
return `Missing value for option: ${flag}`;
|
|
85
|
-
case 'unexpected-value':
|
|
86
|
-
return `Option does not accept a value: ${flag}`;
|
|
87
|
-
case 'unexpected-positional':
|
|
88
|
-
return `Unexpected positional argument: ${flag}`;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|