@lcap/builder
Version:
lcap builder utils
117 lines (116 loc) • 5.06 kB
JavaScript
;
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.executeCreateLogic = exports.createLogic = exports.getCreateLogicOptions = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = require("lodash");
const path_1 = __importDefault(require("path"));
const picocolors_1 = __importDefault(require("picocolors"));
const prompts_1 = __importDefault(require("prompts"));
const logger_1 = __importDefault(require("../utils/logger"));
const LOGICS_PATH = 'src/logics/index.ts';
const LOGIC_TEMPLATE = [
'/**',
' * @NaslLogic',
' * @type {{type}}',
' * @title {{title}}',
' * @desc {{title}}',
' * @param str 参数描述',
' * @returns 返回结果描述',
' */',
'export function {{name}}(str: nasl.core.String) {',
'}',
'',
].join('\n');
function getCreateLogicOptions(rootPath, metaInfo) {
return __awaiter(this, void 0, void 0, function* () {
let result;
try {
result = yield (0, prompts_1.default)([
{
type: 'text',
name: 'name',
message: '请输入逻辑名称(使用小驼峰方式命名,例如:filterNull)',
format: (val) => (0, lodash_1.camelCase)(val).trim(),
validate: (v) => {
if (!v || v.trim === '') {
return '逻辑名字不能为空';
}
return true;
},
},
{
type: 'text',
name: 'title',
message: '请输入别名(中文名), 例如:过滤列表',
format: (val) => val.trim(),
validate: (v) => {
if (!v || v.trim === '') {
return '别名不能为空';
}
return true;
},
},
{
type: metaInfo.framework === 'react' ? null : 'select',
name: 'type',
message: '请选择端',
initial: 0,
choices: [
{ value: 'pc', title: 'PC端' },
{ value: 'h5', title: 'H5端' },
{ value: 'both', title: '全部' },
],
},
], {
onCancel: () => {
// eslint-disable-next-line prefer-template
throw new Error(picocolors_1.default.red('✖') + ' 已取消');
},
});
}
catch (cancelled) {
console.log(cancelled.message);
return null;
}
return result;
});
}
exports.getCreateLogicOptions = getCreateLogicOptions;
function createLogic(rootPath, metaInfo, options) {
return __awaiter(this, void 0, void 0, function* () {
const logicFilePath = path_1.default.resolve(rootPath, LOGICS_PATH);
if (!fs_extra_1.default.existsSync(logicFilePath)) {
throw new Error(`逻辑存储文件 ${logicFilePath} 不存在`);
}
const content = fs_extra_1.default.readFileSync(logicFilePath, 'utf-8').toString();
const logicCode = LOGIC_TEMPLATE.replace(/\{\{type\}\}/g, options.type)
.replace(/\{\{name\}\}/g, options.name)
.replace(/\{\{title\}\}/g, options.title);
fs_extra_1.default.writeFileSync(logicFilePath, `${content}\n${logicCode}`, 'utf-8');
});
}
exports.createLogic = createLogic;
function executeCreateLogic(rootPath, metaInfo, prompt) {
return __awaiter(this, void 0, void 0, function* () {
const options = prompt !== null && prompt !== void 0 ? prompt : yield getCreateLogicOptions(rootPath, metaInfo);
if (!options) {
return;
}
logger_1.default.start(`开始创建逻辑 ${options.name} ......`);
yield createLogic(rootPath, metaInfo, options);
logger_1.default.success(`创建逻辑成功 ${options.name} !`);
});
}
exports.executeCreateLogic = executeCreateLogic;