Comparing version 0.1.0 to 1.0.0
@@ -1,3 +0,4 @@ | ||
import type { Command, Option } from "commander"; | ||
import { prompt } from "enquirer"; | ||
import type { Command, Option, OptionValues } from "commander"; | ||
import enquirer from "enquirer"; | ||
declare const prompt: typeof enquirer.prompt; | ||
interface ValidOption extends Option { | ||
@@ -7,26 +8,14 @@ long: string; | ||
type AutoPromptOptions = { | ||
quiet?: boolean; | ||
prompter?: typeof prompt; | ||
logger?: { | ||
log: (...data: unknown[]) => void; | ||
}; | ||
}; | ||
export declare class AutoPrompt { | ||
autoPromptOpts: AutoPromptOptions; | ||
cmd: Command; | ||
readonly options: ValidOption[]; | ||
parsedOptions: unknown[]; | ||
private logger; | ||
private prompter; | ||
constructor(cmd: Command, autoPromptOpts?: AutoPromptOptions); | ||
static validateOption: (option: Option) => ValidOption; | ||
static buildPrompt: (option: ValidOption) => { | ||
type: string; | ||
name: string; | ||
message: string; | ||
choices: string[] | undefined; | ||
}; | ||
prompt(): Promise<void>; | ||
} | ||
export declare const validateOption: (option: Option) => ValidOption; | ||
export declare const buildPrompt: (option: ValidOption) => { | ||
type: string; | ||
name: string; | ||
message: string; | ||
choices: string[] | undefined; | ||
}; | ||
export declare function autoprompt<T>(program: Command, autoPromptOpts?: AutoPromptOptions): Promise<T>; | ||
export declare function promptForMissingOptions<T>(prompter: typeof prompt, options: ValidOption[], parsedOptions: OptionValues): Promise<T>; | ||
export {}; | ||
//# sourceMappingURL=autoprompt.d.ts.map |
import chalk from "chalk"; | ||
import { prompt } from "enquirer"; | ||
import enquirer from "enquirer"; | ||
import { convertStringToOptions } from "../util.js"; | ||
export class AutoPrompt { | ||
autoPromptOpts; | ||
cmd; | ||
// raw options passed into the command | ||
options; | ||
// parsed options | ||
parsedOptions; | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
logger; | ||
prompter; | ||
constructor(cmd, autoPromptOpts = { | ||
quiet: false, | ||
prompter: prompt, | ||
logger: { log: console.log }, | ||
}) { | ||
this.cmd = cmd; | ||
this.logger = autoPromptOpts.logger ?? { log: console.log }; | ||
this.autoPromptOpts = autoPromptOpts; | ||
this.prompter = autoPromptOpts.prompter; | ||
// clone the options so we can modify them | ||
this.options = [...this.cmd.options.map(AutoPrompt.validateOption)]; | ||
this.cmd.parse(); | ||
this.parsedOptions = this.cmd.opts(); | ||
if (this.autoPromptOpts.quiet) { | ||
this.logger = { | ||
log: () => { | ||
/** quiet mode no log */ | ||
}, | ||
}; | ||
} | ||
const { prompt } = enquirer; | ||
export const validateOption = (option) => { | ||
if (!option.long || option.long === undefined) { | ||
throw new Error("AutoPrompt options (flags) must have a long name"); | ||
} | ||
static validateOption = (option) => { | ||
if (!option.long || option.long === undefined) { | ||
throw new Error("AutoPrompt options (flags) must have a long name"); | ||
} | ||
// TypeScript doesn't know that we're filtering out the undefined values | ||
return option; | ||
// TypeScript doesn't know that we're filtering out the undefined values | ||
return option; | ||
}; | ||
export const buildPrompt = (option) => { | ||
let inputType = "confirm"; | ||
let hasChoices = false; | ||
if (option.flags.includes("<string>")) { | ||
inputType = "input"; | ||
} | ||
if (option.flags.includes("<number>")) { | ||
inputType = "numeral"; | ||
} | ||
if (option.flags.includes("<boolean>")) { | ||
inputType = "confirm"; | ||
} | ||
if (option.flags.includes("oneof")) { | ||
inputType = "select"; | ||
hasChoices = true; | ||
} | ||
else if (option.flags.includes("of")) { | ||
inputType = "multiselect"; | ||
hasChoices = true; | ||
} | ||
return { | ||
type: inputType, | ||
name: option.long.replace("--", ""), | ||
message: option.description, | ||
choices: hasChoices ? convertStringToOptions(option.flags) : undefined, | ||
}; | ||
// this needs to return a prompt option object | ||
// it is not strongly typed for now because enquirer | ||
// does not ship the type. | ||
static buildPrompt = (option) => { | ||
let inputType = "confirm"; | ||
let hasChoices = false; | ||
if (option.flags.includes("<string>")) { | ||
inputType = "input"; | ||
}; | ||
export async function autoprompt(program, autoPromptOpts) { | ||
const options = program.options.map(validateOption); | ||
program.parse(); | ||
const parsedOptions = program.opts(); | ||
const promptedOptions = await promptForMissingOptions(autoPromptOpts?.prompter ?? prompt, options, parsedOptions); | ||
return { ...parsedOptions, ...promptedOptions }; | ||
} | ||
export async function promptForMissingOptions(prompter, options, parsedOptions) { | ||
return await prompter(options | ||
.filter((opt) => { | ||
// only prompt for options that were not passed in | ||
const missingValue = !parsedOptions[opt.long.replace("--", "")]; | ||
if (!missingValue) { | ||
console.log(`${chalk.green("✔")} ${opt.description} · ${parsedOptions[opt.long.replace("--", "")]}`); | ||
} | ||
if (option.flags.includes("<number>")) { | ||
inputType = "numeral"; | ||
} | ||
if (option.flags.includes("<boolean>")) { | ||
inputType = "confirm"; | ||
} | ||
if (option.flags.includes("oneof")) { | ||
inputType = "select"; | ||
hasChoices = true; | ||
} | ||
else if (option.flags.includes("of")) { | ||
inputType = "multiselect"; | ||
hasChoices = true; | ||
} | ||
return { | ||
type: inputType, | ||
name: option.long.replace("--", ""), | ||
message: option.description, | ||
choices: hasChoices ? convertStringToOptions(option.flags) : undefined, | ||
}; | ||
}; | ||
async prompt() { | ||
await this.prompter(this.options | ||
.filter((opt) => { | ||
// only prompt for options that were not passed in | ||
const missingValue = !this.parsedOptions[opt.long.replace("--", "")]; | ||
if (!missingValue) { | ||
this.logger.log(`${chalk.green("✔")} ${opt.description} · ${this.parsedOptions[opt.long.replace("--", "")]}`); | ||
} | ||
return missingValue; | ||
}) | ||
.map(AutoPrompt.buildPrompt)); | ||
} | ||
return missingValue; | ||
}) | ||
.map(buildPrompt)); | ||
} | ||
//# sourceMappingURL=autoprompt.js.map |
@@ -1,2 +0,2 @@ | ||
export { AutoPrompt } from "./autoprompt/autoprompt.js"; | ||
export { autoprompt } from "./autoprompt/autoprompt.js"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export { AutoPrompt } from "./autoprompt/autoprompt.js"; | ||
export { autoprompt } from "./autoprompt/autoprompt.js"; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "autoprompt", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
9384
92