@shell-shock/preset-cli 0.4.0 → 0.4.2

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.
@@ -82,35 +82,6 @@ function BasePromptDeclarations() {
82
82
  returnType: "string",
83
83
  children: code`return " ".repeat(input.length); `
84
84
  }),
85
- createComponent(Spacing, {}),
86
- code`
87
- const DEFAULT_ICONS = process.platform === "win32" ? {
88
- arrowUp: "↑",
89
- arrowDown: "↓",
90
- arrowLeft: "←",
91
- arrowRight: "→",
92
- radioOn: "(*)",
93
- radioOff: "( )",
94
- tick: "√",
95
- cross: "×",
96
- ellipsis: "...",
97
- pointerSmall: "»",
98
- line: "─",
99
- pointer: ">"
100
- } : {
101
- arrowUp: "↑",
102
- arrowDown: "↓",
103
- arrowLeft: "←",
104
- arrowRight: "→",
105
- radioOn: "◉",
106
- radioOff: "◯",
107
- tick: "✔",
108
- cross: "✖",
109
- ellipsis: "…",
110
- pointerSmall: "›",
111
- line: "─",
112
- pointer: "❯"
113
- }; `,
114
85
  createComponent(Spacing, {}),
115
86
  createComponent(InterfaceDeclaration, {
116
87
  "export": true,
@@ -209,7 +180,7 @@ function BasePromptDeclarations() {
209
180
  createComponent(InterfaceMember, {
210
181
  name: "validate",
211
182
  optional: true,
212
- type: "(value: TValue) => boolean | string | Promise<boolean | string>",
183
+ type: "(value: TValue) => boolean | string | null | undefined | Promise<boolean | string | null | undefined>",
213
184
  doc: "A validation function that returns true if the input is valid, false or a string error message if the input is invalid"
214
185
  }),
215
186
  createComponent(Spacing, {}),
@@ -369,7 +340,7 @@ function BasePromptDeclarations() {
369
340
  createComponent(ClassField, {
370
341
  name: "validator",
371
342
  "protected": true,
372
- type: "(value: TValue) => boolean | string | Promise<boolean | string>",
343
+ type: "(value: TValue) => boolean | string | null | undefined | Promise<boolean | string | null | undefined>",
373
344
  children: code`() => true; `
374
345
  }),
375
346
  createIntrinsic("hbr", {}),
@@ -488,12 +459,12 @@ function BasePromptDeclarations() {
488
459
  "protected": true,
489
460
  name: "status",
490
461
  type: "string",
491
- children: code`return this.completed ? "" : \` \\n \${
462
+ children: code`return this.isCompleted ? "" : \` \\n \${
492
463
  colors.italic(
493
464
  this.isError
494
- ? colors.text.prompt.description.error(splitText(this.errorMessage, "1/3").join("\\n"))
495
- : this.isCompleted
496
- ? colors.text.prompt.description.active(this.description)
465
+ ? colors.text.prompt.description.error(splitText(this.errorMessage, "3/4").join("\\n"))
466
+ : this.description
467
+ ? colors.text.prompt.description.active(splitText(this.description, "3/4").join("\\n"))
497
468
  : ""
498
469
  )
499
470
  }\`; `
@@ -517,8 +488,7 @@ function BasePromptDeclarations() {
517
488
  this.displayValue = this.formatter(updatedValue);
518
489
  this.#value = updatedValue;
519
490
  setTimeout(() => {
520
- this.validate(updatedValue);
521
- this.sync();
491
+ Promise.resolve(this.validate(updatedValue)).then(() => this.sync());
522
492
  }, 0);
523
493
 
524
494
  this.sync(); `
@@ -603,12 +573,12 @@ function BasePromptDeclarations() {
603
573
  createComponent(ClassMethod, {
604
574
  doc: "A method to validate the prompt input using the provided validator function, which updates the error message and error state based on the validation result. This method is called whenever the prompt value changes and needs to be validated.",
605
575
  name: "validate",
576
+ async: true,
606
577
  "protected": true,
607
578
  parameters: [{
608
579
  name: "value",
609
580
  type: "TValue"
610
581
  }],
611
- async: true,
612
582
  children: code`let result = await this.validator(value);
613
583
  if (typeof result === "string") {
614
584
  this.errorMessage = result;
@@ -813,20 +783,27 @@ function BasePromptDeclarations() {
813
783
  createComponent(VarDeclaration, {
814
784
  "export": true,
815
785
  name: "CANCEL_SYMBOL",
816
- doc: "A unique symbol used to indicate that a prompt was cancelled, which can be returned from a prompt factory function to signal that the prompt interaction should be cancelled and any pending promises should be rejected with this symbol. This allows for a consistent way to handle prompt cancellations across different prompt types and interactions.",
786
+ doc: "A unique symbol used to indicate that a prompt was cancelled, which can be returned from a prompt function to signal that the prompt interaction should be cancelled and any pending promises should be rejected with this symbol. This allows for a consistent way to handle prompt cancellations across different prompt types and interactions.",
817
787
  children: code`Symbol("shell-shock:prompts:cancel"); `
818
788
  }),
819
789
  createComponent(Spacing, {}),
790
+ createComponent(TSDoc, {
791
+ heading: "A utility function to check if a given value is the {@link CANCEL_SYMBOL | cancel symbol}, which can be used to determine if a prompt interaction was cancelled based on the value returned from a prompt factory function. This function checks if the provided value is strictly equal to the {@link CANCEL_SYMBOL | CANCEL_SYMBOL}, allowing for a consistent way to handle prompt cancellations across different prompt types and interactions.",
792
+ get children() {
793
+ return [createComponent(TSDocParam, {
794
+ name: "value",
795
+ children: `The value to check.`
796
+ }), createComponent(TSDocReturns, { children: `A boolean indicating whether the provided value is the {@link CANCEL_SYMBOL | cancel symbol}, which can be used to determine if a prompt interaction was cancelled.` })];
797
+ }
798
+ }),
820
799
  createComponent(FunctionDeclaration, {
821
800
  name: "isCancel",
822
801
  "export": true,
823
- doc: "Checks if a given value is a {@link CANCEL_SYMBOL | cancel symbol}.",
824
802
  parameters: [{
825
803
  name: "value",
826
- type: "any",
827
- doc: "The value to check"
804
+ type: "any"
828
805
  }],
829
- returnType: "boolean",
806
+ returnType: "value is typeof CANCEL_SYMBOL",
830
807
  children: code`return value === CANCEL_SYMBOL; `
831
808
  })
832
809
  ];
@@ -875,6 +852,7 @@ function TextPromptDeclarations() {
875
852
  createComponent(ClassField, {
876
853
  name: "initialValue",
877
854
  "protected": true,
855
+ override: true,
878
856
  type: "string",
879
857
  children: code`""; `
880
858
  }),
@@ -1108,7 +1086,7 @@ function TextPromptDeclarations() {
1108
1086
  get children() {
1109
1087
  return [
1110
1088
  createComponent(TSDocRemarks, { children: code`This function can be used to easily create and run a text prompt without needing to manually create an instance of the TextPrompt class and handle its events. The function accepts a configuration object that extends the base PromptFactoryConfig with additional options specific to text prompts, such as the initial value and mask function. The returned promise allows for easy handling of the prompt result using async/await syntax or traditional promise chaining.` }),
1111
- createComponent(TSDocExample, { children: `import { text, isCancel } from "shell-shock/prompts";
1089
+ createComponent(TSDocExample, { children: `import { text, isCancel } from "shell-shock:prompts";
1112
1090
 
1113
1091
  async function run() {
1114
1092
  const name = await text({
@@ -1160,6 +1138,10 @@ function SelectPromptDeclarations() {
1160
1138
  createComponent(InterfaceDeclaration, {
1161
1139
  name: "PromptOptionConfig",
1162
1140
  doc: "Configuration for an option the user can select from the select prompt",
1141
+ typeParameters: [{
1142
+ name: "TValue",
1143
+ default: "string"
1144
+ }],
1163
1145
  get children() {
1164
1146
  return [
1165
1147
  createComponent(InterfaceMember, {
@@ -1178,7 +1160,7 @@ function SelectPromptDeclarations() {
1178
1160
  createComponent(Spacing, {}),
1179
1161
  createComponent(InterfaceMember, {
1180
1162
  name: "value",
1181
- type: "any",
1163
+ type: "TValue",
1182
1164
  doc: "The value of the option"
1183
1165
  }),
1184
1166
  createComponent(Spacing, {}),
@@ -1209,10 +1191,19 @@ function SelectPromptDeclarations() {
1209
1191
  createComponent(InterfaceDeclaration, {
1210
1192
  "export": true,
1211
1193
  name: "PromptOption",
1212
- "extends": "PromptOptionConfig",
1194
+ "extends": "PromptOptionConfig<TValue>",
1213
1195
  doc: "An option the user can select from the select prompt",
1196
+ typeParameters: [{
1197
+ name: "TValue",
1198
+ default: "string"
1199
+ }],
1214
1200
  get children() {
1215
1201
  return [
1202
+ createComponent(InterfaceMember, {
1203
+ name: "label",
1204
+ type: "string",
1205
+ doc: "The message label for the option"
1206
+ }),
1216
1207
  createComponent(InterfaceMember, {
1217
1208
  name: "index",
1218
1209
  type: "number",
@@ -1236,8 +1227,12 @@ function SelectPromptDeclarations() {
1236
1227
  createComponent(Spacing, {}),
1237
1228
  createComponent(InterfaceDeclaration, {
1238
1229
  name: "SelectPromptConfig",
1239
- "extends": "PromptConfig<string>",
1230
+ "extends": "PromptConfig<TValue>",
1240
1231
  doc: "An options object for configuring a select prompt",
1232
+ typeParameters: [{
1233
+ name: "TValue",
1234
+ default: "string"
1235
+ }],
1241
1236
  get children() {
1242
1237
  return [
1243
1238
  createComponent(InterfaceMember, {
@@ -1249,7 +1244,7 @@ function SelectPromptDeclarations() {
1249
1244
  createComponent(Spacing, {}),
1250
1245
  createComponent(InterfaceMember, {
1251
1246
  name: "options",
1252
- type: "Array<string | PromptOptionConfig>",
1247
+ type: "Array<string | PromptOptionConfig<TValue>>",
1253
1248
  doc: "The options available for the select prompt"
1254
1249
  }),
1255
1250
  createComponent(Spacing, {}),
@@ -1269,13 +1264,14 @@ function SelectPromptDeclarations() {
1269
1264
  "extends": "Prompt<TValue>",
1270
1265
  typeParameters: [{
1271
1266
  name: "TValue",
1272
- default: "any"
1267
+ default: "string"
1273
1268
  }],
1274
1269
  get children() {
1275
1270
  return [
1276
1271
  createComponent(ClassField, {
1277
1272
  name: "initialValue",
1278
1273
  "protected": true,
1274
+ override: true,
1279
1275
  type: "TValue"
1280
1276
  }),
1281
1277
  createIntrinsic("hbr", {}),
@@ -1289,47 +1285,48 @@ function SelectPromptDeclarations() {
1289
1285
  createComponent(ClassField, {
1290
1286
  name: "options",
1291
1287
  "protected": true,
1292
- type: "PromptOption[]",
1288
+ type: "PromptOption<TValue>[]",
1293
1289
  children: code`[]; `
1294
1290
  }),
1295
1291
  createComponent(Spacing, {}),
1296
- code`constructor(config: SelectPromptConfig) {
1292
+ code`constructor(config: SelectPromptConfig<TValue>) {
1297
1293
  super(config);
1298
1294
 
1299
1295
  if (config.initialValue) {
1300
- this.initialValue = config.initialValue;
1296
+ this.initialValue = config.initialValue as TValue;
1297
+ } else {
1298
+ this.initialValue = undefined as unknown as TValue;
1301
1299
  }
1302
1300
 
1303
- this.options = config.options.map(opt => {
1304
- let option!: PromptOption;
1301
+ this.options = config.options.map((opt, index) => {
1302
+ let option = {} as Partial<PromptOption<TValue>>;
1305
1303
  if (typeof opt === "string") {
1306
- option = { index: -1, label: opt, value: opt, selected: false, disabled: false } as PromptOption;
1304
+ option = { label: opt, value: opt as TValue, selected: false, disabled: false };
1307
1305
  } else if (typeof opt === "object") {
1308
- option = opt as PromptOption;
1306
+ option = opt;
1309
1307
  } else {
1310
1308
  throw new Error(\`Invalid option provided to SelectPrompt at index #\${index}\`);
1311
1309
  }
1312
1310
 
1313
1311
  return {
1314
- label: String(option.value),
1312
+ label: String(option.value) || "",
1315
1313
  ...option,
1316
- index: -1,
1317
1314
  description: option.description,
1318
1315
  selected: !!option.selected || (this.initialValue !== undefined && option.value === this.initialValue),
1319
1316
  disabled: !!option.disabled
1320
- };
1317
+ } as PromptOption<TValue>;
1321
1318
  }).sort((a, b) => a.label.localeCompare(b.label)).map((option, index) => ({ ...option, index }));
1322
1319
 
1323
1320
  const selected = this.options.findIndex(option => option.selected);
1324
1321
  if (selected > -1) {
1325
1322
  this.cursor = selected;
1326
1323
  if (this.options[this.cursor]) {
1327
- this.initialValue = this.options[this.cursor].value;
1324
+ this.initialValue = this.options[this.cursor].value as TValue;
1328
1325
  }
1329
1326
  }
1330
1327
 
1331
1328
  if (this.initialValue === undefined && this.options.length > 0 && this.options[0]) {
1332
- this.initialValue = this.options[0].value;
1329
+ this.initialValue = this.options[0].value as TValue;
1333
1330
  }
1334
1331
 
1335
1332
  if (config.optionsPerPage) {
@@ -1342,9 +1339,9 @@ function SelectPromptDeclarations() {
1342
1339
  createComponent(ClassPropertyGet, {
1343
1340
  doc: "Returns the currently selected option",
1344
1341
  name: "selectedOption",
1345
- type: "PromptOption | null",
1342
+ type: "PromptOption<TValue> | null",
1346
1343
  "protected": true,
1347
- children: code`return this.options[this.cursor] ?? null; `
1344
+ children: code`return this.options.find(option => option.selected) ?? null; `
1348
1345
  }),
1349
1346
  createComponent(Spacing, {}),
1350
1347
  createComponent(ClassMethod, {
@@ -1463,6 +1460,7 @@ function SelectPromptDeclarations() {
1463
1460
  createComponent(ClassMethod, {
1464
1461
  doc: "A method to render the prompt",
1465
1462
  name: "render",
1463
+ override: true,
1466
1464
  "protected": true,
1467
1465
  children: code`if (this.isInitial) {
1468
1466
  this.output.write(cursor.hide);
@@ -1509,7 +1507,7 @@ function SelectPromptDeclarations() {
1509
1507
  : this.cursor === index
1510
1508
  ? colors.bold(colors.underline(colors.text.prompt.input.active(this.options[index]!.label)))
1511
1509
  : colors.text.prompt.input.inactive(this.options[index]!.label)
1512
- } \${" ".repeat(spacing - this.options[index]!.label.length - (this.options[index]!.icon ? this.options[index]!.icon.length + 1 : 0))}\${
1510
+ } \${" ".repeat(spacing - this.options[index]!.label.length - (this.options[index]!.icon ? this.options[index]!.icon!.length + 1 : 0))}\${
1513
1511
  this.options[index]!.description && this.cursor === index
1514
1512
  ? colors.italic(colors.text.prompt.description.active(this.options[index]!.description))
1515
1513
  : ""
@@ -1542,7 +1540,7 @@ function SelectPromptDeclarations() {
1542
1540
  heading: "A function to create and run a select prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
1543
1541
  get children() {
1544
1542
  return [
1545
- createComponent(TSDocExample, { children: `import { select, isCancel } from "shell-shock/prompts";
1543
+ createComponent(TSDocExample, { children: `import { select, isCancel } from "shell-shock:prompts";
1546
1544
 
1547
1545
  async function run() {
1548
1546
  const color = await select({
@@ -1650,6 +1648,7 @@ function NumericPromptDeclarations() {
1650
1648
  createComponent(ClassField, {
1651
1649
  name: "initialValue",
1652
1650
  "protected": true,
1651
+ override: true,
1653
1652
  type: "number",
1654
1653
  children: code`0; `
1655
1654
  }),
@@ -1744,6 +1743,7 @@ function NumericPromptDeclarations() {
1744
1743
  createComponent(ClassMethod, {
1745
1744
  doc: "A method to reset the prompt input",
1746
1745
  name: "reset",
1746
+ override: true,
1747
1747
  "protected": true,
1748
1748
  children: code`super.reset();
1749
1749
  this.currentInput = "";
@@ -1761,6 +1761,7 @@ function NumericPromptDeclarations() {
1761
1761
  createComponent(ClassMethod, {
1762
1762
  doc: "A method to handle keypress events and determine the corresponding action",
1763
1763
  name: "keypress",
1764
+ override: true,
1764
1765
  "protected": true,
1765
1766
  parameters: [{
1766
1767
  name: "char",
@@ -1870,7 +1871,7 @@ function NumericPromptDeclarations() {
1870
1871
  heading: "A function to create and run a numeric prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
1871
1872
  get children() {
1872
1873
  return [
1873
- createComponent(TSDocExample, { children: `import { numeric, isCancel } from "shell-shock/prompts";
1874
+ createComponent(TSDocExample, { children: `import { numeric, isCancel } from "shell-shock:prompts";
1874
1875
 
1875
1876
  async function run() {
1876
1877
  const age = await numeric({
@@ -1954,6 +1955,7 @@ function TogglePromptDeclarations() {
1954
1955
  createComponent(ClassField, {
1955
1956
  name: "initialValue",
1956
1957
  "protected": true,
1958
+ override: true,
1957
1959
  type: "boolean",
1958
1960
  children: code`false; `
1959
1961
  }),
@@ -2016,6 +2018,7 @@ function TogglePromptDeclarations() {
2016
2018
  createComponent(ClassMethod, {
2017
2019
  doc: "A method to handle keypress events and determine the corresponding action",
2018
2020
  name: "keypress",
2021
+ override: true,
2019
2022
  "protected": true,
2020
2023
  parameters: [{
2021
2024
  name: "char",
@@ -2099,7 +2102,7 @@ function TogglePromptDeclarations() {
2099
2102
  heading: "A function to create and run a toggle prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
2100
2103
  get children() {
2101
2104
  return [
2102
- createComponent(TSDocExample, { children: `import { toggle, isCancel } from "shell-shock/prompts";
2105
+ createComponent(TSDocExample, { children: `import { toggle, isCancel } from "shell-shock:prompts";
2103
2106
 
2104
2107
  async function run() {
2105
2108
  const likesIceCream = await toggle({
@@ -2171,7 +2174,7 @@ function PasswordPromptDeclaration() {
2171
2174
  return [
2172
2175
  createComponent(TSDocRemarks, { children: code`This function creates an instance of the TextPrompt class with the provided configuration options and a custom mask function to handle password input. It sets up event listeners for state updates, submission, and cancellation to handle the prompt interactions and return the appropriate results. The password prompt allows users to input text that is masked for privacy, making it suitable for scenarios like entering passwords or sensitive information.` }),
2173
2176
  createComponent(Spacing, {}),
2174
- createComponent(TSDocExample, { children: `import { password, isCancel } from "shell-shock/prompts";
2177
+ createComponent(TSDocExample, { children: `import { password, isCancel } from "shell-shock:prompts";
2175
2178
 
2176
2179
  async function run() {
2177
2180
  const userPassword = await password({