///
"use strict"
/**
*基础方法
*/
import * as chalk from 'chalk';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import {exec} from 'child_process';
var exp = new class umUtil {
/**
* 普通log
* @param {string} msg log文字
* @param {string} msg 文字颜色
*/
log(msg: any, chalk?: Chalk.ChalkChain) {
var t = this;
if (chalk) {
console.log(chalk(msg));
} else {
console.log(msg);
}
t.logAdd(msg);
}
/** 记录日记*/
private logStr: string = '';
/**
* 添加日记
* @param {string} msg 输出文字
* @returns {log} log
*/
private logAdd(msg) {
this.logStr += msg + '\n';
return this
}
/**
* 输出警告
* @param {string} msg log文字
*/
warn(msg): void {
return this.log(msg, chalk.yellow);
}
/**
* 输出错误
* @param {string} msg log文字
*/
error(msg): void {
return this.log(msg, chalk.red);
}
/**
* 输出成功
* @param {string} msg log文字
*/
success(msg): void {
return this.log(msg, chalk.green);
}
/**
* 输入文字
*/
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, this.logStr, '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 {
var startTime = new Date();
await fun();
var endTime = new Date();
var pTime = (endTime.getTime() - endTime.getTime()).toString();
this.warn(pTime);
return pTime;
};
}
export = exp;