create-cliz
Version:
Cliz Multiple Commands CLI Template
137 lines (134 loc) • 5.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = void 0;
const cli_1 = require("@cliz/cli");
const boxen = require("boxen");
const config_1 = require("../config");
const generator_1 = require("../utils/generator");
async function create(options) {
const config = new config_1.CLIConfig();
config.set('codePath', options === null || options === void 0 ? void 0 : options.codePath);
// await generate(config.toJSON());
// return;
const answers = await cli_1.inquirer.prompt([
{
type: 'list',
name: 'type',
message: '请选择 CLI 类型?',
choices: [
{
name: `multiple - ${cli_1.api.color.chalk.gray('组合命令模式,比如 docker / kubectl 等')}`,
value: 'multiple-commands',
},
{
name: `single - ${cli_1.api.color.chalk.gray('单一命令模式,比如 ls / find 等')}`,
value: 'single-command',
},
],
required: true,
},
{
type: 'input',
name: 'name',
message: '请输入 CLI 名称?',
validate: async (input) => {
if (!input) {
return '请输入 CLI 名称!';
}
if (input.length < 2) {
return 'CLI 名称长度不能小于 2 个字符!';
}
if (input.length > 50) {
return 'CLI 名称长度不能大于 50 个字符!';
}
if (!/^[a-zA-Z0-9_]+$/.test(input)) {
return 'CLI 名称只能包含字母、数字、下划线!';
}
if (await cli_1.api.fs.exist(config.getProjectPath(input))) {
return '项目或 CLI 已存在,请使用其他名称!';
}
return true;
},
required: true,
},
{
type: 'input',
name: 'description',
message: '请输入 CLI 描述?',
validate: (input) => {
if (!input) {
return 'CLI 描述是必须的!';
}
if (input.length < 2) {
return 'CLI 描述长度不能小于 2 个字符!';
}
if (input.length > 512) {
return 'CLI 描述长度不能大于 512 个字符!';
}
return true;
},
required: true,
},
{
type: 'input',
name: 'author',
message: '请输入 CLI 作者?',
validate: (input) => {
if (!input) {
return 'CLI 作者是必须的!';
}
if (input.length < 2) {
return '作者长度不能小于 2 个字符!';
}
if (input.length > 100) {
return '作者长度不能大于 100 个字符!';
}
return true;
},
required: true,
},
{
type: 'input',
name: 'scope',
message: '请输入 CLI NPM Scope?(可选)',
},
{
type: 'input',
name: 'repository',
message: '请输入 CLI 仓库地址?(可选)',
},
{
type: 'input',
name: 'homepage',
message: '请输入 CLI 官网地址?(可选)',
},
]);
config.set('type', answers.type);
config.set('name', answers.name);
config.set('description', answers.description);
config.set('author', answers.author);
config.set('scope', answers.scope);
config.set('repository', answers.repository);
config.set('homepage', answers.homepage);
if (cli_1.api.is.debug()) {
console.info(`Config: ${JSON.stringify(config.toJSON(), null, 2)}`);
}
await (0, generator_1.generate)(config.toJSON());
const message = `
${cli_1.api.color.chalk.green('Everything is ready!')}
Project path(${cli_1.api.color.chalk.cyan(config.projectPath)}) (${cli_1.api.color.chalk.grey('copied to clipboard')})
- cd ${cli_1.api.color.chalk.cyan(config.projectPath)}
Available commands:
- ${cli_1.api.color.chalk.bold('Bootstrap ')}: ${cli_1.api.color.chalk.cyan('yarn bootstrap')}
- ${cli_1.api.color.chalk.bold('Dev ')}: ${cli_1.api.color.chalk.cyan('yarn dev')}
- ${cli_1.api.color.chalk.bold('CLI ')}: ${cli_1.api.color.chalk.cyan('yarn cli')}
- ${cli_1.api.color.chalk.bold('Build ')}: ${cli_1.api.color.chalk.cyan('yarn build')}
- ${cli_1.api.color.chalk.bold('Register to global ')}: ${cli_1.api.color.chalk.cyan('yarn register')}
`;
console.log(boxen(message, {
padding: 1,
borderColor: 'green',
margin: 1,
}));
}
exports.create = create;