inquirerer 4.3.1 → 4.4.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/README.md CHANGED
@@ -62,6 +62,10 @@ npm install inquirerer
62
62
  - [Custom Resolvers](#custom-resolvers)
63
63
  - [Resolver Examples](#resolver-examples)
64
64
  - [CLI Helper](#cli-helper)
65
+ - [CLI Utilities](#cli-utilities)
66
+ - [Package Information](#package-information)
67
+ - [Argument Parsing](#argument-parsing)
68
+ - [Error Handling](#error-handling)
65
69
  - [UI Components](#ui-components)
66
70
  - [Spinner](#spinner)
67
71
  - [Progress Bar](#progress-bar)
@@ -1248,6 +1252,130 @@ const cli = new CLI(handler, options);
1248
1252
  await cli.run();
1249
1253
  ```
1250
1254
 
1255
+ ## CLI Utilities
1256
+
1257
+ inquirerer provides a complete set of utilities for building CLI applications, so you can import everything from a single package.
1258
+
1259
+ ### Package Information
1260
+
1261
+ Get information about your CLI's package.json for version display and other metadata:
1262
+
1263
+ ```typescript
1264
+ import { getPackageJson, getPackageVersion, getPackageName } from 'inquirerer';
1265
+
1266
+ // Get the full package.json object
1267
+ const pkg = getPackageJson(__dirname);
1268
+ console.log(`${pkg.name}@${pkg.version}`);
1269
+
1270
+ // Or use the convenience helpers
1271
+ if (argv.version) {
1272
+ console.log(getPackageVersion(__dirname));
1273
+ process.exit(0);
1274
+ }
1275
+
1276
+ const toolName = getPackageName(__dirname);
1277
+ console.log(`Welcome to ${toolName}!`);
1278
+ ```
1279
+
1280
+ ### Argument Parsing
1281
+
1282
+ Parse command-line arguments and extract subcommands:
1283
+
1284
+ ```typescript
1285
+ import { parseArgv, extractFirst } from 'inquirerer';
1286
+
1287
+ const argv = parseArgv(process.argv);
1288
+ const { first, newArgv } = extractFirst(argv);
1289
+
1290
+ // Running: mycli generate --output ./dist
1291
+ // first = 'generate'
1292
+ // newArgv = { output: './dist', _: [] }
1293
+
1294
+ switch (first) {
1295
+ case 'generate':
1296
+ await handleGenerate(newArgv);
1297
+ break;
1298
+ case 'init':
1299
+ await handleInit(newArgv);
1300
+ break;
1301
+ default:
1302
+ console.log('Unknown command');
1303
+ }
1304
+ ```
1305
+
1306
+ ### Error Handling
1307
+
1308
+ Exit gracefully with error messages and optional cleanup:
1309
+
1310
+ ```typescript
1311
+ import { cliExitWithError, CliExitOptions } from 'inquirerer';
1312
+
1313
+ try {
1314
+ await riskyOperation();
1315
+ } catch (error) {
1316
+ await cliExitWithError(error, {
1317
+ context: { operation: 'build', target: 'production' },
1318
+ beforeExit: async () => {
1319
+ await cleanup();
1320
+ },
1321
+ logger: customLogger // optional, defaults to console
1322
+ });
1323
+ }
1324
+ ```
1325
+
1326
+ ### Complete CLI Example
1327
+
1328
+ Here's a complete example using all the CLI utilities:
1329
+
1330
+ ```typescript
1331
+ import {
1332
+ CLI,
1333
+ CLIOptions,
1334
+ Inquirerer,
1335
+ extractFirst,
1336
+ cliExitWithError,
1337
+ getPackageVersion,
1338
+ ParsedArgs
1339
+ } from 'inquirerer';
1340
+
1341
+ const options: Partial<CLIOptions> = {
1342
+ minimistOpts: {
1343
+ alias: { v: 'version', h: 'help' },
1344
+ boolean: ['help', 'version']
1345
+ }
1346
+ };
1347
+
1348
+ const handler = async (argv: Partial<ParsedArgs>, prompter: Inquirerer) => {
1349
+ if (argv.version) {
1350
+ console.log(getPackageVersion(__dirname));
1351
+ process.exit(0);
1352
+ }
1353
+
1354
+ const { first, newArgv } = extractFirst(argv);
1355
+
1356
+ try {
1357
+ switch (first) {
1358
+ case 'init':
1359
+ await handleInit(newArgv, prompter);
1360
+ break;
1361
+ case 'build':
1362
+ await handleBuild(newArgv, prompter);
1363
+ break;
1364
+ default:
1365
+ console.log('Usage: mycli <command> [options]');
1366
+ console.log('Commands: init, build');
1367
+ }
1368
+ } catch (error) {
1369
+ await cliExitWithError(error, {
1370
+ context: { command: first }
1371
+ });
1372
+ }
1373
+ };
1374
+
1375
+ const cli = new CLI(handler, options);
1376
+ await cli.run();
1377
+ ```
1378
+
1251
1379
  ## UI Components
1252
1380
 
1253
1381
  inquirerer includes a set of UI components for building rich terminal interfaces beyond simple prompts. These are useful for showing progress, loading states, and streaming output.
package/cli/argv.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { Opts, ParsedArgs } from 'minimist';
2
+ /**
3
+ * Parse command-line arguments using minimist.
4
+ * Wrapper around minimist so you don't need to import it directly.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * const argv = parseArgv(process.argv);
9
+ * console.log(argv.config); // --config value
10
+ * ```
11
+ */
12
+ export declare const parseArgv: (args?: string[], opts?: Opts) => ParsedArgs;
13
+ /**
14
+ * Extracts the first positional argument from argv and returns it along with the remaining argv.
15
+ * Useful for command routing where the first argument is a subcommand.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const { first: command, newArgv } = extractFirst(argv);
20
+ * if (command === 'init') {
21
+ * await handleInit(newArgv);
22
+ * }
23
+ * ```
24
+ */
25
+ export declare const extractFirst: (argv: Partial<ParsedArgs>) => {
26
+ first: string;
27
+ newArgv: {
28
+ _: string[];
29
+ "--"?: string[] | undefined;
30
+ };
31
+ };
32
+ export type { ParsedArgs, Opts as ParseArgvOptions } from 'minimist';
package/cli/argv.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.extractFirst = exports.parseArgv = void 0;
7
+ const minimist_1 = __importDefault(require("minimist"));
8
+ /**
9
+ * Parse command-line arguments using minimist.
10
+ * Wrapper around minimist so you don't need to import it directly.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const argv = parseArgv(process.argv);
15
+ * console.log(argv.config); // --config value
16
+ * ```
17
+ */
18
+ const parseArgv = (args = process.argv, opts) => {
19
+ return (0, minimist_1.default)(args.slice(2), opts);
20
+ };
21
+ exports.parseArgv = parseArgv;
22
+ /**
23
+ * Extracts the first positional argument from argv and returns it along with the remaining argv.
24
+ * Useful for command routing where the first argument is a subcommand.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const { first: command, newArgv } = extractFirst(argv);
29
+ * if (command === 'init') {
30
+ * await handleInit(newArgv);
31
+ * }
32
+ * ```
33
+ */
34
+ const extractFirst = (argv) => {
35
+ const first = argv._?.[0];
36
+ const newArgv = {
37
+ ...argv,
38
+ _: argv._?.slice(1) ?? []
39
+ };
40
+ return { first, newArgv };
41
+ };
42
+ exports.extractFirst = extractFirst;
package/cli/error.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ export interface CliExitOptions {
2
+ context?: Record<string, any>;
3
+ beforeExit?: () => Promise<void>;
4
+ logger?: {
5
+ error: (message: string, ...args: any[]) => void;
6
+ debug: (message: string, ...args: any[]) => void;
7
+ warn: (message: string, ...args: any[]) => void;
8
+ };
9
+ }
10
+ /**
11
+ * Exits the CLI with an error message and optional cleanup.
12
+ * Supports a beforeExit hook for cleanup operations (e.g., closing database connections).
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * await cliExitWithError('Invalid configuration file');
17
+ *
18
+ * // With cleanup
19
+ * await cliExitWithError(error, {
20
+ * beforeExit: async () => {
21
+ * await db.close();
22
+ * }
23
+ * });
24
+ * ```
25
+ */
26
+ export declare const cliExitWithError: (error: Error | string, options?: CliExitOptions) => Promise<never>;
package/cli/error.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cliExitWithError = void 0;
4
+ /**
5
+ * Exits the CLI with an error message and optional cleanup.
6
+ * Supports a beforeExit hook for cleanup operations (e.g., closing database connections).
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * await cliExitWithError('Invalid configuration file');
11
+ *
12
+ * // With cleanup
13
+ * await cliExitWithError(error, {
14
+ * beforeExit: async () => {
15
+ * await db.close();
16
+ * }
17
+ * });
18
+ * ```
19
+ */
20
+ const cliExitWithError = async (error, options = {}) => {
21
+ const { context, beforeExit, logger = console } = options;
22
+ if (error instanceof Error) {
23
+ logger.error(`Error: ${error.message}`);
24
+ if (context) {
25
+ logger.debug('Context:', context);
26
+ }
27
+ }
28
+ else if (typeof error === 'string') {
29
+ logger.error(`Error: ${error}`);
30
+ if (context) {
31
+ logger.debug('Context:', context);
32
+ }
33
+ }
34
+ if (beforeExit) {
35
+ try {
36
+ await beforeExit();
37
+ logger.debug('Cleanup completed');
38
+ }
39
+ catch (cleanupError) {
40
+ logger.warn('Failed to complete cleanup:', cleanupError);
41
+ }
42
+ }
43
+ process.exit(1);
44
+ };
45
+ exports.cliExitWithError = cliExitWithError;
package/cli/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { parseArgv, extractFirst } from './argv';
2
+ export type { ParsedArgs, ParseArgvOptions } from './argv';
3
+ export { cliExitWithError } from './error';
4
+ export type { CliExitOptions } from './error';
5
+ export { getPackageJson, getPackageVersion, getPackageName } from './package';
6
+ export type { PackageJson } from './package';
package/cli/index.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPackageName = exports.getPackageVersion = exports.getPackageJson = exports.cliExitWithError = exports.extractFirst = exports.parseArgv = void 0;
4
+ var argv_1 = require("./argv");
5
+ Object.defineProperty(exports, "parseArgv", { enumerable: true, get: function () { return argv_1.parseArgv; } });
6
+ Object.defineProperty(exports, "extractFirst", { enumerable: true, get: function () { return argv_1.extractFirst; } });
7
+ var error_1 = require("./error");
8
+ Object.defineProperty(exports, "cliExitWithError", { enumerable: true, get: function () { return error_1.cliExitWithError; } });
9
+ var package_1 = require("./package");
10
+ Object.defineProperty(exports, "getPackageJson", { enumerable: true, get: function () { return package_1.getPackageJson; } });
11
+ Object.defineProperty(exports, "getPackageVersion", { enumerable: true, get: function () { return package_1.getPackageVersion; } });
12
+ Object.defineProperty(exports, "getPackageName", { enumerable: true, get: function () { return package_1.getPackageName; } });
@@ -0,0 +1,40 @@
1
+ export interface PackageJson {
2
+ name: string;
3
+ version: string;
4
+ [key: string]: any;
5
+ }
6
+ /**
7
+ * Gets the package.json for the current package by searching up from the given directory.
8
+ * This is useful for CLIs to get their own package information.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const pkg = getPackageJson(__dirname);
13
+ * console.log(`${pkg.name}@${pkg.version}`);
14
+ * ```
15
+ */
16
+ export declare const getPackageJson: (dirname: string) => PackageJson;
17
+ /**
18
+ * Gets the version from the package.json for the current package.
19
+ * Shorthand for `getPackageJson(dirname).version`.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * if (argv.version) {
24
+ * console.log(getPackageVersion(__dirname));
25
+ * process.exit(0);
26
+ * }
27
+ * ```
28
+ */
29
+ export declare const getPackageVersion: (dirname: string) => string;
30
+ /**
31
+ * Gets the name from the package.json for the current package.
32
+ * Shorthand for `getPackageJson(dirname).name`.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const toolName = getPackageName(__dirname);
37
+ * console.log(`Welcome to ${toolName}!`);
38
+ * ```
39
+ */
40
+ export declare const getPackageName: (dirname: string) => string;
package/cli/package.js ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPackageName = exports.getPackageVersion = exports.getPackageJson = void 0;
4
+ const find_and_require_package_json_1 = require("find-and-require-package-json");
5
+ /**
6
+ * Gets the package.json for the current package by searching up from the given directory.
7
+ * This is useful for CLIs to get their own package information.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const pkg = getPackageJson(__dirname);
12
+ * console.log(`${pkg.name}@${pkg.version}`);
13
+ * ```
14
+ */
15
+ const getPackageJson = (dirname) => {
16
+ return (0, find_and_require_package_json_1.findAndRequirePackageJson)(dirname);
17
+ };
18
+ exports.getPackageJson = getPackageJson;
19
+ /**
20
+ * Gets the version from the package.json for the current package.
21
+ * Shorthand for `getPackageJson(dirname).version`.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * if (argv.version) {
26
+ * console.log(getPackageVersion(__dirname));
27
+ * process.exit(0);
28
+ * }
29
+ * ```
30
+ */
31
+ const getPackageVersion = (dirname) => {
32
+ return (0, exports.getPackageJson)(dirname).version;
33
+ };
34
+ exports.getPackageVersion = getPackageVersion;
35
+ /**
36
+ * Gets the name from the package.json for the current package.
37
+ * Shorthand for `getPackageJson(dirname).name`.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const toolName = getPackageName(__dirname);
42
+ * console.log(`Welcome to ${toolName}!`);
43
+ * ```
44
+ */
45
+ const getPackageName = (dirname) => {
46
+ return (0, exports.getPackageJson)(dirname).name;
47
+ };
48
+ exports.getPackageName = getPackageName;
@@ -0,0 +1,34 @@
1
+ import minimist from 'minimist';
2
+ /**
3
+ * Parse command-line arguments using minimist.
4
+ * Wrapper around minimist so you don't need to import it directly.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * const argv = parseArgv(process.argv);
9
+ * console.log(argv.config); // --config value
10
+ * ```
11
+ */
12
+ export const parseArgv = (args = process.argv, opts) => {
13
+ return minimist(args.slice(2), opts);
14
+ };
15
+ /**
16
+ * Extracts the first positional argument from argv and returns it along with the remaining argv.
17
+ * Useful for command routing where the first argument is a subcommand.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const { first: command, newArgv } = extractFirst(argv);
22
+ * if (command === 'init') {
23
+ * await handleInit(newArgv);
24
+ * }
25
+ * ```
26
+ */
27
+ export const extractFirst = (argv) => {
28
+ const first = argv._?.[0];
29
+ const newArgv = {
30
+ ...argv,
31
+ _: argv._?.slice(1) ?? []
32
+ };
33
+ return { first, newArgv };
34
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Exits the CLI with an error message and optional cleanup.
3
+ * Supports a beforeExit hook for cleanup operations (e.g., closing database connections).
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * await cliExitWithError('Invalid configuration file');
8
+ *
9
+ * // With cleanup
10
+ * await cliExitWithError(error, {
11
+ * beforeExit: async () => {
12
+ * await db.close();
13
+ * }
14
+ * });
15
+ * ```
16
+ */
17
+ export const cliExitWithError = async (error, options = {}) => {
18
+ const { context, beforeExit, logger = console } = options;
19
+ if (error instanceof Error) {
20
+ logger.error(`Error: ${error.message}`);
21
+ if (context) {
22
+ logger.debug('Context:', context);
23
+ }
24
+ }
25
+ else if (typeof error === 'string') {
26
+ logger.error(`Error: ${error}`);
27
+ if (context) {
28
+ logger.debug('Context:', context);
29
+ }
30
+ }
31
+ if (beforeExit) {
32
+ try {
33
+ await beforeExit();
34
+ logger.debug('Cleanup completed');
35
+ }
36
+ catch (cleanupError) {
37
+ logger.warn('Failed to complete cleanup:', cleanupError);
38
+ }
39
+ }
40
+ process.exit(1);
41
+ };
@@ -0,0 +1,3 @@
1
+ export { parseArgv, extractFirst } from './argv';
2
+ export { cliExitWithError } from './error';
3
+ export { getPackageJson, getPackageVersion, getPackageName } from './package';
@@ -0,0 +1,42 @@
1
+ import { findAndRequirePackageJson } from 'find-and-require-package-json';
2
+ /**
3
+ * Gets the package.json for the current package by searching up from the given directory.
4
+ * This is useful for CLIs to get their own package information.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * const pkg = getPackageJson(__dirname);
9
+ * console.log(`${pkg.name}@${pkg.version}`);
10
+ * ```
11
+ */
12
+ export const getPackageJson = (dirname) => {
13
+ return findAndRequirePackageJson(dirname);
14
+ };
15
+ /**
16
+ * Gets the version from the package.json for the current package.
17
+ * Shorthand for `getPackageJson(dirname).version`.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * if (argv.version) {
22
+ * console.log(getPackageVersion(__dirname));
23
+ * process.exit(0);
24
+ * }
25
+ * ```
26
+ */
27
+ export const getPackageVersion = (dirname) => {
28
+ return getPackageJson(dirname).version;
29
+ };
30
+ /**
31
+ * Gets the name from the package.json for the current package.
32
+ * Shorthand for `getPackageJson(dirname).name`.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const toolName = getPackageName(__dirname);
37
+ * console.log(`Welcome to ${toolName}!`);
38
+ * ```
39
+ */
40
+ export const getPackageName = (dirname) => {
41
+ return getPackageJson(dirname).name;
42
+ };
package/esm/index.js CHANGED
@@ -3,3 +3,5 @@ export * from './prompt';
3
3
  export * from './question';
4
4
  export * from './resolvers';
5
5
  export * from './ui';
6
+ // CLI utilities for building command-line applications
7
+ export * from './cli';
package/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './prompt';
3
3
  export * from './question';
4
4
  export * from './resolvers';
5
5
  export * from './ui';
6
+ export * from './cli';
package/index.js CHANGED
@@ -19,3 +19,5 @@ __exportStar(require("./prompt"), exports);
19
19
  __exportStar(require("./question"), exports);
20
20
  __exportStar(require("./resolvers"), exports);
21
21
  __exportStar(require("./ui"), exports);
22
+ // CLI utilities for building command-line applications
23
+ __exportStar(require("./cli"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "4.3.1",
3
+ "version": "4.4.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
6
6
  "main": "index.js",
@@ -35,16 +35,16 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "deepmerge": "^4.3.1",
38
- "find-and-require-package-json": "0.8.6",
38
+ "find-and-require-package-json": "0.9.0",
39
39
  "minimist": "^1.2.8",
40
- "yanse": "0.1.11"
40
+ "yanse": "0.2.0"
41
41
  },
42
42
  "keywords": [],
43
43
  "devDependencies": {
44
44
  "@types/minimist": "^1.2.5",
45
- "clean-ansi": "0.1.8",
46
- "makage": "0.1.8",
45
+ "clean-ansi": "0.2.0",
46
+ "makage": "0.1.10",
47
47
  "string-width": "4.2.3"
48
48
  },
49
- "gitHead": "c5a1d1d1440805103acfb584c4c97c78eeaa50e9"
49
+ "gitHead": "c6f85c28e227126d86839ba1628748ba7d857da9"
50
50
  }