Skip to content

Instantly share code, notes, and snippets.

@linbudu599
Created December 17, 2021 10:11
Show Gist options
  • Save linbudu599/b9072a295b50d220c2068f77c914962b to your computer and use it in GitHub Desktop.
Save linbudu599/b9072a295b50d220c2068f77c914962b to your computer and use it in GitHub Desktop.
import { Cli, Command } from 'mustard-cli';
import { Register, Context, Options } from 'mustard-cli/decorators'
import { PackageManager } from 'mustard-cli/types'
const cli = Cli();
interface ICoreOptions {
name: string;
sayHi: boolean;
}
// foo --name linbudu --sayHi
@RegisterCommand()
class CoreCommand extends Command {
@Options()
commandArgs: ICoreOptions;
run(){
this.commandArgs.sayHi && this.logger.info(`Hi, ${this.commandArgs.name}!`)
return 0;
}
}
// foo create react-app --typescript --manager pnpm
@RegisterCommand("create")
class CreateCommand extends Command {
// react-app
@Input(Validator.required().isString())
input: string;
// true
@Option()
typescript: boolean;
// pnpm
@Option(Validator.custom(this.customManagerValidation))
manager: PackageManager = this.preferredPackageManager();
constructor(){
super();
}
// when there is no returned value, it's regarded as succeed
customManagerValidation(manager: string){
if(!['npm', 'yarn', 'pnpm'].includes(manager)){
return {
success: false,
message: `${manager} is not any known manager.`
}
}
}
run(){
this.spinner().start("Creating your React application...");
this.writeJson(process.cwd(), 'This is your React project!').then((res)=>{
this.spinner().end("Success!");
this.exitManually(0);
}).catch((err)=>{
this.log.error(err);
this.exitManually(1);
})
}
}
cli.initialize();
cli.useHelp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment