Last active
December 23, 2021 09:08
-
-
Save linbudu599/b35ee10dbeb33facd54de6962f9eb31a to your computer and use it in GitHub Desktop.
This file contains 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, Validator, OptionFactory } from "mustard-cli"; | |
import { RegisterCommand, Option, Options, Input } 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}!`); | |
} | |
} | |
// foo create react-app --typescript --manager pnpm | |
@RegisterCommand("create", "create command!", { | |
ignoreUnknownArgs: true, | |
}) | |
class CreateSubCommand extends Command { | |
// value: ‘react-app’ | |
@Input(Validator.required().isString()) | |
input: string; | |
// value: true | |
@Option() | |
typescript: boolean; | |
// value: ‘pnpm’ | |
@Option(Validator.custom(this.customManagerValidation)) | |
manager: PackageManager = this.preferredPackageManager(); | |
constructor() { | |
super(); | |
} | |
customManagerValidation(manager: string) { | |
if (!["npm", "yarn", "pnpm"].includes(manager)) { | |
return { | |
success: false, | |
message: `${manager} is not any known manager.`, | |
}; | |
} | |
} | |
run() {} | |
} | |
cli.initialize(); | |
cli.enableUsage({}); | |
cli.enableHelpCommand(); | |
cli.enableVersionCommand(require("../package.json").version, { | |
flag: OptionFactory.create("version").alias("v"), | |
}); | |
cli.enableUpdateNotifier({ | |
forceUpdate: true, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment