@tuzki/cli
Version:
🐇 lowcode-cli is an efficient cli tool for Rabbitpre plugin component secondary development. ❤️
92 lines (91 loc) • 2.86 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());
});
};
/*
* debug 工具方法
*
* @Author: nichubiao
* @Date: 2023-02-22 16:55:33
*
* Copyright © 2014-2023 Rabbitpre.com. All Rights Reserved.
*/
import fs from 'fs';
import path from 'path';
import Logger from '@tuzki/scaffold-logger';
import inquirer from 'inquirer';
import { parse } from '../utils/comment-json.js';
const logger = Logger.get('cli:utils:debug-config');
const cwd = process.cwd();
/**
* 根据文件路径判断是否存在
* @param filePath 路径
* @returns
*/
export function isFileExisted(filePath) {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(cwd, filePath);
try {
fs.accessSync(absolutePath, fs.constants.F_OK);
return true;
}
catch (err) {
return false;
}
}
/**
* 获取 debug.json
* @returns
*/
export function getDebugConfig() {
const filePath = path.resolve(cwd, '.tuzki/debug.json');
let config;
try {
const configInfo = fs.readFileSync(filePath, 'utf8');
config = parse(configInfo);
}
catch (err) {
logger.debug(`找不到文件 ${filePath}`);
logger.error(err);
}
return config;
}
/**
* 是否存在 debug.json
* @returns
*/
export function hasDebugConfig() {
const dirPath = path.resolve(cwd, '.tuzki');
const debugPath = path.resolve(dirPath, 'debug.json');
return isFileExisted(debugPath);
}
export function selectDebugConfiguration(configurations) {
return __awaiter(this, void 0, void 0, function* () {
try {
const choices = configurations.map(c => ({
name: c.debugName,
value: c.debugName,
}));
const answer = yield inquirer.prompt([
{
type: 'list',
name: 'name',
message: `请选择启动配置(共${choices.length}个):`,
choices,
default: choices[0],
pageSize: 15,
},
]);
return answer.name;
}
catch (err) {
logger.error('选择 debug 配置失败:', err);
}
});
}