@shencom/oss-upload
Version:
code upload to oss
152 lines (125 loc) • 3.46 kB
text/typescript
import { execSync } from 'child_process';
import { existsSync, statSync } from 'fs';
import crypto from 'crypto';
import minimist from 'minimist';
import ora from 'ora';
import inquirer from 'inquirer';
import log from '@shencom/npmlog';
import chalk from 'chalk';
import semver from 'semver';
export const _argv = minimist(process.argv);
/** 构建模式 */
export const Mode = _argv._[2] as 'build' | 'serve';
/** 构建环境 */
export const Env = _argv.mode as 'tst' | 'production' | 'uat';
export const isTst = Env === 'tst';
export const isUat = Env === 'uat';
export const isPro = Env === 'production';
export const isBuild = Mode === 'build';
export const isServe = Mode === 'serve';
/** 跳过打包,直接上传 */
export const isUpload = _argv.u as boolean;
export const isDebug = _argv.debug as boolean;
/** 环境名 */
export const envName = `${isTst ? '测试' : isUat ? 'UAT' : '正式'}服` as
| '测试服'
| 'UAT'
| '正式服';
export const spinner = ora();
spinner.spinner = 'clock';
function _branch() {
try {
return execSync('git symbolic-ref --short -q HEAD', { encoding: 'utf8' }).replace('\n', '');
} catch (error) {
return '';
}
}
export const branch = _branch();
/**
* md5加密
*
* @export
* @param {string | Buffer | NodeJS.TypedArray | DataView} val
* @returns
*/
export function md5(val: string | Buffer | NodeJS.TypedArray | DataView) {
const _md5 = crypto.createHash('md5');
const result = _md5.update(val as crypto.BinaryLike).digest('hex');
return result.toLocaleUpperCase();
}
/**
* 获取命令行参数
*
* @export
* @returns
*/
export function getCliParam() {
const argvKey = Object.keys(_argv).filter((k) => k !== '_');
return argvKey.map((key) => `--${key} ${_argv[key]}`);
}
export function confirm(message: string) {
return new Promise<boolean>((resolve, reject) => {
inquirer
.prompt({
type: 'confirm',
name: 'flag',
message: chalk.yellow(message),
default: true,
prefix: '⚠️ ',
})
.then((answers: { flag: boolean }) => {
resolve(answers.flag);
})
.catch(reject);
});
}
/**
* 验证打包 git 分支 是否对应
*
* `master => 正式`
*
* `uat => UAT`
*
* @export
* @returns
*/
export function verifyBuild() {
if (isUat && branch !== 'uat') {
spinner.fail(chalk.red(' 请使用 uat 分支打包UAT'));
return false;
}
if (isPro && branch !== 'master') {
spinner.fail(chalk.red(' 请使用 master 分支打包正式服'));
return false;
}
return true;
}
/**
* 文件是否存在
*
* @export
* @param {string} filePath 文件路径
* @returns
*/
export function isFile(filePath: string) {
try {
const isFile = statSync(filePath).isFile();
return isFile;
} catch (error) {
return false;
}
}
/** 检测路径是否存在 */
export function CheckEnvPath(envPath: string) {
if (!envPath) return '';
const envPathExists = existsSync(envPath);
return envPathExists ? envPath : '';
}
export function throwErr(prefix: string, message: string) {
log.error(prefix, message);
process.exit();
}
export const versionReg = /\/(\d+(.\d+){0,3})\//;
export const versionExec = (v: string) => versionReg.exec(v);
export const versionSort = (v: string[]) => v.sort((a, b) => semver.compare(a, b, true));
export const versionValid = (v: string) => semver.valid(semver.coerce(v || '', { loose: true }));