UNPKG

@lark-project/cli

Version:

飞书项目插件开发工具

111 lines (110 loc) 5.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addInitCommand = void 0; const path_1 = __importDefault(require("path")); const inquirer_1 = __importDefault(require("inquirer")); const types_1 = require("../../types"); const logger_1 = require("../../utils/logger"); const validate_tools_1 = require("../../utils/validate-tools"); const run_script_1 = __importDefault(require("../../utils/run-script")); const is_empty_dir_1 = require("../../utils/is-empty-dir"); const DEFAULT_PLUGIN_NAME = 'project-name'; const inquirerProjectName = async (defaultName, force) => { const { action } = await inquirer_1.default.prompt([ { name: 'action', type: 'input', message: 'What is your project named?', default: defaultName, validate: (input) => { const targetDir = path_1.default.resolve(process.cwd(), input); if (!(0, validate_tools_1.isValidDirectoryName)(input)) { return `The projectName "${input}" is not valid, the name can contain only a-zA-Z,0-9,_,-. please check and retry.`; } if (!(0, is_empty_dir_1.isEmptyDir)(targetDir) && !force) { return `The target directory ${targetDir} already exists, please check and retry.`; } return true; }, }, ]); return action; }; function addInitCommand(program) { program .command('init') .description('Create a new plugin project.') .argument('<project-name>', 'Specify the name of a new directory in the current working directory. The name can contain only alpha、number、underscore、hyphens.') .argument('<plugin-id>', 'Specify the plugin id from the developer site. The plugin id starts with MII.') .argument('<plugin-secret>', 'Specify the plugin secret from the developer site.') // 原先 arguments 有三个 <project-name> <plugin-id> <plugin-secret> // 考虑 plugin-secret 后续要下线,argument 是依赖顺序的,所以 site-domain 作为 option 不作为 argument .requiredOption('--site-domain <site-domain>', 'Specify the origin of the developer site, e.g. https://[example].com .') .option('--template-id [template-id]', 'The IDs used to identify different template codes.') .option('-f, --force', 'Overwrite the target directory if it exist.') .action(async (projectName, pluginId, pluginSecret, options) => { const { siteDomain, templateId = 'react-initialized', force } = options; let newProjectName = projectName; // 默认值走重新输入逻辑 if (projectName === DEFAULT_PLUGIN_NAME) { newProjectName = await inquirerProjectName(DEFAULT_PLUGIN_NAME); } else { // 可能用户直接更改了命令行取名的,那就直接校验 if (!(0, validate_tools_1.isValidDirectoryName)(projectName)) { logger_1.logger.error(`The projectName "${projectName}" is not valid, the name can contain only alpha、number、underscore、hyphens, please check and retry.`); newProjectName = await inquirerProjectName(projectName); } } if (!(0, validate_tools_1.isValidPluginId)(pluginId)) { logger_1.logger.error(`The pluginId "${pluginId}" is not valid, please check and retry.`); process.exit(1); } if (!(0, validate_tools_1.isValidURL)(siteDomain) || !siteDomain.startsWith('http')) { logger_1.logger.error(`The siteDomain "${siteDomain}" is not valid, please check and retry.`); process.exit(1); } const targetDir = path_1.default.resolve(process.cwd(), newProjectName); // 目录不为空且不指定 force 需二次确认 if (!(0, is_empty_dir_1.isEmptyDir)(targetDir) && !force) { const { action } = await inquirer_1.default.prompt([ { name: 'action', type: 'list', message: `The target directory ${targetDir} already exists, pick an action:`, choices: [ { name: 'Overwrite, will delete files in the directory', value: 'overwrite', }, { name: 'Cancel', value: false, }, ], default: false, }, ]); if (action !== 'overwrite') { logger_1.logger.warn('The `lpm init` is canceled, please check and retry.'); process.exit(1); } } (0, run_script_1.default)(path_1.default.join(__dirname, '../dispatcher'), [ '--command', types_1.ECommandName.init, '--payload', JSON.stringify({ projectName: newProjectName, pluginId, pluginSecret, siteDomain, templateId, }), ]); }); } exports.addInitCommand = addInitCommand;