UNPKG

@ainc/script

Version:

Script compiler for typescript

138 lines 3.73 kB
/** ***************************************** * Created by edonet@163.com * Created on 2021-04-11 12:17:19 ***************************************** */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = exports.Command = void 0; /** ***************************************** * 加载依赖 ***************************************** */ const cwd_1 = require("@ainc/fs/dist/cwd"); const json_1 = require("@ainc/fs/dist/json"); const argv_1 = require("./helpers/argv"); /** ***************************************** * 选项 ***************************************** */ const parser = new argv_1.Parser({ project: { type: 'array', alias: 'p', description: 'compile typescript project.', }, eval: { type: 'boolean', alias: 'e', description: 'eval source code.', }, watch: { type: 'boolean', alias: 'w', description: 'watch input files.', }, exclude: { type: 'array', description: 'the exclude pattern for compile.', }, version: { type: 'boolean', alias: 'v', description: 'Print the command\'s version.', }, help: { type: 'boolean', alias: 'h', description: 'print this message.', }, }); /** ***************************************** * 定义命令 ***************************************** */ class Command { /** 执行命令 */ run(argv) { const opts = parser.parse(argv); // 显示帮助信息 if (opts.help || !argv.length) { return this.help(); } // 显示版本信息 if (opts.version) { return this.version(); } // 编译项目 if (opts.project) { const { compile } = require('./compiler'); // 执行编译 return compile({ watch: opts.watch, argv: opts.argv, include: opts.project, exclude: opts.exclude, }); } // 获取脚本输入 const input = opts.argv.find(key => key.charAt(0) !== '-'); // 执行文件 if (input) { const { Script } = require('./script'); const script = new Script(); // 打印结果 this.print(opts.eval ? script.run(input) : script.execute(input)); // 监听文件 opts.watch && script.watch(this.print); } } /** 打印帮助信息 */ help() { const path = (0, cwd_1.cwd)(__dirname, '../package.json'); const pkg = (0, json_1.json)(path) || {}; // 打印配置信息 parser.print({ name: 'esc', usage: '[flags]', version: pkg.version, description: pkg.description, }); } /** 打印版本信息 */ version() { const path = (0, cwd_1.cwd)(__dirname, '../package.json'); const pkg = (0, json_1.json)(path) || {}; // 打印内容 console.log(`Version: ${pkg.version}`); } /** 执行脚本 */ print(status) { if (typeof status !== 'object' || (status && Object.keys(status).length)) { console.log(status); } } } exports.Command = Command; /** ***************************************** * 以主模块启动 ***************************************** */ if (module === require.main) { run(); } /** ***************************************** * 执行命令 ***************************************** */ function run(argv = process.argv.slice(2)) { return new Command().run(argv); } exports.run = run; //# sourceMappingURL=cli.js.map