UNPKG

@ywfe/cli

Version:

遥望前端开发命令行工具

176 lines (175 loc) 8.45 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.api = void 0; const chalk_1 = __importDefault(require("chalk")); const fs_extra_1 = __importDefault(require("fs-extra")); const R = __importStar(require("ramda")); const inquirer_1 = __importDefault(require("inquirer")); const messages_1 = require("../util/messages"); const yapi_1 = require("../service/yapi"); const config_1 = require("../util/config"); const utils_1 = require("../module/apiGen/utils"); const api_factory_1 = require("../module/apiGen/api.factory"); const api_remove_1 = require("../module/apiGen/api.remove"); const constants_1 = require("../util/constants"); const DEFAULT_PROJECT_ID = 'DEFAULT'; const APILIST_SLICE_NUM = 30; const api = (cliInputIds, options) => __awaiter(void 0, void 0, void 0, function* () { let sTime = Date.now(); let ywConfig = {}; if (fs_extra_1.default.existsSync(constants_1.YW_CONFIG_FILE)) { try { ywConfig = fs_extra_1.default.readJSONSync(constants_1.YW_CONFIG_FILE); } catch (err) { (0, utils_1.logger)(chalk_1.default.red(`✖️ ${constants_1.YW_CONFIG_FILENAME} 格式错误,程序退出。`)); process.exit(1); } } else { (0, utils_1.logger)(chalk_1.default.red(`✖️ 当前目录中不存在 ${constants_1.YW_CONFIG_FILENAME} 配置文件,程序退出。`)); process.exit(1); } const idInput = cliInputIds || ''; const projectIdInput = options.projectId || DEFAULT_PROJECT_ID; const apiTypeInput = options.apiType || ''; const isOld = (options.old && options.old === 'true') ? true : false; if (!cliInputIds) { const generateAllPrompt = (optProjectId) => __awaiter(void 0, void 0, void 0, function* () { const answer = yield inquirer_1.default.prompt([ { type: 'confirm', name: 'continue', message: chalk_1.default.yellow(`⚠️ 未指定 ApiId ${(!optProjectId || optProjectId === '') ? ' 和 projectId' : ''},将生成[ ${optProjectId ? optProjectId : 'yapi_apiId'} ]下全部API,是否继续?`), default: false } ]); if (!answer.continue) { (0, utils_1.logger)('退出程序'); process.exit(0); } else { } }); yield generateAllPrompt(options.projectId || ''); } if (!cliInputIds && !projectIdInput) { (0, utils_1.logger)(chalk_1.default.red('⚠️ 指定apiId生成,需同时指定个projectID, 如: --projectId=PRO_DIANHU')); process.exit(1); } const { yapi_projectId = {}, yapi_apiId = {}, source_path, yapi_apiId_mock = [], } = ywConfig; const ywfeConfigYApi = (0, config_1.getYWFEConfig)('yapi'); const loginCookie = yield (0, yapi_1.yapiAutoLogin)(ywfeConfigYApi); if (!loginCookie) { (0, utils_1.logger)(chalk_1.default.red(messages_1.MESSAGES.YAPI_LOGIN_ERROR)); process.exit(1); } ywConfig.yapiCookie = encodeURIComponent(JSON.stringify(loginCookie.join(';'))); ywConfig.yapiDomain = ywfeConfigYApi.yapiDomain; const generateApiFiles = (options, batchIndex = 1, totalBatch = 1) => __awaiter(void 0, void 0, void 0, function* () { yield (0, api_factory_1.apiGen)(Object.assign({}, options)); if (batchIndex === totalBatch) { (0, utils_1.logger)(chalk_1.default.green('✓ 全部生成完成')); (0, utils_1.logger)(chalk_1.default.green(`⏲ 耗时:${(Date.now()) - sTime}ms`)); } else { (0, utils_1.logger)(chalk_1.default.green('✓ 生成成功')); } }); if (idInput && projectIdInput) { const inputIds = cliInputIds.split(','); if (options === null || options === void 0 ? void 0 : options.remove) { (0, api_remove_1.apiRemove)(inputIds, projectIdInput); (0, utils_1.logger)(chalk_1.default.green('✔️ 删除API完成')); return; } const tempApiList = []; inputIds.map((item) => { tempApiList.push({ id: item, projectId: projectIdInput, isV2: projectIdInput.includes('_V2') ? true : false, isFunction: false, isMethod: false, isMock: yapi_apiId_mock && yapi_apiId_mock.includes(+item), }); if (idInput && projectIdInput && yapi_apiId[projectIdInput] && yapi_apiId[projectIdInput].indexOf(item) < 0) { yapi_apiId[projectIdInput].push(Number(item)); } }); const generateOptions = { 'yapi_domain': ywConfig.yapiDomain, 'source_path': source_path, 'yapi_apis': encodeURIComponent(JSON.stringify(tempApiList)), 'yapi_apiId': encodeURIComponent(JSON.stringify(yapi_apiId)), 'yapiCookie': ywConfig.yapiCookie, 'old': isOld, 'total': tempApiList.length, }; yield generateApiFiles(generateOptions, 1, 1); } else { const apis = (0, utils_1.getApiIdsList)(ywConfig, options.projectId || ''); if (apis.length) { const apisList = R.splitEvery(APILIST_SLICE_NUM, apis); const loop = () => __awaiter(void 0, void 0, void 0, function* () { (0, utils_1.logger)(chalk_1.default.green(`»» 分批次生成API请求文件。[共 ${apis.length} 个API, 每批次 ${APILIST_SLICE_NUM} 个,分 ${apisList.length} 批生成]`)); let batchIndex = 0; for (let i = 0; i < apisList.length; i++) { batchIndex++; (0, utils_1.logger)(chalk_1.default.green(`»» 开始生成第 ${batchIndex} 批`)); const generateOptions = { 'yapi_domain': ywConfig.yapiDomain, 'source_path': source_path, 'yapi_projectId': encodeURIComponent(JSON.stringify(yapi_projectId)), 'yapi_apis': encodeURIComponent(JSON.stringify(apisList[i])), 'yapiCookie': ywConfig.yapiCookie, 'old': isOld, 'total': apis.length, }; yield generateApiFiles(generateOptions, batchIndex, apisList.length); } }); loop(); } } }); exports.api = api;