inquirerer 2.1.12 → 2.1.14

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
@@ -133,6 +133,7 @@ interface BaseQuestion {
133
133
  description?: string; // Additional context
134
134
  default?: any; // Default value
135
135
  defaultFrom?: string; // Dynamic default from resolver (e.g., 'git.user.name')
136
+ setFrom?: string; // Auto-set value from resolver, bypassing prompt entirely
136
137
  useDefault?: boolean; // Skip prompt and use default
137
138
  required?: boolean; // Validation requirement
138
139
  validate?: (input: any, answers: any) => boolean | Validation;
@@ -746,14 +747,28 @@ Inquirerer comes with several built-in resolvers ready to use:
746
747
  | `date.now` | ISO timestamp | `"2025-11-23T15:30:45.123Z"` |
747
748
  | `date.timestamp` | Unix timestamp (ms) | `"1732375845123"` |
748
749
 
750
+ #### Workspace (nearest package.json)
751
+
752
+ | Resolver | Description | Example Output |
753
+ |----------|-------------|----------------|
754
+ | `workspace.name` | Repo slug from `repository` URL (fallback: `package.json` `name`) | `"dev-utils"` |
755
+ | `workspace.repo.name` | Repo name from `repository` URL | `"dev-utils"` |
756
+ | `workspace.repo.organization` | Repo org/owner from `repository` URL | `"constructive-io"` |
757
+ | `workspace.organization.name` | Alias for `workspace.repo.organization` | `"constructive-io"` |
758
+ | `workspace.license` | License field from `package.json` | `"MIT"` |
759
+ | `workspace.author` | Author name from `package.json` | `"Constructive"` |
760
+ | `workspace.author.name` | Author name from `package.json` | `"Constructive"` |
761
+ | `workspace.author.email` | Author email from `package.json` | `"email@example.org"` |
762
+
749
763
  ### Priority Order
750
764
 
751
765
  When resolving default values, inquirerer follows this priority:
752
766
 
753
767
  1. **CLI Arguments** - Values passed via command line (highest priority)
754
- 2. **`defaultFrom`** - Dynamically resolved values
755
- 3. **`default`** - Static default values
756
- 4. **`undefined`** - No default available
768
+ 2. **`setFrom`** - Auto-set values (bypasses prompt entirely)
769
+ 3. **`defaultFrom`** - Dynamically resolved default values
770
+ 4. **`default`** - Static default values
771
+ 5. **`undefined`** - No default available
757
772
 
758
773
  ```typescript
759
774
  {
@@ -764,6 +779,75 @@ When resolving default values, inquirerer follows this priority:
764
779
  }
765
780
  ```
766
781
 
782
+ ### `setFrom` vs `defaultFrom`
783
+
784
+ Both `setFrom` and `defaultFrom` use resolvers to get values, but they behave differently:
785
+
786
+ | Feature | `defaultFrom` | `setFrom` |
787
+ |---------|---------------|-----------|
788
+ | Sets value as | Default (user can override) | Final value (no prompt) |
789
+ | User prompted? | Yes, with pre-filled default | No, question is skipped |
790
+ | Use case | Suggested values | Auto-computed values |
791
+
792
+ **`defaultFrom`** - The resolved value becomes the default, but the user is still prompted and can change it:
793
+
794
+ ```typescript
795
+ {
796
+ type: 'text',
797
+ name: 'authorName',
798
+ message: 'Author name?',
799
+ defaultFrom: 'git.user.name' // User sees "Author name? [John Doe]" and can change it
800
+ }
801
+ ```
802
+
803
+ **`setFrom`** - The resolved value is set directly and the question is skipped entirely:
804
+
805
+ ```typescript
806
+ {
807
+ type: 'text',
808
+ name: 'year',
809
+ message: 'Copyright year?',
810
+ setFrom: 'date.year' // Automatically set to "2025", no prompt shown
811
+ }
812
+ ```
813
+
814
+ #### When to use each
815
+
816
+ Use `defaultFrom` when:
817
+ - The value is a suggestion the user might want to change
818
+ - User confirmation is desired
819
+
820
+ Use `setFrom` when:
821
+ - The value should be computed automatically
822
+ - No user input is needed (e.g., timestamps, computed fields)
823
+ - You want to reduce the number of prompts
824
+
825
+ #### Combined example
826
+
827
+ ```typescript
828
+ const questions = [
829
+ {
830
+ type: 'text',
831
+ name: 'authorName',
832
+ message: 'Author name?',
833
+ defaultFrom: 'git.user.name' // User can override
834
+ },
835
+ {
836
+ type: 'text',
837
+ name: 'createdAt',
838
+ setFrom: 'date.iso' // Auto-set, no prompt
839
+ },
840
+ {
841
+ type: 'text',
842
+ name: 'copyrightYear',
843
+ setFrom: 'date.year' // Auto-set, no prompt
844
+ }
845
+ ];
846
+
847
+ // User only sees prompt for authorName
848
+ // createdAt and copyrightYear are set automatically
849
+ ```
850
+
767
851
  ### Custom Resolvers
768
852
 
769
853
  Register your own custom resolvers for project-specific needs:
package/esm/prompt.js CHANGED
@@ -346,6 +346,8 @@ export class Inquirerer {
346
346
  let obj = shouldMutate ? argv : { ...argv };
347
347
  // Resolve dynamic defaults before processing questions
348
348
  await this.resolveDynamicDefaults(questions);
349
+ // Resolve setFrom values - these bypass prompting entirely
350
+ await this.resolveSetValues(questions, obj);
349
351
  // first loop through the question, and set any overrides in case other questions use objs for validation
350
352
  this.applyOverrides(argv, obj, questions);
351
353
  // Check for required arguments when no terminal is available (non-interactive mode)
@@ -446,6 +448,23 @@ export class Inquirerer {
446
448
  }
447
449
  }
448
450
  }
451
+ /**
452
+ * Resolves setFrom values for all questions that have setFrom specified.
453
+ * Sets the value directly in obj, bypassing the prompt entirely.
454
+ */
455
+ async resolveSetValues(questions, obj) {
456
+ for (const question of questions) {
457
+ if ('setFrom' in question && question.setFrom) {
458
+ // Only set if not already provided in args
459
+ if (!(question.name in obj)) {
460
+ const resolved = await this.resolverRegistry.resolve(question.setFrom);
461
+ if (resolved !== undefined) {
462
+ obj[question.name] = resolved;
463
+ }
464
+ }
465
+ }
466
+ }
467
+ }
449
468
  applyDefaultValues(questions, obj) {
450
469
  questions.forEach(question => {
451
470
  if ('default' in question) {
@@ -1,6 +1,7 @@
1
1
  import { gitResolvers } from './git';
2
2
  import { dateResolvers } from './date';
3
3
  import { npmResolvers } from './npm';
4
+ import { workspaceResolvers } from './workspace';
4
5
  /**
5
6
  * A registry for managing default value resolvers.
6
7
  * Allows registration of custom resolvers and provides resolution logic.
@@ -80,6 +81,7 @@ export const globalResolverRegistry = new DefaultResolverRegistry({
80
81
  ...gitResolvers,
81
82
  ...dateResolvers,
82
83
  ...npmResolvers,
84
+ ...workspaceResolvers,
83
85
  });
84
86
  /**
85
87
  * Convenience function to register a resolver on the global registry.
@@ -0,0 +1,141 @@
1
+ import { findAndRequirePackageJson } from 'find-and-require-package-json';
2
+ /**
3
+ * Find and read the nearest package.json starting from cwd.
4
+ * Returns undefined if not found (instead of throwing).
5
+ */
6
+ function findPackageJsonFromCwd() {
7
+ try {
8
+ return findAndRequirePackageJson(process.cwd());
9
+ }
10
+ catch {
11
+ return undefined;
12
+ }
13
+ }
14
+ /**
15
+ * Parse a GitHub URL and extract organization and repo name.
16
+ * Handles various formats:
17
+ * - https://github.com/org/repo
18
+ * - https://github.com/org/repo.git
19
+ * - git@github.com:org/repo.git
20
+ * - git://github.com/org/repo.git
21
+ */
22
+ function parseGitHubUrl(url) {
23
+ if (!url) {
24
+ return {};
25
+ }
26
+ // Handle git@github.com:org/repo.git format
27
+ const sshMatch = url.match(/git@github\.com:([^/]+)\/([^/.]+)(?:\.git)?/);
28
+ if (sshMatch) {
29
+ return { organization: sshMatch[1], name: sshMatch[2] };
30
+ }
31
+ // Handle https://github.com/org/repo or git://github.com/org/repo formats
32
+ const httpsMatch = url.match(/(?:https?|git):\/\/github\.com\/([^/]+)\/([^/.]+)(?:\.git)?/);
33
+ if (httpsMatch) {
34
+ return { organization: httpsMatch[1], name: httpsMatch[2] };
35
+ }
36
+ return {};
37
+ }
38
+ /**
39
+ * Get the repository URL from package.json.
40
+ * Handles both string and object formats.
41
+ */
42
+ function getRepositoryUrl(pkg) {
43
+ if (!pkg.repository) {
44
+ return undefined;
45
+ }
46
+ if (typeof pkg.repository === 'string') {
47
+ return pkg.repository;
48
+ }
49
+ return pkg.repository.url;
50
+ }
51
+ /**
52
+ * Parse author field which can be a string or object.
53
+ * String format: "Name <email> (url)" where email and url are optional
54
+ */
55
+ function parseAuthor(author) {
56
+ if (!author) {
57
+ return {};
58
+ }
59
+ if (typeof author === 'object') {
60
+ return { name: author.name, email: author.email };
61
+ }
62
+ // Parse string format: "Name <email> (url)"
63
+ const nameMatch = author.match(/^([^<(]+)/);
64
+ const emailMatch = author.match(/<([^>]+)>/);
65
+ return {
66
+ name: nameMatch ? nameMatch[1].trim() : undefined,
67
+ email: emailMatch ? emailMatch[1] : undefined,
68
+ };
69
+ }
70
+ /**
71
+ * Built-in workspace resolvers.
72
+ * These resolve values from the nearest package.json in the current working directory.
73
+ */
74
+ export const workspaceResolvers = {
75
+ 'workspace.name': () => {
76
+ const pkg = findPackageJsonFromCwd();
77
+ if (!pkg)
78
+ return undefined;
79
+ const url = getRepositoryUrl(pkg);
80
+ // Prefer repo slug when repository is set; fall back to package name
81
+ if (url) {
82
+ const parsed = parseGitHubUrl(url);
83
+ if (parsed.name)
84
+ return parsed.name;
85
+ }
86
+ return pkg.name;
87
+ },
88
+ 'workspace.repo.name': () => {
89
+ const pkg = findPackageJsonFromCwd();
90
+ if (!pkg)
91
+ return undefined;
92
+ const url = getRepositoryUrl(pkg);
93
+ if (!url)
94
+ return undefined;
95
+ return parseGitHubUrl(url).name;
96
+ },
97
+ 'workspace.repo.organization': () => {
98
+ const pkg = findPackageJsonFromCwd();
99
+ if (!pkg)
100
+ return undefined;
101
+ const url = getRepositoryUrl(pkg);
102
+ if (!url)
103
+ return undefined;
104
+ return parseGitHubUrl(url).organization;
105
+ },
106
+ // Alias for repo.organization for template readability
107
+ 'workspace.organization.name': () => {
108
+ const pkg = findPackageJsonFromCwd();
109
+ if (!pkg)
110
+ return undefined;
111
+ const url = getRepositoryUrl(pkg);
112
+ if (!url)
113
+ return undefined;
114
+ return parseGitHubUrl(url).organization;
115
+ },
116
+ 'workspace.license': () => {
117
+ const pkg = findPackageJsonFromCwd();
118
+ return pkg?.license;
119
+ },
120
+ 'workspace.author': () => {
121
+ const pkg = findPackageJsonFromCwd();
122
+ if (!pkg)
123
+ return undefined;
124
+ const parsed = parseAuthor(pkg.author);
125
+ return parsed.name;
126
+ },
127
+ 'workspace.author.name': () => {
128
+ const pkg = findPackageJsonFromCwd();
129
+ if (!pkg)
130
+ return undefined;
131
+ const parsed = parseAuthor(pkg.author);
132
+ return parsed.name;
133
+ },
134
+ 'workspace.author.email': () => {
135
+ const pkg = findPackageJsonFromCwd();
136
+ if (!pkg)
137
+ return undefined;
138
+ const parsed = parseAuthor(pkg.author);
139
+ return parsed.email;
140
+ },
141
+ };
package/esm/utils.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { green, blue } from 'yanse';
2
- import { findAndRequirePackageJson } from "find-and-require-package-json";
2
+ import { findAndRequirePackageJson } from 'find-and-require-package-json';
3
3
  // Function to display the version information
4
4
  export function displayVersion() {
5
5
  const pkg = findAndRequirePackageJson(__dirname);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "2.1.12",
4
- "author": "Constructive <pyramation@constructive.io>",
3
+ "version": "2.1.14",
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",
7
7
  "module": "esm/index.js",
@@ -30,16 +30,16 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "deepmerge": "^4.3.1",
33
- "find-and-require-package-json": "0.8.1",
33
+ "find-and-require-package-json": "0.8.2",
34
34
  "js-yaml": "^4.1.0",
35
35
  "minimist": "^1.2.8",
36
- "yanse": "0.1.7"
36
+ "yanse": "0.1.8"
37
37
  },
38
38
  "keywords": [],
39
39
  "devDependencies": {
40
40
  "@types/minimist": "^1.2.5",
41
- "clean-ansi": "0.1.4",
41
+ "clean-ansi": "0.1.5",
42
42
  "makage": "0.1.8"
43
43
  },
44
- "gitHead": "a30d0a87aaccc1ae729b86d24b1bc3a0f73aeb19"
44
+ "gitHead": "64ad2e12049b05169df7dc7e3bcc401da6f42842"
45
45
  }
package/prompt.d.ts CHANGED
@@ -70,6 +70,11 @@ export declare class Inquirerer {
70
70
  * Updates the question.default property with the resolved value.
71
71
  */
72
72
  private resolveDynamicDefaults;
73
+ /**
74
+ * Resolves setFrom values for all questions that have setFrom specified.
75
+ * Sets the value directly in obj, bypassing the prompt entirely.
76
+ */
77
+ private resolveSetValues;
73
78
  private applyDefaultValues;
74
79
  private applyOverrides;
75
80
  private handleOverrides;
package/prompt.js CHANGED
@@ -353,6 +353,8 @@ class Inquirerer {
353
353
  let obj = shouldMutate ? argv : { ...argv };
354
354
  // Resolve dynamic defaults before processing questions
355
355
  await this.resolveDynamicDefaults(questions);
356
+ // Resolve setFrom values - these bypass prompting entirely
357
+ await this.resolveSetValues(questions, obj);
356
358
  // first loop through the question, and set any overrides in case other questions use objs for validation
357
359
  this.applyOverrides(argv, obj, questions);
358
360
  // Check for required arguments when no terminal is available (non-interactive mode)
@@ -453,6 +455,23 @@ class Inquirerer {
453
455
  }
454
456
  }
455
457
  }
458
+ /**
459
+ * Resolves setFrom values for all questions that have setFrom specified.
460
+ * Sets the value directly in obj, bypassing the prompt entirely.
461
+ */
462
+ async resolveSetValues(questions, obj) {
463
+ for (const question of questions) {
464
+ if ('setFrom' in question && question.setFrom) {
465
+ // Only set if not already provided in args
466
+ if (!(question.name in obj)) {
467
+ const resolved = await this.resolverRegistry.resolve(question.setFrom);
468
+ if (resolved !== undefined) {
469
+ obj[question.name] = resolved;
470
+ }
471
+ }
472
+ }
473
+ }
474
+ }
456
475
  applyDefaultValues(questions, obj) {
457
476
  questions.forEach(question => {
458
477
  if ('default' in question) {
@@ -17,6 +17,7 @@ export interface BaseQuestion {
17
17
  type: string;
18
18
  default?: any;
19
19
  defaultFrom?: string;
20
+ setFrom?: string;
20
21
  useDefault?: boolean;
21
22
  required?: boolean;
22
23
  message?: string;
@@ -6,6 +6,7 @@ exports.resolveDefault = resolveDefault;
6
6
  const git_1 = require("./git");
7
7
  const date_1 = require("./date");
8
8
  const npm_1 = require("./npm");
9
+ const workspace_1 = require("./workspace");
9
10
  /**
10
11
  * A registry for managing default value resolvers.
11
12
  * Allows registration of custom resolvers and provides resolution logic.
@@ -86,6 +87,7 @@ exports.globalResolverRegistry = new DefaultResolverRegistry({
86
87
  ...git_1.gitResolvers,
87
88
  ...date_1.dateResolvers,
88
89
  ...npm_1.npmResolvers,
90
+ ...workspace_1.workspaceResolvers,
89
91
  });
90
92
  /**
91
93
  * Convenience function to register a resolver on the global registry.
@@ -0,0 +1,6 @@
1
+ import type { ResolverRegistry } from './types';
2
+ /**
3
+ * Built-in workspace resolvers.
4
+ * These resolve values from the nearest package.json in the current working directory.
5
+ */
6
+ export declare const workspaceResolvers: ResolverRegistry;
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.workspaceResolvers = void 0;
4
+ const find_and_require_package_json_1 = require("find-and-require-package-json");
5
+ /**
6
+ * Find and read the nearest package.json starting from cwd.
7
+ * Returns undefined if not found (instead of throwing).
8
+ */
9
+ function findPackageJsonFromCwd() {
10
+ try {
11
+ return (0, find_and_require_package_json_1.findAndRequirePackageJson)(process.cwd());
12
+ }
13
+ catch {
14
+ return undefined;
15
+ }
16
+ }
17
+ /**
18
+ * Parse a GitHub URL and extract organization and repo name.
19
+ * Handles various formats:
20
+ * - https://github.com/org/repo
21
+ * - https://github.com/org/repo.git
22
+ * - git@github.com:org/repo.git
23
+ * - git://github.com/org/repo.git
24
+ */
25
+ function parseGitHubUrl(url) {
26
+ if (!url) {
27
+ return {};
28
+ }
29
+ // Handle git@github.com:org/repo.git format
30
+ const sshMatch = url.match(/git@github\.com:([^/]+)\/([^/.]+)(?:\.git)?/);
31
+ if (sshMatch) {
32
+ return { organization: sshMatch[1], name: sshMatch[2] };
33
+ }
34
+ // Handle https://github.com/org/repo or git://github.com/org/repo formats
35
+ const httpsMatch = url.match(/(?:https?|git):\/\/github\.com\/([^/]+)\/([^/.]+)(?:\.git)?/);
36
+ if (httpsMatch) {
37
+ return { organization: httpsMatch[1], name: httpsMatch[2] };
38
+ }
39
+ return {};
40
+ }
41
+ /**
42
+ * Get the repository URL from package.json.
43
+ * Handles both string and object formats.
44
+ */
45
+ function getRepositoryUrl(pkg) {
46
+ if (!pkg.repository) {
47
+ return undefined;
48
+ }
49
+ if (typeof pkg.repository === 'string') {
50
+ return pkg.repository;
51
+ }
52
+ return pkg.repository.url;
53
+ }
54
+ /**
55
+ * Parse author field which can be a string or object.
56
+ * String format: "Name <email> (url)" where email and url are optional
57
+ */
58
+ function parseAuthor(author) {
59
+ if (!author) {
60
+ return {};
61
+ }
62
+ if (typeof author === 'object') {
63
+ return { name: author.name, email: author.email };
64
+ }
65
+ // Parse string format: "Name <email> (url)"
66
+ const nameMatch = author.match(/^([^<(]+)/);
67
+ const emailMatch = author.match(/<([^>]+)>/);
68
+ return {
69
+ name: nameMatch ? nameMatch[1].trim() : undefined,
70
+ email: emailMatch ? emailMatch[1] : undefined,
71
+ };
72
+ }
73
+ /**
74
+ * Built-in workspace resolvers.
75
+ * These resolve values from the nearest package.json in the current working directory.
76
+ */
77
+ exports.workspaceResolvers = {
78
+ 'workspace.name': () => {
79
+ const pkg = findPackageJsonFromCwd();
80
+ if (!pkg)
81
+ return undefined;
82
+ const url = getRepositoryUrl(pkg);
83
+ // Prefer repo slug when repository is set; fall back to package name
84
+ if (url) {
85
+ const parsed = parseGitHubUrl(url);
86
+ if (parsed.name)
87
+ return parsed.name;
88
+ }
89
+ return pkg.name;
90
+ },
91
+ 'workspace.repo.name': () => {
92
+ const pkg = findPackageJsonFromCwd();
93
+ if (!pkg)
94
+ return undefined;
95
+ const url = getRepositoryUrl(pkg);
96
+ if (!url)
97
+ return undefined;
98
+ return parseGitHubUrl(url).name;
99
+ },
100
+ 'workspace.repo.organization': () => {
101
+ const pkg = findPackageJsonFromCwd();
102
+ if (!pkg)
103
+ return undefined;
104
+ const url = getRepositoryUrl(pkg);
105
+ if (!url)
106
+ return undefined;
107
+ return parseGitHubUrl(url).organization;
108
+ },
109
+ // Alias for repo.organization for template readability
110
+ 'workspace.organization.name': () => {
111
+ const pkg = findPackageJsonFromCwd();
112
+ if (!pkg)
113
+ return undefined;
114
+ const url = getRepositoryUrl(pkg);
115
+ if (!url)
116
+ return undefined;
117
+ return parseGitHubUrl(url).organization;
118
+ },
119
+ 'workspace.license': () => {
120
+ const pkg = findPackageJsonFromCwd();
121
+ return pkg?.license;
122
+ },
123
+ 'workspace.author': () => {
124
+ const pkg = findPackageJsonFromCwd();
125
+ if (!pkg)
126
+ return undefined;
127
+ const parsed = parseAuthor(pkg.author);
128
+ return parsed.name;
129
+ },
130
+ 'workspace.author.name': () => {
131
+ const pkg = findPackageJsonFromCwd();
132
+ if (!pkg)
133
+ return undefined;
134
+ const parsed = parseAuthor(pkg.author);
135
+ return parsed.name;
136
+ },
137
+ 'workspace.author.email': () => {
138
+ const pkg = findPackageJsonFromCwd();
139
+ if (!pkg)
140
+ return undefined;
141
+ const parsed = parseAuthor(pkg.author);
142
+ return parsed.email;
143
+ },
144
+ };