UNPKG

@cliz/gpm

Version:
251 lines (250 loc) 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DevTools = void 0; const path_1 = require("path"); const cli_1 = require("@cliz/cli"); const utils_1 = require("../utils"); const config_1 = require("./config"); class DevTools { constructor() { this.logger = cli_1.doreamon.logger.getLogger('DevTools'); this.config = new config_1.ConfigManager(cli_1.api.path.cwd('.gpm.yml')); } async bootstrap(options) { var _a, _b; let command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('bootstrap')) !== null && _b !== void 0 ? _b : ''; if (command === '') { if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'yarn.lock'))) { command = 'yarn bootstrap'; } else if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'pnpm-lock.yaml'))) { command = 'pnpm run bootstrap'; } else if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'package-lock.json'))) { command = 'npm run bootstrap'; } else { command = 'yarn bootstrap'; } } if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async dev(options) { var _a, _b; const { logger } = this; const command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('dev')) !== null && _b !== void 0 ? _b : 'yarn dev'; // parallel if (Array.isArray(command)) { logger.info(`[dev] command: ${command.join(',')} in context(${options === null || options === void 0 ? void 0 : options.context})`); await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } logger.info(`[dev] command: ${command} in context(${options === null || options === void 0 ? void 0 : options.context})`); await (0, utils_1.runInShell)(command); } async build(options) { var _a, _b; const command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('build')) !== null && _b !== void 0 ? _b : 'yarn build'; if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async test(options) { var _a, _b; const command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('test')) !== null && _b !== void 0 ? _b : 'yarn test'; if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async fmt(options) { var _a, _b, _c; let command = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('fmt'); const context = (_b = options === null || options === void 0 ? void 0 : options.context) !== null && _b !== void 0 ? _b : process.cwd(); if (!command) { const prettier = `${(0, path_1.resolve)(__dirname, '../../../node_modules/.bin')}/prettier`; const prettierConfigPath = cli_1.api.path.join(__dirname, '../../../config/prettierrc.json'); let glob = (_c = options === null || options === void 0 ? void 0 : options.dirOrGlob) !== null && _c !== void 0 ? _c : `src/**/*.{ts,tsx,js,jsx,json,md}`; // monorepo if (await cli_1.api.fs.exist((0, path_1.resolve)(context, 'packages/'))) { glob = (options === null || options === void 0 ? void 0 : options.dirOrGlob) ? `packages/**/${options === null || options === void 0 ? void 0 : options.dirOrGlob}` : `packages/**/src/*.{ts,tsx,js,jsx,json,md}`; } command = `${prettier} --no-error-on-unmatched-pattern --write '${glob}' --config ${prettierConfigPath}`; } if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async run(options) { var _a, _b; const command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('run')) !== null && _b !== void 0 ? _b : 'yarn prod'; if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async watch(options) { var _a; // const logger = doreamon.logger.getLogger('WatchDog'); const { logger } = this; const command = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('watch'); const context = (options === null || options === void 0 ? void 0 : options.context) || process.cwd(); const ignore = [/\.git/]; if (options.ignore) { if (!Array.isArray(options.ignore)) { ignore.push(options.ignore); } else { ignore.push(...options.ignore); } } let path = (options === null || options === void 0 ? void 0 : options.path) || context; if (!command) { throw new Error(`Watch no command found`); } if (!/^\//.test(path)) { path = `${process.cwd()}/${path}`; } const run = async (first) => { if (Array.isArray(command)) { if (!first) { console.log(); logger.info(`[watch] file changing, exec commands ...`); } return await Promise.all(command.map((e) => (0, utils_1.runInBackground)(e, { cwd: context, }))); } if (!first) { console.log(); logger.info(`[watch] file changing, exec \`${command}\` ...`); } return await (0, utils_1.runInBackground)(command, { cwd: context, }); }; logger.info(`[watch] command: ${command} in context(${context})`); logger.info(`[watch] watching ${path} ...`); function createRun() { let child; return cli_1.doreamon.func.debounce(async (options) => { if (child) { if (Array.isArray(child)) { for (const c of child) { await c.kill(); } } else { await child.kill(); } } child = await run(options); }, 1000); } const _run = createRun(); cli_1.api.fs.watch({ path, ignore, }, async () => { await _run(); }); await _run(true); } async cli(options) { var _a, _b; let command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('cli')) !== null && _b !== void 0 ? _b : ''; if (command === '') { if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'yarn.lock'))) { command = 'yarn cli'; } else if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'pnpm-lock.yaml'))) { command = 'pnpm run cli'; } else if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'package-lock.json'))) { command = 'npm run cli'; } else { command = 'yarn cli'; } } // parallel if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async install(options) { var _a, _b; let command = (_b = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('install')) !== null && _b !== void 0 ? _b : ''; if (command === '') { if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'yarn.lock'))) { command = 'yarn'; } else if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'pnpm-lock.yaml'))) { command = 'pnpm'; } else if (await cli_1.api.fs.exist(cli_1.api.path.join(process.cwd(), 'package-lock.json'))) { command = 'npm'; } else { command = 'yarn'; } } if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } async sync() { await (0, utils_1.runInShell)(`git pull --progress`); } async clean(options) { var _a; const command = (_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : this.config.get('clean'); if (command) { if (Array.isArray(command)) { await Promise.all(command.map((e) => (0, utils_1.runInShell)(e))); return; } await (0, utils_1.runInShell)(command); } const dirs = ['node_modules', 'dist', 'lib']; const answers = await cli_1.inquirer.prompt([ { name: 'ok', type: 'confirm', message: `Confirm to remove ${dirs.join('/')} ?`, default: false, }, ]); if (answers.ok) { for (const name of dirs) { const dir = (0, path_1.resolve)(process.cwd(), name); if ((await cli_1.api.fs.exist(dir)) && (await cli_1.api.fs.isDir(dir))) { await cli_1.api.fs.rimraf(dir); } } } } // xxx async commit() { const command = `${(0, path_1.resolve)(__dirname, '../../../node_modules/.bin')}/cliz-commit`; await (0, utils_1.runInShell)(command); } async prepare() { await this.config.prepare(); } } exports.DevTools = DevTools;