Getting Started
📦 Installation
npm install @hyperse/wizard
🚀 Quick Start
Create CLI Instance
CLI instance is the core of Wizard CLI. You can use it to register commands, plugins, and other configurations. The following demonstrates how to create a CLI instance, define multilingual support, and register the commands and plugins.
// @filename: @hyperse/wizard/dist/index.d.ts
import {
,
,
,
} from '@hyperse/wizard';
// define cli locale messages
const = ({
: {
: {
: 'CLI description',
: 'CLI v1.0.0',
},
},
: {
: {
: '帮助信息插件',
: 'CLI v1.0.0',
},
},
});
// merge cli locale messages
declare module '@hyperse/wizard' {
export interface CliLocaleMessages
extends <typeof > {}
}
const = ({
: 'hps',
// Use internationalization messages
: 'cli.hpsCli.version',
// cliMessages only provide the messages for the cli, not the commands and plugins
: cliMessages,
// Use internationalization messages
: 'cli.hpsCli.- cli.hpsCli.description
- cli.hpsCli.version
description',
});
Define Commands and Subcommands
Commands and subcommands are the main features of Wizard CLI. You can use commands and subcommands to organize your CLI and extend its functionality. The following demonstrates how to define a command and a subcommand, and how to use them in the CLI instance.
// @filename: @hyperse/wizard/dist/index.d.ts
import type { } from '@hyperse/wizard';
import { , } from '@hyperse/wizard';
// define plugin locale messages
const = ({
: {
: {
: {
: 'Build description',
: {
: 'Build mini description',
: {
: 'Compiler',
},
},
: {
: 'projectCwd',
},
},
},
},
: {
: {
: {
: '构建项目插件',
: {
: '构建小程序插件',
: {
: '构建小程序插件',
},
},
: {
: '项目路径',
},
},
},
},
});
// merge plugin locale messages
declare module '@hyperse/wizard' {
export interface PluginLocaleMessages
extends <typeof > {}
}
type = {
: {
: 'webpack' | 'vite';
};
};
type = {
: string;
};
const = <'build', >('build', {
// i18n message key
: 'plugins.buildPlugin.build.description',
})
.(
<'mini', >('mini', {
// i18n message key
: 'plugins.buildPlugin.build.mini.description',
})
.({
: {
: ,
// i18n message key
: 'plugins.- plugins.buildPlugin.build.description
- plugins.buildPlugin.build.flags.projectCwd
- plugins.buildPlugin.build.mini.description
- plugins.buildPlugin.build.mini.flags.compiler
buildPlugin.build.mini.flags.compiler',
: 'webpack',
},
})
.(async () => {})
)
.({
: {
: ,
// i18n message key
: 'plugins.buildPlugin.build.flags.projectCwd',
: .(),
: true,
},
})
.((ctx: ResolveSubContextCtx<"build", BuildContext>
ctx) => {
return {
: '/foo',
};
});
Define Plugin
Plugin is the extension mechanism of Wizard CLI. You can use plugins to register commands, extend features, and add localization messages. The following demonstrates how to define a plugin, add multilingual support, and register the plugin to the CLI instance.
// @filename: @hyperse/wizard/dist/index.d.ts
import type { } from '@hyperse/wizard';
import { , } from '@hyperse/wizard';
// define plugin locale messages
const = ({
: {
: {
: 'build plugin',
},
},
: {
: {
: '构建项目插件',
},
},
});
// merge plugin locale messages
declare module '@hyperse/wizard' {
export interface PluginLocaleMessages
extends <typeof > {}
}
hpsCli
.use(
({
: 'plugins.buildPlug- plugins.buildPlugin.name
in.name',
: () => {
return .(buildCommand);
},
})
)
.on('build.mini', () => {
// when the build.mini command is executed, the ctx will be the context of the command
.log.info('build mini', .flags);
});
// parse the command
hpsCli.parse(['build', 'mini', '--compiler=webpack']);