/** *基础方法 */ import * as chalk from 'chalk'; import * as fs from 'fs'; import * as inquirer from 'inquirer'; import { exec } from 'child_process'; import * as readline from 'readline'; export class Util { private rl: readline.ReadLine; private lastIsWrite = false; /** * 最后一行替代输出 * @param {string} msg log文字 * @param {string} msg 文字颜色 */ write(msg: any) { var t = this; readline.moveCursor(process.stdout, -1000, 0); readline.clearScreenDown(process.stdout); process.stdout.write(msg); !this.lastIsWrite && this.logArr.pop(); this.lastIsWrite = true; t.logAdd(msg); } /** * 普通log * @param {string} msg log文字 */ log(msg: any) { var t = this; this.lastIsWrite && process.stdout.write('\n'); this.lastIsWrite = false; console.log(msg); t.logAdd(msg); } /** 记录日记*/ private logArr: string[] = []; /** * 添加日记 * @param {string} msg 输出文字 * @returns {log} log */ private logAdd(msg: string) { this.logArr.push(msg); return this } /** * 输出警告 * @param {string} msg log文字 */ warn(msg): void { return this.log(chalk.yellow(msg)); } /** * 输出错误 * @param {string} msg log文字 */ error(msg): void { return this.log(chalk.red(msg)); } /** * 输出成功 * @param {string} msg log文字 */ success(msg): void { return this.log(chalk.green(msg)); } /** * 输入文字 */ input(message: string): Promise { return new Promise(function (resolve) { inquirer.prompt([{ type: 'input', name: 'type', message: message + '\n' }]).then(function (answers) { resolve(answers['type'].toString().replace(/(^\s*)|(\s*$)/g, '')); }); }); } /** * 询问列表 */ confirm(msg: string): Promise { return new Promise(function (resolve, reject) { inquirer.prompt([{ type: 'confirm', name: 'type', message: msg + '\n' }]).then(function (answers) { if (answers['type']) { resolve(true); } else { resolve(false); } }); }); } /** * 列表选择 */ list(msg: string, list: string[]): Promise { return new Promise(function (resolve) { for (var i in list) { list[i] = parseInt(i) + 1 + '. ' + list[i]; } inquirer.prompt([{ type: 'list', name: 'type', message: msg, choices: list }]).then(function (answers) { var str = answers['type'].toString(); resolve(parseInt(str.substring(0, str.indexOf('.'))) - 1); }); }); } /** * 生成日记文件 */ createLogFile(path: string = './log.txt'): Promise { return new Promise((resolve, reject) => { fs.writeFile(path, chalk.stripColor(this.logArr.join('\n\r')), 'utf8', () => { this.success('文件生成成功!'); resolve(); }); }) } /** * 执行cmd 命令 * @param text 命令 */ exec(text: string): Promise { var t = this; return new Promise((resolve, reject) => { exec(text, { cwd: process.cwd(), maxBuffer: 10240 * 10240 }, function (error, stdout, stderr) { if (error !== null) { t.error('exec 命令出错: ' + text); t.log(error); reject(stdout); } else { resolve(stdout); } }); }); } /** * 唯一码 */ uuid(): string { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; } /** * 清除前后空格 * @param str */ trim(str: String): string { return str.replace(/(^\s*)|(\s*$)/g, ''); }; /** * 性能测试 * @param fun */ async performanceTest(fun: () => any): Promise { let start = new Date().getTime();//起始时间 await fun(); var end = new Date().getTime();//接受时间 return end - start; }; } var exp = new Util(); export default exp;