@doist/cli-core 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
+ ## [0.3.0](https://github.com/Doist/cli-core/compare/v0.2.0...v0.3.0) (2026-05-06)
2
+
3
+ ### Features
4
+
5
+ * add createSpinner factory ([#5](https://github.com/Doist/cli-core/issues/5)) ([7092427](https://github.com/Doist/cli-core/commit/70924271385bb9ad2009a0d56df5a8768c9943f7))
6
+
7
+ ## [0.2.0](https://github.com/Doist/cli-core/compare/v0.1.0...v0.2.0) (2026-05-06)
8
+
9
+ ### Features
10
+
11
+ * bake cli-core codes into CliError, add CliErrorCode aggregator ([#4](https://github.com/Doist/cli-core/issues/4)) ([8c0d959](https://github.com/Doist/cli-core/commit/8c0d95987db3b35b1715cd72eb603cbb99420211))
12
+
1
13
  ## [0.1.0](https://github.com/Doist/cli-core/compare/v0.0.1...v0.1.0) (2026-05-06)
2
14
 
3
15
  ### Features
4
16
 
5
- * add CliError + config file I/O helpers ([#1](https://github.com/Doist/cli-core/issues/1)) ([8daf2d1](https://github.com/Doist/cli-core/commit/8daf2d1f67f44f91713ffc2192b681704fa86d88))
6
- * add terminal detection + JSON/NDJSON formatters ([#2](https://github.com/Doist/cli-core/issues/2)) ([f2bfde8](https://github.com/Doist/cli-core/commit/f2bfde8ff3a2a41eb402acc49223980c5d4be393))
17
+ - add CliError + config file I/O helpers ([#1](https://github.com/Doist/cli-core/issues/1)) ([8daf2d1](https://github.com/Doist/cli-core/commit/8daf2d1f67f44f91713ffc2192b681704fa86d88))
18
+ - add terminal detection + JSON/NDJSON formatters ([#2](https://github.com/Doist/cli-core/issues/2)) ([f2bfde8](https://github.com/Doist/cli-core/commit/f2bfde8ff3a2a41eb402acc49223980c5d4be393))
package/dist/config.d.ts CHANGED
@@ -30,6 +30,31 @@ export type ReadConfigStrictResult = {
30
30
  state: 'present';
31
31
  config: Record<string, unknown>;
32
32
  };
33
+ /**
34
+ * Canonical CliError codes for the broken states of `readConfigStrict`. The
35
+ * `satisfies` clause guarantees every failure state has a corresponding code.
36
+ *
37
+ * Exported as both a runtime map (so a future `readConfigOrThrow` helper — and
38
+ * consumers writing their own state-to-throw translation — can look codes up
39
+ * instead of hand-writing strings) and as the `ConfigErrorCode` type alias.
40
+ */
41
+ export declare const BROKEN_CONFIG_STATE_TO_CODE: {
42
+ readonly 'read-failed': "CONFIG_READ_FAILED";
43
+ readonly 'invalid-json': "CONFIG_INVALID_JSON";
44
+ readonly 'invalid-shape': "CONFIG_INVALID_SHAPE";
45
+ };
46
+ /**
47
+ * Canonical CliError codes emitted when `readConfigStrict` reports a broken
48
+ * config file. Derived from `BROKEN_CONFIG_STATE_TO_CODE` so the type and the
49
+ * runtime map can never drift.
50
+ *
51
+ * cli-core does not throw these itself (the library returns a discriminated
52
+ * result so consumers control formatting), but every consumer that does the
53
+ * states-to-throw translation ends up using the same three codes. Including
54
+ * this in each CLI's `ErrorCode` union is also unnecessary now that
55
+ * `CliError`'s constructor accepts the cli-core canonical codes directly.
56
+ */
57
+ export type ConfigErrorCode = (typeof BROKEN_CONFIG_STATE_TO_CODE)[keyof typeof BROKEN_CONFIG_STATE_TO_CODE];
33
58
  /**
34
59
  * Read and parse a JSON config file strictly, distinguishing missing files
35
60
  * from broken ones. The library returns a discriminated result instead of
@@ -40,13 +65,13 @@ export type ReadConfigStrictResult = {
40
65
  * decode in the consumer.
41
66
  */
42
67
  export declare function readConfigStrict(path: string): Promise<ReadConfigStrictResult>;
43
- export interface WriteConfigOptions {
68
+ export type WriteConfigOptions = {
44
69
  /**
45
70
  * When true and the supplied config has no own enumerable keys, delete the
46
71
  * file instead of writing an empty `{}`. Default false.
47
72
  */
48
73
  deleteWhenEmpty?: boolean;
49
- }
74
+ };
50
75
  /**
51
76
  * Write a config file with restrictive permissions (parent dir 0700, file 0600)
52
77
  * and a trailing newline. Creates parent directories as needed and tightens
@@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EAC9C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,EACT,OAAO,GAAE,kBAAuB,GACjC,OAAO,CAAC,IAAI,CAAC,CAkBf;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,CAAC,SAAS,MAAM,EAC/C,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,OAAO,GAAE,kBAAuB,GACjC,OAAO,CAAC,IAAI,CAAC,CAoBf"}
package/dist/config.js CHANGED
@@ -25,6 +25,19 @@ export async function readConfig(path) {
25
25
  const result = await readConfigStrict(path);
26
26
  return result.state === 'present' ? result.config : {};
27
27
  }
28
+ /**
29
+ * Canonical CliError codes for the broken states of `readConfigStrict`. The
30
+ * `satisfies` clause guarantees every failure state has a corresponding code.
31
+ *
32
+ * Exported as both a runtime map (so a future `readConfigOrThrow` helper — and
33
+ * consumers writing their own state-to-throw translation — can look codes up
34
+ * instead of hand-writing strings) and as the `ConfigErrorCode` type alias.
35
+ */
36
+ export const BROKEN_CONFIG_STATE_TO_CODE = {
37
+ 'read-failed': 'CONFIG_READ_FAILED',
38
+ 'invalid-json': 'CONFIG_INVALID_JSON',
39
+ 'invalid-shape': 'CONFIG_INVALID_SHAPE',
40
+ };
28
41
  /**
29
42
  * Read and parse a JSON config file strictly, distinguishing missing files
30
43
  * from broken ones. The library returns a discriminated result instead of
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC5E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC;;;GAGG;AACH,MAAM,UAAU,aAAa,C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AK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC/E,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAc;IACtC,OAAO,CACH,KAAK,YAAY,KAAK,IAAI,MAAM,IAAI,KAAK,IAAK,KAA4B,CAAC,IAAI,KAAK,QAAQ,CAC/F,CAAA;AACL,CAAC;AAED,SAAS,OAAO,CAAC,KAAc;IAC3B,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;AACpE,CAAC;AAED,SAAS,iBAAiB,CAAC,KAAc;IACrC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,OAAO,CAAA;IACxC,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,MAAM,CAAA;IACjC,MAAM,CAAC,GAAG,OAAO,KAAK,CAAA;IACtB,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAA;IACjE,OAAO,MAAM,CAAA;AACjB,CAAC"}
package/dist/errors.d.ts CHANGED
@@ -1,12 +1,28 @@
1
+ import type { ConfigErrorCode } from './config.js';
1
2
  export type ErrorType = 'error' | 'info';
2
- export interface CliErrorOptions {
3
+ export type CliErrorOptions = {
3
4
  hints?: string[];
4
5
  type?: ErrorType;
5
- }
6
+ };
7
+ /**
8
+ * Aggregator of every error code that cli-core itself defines. Baked into the
9
+ * `CliError` constructor so consumers don't have to redeclare these strings in
10
+ * their own `ErrorCode` union — they're always accepted.
11
+ *
12
+ * Grows as future modules add their own well-known codes:
13
+ *
14
+ * ```ts
15
+ * export type CliErrorCode = ConfigErrorCode | SpinnerErrorCode | …
16
+ * ```
17
+ */
18
+ export type CliErrorCode = ConfigErrorCode;
6
19
  /**
7
- * Generic CLI error carrying a structured code, optional hints, and a severity type.
20
+ * Generic CLI error carrying a structured code, optional hints, and a severity
21
+ * type.
8
22
  *
9
- * Pass a string-literal union as `TCode` to constrain codes per CLI:
23
+ * `code` accepts either the consumer's `TCode` union or any code defined by
24
+ * cli-core itself (`CliErrorCode`). Pass a string-literal union as `TCode` to
25
+ * constrain codes per CLI; the cli-core codes are always allowed alongside.
10
26
  *
11
27
  * ```ts
12
28
  * import { CliError } from '@doist/cli-core'
@@ -14,14 +30,16 @@ export interface CliErrorOptions {
14
30
  * throw new CliError<Code>('AUTH_FAILED', 'Token rejected', {
15
31
  * hints: ['Run td auth login'],
16
32
  * })
33
+ * // CONFIG_INVALID_JSON also accepted without listing it in `Code`:
34
+ * throw new CliError<Code>('CONFIG_INVALID_JSON', 'Bad JSON')
17
35
  * ```
18
36
  *
19
37
  * The `(string & {})` trick preserves intellisense while accepting dynamic codes.
20
38
  */
21
39
  export declare class CliError<TCode extends string = string> extends Error {
22
- readonly code: TCode;
40
+ readonly code: TCode | CliErrorCode;
23
41
  readonly hints?: string[];
24
42
  readonly type: ErrorType;
25
- constructor(code: TCode, message: string, options?: CliErrorOptions);
43
+ constructor(code: TCode | CliErrorCode, message: string, options?: CliErrorOptions);
26
44
  }
27
45
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,CAAA;AAExC,MAAM,WAAW,eAAe;IAC5B,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,IAAI,CAAC,EAAE,SAAS,CAAA;CACnB;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,QAAQ,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,KAAK;aAK1C,IAAI,EAAE,KAAK;IAJ/B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;gBAGJ,IAAI,EAAE,KAAK,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,eAAoB;CAOpC"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElD,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,CAAA;AAExC,MAAM,MAAM,eAAe,GAAG;IAC1B,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,IAAI,CAAC,EAAE,SAAS,CAAA;CACnB,CAAA;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,YAAY,GAAG,eAAe,CAAA;AAE1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,QAAQ,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,KAAK;IAC9D,QAAQ,CAAC,IAAI,EAAE,KAAK,GAAG,YAAY,CAAA;IACnC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;gBAEZ,IAAI,EAAE,KAAK,GAAG,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB;CAOzF"}
package/dist/errors.js CHANGED
@@ -1,7 +1,10 @@
1
1
  /**
2
- * Generic CLI error carrying a structured code, optional hints, and a severity type.
2
+ * Generic CLI error carrying a structured code, optional hints, and a severity
3
+ * type.
3
4
  *
4
- * Pass a string-literal union as `TCode` to constrain codes per CLI:
5
+ * `code` accepts either the consumer's `TCode` union or any code defined by
6
+ * cli-core itself (`CliErrorCode`). Pass a string-literal union as `TCode` to
7
+ * constrain codes per CLI; the cli-core codes are always allowed alongside.
5
8
  *
6
9
  * ```ts
7
10
  * import { CliError } from '@doist/cli-core'
@@ -9,6 +12,8 @@
9
12
  * throw new CliError<Code>('AUTH_FAILED', 'Token rejected', {
10
13
  * hints: ['Run td auth login'],
11
14
  * })
15
+ * // CONFIG_INVALID_JSON also accepted without listing it in `Code`:
16
+ * throw new CliError<Code>('CONFIG_INVALID_JSON', 'Bad JSON')
12
17
  * ```
13
18
  *
14
19
  * The `(string & {})` trick preserves intellisense while accepting dynamic codes.
@@ -19,8 +24,8 @@ export class CliError extends Error {
19
24
  type;
20
25
  constructor(code, message, options = {}) {
21
26
  super(message);
22
- this.code = code;
23
27
  this.name = 'CliError';
28
+ this.code = code;
24
29
  this.hints = options.hints;
25
30
  this.type = options.type ?? 'error';
26
31
  }
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,QAAwC,SAAQ,KAAK;IAK1C;IAJX,KAAK,CAAW;IAChB,IAAI,CAAW;IAExB,YACoB,IAAW,EAC3B,OAAe,EACf,UAA2B,EAAE;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAA;QAJE,SAAI,GAAJ,IAAI,CAAO;QAK3B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAA;QACtB,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC1B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,CAAA;IACvC,CAAC;CACJ"}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,QAAwC,SAAQ,KAAK;IACrD,IAAI,CAAsB;IAC1B,KAAK,CAAW;IAChB,IAAI,CAAW;IAExB,YAAY,IAA0B,EAAE,OAAe,EAAE,UAA2B,EAAE;QAClF,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,UAAU,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC1B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,CAAA;IACvC,CAAC;CACJ"}
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- export { getConfigPath, readConfig, readConfigStrict, updateConfig, writeConfig } from './config.js';
2
- export type { ReadConfigStrictResult, WriteConfigOptions } from './config.js';
1
+ export { BROKEN_CONFIG_STATE_TO_CODE, getConfigPath, readConfig, readConfigStrict, updateConfig, writeConfig, } from './config.js';
2
+ export type { ConfigErrorCode, ReadConfigStrictResult, WriteConfigOptions } from './config.js';
3
3
  export { CliError } from './errors.js';
4
- export type { CliErrorOptions, ErrorType } from './errors.js';
4
+ export type { CliErrorCode, CliErrorOptions, ErrorType } from './errors.js';
5
5
  export { formatJson, formatNdjson } from './json.js';
6
+ export { createSpinner } from './spinner.js';
7
+ export type { LoadingSpinner, SpinnerColor, SpinnerConfig, SpinnerKit, SpinnerOptions, } from './spinner.js';
6
8
  export { isCI, isStderrTTY, isStdinTTY, isStdoutTTY } from './terminal.js';
7
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,gBAAgB,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACpG,YAAY,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAC7E,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC7D,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACpD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
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,EAAE,eAAe,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC3E,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACpD,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,5 +1,6 @@
1
- export { getConfigPath, readConfig, readConfigStrict, updateConfig, writeConfig } from './config.js';
1
+ export { BROKEN_CONFIG_STATE_TO_CODE, getConfigPath, readConfig, readConfigStrict, updateConfig, writeConfig, } from './config.js';
2
2
  export { CliError } from './errors.js';
3
3
  export { formatJson, formatNdjson } from './json.js';
4
+ export { createSpinner } from './spinner.js';
4
5
  export { isCI, isStderrTTY, isStdinTTY, isStdoutTTY } from './terminal.js';
5
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,gBAAgB,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAEpG,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACpD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
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;AAEpB,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAQ5C,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
@@ -0,0 +1,60 @@
1
+ export type SpinnerColor = 'green' | 'yellow' | 'blue' | 'red' | 'gray' | 'cyan' | 'magenta';
2
+ export type SpinnerOptions = {
3
+ text: string;
4
+ color?: SpinnerColor;
5
+ /** Per-call opt-out (e.g. when a flag like `--no-spinner` was passed). */
6
+ noSpinner?: boolean;
7
+ };
8
+ export type SpinnerConfig = {
9
+ /**
10
+ * Returns true to suppress every spinner produced by this kit. CLIs
11
+ * supply this to combine well-known signals (CI, `--json`, etc.) with
12
+ * their own opt-out env vars (e.g. `TD_SPINNER=false`).
13
+ *
14
+ * Defaults to `() => false`.
15
+ */
16
+ isDisabled?: () => boolean;
17
+ /** Default colour name. Defaults to `'blue'`. */
18
+ defaultColor?: SpinnerColor;
19
+ /** Default text shown by `startEarlySpinner` when no override is passed. */
20
+ earlySpinnerText?: string;
21
+ };
22
+ export type LoadingSpinner = {
23
+ start(options: SpinnerOptions): LoadingSpinner;
24
+ succeed(text?: string): void;
25
+ fail(text?: string): void;
26
+ stop(): void;
27
+ };
28
+ export type SpinnerKit = {
29
+ /** Class so consumers can use `new spinner.LoadingSpinner()` if they want
30
+ * to manage start/stop manually. Most call sites should prefer `withSpinner`. */
31
+ LoadingSpinner: new () => LoadingSpinner;
32
+ /** Run an async operation flanked by a spinner. Stops on success, fails on throw. */
33
+ withSpinner: <T>(options: SpinnerOptions, op: () => Promise<T>) => Promise<T>;
34
+ /** Show a long-running spinner before the command module loads. Intercepts
35
+ * stdout.write so it auto-clears the moment any output is produced. */
36
+ startEarlySpinner: (text?: string) => void;
37
+ /** Stop the early spinner if running and restore stdout.write. */
38
+ stopEarlySpinner: () => void;
39
+ /**
40
+ * @internal Reset internal state without calling `.stop()`. Exposed only
41
+ * so consumer test suites can fully reset the kit between cases; do not
42
+ * call from production code.
43
+ */
44
+ resetEarlySpinner: () => void;
45
+ };
46
+ /**
47
+ * Build a spinner kit configured for one CLI. Each kit owns its own early-
48
+ * spinner singleton state, so two kits running in the same process don't
49
+ * step on each other.
50
+ *
51
+ * ```ts
52
+ * import { createSpinner } from '@doist/cli-core'
53
+ * import { shouldDisableSpinner } from './global-args.js'
54
+ *
55
+ * const { LoadingSpinner, withSpinner, startEarlySpinner, stopEarlySpinner } =
56
+ * createSpinner({ isDisabled: shouldDisableSpinner })
57
+ * ```
58
+ */
59
+ export declare function createSpinner(config?: SpinnerConfig): SpinnerKit;
60
+ //# sourceMappingURL=spinner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spinner.d.ts","sourceRoot":"","sources":["../src/spinner.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;AAE5F,MAAM,MAAM,cAAc,GAAG;IACzB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,YAAY,CAAA;IACpB,0EAA0E;IAC1E,SAAS,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IACxB;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,MAAM,OAAO,CAAA;IAC1B,iDAAiD;IACjD,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B,4EAA4E;IAC5E,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAED,MAAM,MAAM,cAAc,GAAG;IACzB,KAAK,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,CAAA;IAC9C,OAAO,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACzB,IAAI,IAAI,IAAI,CAAA;CACf,CAAA;AAED,MAAM,MAAM,UAAU,GAAG;IACrB;sFACkF;IAClF,cAAc,EAAE,UAAU,cAAc,CAAA;IACxC,qFAAqF;IACrF,WAAW,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,cAAc,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IAC7E;4EACwE;IACxE,iBAAiB,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;IAC1C,kEAAkE;IAClE,gBAAgB,EAAE,MAAM,IAAI,CAAA;IAC5B;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,IAAI,CAAA;CAChC,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,MAAM,GAAE,aAAkB,GAAG,UAAU,CAiJpE"}
@@ -0,0 +1,151 @@
1
+ import chalk from 'chalk';
2
+ import yoctoSpinner from 'yocto-spinner';
3
+ import { isStdoutTTY } from './terminal.js';
4
+ /**
5
+ * Build a spinner kit configured for one CLI. Each kit owns its own early-
6
+ * spinner singleton state, so two kits running in the same process don't
7
+ * step on each other.
8
+ *
9
+ * ```ts
10
+ * import { createSpinner } from '@doist/cli-core'
11
+ * import { shouldDisableSpinner } from './global-args.js'
12
+ *
13
+ * const { LoadingSpinner, withSpinner, startEarlySpinner, stopEarlySpinner } =
14
+ * createSpinner({ isDisabled: shouldDisableSpinner })
15
+ * ```
16
+ */
17
+ export function createSpinner(config = {}) {
18
+ const isDisabled = config.isDisabled ?? (() => false);
19
+ const defaultColor = config.defaultColor ?? 'blue';
20
+ const earlyText = config.earlySpinnerText ?? 'Loading...';
21
+ let earlyInstance = null;
22
+ let originalStdoutWrite = null;
23
+ let stdoutInterceptor = null;
24
+ function restoreStdoutWrite() {
25
+ if (!originalStdoutWrite)
26
+ return;
27
+ // Only restore if our interceptor is still on top — anything else that
28
+ // monkey-patched process.stdout.write after us should keep its hook.
29
+ if (process.stdout.write === stdoutInterceptor) {
30
+ process.stdout.write = originalStdoutWrite;
31
+ }
32
+ originalStdoutWrite = null;
33
+ stdoutInterceptor = null;
34
+ }
35
+ function startEarlySpinner(text = earlyText) {
36
+ if (!isStdoutTTY() || isDisabled())
37
+ return;
38
+ earlyInstance = yoctoSpinner({ text: chalk[defaultColor](text) });
39
+ earlyInstance.start();
40
+ // Capture the original in a local closure variable (no .bind()) so
41
+ // repeat startEarlySpinner calls don't nest .bind() copies, and so
42
+ // the wrapper's reference survives `stopEarlySpinner` clearing the
43
+ // module-level `originalStdoutWrite` field.
44
+ const savedWrite = process.stdout.write;
45
+ originalStdoutWrite = savedWrite;
46
+ const wrapper = function (...args) {
47
+ stopEarlySpinner();
48
+ return savedWrite.apply(this, args);
49
+ };
50
+ stdoutInterceptor = wrapper;
51
+ process.stdout.write = wrapper;
52
+ }
53
+ function stopEarlySpinner() {
54
+ restoreStdoutWrite();
55
+ if (earlyInstance) {
56
+ earlyInstance.stop();
57
+ earlyInstance = null;
58
+ }
59
+ }
60
+ function resetEarlySpinner() {
61
+ restoreStdoutWrite();
62
+ earlyInstance = null;
63
+ }
64
+ class LoadingSpinnerImpl {
65
+ instance = null;
66
+ adopted = false;
67
+ start(options) {
68
+ if (!isStdoutTTY() || options.noSpinner || isDisabled()) {
69
+ return this;
70
+ }
71
+ const colorFn = chalk[options.color ?? defaultColor];
72
+ // Adopt an existing early spinner if one's running rather than
73
+ // stacking a second one on top.
74
+ if (earlyInstance) {
75
+ this.instance = earlyInstance;
76
+ this.adopted = true;
77
+ earlyInstance = null;
78
+ // Adoption transfers stdout-clear responsibility to this
79
+ // spinner. The early-spinner's stdout interceptor would
80
+ // otherwise still fire (calling stopEarlySpinner with a now-
81
+ // null earlyInstance) while the adopted spinner kept running.
82
+ restoreStdoutWrite();
83
+ this.instance.text = colorFn(options.text);
84
+ return this;
85
+ }
86
+ this.instance = yoctoSpinner({ text: colorFn(options.text) });
87
+ this.instance.start();
88
+ return this;
89
+ }
90
+ /** Release an adopted spinner back to the pool. Returns true if released. */
91
+ releaseAdopted() {
92
+ if (!this.adopted || !this.instance)
93
+ return false;
94
+ earlyInstance = this.instance;
95
+ this.instance = null;
96
+ this.adopted = false;
97
+ return true;
98
+ }
99
+ succeed(text) {
100
+ if (!this.instance)
101
+ return;
102
+ // Release-back is silent; only do it when the caller didn't ask
103
+ // for a visible success line. With text, the user wants the
104
+ // checkmark + message and we terminate.
105
+ if (text === undefined && this.releaseAdopted())
106
+ return;
107
+ // yocto-spinner prepends its own ✔ glyph; passing the bare text
108
+ // (coloured) avoids a duplicated symbol.
109
+ this.instance.success(text ? chalk.green(text) : undefined);
110
+ this.instance = null;
111
+ this.adopted = false;
112
+ }
113
+ fail(text) {
114
+ if (!this.instance)
115
+ return;
116
+ // yocto-spinner prepends its own ✖ glyph; passing the bare text
117
+ // (coloured) avoids a duplicated symbol. Errors always terminate.
118
+ this.instance.error(text ? chalk.red(text) : undefined);
119
+ this.instance = null;
120
+ this.adopted = false;
121
+ }
122
+ stop() {
123
+ if (!this.instance)
124
+ return;
125
+ if (this.releaseAdopted())
126
+ return;
127
+ this.instance.stop();
128
+ this.instance = null;
129
+ }
130
+ }
131
+ async function withSpinner(options, op) {
132
+ const spinner = new LoadingSpinnerImpl().start(options);
133
+ try {
134
+ const result = await op();
135
+ spinner.stop();
136
+ return result;
137
+ }
138
+ catch (error) {
139
+ spinner.fail();
140
+ throw error;
141
+ }
142
+ }
143
+ return {
144
+ LoadingSpinner: LoadingSpinnerImpl,
145
+ withSpinner,
146
+ startEarlySpinner,
147
+ stopEarlySpinner,
148
+ resetEarlySpinner,
149
+ };
150
+ }
151
+ //# sourceMappingURL=spinner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spinner.js","sourceRoot":"","sources":["../src/spinner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,YAAY,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAoD3C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAAC,SAAwB,EAAE;IACpD,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAA;IACrD,MAAM,YAAY,GAAiB,MAAM,CAAC,YAAY,IAAI,MAAM,CAAA;IAChE,MAAM,SAAS,GAAG,MAAM,CAAC,gBAAgB,IAAI,YAAY,CAAA;IAEzD,IAAI,aAAa,GAA2C,IAAI,CAAA;IAChE,IAAI,mBAAmB,GAAuC,IAAI,CAAA;IAClE,IAAI,iBAAiB,GAAuC,IAAI,CAAA;IAEhE,SAAS,kBAAkB;QACvB,IAAI,CAAC,mBAAmB;YAAE,OAAM;QAChC,uEAAuE;QACvE,qEAAqE;QACrE,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,KAAK,iBAAiB,EAAE,CAAC;YAC7C,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,mBAAmB,CAAA;QAC9C,CAAC;QACD,mBAAmB,GAAG,IAAI,CAAA;QAC1B,iBAAiB,GAAG,IAAI,CAAA;IAC5B,CAAC;IAED,SAAS,iBAAiB,CAAC,OAAe,SAAS;QAC/C,IAAI,CAAC,WAAW,EAAE,IAAI,UAAU,EAAE;YAAE,OAAM;QAE1C,a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Aa;YACd,IAAI,CAAC,IAAI,CAAC,QAAQ;gBAAE,OAAM;YAC1B,gEAAgE;YAChE,kEAAkE;YAClE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;YACvD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;YACpB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACxB,CAAC;QAED,IAAI;YACA,IAAI,CAAC,IAAI,CAAC,QAAQ;gBAAE,OAAM;YAC1B,IAAI,IAAI,CAAC,cAAc,EAAE;gBAAE,OAAM;YACjC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;YACpB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;QACxB,CAAC;KACJ;IAED,KAAK,UAAU,WAAW,CAAI,OAAuB,EAAE,EAAoB;QACvE,MAAM,OAAO,GAAG,IAAI,kBAAkB,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACvD,IAAI,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,EAAE,EAAE,CAAA;YACzB,OAAO,CAAC,IAAI,EAAE,CAAA;YACd,OAAO,MAAM,CAAA;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,EAAE,CAAA;YACd,MAAM,KAAK,CAAA;QACf,CAAC;IACL,CAAC;IAED,OAAO;QACH,cAAc,EAAE,kBAAkB;QAClC,WAAW;QACX,iBAAiB;QACjB,gBAAgB;QAChB,iBAAiB;KACpB,CAAA;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doist/cli-core",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Shared core utilities for Doist CLI projects",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -58,5 +58,9 @@
58
58
  "semantic-release": "25.0.3",
59
59
  "typescript": "6.0.3",
60
60
  "vitest": "4.1.5"
61
+ },
62
+ "dependencies": {
63
+ "chalk": "5.6.2",
64
+ "yocto-spinner": "1.1.0"
61
65
  }
62
66
  }