UNPKG

@tuzki/cli

Version:

🐇 lowcode-cli is an efficient cli tool for Rabbitpre plugin component secondary development. ❤️

87 lines (86 loc) 2.75 kB
/* * cli 公共方法 * * @Author: xu.jin * @Date: 2023-08-15 19:21:16 * * Copyright © 2014-2023 Rabbitpre.com. All Rights Reserved. */ 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()); }); }; import Logger from '@tuzki/scaffold-logger'; import detect from 'detect-port'; import fse from 'fs-extra'; import { isAbsolute, join, resolve } from 'path'; import { getConfig } from '../utils/config.js'; const logger = Logger.get('cli:utils:common'); /** * 获取完整的 rootDir * @param {string} rootDir 传入的 rootDir * @returns */ export function getRootDir(rootDir) { return isAbsolute(rootDir) ? rootDir : join(process.cwd(), rootDir); } /** * 检查是否登录 * * @export * @return {*} */ export function checkLogin() { const { token } = getConfig(); const isLogin = !!token; if (!isLogin) { logger.error('您还未登录,请先使用 tuzki login 命令进行登录。'); process.exit(1); } else { return isLogin; } } /** * cleanup * * @export * @param {CreateConfig} config config */ export function cleanup(dirPath) { return __awaiter(this, void 0, void 0, function* () { const projPath = resolve(dirPath); yield fse.rm(projPath, { force: true, recursive: true }); }); } /** * 获取有效端口号 * * @export * @param {(string | number)} port 默认端口号 * @return {*} {Promise<number>} */ export function getValidPort(port) { return __awaiter(this, void 0, void 0, function* () { try { const newPort = (port || process.env.PORT); const defaultPort = parseInt(newPort, 10); const validPort = yield detect(defaultPort); // 检查服务启动的端口号是否被占用 if (validPort !== defaultPort) { logger.error(`${defaultPort} 端口已被占用,请换一个端口号再次尝试!`); process.exit(1); } return validPort; } catch (err) { logger.error('获取有效端口号失败', err); process.exit(1); } }); }