@doist/cli-core 0.6.0 → 0.7.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.
- package/CHANGELOG.md +6 -0
- package/README.md +34 -13
- package/dist/commands/errors.d.ts +1 -1
- package/dist/commands/errors.d.ts.map +1 -1
- package/dist/commands/index.d.ts +2 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +1 -0
- package/dist/commands/index.js.map +1 -1
- package/dist/commands/update.d.ts +88 -0
- package/dist/commands/update.d.ts.map +1 -0
- package/dist/commands/update.js +294 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +49 -0
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
## [0.7.0](https://github.com/Doist/cli-core/compare/v0.6.0...v0.7.0) (2026-05-09)
|
|
2
|
+
|
|
3
|
+
### Features
|
|
4
|
+
|
|
5
|
+
* add registerUpdateCommand to ./commands subpath ([#9](https://github.com/Doist/cli-core/issues/9)) ([17c6dc7](https://github.com/Doist/cli-core/commit/17c6dc74cd180ddcdb3e30bf2395ad3db05fe5c9))
|
|
6
|
+
|
|
1
7
|
## [0.6.0](https://github.com/Doist/cli-core/compare/v0.5.0...v0.6.0) (2026-05-09)
|
|
2
8
|
|
|
3
9
|
### Features
|
package/README.md
CHANGED
|
@@ -12,19 +12,19 @@ npm install @doist/cli-core
|
|
|
12
12
|
|
|
13
13
|
## What's in it
|
|
14
14
|
|
|
15
|
-
| Module | Key exports | Purpose
|
|
16
|
-
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
|
17
|
-
| `commands` (subpath) | `registerChangelogCommand` (
|
|
18
|
-
| `config` | `getConfigPath`, `readConfig`, `readConfigStrict`, `writeConfig`, `updateConfig`
|
|
19
|
-
| `empty` | `printEmpty` | Print an empty-state message gated on `--json` / `--ndjson` so machine consumers never see human strings on stdout.
|
|
20
|
-
| `errors` | `CliError` | Typed CLI error class with `code` and exit-code mapping.
|
|
21
|
-
| `global-args` | `parseGlobalArgs`, `createGlobalArgsStore`, `createAccessibleGate`, `createSpinnerGate`, `getProgressJsonlPath`, `isProgressJsonlEnabled` | Parse well-known global flags (`--json`, `--ndjson`, `--quiet`, `--verbose`, `--accessible`, `--no-spinner`, `--progress-jsonl`) and derive predicates from them.
|
|
22
|
-
| `json` | `formatJson`, `formatNdjson` | Stable JSON / newline-delimited JSON formatting for stdout.
|
|
23
|
-
| `markdown` (subpath) | `preloadMarkdown`, `renderMarkdown`, `TerminalRendererOptions` | Lazy-init terminal markdown renderer. **Requires** `marked` and `marked-terminal-renderer` as peer-deps — install only if your CLI uses this subpath.
|
|
24
|
-
| `options` | `ViewOptions` | Type contract for `{ json?, ndjson? }` per-command options that machine-output gates derive from.
|
|
25
|
-
| `spinner` | `createSpinner` | Loading spinner factory wrapping `yocto-spinner` with disable gates.
|
|
26
|
-
| `terminal` | `isCI`, `isStderrTTY`, `isStdinTTY`, `isStdoutTTY` | TTY / CI detection helpers.
|
|
27
|
-
| `testing` (subpath) | `describeEmptyMachineOutput` | Vitest helpers reusable by consuming CLIs (e.g. parametrised empty-state suite covering `--json` / `--ndjson` / human modes).
|
|
15
|
+
| Module | Key exports | Purpose |
|
|
16
|
+
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
17
|
+
| `commands` (subpath) | `registerChangelogCommand`, `registerUpdateCommand` (+ semver helpers) | Commander wiring for cli-core's standard commands (e.g. `<cli> changelog`, `<cli> update`, `<cli> update switch`). **Requires** `commander` as an optional peer-dep. |
|
|
18
|
+
| `config` | `getConfigPath`, `readConfig`, `readConfigStrict`, `writeConfig`, `updateConfig`, `CoreConfig`, `UpdateChannel` | Read / write a per-CLI JSON config file with typed error codes; `CoreConfig` is the shape of fields cli-core itself owns (extend it for per-CLI fields). |
|
|
19
|
+
| `empty` | `printEmpty` | Print an empty-state message gated on `--json` / `--ndjson` so machine consumers never see human strings on stdout. |
|
|
20
|
+
| `errors` | `CliError` | Typed CLI error class with `code` and exit-code mapping. |
|
|
21
|
+
| `global-args` | `parseGlobalArgs`, `createGlobalArgsStore`, `createAccessibleGate`, `createSpinnerGate`, `getProgressJsonlPath`, `isProgressJsonlEnabled` | Parse well-known global flags (`--json`, `--ndjson`, `--quiet`, `--verbose`, `--accessible`, `--no-spinner`, `--progress-jsonl`) and derive predicates from them. |
|
|
22
|
+
| `json` | `formatJson`, `formatNdjson` | Stable JSON / newline-delimited JSON formatting for stdout. |
|
|
23
|
+
| `markdown` (subpath) | `preloadMarkdown`, `renderMarkdown`, `TerminalRendererOptions` | Lazy-init terminal markdown renderer. **Requires** `marked` and `marked-terminal-renderer` as peer-deps — install only if your CLI uses this subpath. |
|
|
24
|
+
| `options` | `ViewOptions` | Type contract for `{ json?, ndjson? }` per-command options that machine-output gates derive from. |
|
|
25
|
+
| `spinner` | `createSpinner` | Loading spinner factory wrapping `yocto-spinner` with disable gates. |
|
|
26
|
+
| `terminal` | `isCI`, `isStderrTTY`, `isStdinTTY`, `isStdoutTTY` | TTY / CI detection helpers. |
|
|
27
|
+
| `testing` (subpath) | `describeEmptyMachineOutput` | Vitest helpers reusable by consuming CLIs (e.g. parametrised empty-state suite covering `--json` / `--ndjson` / human modes). |
|
|
28
28
|
|
|
29
29
|
## Usage
|
|
30
30
|
|
|
@@ -100,6 +100,27 @@ registerChangelogCommand(program, {
|
|
|
100
100
|
|
|
101
101
|
The helper throws `CliError` (`INVALID_TYPE` for a bad `--count`, `FILE_READ_ERROR` if the file can't be read) so the CLI's top-level error handler formats and exits.
|
|
102
102
|
|
|
103
|
+
Wire `<cli> update` and `<cli> update switch` similarly:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { createSpinner, getConfigPath } from '@doist/cli-core'
|
|
107
|
+
import { registerUpdateCommand } from '@doist/cli-core/commands'
|
|
108
|
+
import packageJson from '../package.json' with { type: 'json' }
|
|
109
|
+
|
|
110
|
+
const { withSpinner } = createSpinner()
|
|
111
|
+
registerUpdateCommand(program, {
|
|
112
|
+
packageName: '@doist/todoist-cli',
|
|
113
|
+
currentVersion: packageJson.version,
|
|
114
|
+
configPath: getConfigPath('todoist-cli'),
|
|
115
|
+
changelogCommandName: 'td changelog',
|
|
116
|
+
withSpinner,
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`update` checks the configured channel's npm dist-tag (`stable` → `latest`, `pre-release` → `next`), compares against `currentVersion`, and shells out to `npm i -g` (or `pnpm add -g` if `npm_execpath` indicates pnpm). `update switch --stable | --pre-release` flips the persisted `update_channel` field via `updateConfig`, preserving any sibling keys. Both subcommands accept `--json` / `--ndjson`. Errors are `CliError` (`INVALID_FLAGS`, `UPDATE_CHECK_FAILED`, `UPDATE_INSTALL_FAILED`, or the canonical `CONFIG_*` codes if the config file is broken).
|
|
121
|
+
|
|
122
|
+
The semver helpers (`parseVersion`, `compareVersions`, `isNewer`, `getInstallTag`, `fetchLatestVersion`, `getConfiguredUpdateChannel`) are also exported for ad-hoc use outside the registered command.
|
|
123
|
+
|
|
103
124
|
## Development
|
|
104
125
|
|
|
105
126
|
```bash
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* into the `CliErrorCode` aggregator in `../errors.ts` so consumers don't have
|
|
4
4
|
* to redeclare them in their own `TCode` union when catching.
|
|
5
5
|
*/
|
|
6
|
-
export type CommandErrorCode = 'INVALID_TYPE' | 'FILE_READ_ERROR';
|
|
6
|
+
export type CommandErrorCode = 'INVALID_TYPE' | 'FILE_READ_ERROR' | 'INVALID_FLAGS' | 'INVALID_UPDATE_CHANNEL' | 'UPDATE_CHECK_FAILED' | 'UPDATE_INSTALL_FAILED';
|
|
7
7
|
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/commands/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/commands/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GACtB,cAAc,GACd,iBAAiB,GACjB,eAAe,GACf,wBAAwB,GACxB,qBAAqB,GACrB,uBAAuB,CAAA"}
|
package/dist/commands/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { registerChangelogCommand } from './changelog.js';
|
|
2
2
|
export type { ChangelogBulletMarker, ChangelogCommandOptions, ChangelogHeadingLevel, } from './changelog.js';
|
|
3
3
|
export type { CommandErrorCode } from './errors.js';
|
|
4
|
+
export { compareVersions, fetchLatestVersion, getConfiguredUpdateChannel, getInstallTag, isNewer, parseVersion, registerUpdateCommand, } from './update.js';
|
|
5
|
+
export type { UpdateCommandOptions } from './update.js';
|
|
4
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/commands/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AACzD,YAAY,EACR,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,GACxB,MAAM,gBAAgB,CAAA;AACvB,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/commands/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AACzD,YAAY,EACR,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,GACxB,MAAM,gBAAgB,CAAA;AACvB,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AACnD,OAAO,EACH,eAAe,EACf,kBAAkB,EAClB,0BAA0B,EAC1B,aAAa,EACb,OAAO,EACP,YAAY,EACZ,qBAAqB,GACxB,MAAM,aAAa,CAAA;AACpB,YAAY,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/commands/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/commands/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/commands/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAOzD,OAAO,EACH,eAAe,EACf,kBAAkB,EAClB,0BAA0B,EAC1B,aAAa,EACb,OAAO,EACP,YAAY,EACZ,qBAAqB,GACxB,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Command } from 'commander';
|
|
2
|
+
import { type UpdateChannel } from '../config.js';
|
|
3
|
+
import type { SpinnerOptions } from '../spinner.js';
|
|
4
|
+
type WithSpinner = <T>(options: SpinnerOptions, op: () => Promise<T>) => Promise<T>;
|
|
5
|
+
export type UpdateCommandOptions = {
|
|
6
|
+
/** npm package name to query the registry for, e.g. `'@doist/todoist-cli'`. */
|
|
7
|
+
packageName: string;
|
|
8
|
+
/** Caller's current version (read from their package.json at startup). */
|
|
9
|
+
currentVersion: string;
|
|
10
|
+
/** Absolute path to the CLI's config file (use `getConfigPath(appName)`). */
|
|
11
|
+
configPath: string;
|
|
12
|
+
/** Override the npm registry base URL. Default `'https://registry.npmjs.org'`. */
|
|
13
|
+
registryUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Hint shown after a successful stable-channel install, e.g. `'td changelog'`.
|
|
16
|
+
* Omit to skip the post-update tip.
|
|
17
|
+
*/
|
|
18
|
+
changelogCommandName?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Optional spinner runner — typically `withSpinner` from a `createSpinner()`
|
|
21
|
+
* kit. When omitted, the operation runs without a spinner.
|
|
22
|
+
*/
|
|
23
|
+
withSpinner?: WithSpinner;
|
|
24
|
+
};
|
|
25
|
+
type ParsedVersion = {
|
|
26
|
+
major: number;
|
|
27
|
+
minor: number;
|
|
28
|
+
patch: number;
|
|
29
|
+
prerelease: string | undefined;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Parse a semver-ish string into core triplet + optional prerelease tag. Build
|
|
33
|
+
* metadata (`+build…`) is stripped per semver §10 since it doesn't affect
|
|
34
|
+
* ordering. Throws on input that doesn't have a numeric `major.minor.patch`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseVersion(version: string): ParsedVersion;
|
|
37
|
+
/** Numeric-aware semver compare. Returns -1, 0, or 1. Pre-releases sort below the same core. */
|
|
38
|
+
export declare function compareVersions(a: string, b: string): number;
|
|
39
|
+
/** Returns true when `candidate` is strictly newer than `current` per semver. */
|
|
40
|
+
export declare function isNewer(current: string, candidate: string): boolean;
|
|
41
|
+
/** Map an `UpdateChannel` to its npm dist-tag. */
|
|
42
|
+
export declare function getInstallTag(channel: UpdateChannel): string;
|
|
43
|
+
export declare function fetchLatestVersion(args: {
|
|
44
|
+
packageName: string;
|
|
45
|
+
channel: UpdateChannel;
|
|
46
|
+
registryUrl?: string;
|
|
47
|
+
}): Promise<string>;
|
|
48
|
+
/**
|
|
49
|
+
* Read the persisted channel. Missing config returns `'stable'`; broken config
|
|
50
|
+
* surfaces as a canonical `CONFIG_*` `CliError`; an unrecognised
|
|
51
|
+
* `update_channel` value surfaces as `INVALID_UPDATE_CHANNEL`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getConfiguredUpdateChannel(configPath: string): Promise<UpdateChannel>;
|
|
54
|
+
/**
|
|
55
|
+
* Register the standard `<cli> update` and `<cli> update switch` commands on a
|
|
56
|
+
* Commander program. The `update` action checks the npm registry for the
|
|
57
|
+
* configured channel's dist-tag, compares against `currentVersion`, and shells
|
|
58
|
+
* out to `npm i -g` (or `pnpm add -g` if pnpm is detected on the running
|
|
59
|
+
* script's path). `update switch` flips the persisted `update_channel` field
|
|
60
|
+
* between `'stable'` and `'pre-release'`.
|
|
61
|
+
*
|
|
62
|
+
* Errors as `CliError` (`INVALID_FLAGS`, `INVALID_UPDATE_CHANNEL`,
|
|
63
|
+
* `UPDATE_CHECK_FAILED`, `UPDATE_INSTALL_FAILED`, or the canonical `CONFIG_*`
|
|
64
|
+
* codes when the config file is broken). The consumer's top-level error
|
|
65
|
+
* handler is expected to format and exit.
|
|
66
|
+
*
|
|
67
|
+
* Both subcommands accept `--json` / `--ndjson`; success branches emit a single
|
|
68
|
+
* record (`{ currentVersion, latestVersion, channel, updateAvailable | installed }`
|
|
69
|
+
* for `update`, `{ channel }` for `update switch`).
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { getConfigPath, createSpinner } from '@doist/cli-core'
|
|
73
|
+
* import { registerUpdateCommand } from '@doist/cli-core/commands'
|
|
74
|
+
* import packageJson from '../package.json' with { type: 'json' }
|
|
75
|
+
*
|
|
76
|
+
* const { withSpinner } = createSpinner()
|
|
77
|
+
* registerUpdateCommand(program, {
|
|
78
|
+
* packageName: '@doist/todoist-cli',
|
|
79
|
+
* currentVersion: packageJson.version,
|
|
80
|
+
* configPath: getConfigPath('todoist-cli'),
|
|
81
|
+
* changelogCommandName: 'td changelog',
|
|
82
|
+
* withSpinner,
|
|
83
|
+
* })
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function registerUpdateCommand(program: Command, options: UpdateCommandOptions): void;
|
|
87
|
+
export {};
|
|
88
|
+
//# sourceMappingURL=update.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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,kBAAkB,CAAC,IAAI,EAAE;IAC3C,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,aAAa,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,CAAA;CACvB,GAAG,OAAO,CAAC,MAAM,CAAC,CAalB;AAMD;;;;GAIG;AACH,wBAAsB,0BAA0B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAW3F;AAmOD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,oBAAoB,GAAG,IAAI,CAyB3F"}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { readConfigOrThrow, updateConfigOrThrow, } from '../config.js';
|
|
4
|
+
import { CliError } from '../errors.js';
|
|
5
|
+
import { formatJson, formatNdjson } from '../json.js';
|
|
6
|
+
const DEFAULT_REGISTRY_URL = 'https://registry.npmjs.org';
|
|
7
|
+
/**
|
|
8
|
+
* Parse a semver-ish string into core triplet + optional prerelease tag. Build
|
|
9
|
+
* metadata (`+build…`) is stripped per semver §10 since it doesn't affect
|
|
10
|
+
* ordering. Throws on input that doesn't have a numeric `major.minor.patch`.
|
|
11
|
+
*/
|
|
12
|
+
export function parseVersion(version) {
|
|
13
|
+
// Strip leading `v` and any `+build` metadata before splitting prerelease.
|
|
14
|
+
const stripped = version.replace(/^v/, '').split('+', 1)[0];
|
|
15
|
+
const [core, ...rest] = stripped.split('-');
|
|
16
|
+
const [major, minor, patch] = core.split('.').map(Number);
|
|
17
|
+
if (!Number.isInteger(major) || !Number.isInteger(minor) || !Number.isInteger(patch)) {
|
|
18
|
+
throw new Error(`Invalid version string: '${version}'`);
|
|
19
|
+
}
|
|
20
|
+
return { major, minor, patch, prerelease: rest.length > 0 ? rest.join('-') : undefined };
|
|
21
|
+
}
|
|
22
|
+
/** Numeric-aware semver compare. Returns -1, 0, or 1. Pre-releases sort below the same core. */
|
|
23
|
+
export function compareVersions(a, b) {
|
|
24
|
+
const left = parseVersion(a);
|
|
25
|
+
const right = parseVersion(b);
|
|
26
|
+
for (const key of ['major', 'minor', 'patch']) {
|
|
27
|
+
if (left[key] !== right[key])
|
|
28
|
+
return left[key] > right[key] ? 1 : -1;
|
|
29
|
+
}
|
|
30
|
+
if (!left.prerelease && right.prerelease)
|
|
31
|
+
return 1;
|
|
32
|
+
if (left.prerelease && !right.prerelease)
|
|
33
|
+
return -1;
|
|
34
|
+
if (left.prerelease && right.prerelease) {
|
|
35
|
+
return left.prerelease.localeCompare(right.prerelease, undefined, { numeric: true });
|
|
36
|
+
}
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
/** Returns true when `candidate` is strictly newer than `current` per semver. */
|
|
40
|
+
export function isNewer(current, candidate) {
|
|
41
|
+
return compareVersions(candidate, current) > 0;
|
|
42
|
+
}
|
|
43
|
+
/** Map an `UpdateChannel` to its npm dist-tag. */
|
|
44
|
+
export function getInstallTag(channel) {
|
|
45
|
+
return channel === 'pre-release' ? 'next' : 'latest';
|
|
46
|
+
}
|
|
47
|
+
export async function fetchLatestVersion(args) {
|
|
48
|
+
const base = args.registryUrl ?? DEFAULT_REGISTRY_URL;
|
|
49
|
+
const url = `${base}/${args.packageName}/${getInstallTag(args.channel)}`;
|
|
50
|
+
// The abbreviated metadata format skips `readme`, dependency trees, etc.
|
|
51
|
+
// (~50× smaller than the default response on a typical package).
|
|
52
|
+
const response = await fetch(url, {
|
|
53
|
+
headers: { Accept: 'application/vnd.npm.install-v1+json' },
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw new Error(`Registry request failed (HTTP ${response.status})`);
|
|
57
|
+
}
|
|
58
|
+
const data = (await response.json());
|
|
59
|
+
return data.version;
|
|
60
|
+
}
|
|
61
|
+
function isValidChannel(value) {
|
|
62
|
+
return value === 'stable' || value === 'pre-release';
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Read the persisted channel. Missing config returns `'stable'`; broken config
|
|
66
|
+
* surfaces as a canonical `CONFIG_*` `CliError`; an unrecognised
|
|
67
|
+
* `update_channel` value surfaces as `INVALID_UPDATE_CHANNEL`.
|
|
68
|
+
*/
|
|
69
|
+
export async function getConfiguredUpdateChannel(configPath) {
|
|
70
|
+
const config = await readConfigOrThrow(configPath);
|
|
71
|
+
const channel = config.update_channel;
|
|
72
|
+
if (channel === undefined)
|
|
73
|
+
return 'stable';
|
|
74
|
+
if (!isValidChannel(channel)) {
|
|
75
|
+
throw new CliError('INVALID_UPDATE_CHANNEL', `Invalid update_channel '${String(channel)}' in config; expected 'stable' or 'pre-release'`);
|
|
76
|
+
}
|
|
77
|
+
return channel;
|
|
78
|
+
}
|
|
79
|
+
function detectPackageManager() {
|
|
80
|
+
// `npm_execpath` is only set when the CLI is invoked via a package manager
|
|
81
|
+
// (e.g. `npm run`). Globally-installed CLIs run directly from the shell, so
|
|
82
|
+
// also widen the search to the running script path and node binary path —
|
|
83
|
+
// both typically include `pnpm` when installed via pnpm's global store.
|
|
84
|
+
const haystack = [process.env.npm_execpath, process.argv[1], process.execPath]
|
|
85
|
+
.filter(Boolean)
|
|
86
|
+
.join(' ');
|
|
87
|
+
return haystack.includes('pnpm') ? 'pnpm' : 'npm';
|
|
88
|
+
}
|
|
89
|
+
function runInstall(pm, packageName, tag) {
|
|
90
|
+
const command = pm === 'pnpm' ? 'add' : 'install';
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const child = spawn(pm, [command, '-g', `${packageName}@${tag}`], {
|
|
93
|
+
// Ignore stdout so a chatty install can't deadlock by filling an
|
|
94
|
+
// unread pipe buffer; keep stderr piped so we can surface the tail
|
|
95
|
+
// in the CliError hint on a non-zero exit.
|
|
96
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
97
|
+
// npm/pnpm on Windows are `.cmd` shims that spawn() can't resolve
|
|
98
|
+
// without the shell. Safe to enable here because every argv element
|
|
99
|
+
// is library-controlled (literal command, fixed flags, validated
|
|
100
|
+
// package name + tag).
|
|
101
|
+
shell: process.platform === 'win32',
|
|
102
|
+
});
|
|
103
|
+
let stderr = '';
|
|
104
|
+
child.stderr?.on('data', (data) => {
|
|
105
|
+
stderr += data.toString();
|
|
106
|
+
});
|
|
107
|
+
child.on('error', reject);
|
|
108
|
+
child.on('close', (code) => resolve({ exitCode: code ?? 1, stderr }));
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function emit(view, payload, humanLines) {
|
|
112
|
+
if (view.json) {
|
|
113
|
+
console.log(formatJson(payload));
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (view.ndjson) {
|
|
117
|
+
console.log(formatNdjson([payload]));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
for (const line of humanLines())
|
|
121
|
+
console.log(line);
|
|
122
|
+
}
|
|
123
|
+
function formatChannel(channel) {
|
|
124
|
+
return channel === 'pre-release' ? chalk.magenta('pre-release') : chalk.green('stable');
|
|
125
|
+
}
|
|
126
|
+
function channelLabel(channel) {
|
|
127
|
+
return channel === 'pre-release' ? ` ${chalk.magenta('(pre-release)')}` : '';
|
|
128
|
+
}
|
|
129
|
+
async function runWithSpinner(withSpinner, text, op) {
|
|
130
|
+
return withSpinner ? withSpinner({ text, color: 'blue' }, op) : op();
|
|
131
|
+
}
|
|
132
|
+
async function runUpdate(options, cmd) {
|
|
133
|
+
if (cmd.check && cmd.channel) {
|
|
134
|
+
throw new CliError('INVALID_FLAGS', 'Specify either --check or --channel, not both.');
|
|
135
|
+
}
|
|
136
|
+
const view = { json: cmd.json, ndjson: cmd.ndjson };
|
|
137
|
+
const channel = await getConfiguredUpdateChannel(options.configPath);
|
|
138
|
+
if (cmd.channel) {
|
|
139
|
+
emit(view, { channel }, () => [`Update channel: ${formatChannel(channel)}`]);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const tag = getInstallTag(channel);
|
|
143
|
+
const label = channelLabel(channel);
|
|
144
|
+
let latestVersion;
|
|
145
|
+
try {
|
|
146
|
+
latestVersion = await runWithSpinner(options.withSpinner, `Checking for updates${label}...`, () => fetchLatestVersion({
|
|
147
|
+
packageName: options.packageName,
|
|
148
|
+
channel,
|
|
149
|
+
registryUrl: options.registryUrl,
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
154
|
+
throw new CliError('UPDATE_CHECK_FAILED', `Failed to check for updates: ${message}`);
|
|
155
|
+
}
|
|
156
|
+
const { currentVersion } = options;
|
|
157
|
+
const upToDate = currentVersion === latestVersion;
|
|
158
|
+
const updateAvailable = !upToDate && isNewer(currentVersion, latestVersion);
|
|
159
|
+
if (cmd.check) {
|
|
160
|
+
emit(view, { currentVersion, latestVersion, channel, updateAvailable }, () => {
|
|
161
|
+
const channelLine = ` Channel: ${formatChannel(channel)}`;
|
|
162
|
+
const headline = upToDate
|
|
163
|
+
? `${chalk.green('✓')} Already up to date (v${currentVersion})`
|
|
164
|
+
: updateAvailable
|
|
165
|
+
? `Update available: ${chalk.dim(`v${currentVersion}`)} → ${chalk.green(`v${latestVersion}`)}`
|
|
166
|
+
: `Downgrade available: ${chalk.dim(`v${currentVersion}`)} → ${chalk.yellow(`v${latestVersion}`)}`;
|
|
167
|
+
return [headline, channelLine];
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (upToDate) {
|
|
172
|
+
emit(view, { currentVersion, latestVersion, channel, installed: false }, () => [
|
|
173
|
+
`${chalk.green('✓')} Already up to date${label} (v${currentVersion})`,
|
|
174
|
+
]);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (!view.json && !view.ndjson) {
|
|
178
|
+
const headline = updateAvailable
|
|
179
|
+
? `Update available${label}: ${chalk.dim(`v${currentVersion}`)} → ${chalk.green(`v${latestVersion}`)}`
|
|
180
|
+
: `Downgrade available${label}: ${chalk.dim(`v${currentVersion}`)} → ${chalk.yellow(`v${latestVersion}`)}`;
|
|
181
|
+
console.log(headline);
|
|
182
|
+
}
|
|
183
|
+
const pm = detectPackageManager();
|
|
184
|
+
let result;
|
|
185
|
+
try {
|
|
186
|
+
result = await runWithSpinner(options.withSpinner, `Updating to v${latestVersion}${label}...`, () => runInstall(pm, options.packageName, tag));
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
if (error instanceof Error &&
|
|
190
|
+
'code' in error &&
|
|
191
|
+
error.code === 'EACCES') {
|
|
192
|
+
throw new CliError('UPDATE_INSTALL_FAILED', 'Permission denied.', {
|
|
193
|
+
hints: [
|
|
194
|
+
`Run with sudo: sudo ${pm} ${pm === 'pnpm' ? 'add' : 'install'} -g ${options.packageName}@${tag}`,
|
|
195
|
+
],
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
199
|
+
throw new CliError('UPDATE_INSTALL_FAILED', `Install failed: ${message}`);
|
|
200
|
+
}
|
|
201
|
+
if (result.exitCode !== 0) {
|
|
202
|
+
throw new CliError('UPDATE_INSTALL_FAILED', `${pm} exited with code ${result.exitCode}`, result.stderr ? { hints: [result.stderr.trim()] } : {});
|
|
203
|
+
}
|
|
204
|
+
emit(view, { currentVersion, latestVersion, channel, installed: true }, () => {
|
|
205
|
+
const lines = [`${chalk.green('✓')} Updated to v${latestVersion}${label}`];
|
|
206
|
+
if (channel === 'stable' && options.changelogCommandName) {
|
|
207
|
+
lines.push(`${chalk.dim(' Run')} ${chalk.cyan(options.changelogCommandName)} ${chalk.dim('to see what changed')}`);
|
|
208
|
+
}
|
|
209
|
+
return lines;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async function runSwitch(options, cmd, program) {
|
|
213
|
+
if (cmd.stable && cmd.preRelease) {
|
|
214
|
+
throw new CliError('INVALID_FLAGS', 'Specify either --stable or --pre-release, not both.');
|
|
215
|
+
}
|
|
216
|
+
if (!cmd.stable && !cmd.preRelease) {
|
|
217
|
+
throw new CliError('INVALID_FLAGS', 'Specify --stable or --pre-release.');
|
|
218
|
+
}
|
|
219
|
+
const channel = cmd.preRelease ? 'pre-release' : 'stable';
|
|
220
|
+
const view = { json: cmd.json, ndjson: cmd.ndjson };
|
|
221
|
+
await updateConfigOrThrow(options.configPath, { update_channel: channel });
|
|
222
|
+
emit(view, { channel }, () => {
|
|
223
|
+
if (channel === 'pre-release') {
|
|
224
|
+
return [
|
|
225
|
+
`${chalk.green('✓')} Update channel set to ${formatChannel(channel)}`,
|
|
226
|
+
'',
|
|
227
|
+
`${chalk.yellow('Note:')} Pre-release updates follow the ${chalk.cyan('next')} branch.`,
|
|
228
|
+
'When pre-release changes are merged into a stable release, no further',
|
|
229
|
+
'pre-release updates will be published until a new pre-release cycle begins.',
|
|
230
|
+
'Remember to switch back to stable when done:',
|
|
231
|
+
chalk.dim(` ${program.name()} update switch --stable`),
|
|
232
|
+
];
|
|
233
|
+
}
|
|
234
|
+
return [`${chalk.green('✓')} Update channel set to ${formatChannel(channel)}`];
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Register the standard `<cli> update` and `<cli> update switch` commands on a
|
|
239
|
+
* Commander program. The `update` action checks the npm registry for the
|
|
240
|
+
* configured channel's dist-tag, compares against `currentVersion`, and shells
|
|
241
|
+
* out to `npm i -g` (or `pnpm add -g` if pnpm is detected on the running
|
|
242
|
+
* script's path). `update switch` flips the persisted `update_channel` field
|
|
243
|
+
* between `'stable'` and `'pre-release'`.
|
|
244
|
+
*
|
|
245
|
+
* Errors as `CliError` (`INVALID_FLAGS`, `INVALID_UPDATE_CHANNEL`,
|
|
246
|
+
* `UPDATE_CHECK_FAILED`, `UPDATE_INSTALL_FAILED`, or the canonical `CONFIG_*`
|
|
247
|
+
* codes when the config file is broken). The consumer's top-level error
|
|
248
|
+
* handler is expected to format and exit.
|
|
249
|
+
*
|
|
250
|
+
* Both subcommands accept `--json` / `--ndjson`; success branches emit a single
|
|
251
|
+
* record (`{ currentVersion, latestVersion, channel, updateAvailable | installed }`
|
|
252
|
+
* for `update`, `{ channel }` for `update switch`).
|
|
253
|
+
*
|
|
254
|
+
* ```ts
|
|
255
|
+
* import { getConfigPath, createSpinner } from '@doist/cli-core'
|
|
256
|
+
* import { registerUpdateCommand } from '@doist/cli-core/commands'
|
|
257
|
+
* import packageJson from '../package.json' with { type: 'json' }
|
|
258
|
+
*
|
|
259
|
+
* const { withSpinner } = createSpinner()
|
|
260
|
+
* registerUpdateCommand(program, {
|
|
261
|
+
* packageName: '@doist/todoist-cli',
|
|
262
|
+
* currentVersion: packageJson.version,
|
|
263
|
+
* configPath: getConfigPath('todoist-cli'),
|
|
264
|
+
* changelogCommandName: 'td changelog',
|
|
265
|
+
* withSpinner,
|
|
266
|
+
* })
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export function registerUpdateCommand(program, options) {
|
|
270
|
+
const update = program
|
|
271
|
+
.command('update')
|
|
272
|
+
.description('Update the CLI to the latest version for the configured channel')
|
|
273
|
+
.option('--check', 'Check for updates without installing')
|
|
274
|
+
.option('--channel', 'Show the current update channel')
|
|
275
|
+
.option('--json', 'Emit machine-readable JSON output')
|
|
276
|
+
.option('--ndjson', 'Emit machine-readable NDJSON output')
|
|
277
|
+
.action(async (cmdOptions) => {
|
|
278
|
+
await runUpdate(options, cmdOptions);
|
|
279
|
+
});
|
|
280
|
+
update
|
|
281
|
+
.command('switch')
|
|
282
|
+
.description('Switch update channel between stable and pre-release')
|
|
283
|
+
.option('--stable', 'Use the stable release channel')
|
|
284
|
+
.option('--pre-release', 'Use the pre-release (next) channel')
|
|
285
|
+
.option('--json', 'Emit machine-readable JSON output')
|
|
286
|
+
.option('--ndjson', 'Emit machine-readable NDJSON output')
|
|
287
|
+
.action(async function () {
|
|
288
|
+
// optsWithGlobals merges parent (`update`) options into the
|
|
289
|
+
// subcommand's view; without this, `--json` / `--ndjson` would land
|
|
290
|
+
// on the parent because they're declared on both.
|
|
291
|
+
await runSwitch(options, this.optsWithGlobals(), program);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
//# sourceMappingURL=update.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update.js","sourceRoot":"","sources":["../../src/commands/update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAEH,iBAAiB,EAEjB,mBAAmB,GACtB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAIrD,MAAM,oBAAoB,GAAG,4BAA4B,CAAA;AAgCzD;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,OAAe;IACxC,2EAA2E;IAC3E,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC3D,MAAM,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC3C,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IACzD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QACnF,MAAM,IAAI,KAAK,CAAC,4BAA4B,OAAO,GAAG,CAAC,CAA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SACzD,MAAM,CAAC,WAAW,EAAE,iCAAiC,CAAC;SACtD,MAAM,CAAC,QAAQ,EAAE,mCAAmC,CAAC;SACrD,MAAM,CAAC,UAAU,EAAE,qCAAqC,CAAC;SACzD,MAAM,CAAC,KAAK,EAAE,UAA4B,EAAE,EAAE;QAC3C,MAAM,SAAS,CAAC,OAAO,EAAE,UAAU,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEN,MAAM;SACD,OAAO,CAAC,QAAQ,CAAC;SACjB,WAAW,CAAC,sDAAsD,CAAC;SACnE,MAAM,CAAC,UAAU,EAAE,gCAAgC,CAAC;SACpD,MAAM,CAAC,eAAe,EAAE,oCAAoC,CAAC;SAC7D,MAAM,CAAC,QAAQ,EAAE,mCAAmC,CAAC;SACrD,MAAM,CAAC,UAAU,EAAE,qCAAqC,CAAC;SACzD,MAAM,CAAC,KAAK;QACT,4DAA4D;QAC5D,oEAAoE;QACpE,kDAAkD;QAClD,MAAM,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,eAAe,EAAsB,EAAE,OAAO,CAAC,CAAA;IACjF,CAAC,CAAC,CAAA;AACV,CAAC"}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
export type UpdateChannel = 'stable' | 'pre-release';
|
|
2
|
+
/**
|
|
3
|
+
* Shape of the fields cli-core itself reads or writes in a CLI's config file.
|
|
4
|
+
* Consumers compose their own config type with `type MyConfig = CoreConfig & { … }`
|
|
5
|
+
* so a single CLI config can be typed once and extended as future cli-core
|
|
6
|
+
* modules (auth, token storage, …) claim more fields.
|
|
7
|
+
*/
|
|
8
|
+
export type CoreConfig = {
|
|
9
|
+
update_channel?: UpdateChannel;
|
|
10
|
+
};
|
|
1
11
|
/**
|
|
2
12
|
* Resolve the canonical config path for a CLI, honouring `XDG_CONFIG_HOME`
|
|
3
13
|
* when set: `${XDG_CONFIG_HOME ?? ~/.config}/<appName>/config.json`.
|
|
@@ -85,4 +95,20 @@ export declare function writeConfig<T extends object>(path: string, config: T, o
|
|
|
85
95
|
* fixing their config file.
|
|
86
96
|
*/
|
|
87
97
|
export declare function updateConfig<T extends object>(path: string, updates: Partial<T>, options?: WriteConfigOptions): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* `readConfig` variant that surfaces broken-config states as `CliError` keyed
|
|
100
|
+
* off `BROKEN_CONFIG_STATE_TO_CODE`. Use this when a command needs to fail
|
|
101
|
+
* loudly on a damaged file (rather than silently treating it as empty), so the
|
|
102
|
+
* consumer's top-level handler can format and exit consistently.
|
|
103
|
+
*
|
|
104
|
+
* Missing files still return an empty object — that's the same "no config"
|
|
105
|
+
* affordance `readConfig` already offers.
|
|
106
|
+
*/
|
|
107
|
+
export declare function readConfigOrThrow<T extends object>(path: string): Promise<Partial<T>>;
|
|
108
|
+
/**
|
|
109
|
+
* `updateConfig` variant that translates broken-config states to `CliError`
|
|
110
|
+
* keyed off `BROKEN_CONFIG_STATE_TO_CODE` in the same single read pass — no
|
|
111
|
+
* pre-check needed at the call site.
|
|
112
|
+
*/
|
|
113
|
+
export declare function updateConfigOrThrow<T extends object>(path: string, updates: Partial<T>, options?: WriteConfigOptions): Promise<void>;
|
|
88
114
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,aAAa,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG;IACrB,cAAc,CAAC,EAAE,aAAa,CAAA;CACjC,CAAA;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGrD;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,UAAU,CA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O,GAAE,kBAAuB,GACjC,OAAO,CAAC,IAAI,CAAC,CAoBf;AAgBD;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAe3F;AAED;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,SAAS,MAAM,EACtD,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,OAAO,GAAE,kBAAuB,GACjC,OAAO,CAAC,IAAI,CAAC,CAiBf"}
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { chmod, mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
|
+
import { CliError } from './errors.js';
|
|
4
5
|
import { formatJson } from './json.js';
|
|
5
6
|
/**
|
|
6
7
|
* Resolve the canonical config path for a CLI, honouring `XDG_CONFIG_HOME`
|
|
@@ -117,6 +118,54 @@ export async function updateConfig(path, updates, options = {}) {
|
|
|
117
118
|
throw new Error(`Cannot update config at ${path}: file contents are ${result.actual}, not a JSON object. Fix or remove the file before retrying.`);
|
|
118
119
|
}
|
|
119
120
|
}
|
|
121
|
+
function describeBrokenConfig(verb, path, result) {
|
|
122
|
+
if (result.state === 'invalid-shape') {
|
|
123
|
+
return `Cannot ${verb} config at ${path}: contents are ${result.actual}, not a JSON object`;
|
|
124
|
+
}
|
|
125
|
+
return `Cannot ${verb} config at ${path}: ${result.error.message}`;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* `readConfig` variant that surfaces broken-config states as `CliError` keyed
|
|
129
|
+
* off `BROKEN_CONFIG_STATE_TO_CODE`. Use this when a command needs to fail
|
|
130
|
+
* loudly on a damaged file (rather than silently treating it as empty), so the
|
|
131
|
+
* consumer's top-level handler can format and exit consistently.
|
|
132
|
+
*
|
|
133
|
+
* Missing files still return an empty object — that's the same "no config"
|
|
134
|
+
* affordance `readConfig` already offers.
|
|
135
|
+
*/
|
|
136
|
+
export async function readConfigOrThrow(path) {
|
|
137
|
+
const result = await readConfigStrict(path);
|
|
138
|
+
switch (result.state) {
|
|
139
|
+
case 'missing':
|
|
140
|
+
return {};
|
|
141
|
+
case 'present':
|
|
142
|
+
return result.config;
|
|
143
|
+
case 'read-failed':
|
|
144
|
+
case 'invalid-json':
|
|
145
|
+
case 'invalid-shape':
|
|
146
|
+
throw new CliError(BROKEN_CONFIG_STATE_TO_CODE[result.state], describeBrokenConfig('read', path, result));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* `updateConfig` variant that translates broken-config states to `CliError`
|
|
151
|
+
* keyed off `BROKEN_CONFIG_STATE_TO_CODE` in the same single read pass — no
|
|
152
|
+
* pre-check needed at the call site.
|
|
153
|
+
*/
|
|
154
|
+
export async function updateConfigOrThrow(path, updates, options = {}) {
|
|
155
|
+
const result = await readConfigStrict(path);
|
|
156
|
+
switch (result.state) {
|
|
157
|
+
case 'missing':
|
|
158
|
+
await writeConfig(path, updates, options);
|
|
159
|
+
return;
|
|
160
|
+
case 'present':
|
|
161
|
+
await writeConfig(path, { ...result.config, ...updates }, options);
|
|
162
|
+
return;
|
|
163
|
+
case 'read-failed':
|
|
164
|
+
case 'invalid-json':
|
|
165
|
+
case 'invalid-shape':
|
|
166
|
+
throw new CliError(BROKEN_CONFIG_STATE_TO_CODE[result.state], describeBrokenConfig('update', path, result));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
120
169
|
function isPlainObject(value) {
|
|
121
170
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
122
171
|
}
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,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_CODE, getConfigPath, readConfig, readConfigStrict, updateConfig, writeConfig, } from './config.js';
|
|
2
|
-
export type { ConfigErrorCode, ReadConfigStrictResult, WriteConfigOptions } from './config.js';
|
|
1
|
+
export { BROKEN_CONFIG_STATE_TO_CODE, getConfigPath, readConfig, readConfigOrThrow, readConfigStrict, updateConfig, updateConfigOrThrow, writeConfig, } from './config.js';
|
|
2
|
+
export type { ConfigErrorCode, CoreConfig, ReadConfigStrictResult, UpdateChannel, WriteConfigOptions, } from './config.js';
|
|
3
3
|
export { printEmpty } from './empty.js';
|
|
4
4
|
export { CliError } from './errors.js';
|
|
5
5
|
export type { CliErrorCode, CliErrorOptions, ErrorType } from './errors.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,2BAA2B,EAC3B,aAAa,EACb,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,WAAW,GACd,MAAM,aAAa,CAAA;AACpB,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,2BAA2B,EAC3B,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,WAAW,GACd,MAAM,aAAa,CAAA;AACpB,YAAY,EACR,eAAe,EACf,UAAU,EACV,sBAAsB,EACtB,aAAa,EACb,kBAAkB,GACrB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC3E,OAAO,EACH,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,GAClB,MAAM,kBAAkB,CAAA;AACzB,YAAY,EACR,qBAAqB,EACrB,UAAU,EACV,eAAe,EACf,kBAAkB,GACrB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACpD,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5C,YAAY,EACR,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,cAAc,GACjB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { BROKEN_CONFIG_STATE_TO_CODE, getConfigPath, readConfig, readConfigStrict, updateConfig, writeConfig, } from './config.js';
|
|
1
|
+
export { BROKEN_CONFIG_STATE_TO_CODE, getConfigPath, readConfig, readConfigOrThrow, readConfigStrict, updateConfig, updateConfigOrThrow, writeConfig, } from './config.js';
|
|
2
2
|
export { printEmpty } from './empty.js';
|
|
3
3
|
export { CliError } from './errors.js';
|
|
4
4
|
export { createAccessibleGate, createGlobalArgsStore, createSpinnerGate, getProgressJsonlPath, isProgressJsonlEnabled, parseGlobalArgs, } from './global-args.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,2BAA2B,EAC3B,aAAa,EACb,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,WAAW,GACd,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,2BAA2B,EAC3B,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,WAAW,GACd,MAAM,aAAa,CAAA;AAQpB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,OAAO,EACH,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,GAClB,MAAM,kBAAkB,CAAA;AAOzB,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAQ5C,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
|