Created
December 17, 2021 10:11
-
-
Save linbudu599/b9072a295b50d220c2068f77c914962b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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