ph-utils 0.2.23 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/server.js CHANGED
@@ -1,93 +1,77 @@
1
- "use strict";
2
- const child_process_1 = require("child_process");
3
- /**
4
- * node 端工具
5
- */
6
- const crypto = require("crypto");
7
- function random() {
8
- let lenOrMin = arguments[0];
9
- let max = arguments[1];
10
- if (typeof max === 'number') {
11
- return crypto.randomInt(lenOrMin, max);
12
- }
13
- let hash = crypto.randomBytes(lenOrMin).toString('hex');
14
- hash = hash.substr(0, lenOrMin);
15
- return max === true ? hash.toLocaleUpperCase() : hash;
16
- }
17
- module.exports = {
18
- /**
19
- * 进行 MD5 加密
20
- * @param {String} data 待加密的数据
21
- */
22
- md5(data) {
23
- return crypto.createHash('md5').update(String(data)).digest('hex');
24
- },
25
- /**
26
- * 生成随机数:
27
- * 1. 生成指定长度的随机字符串(包含字母), 传递的第二个参数为 boolean 类型;如果想实现纯数字的,可以考虑通过 min - max 的形式,例如:
28
- *
29
- * a. 生成 4 位长度纯数字(首位不包含0):random(1000, 10000)
30
- *
31
- * b. 生成 4 位长度(首位可以包含0):random(0, 10) + random(1000, 10000)
32
- *
33
- * c. 生成 4 位长度(首位可以包含0,使用 Math.random()实现),具体实现可以参考该工具类的 web 端的 random 代码
34
- *
35
- * 2. 生成 [min, max) 之间的随机数字(**整形**),传递的第二个参数为 number 类型,如果想要生成包含 max 的传递参数的时候,手动将 max + 1 即可
36
- * @param minOrLen 如果第二个参数为 number 类型,则表示 min,生成 [min,max) 之间的随机数,返回值类型为 int;
37
- * 如果第二个参数为 boolean 类型,则表示 len,生成的随机数的长度
38
- * @param maxOrUpper 如果类型为 number 类型则表示 max,生成 [min,max) 之间的随机数,返回值类型为 int;
39
- * 如果类型为 boolean 类型则表示 upper,生成指定长度(len)的随机字符串(包含字母),true - 返回大写, 默认为 false
40
- */
41
- random(minOrLen, maxOrUpper) {
42
- let lenOrMin = minOrLen;
43
- let max = maxOrUpper;
44
- if (typeof max === 'number') {
45
- return crypto.randomInt(lenOrMin, max);
46
- }
47
- let hash = crypto.randomBytes(lenOrMin).toString('hex');
48
- hash = hash.substr(0, lenOrMin);
49
- return max === true ? hash.toLocaleUpperCase() : hash;
50
- },
51
- /**
52
- * exec Promise 版本
53
- * @param cmd 命令名称
54
- * @param options 命令参数,基于 exec-options 基础上,增加 errorName 字段
55
- */
56
- // eslint-disable-next-line
57
- execPromise(cmd, options) {
58
- return new Promise((resolve, rejects) => {
59
- options = { ...(options || {}) };
60
- const errorName = options.errorName || 'ExecError';
61
- delete options.errorName;
62
- (0, child_process_1.exec)(cmd, options, (error, stdout, stderr) => {
63
- let err = null;
64
- let rs = null;
65
- if (error) {
66
- err = error;
67
- err.name = errorName;
68
- }
69
- else {
70
- if (stdout != null) {
71
- if (stdout.includes('ERROR') || stdout.includes('Error')) {
72
- err = new Error(stdout);
73
- err.name = errorName;
74
- }
75
- else {
76
- rs = stdout;
77
- }
78
- }
79
- else {
80
- err = new Error(stderr);
81
- err.name = errorName;
82
- }
83
- }
84
- if (err != null) {
85
- rejects(err);
86
- }
87
- else {
88
- resolve(rs);
89
- }
90
- });
91
- });
92
- },
93
- };
1
+ import { exec as execCmd, spawn } from 'child_process';
2
+ import { isBlank } from './index';
3
+ /**
4
+ * 执行命令
5
+ * @param cmd 执行的命令
6
+ * @returns
7
+ */
8
+ export function exec(cmd) {
9
+ return new Promise((resolve, reject) => {
10
+ execCmd(cmd, (err, stdout, stderr) => {
11
+ if (err) {
12
+ reject(err);
13
+ }
14
+ else {
15
+ if (!isBlank(stderr) && stderr.indexOf('error') !== -1) {
16
+ reject(new Error(stderr));
17
+ }
18
+ else {
19
+ resolve((isBlank(stdout) ? stderr : stdout).trim());
20
+ }
21
+ }
22
+ });
23
+ });
24
+ }
25
+ /**
26
+ * 执行 spawn 命令
27
+ * @param command 执行的命令: 例如: git
28
+ * @param args 命令参数: ['clone', 'https://xxxx.git']
29
+ * @param options 参数
30
+ */
31
+ export function spawnCmd(command, args, options) {
32
+ const spawnClient = spawn(command, args, { cwd: options?.cwd });
33
+ let err;
34
+ spawnClient.stdout.on('data', (chunk) => {
35
+ if (options?.data != null && typeof options.data === 'function') {
36
+ options.data(chunk);
37
+ }
38
+ });
39
+ spawnClient.stderr.on('error', (error) => {
40
+ err = error;
41
+ if (options?.error && typeof options.error === 'function') {
42
+ options.error(error);
43
+ }
44
+ });
45
+ spawnClient.on('error', (error) => {
46
+ err = error;
47
+ if (options?.error && typeof options.error === 'function') {
48
+ options.error(error);
49
+ }
50
+ });
51
+ spawnClient.on('close', () => {
52
+ if (options?.finally && typeof options.finally === 'function') {
53
+ options.finally(err);
54
+ }
55
+ });
56
+ }
57
+ /**
58
+ * 执行 spawn 命令
59
+ * @param command 执行的命令: 例如: git
60
+ * @param args 命令参数: ['clone', 'https://xxxx.git']
61
+ * @param options 参数
62
+ * @returns
63
+ */
64
+ export function spawnPromise(command, args, options) {
65
+ return new Promise((resolve, reject) => {
66
+ options = options || {};
67
+ options.finally = (err) => {
68
+ if (err == null) {
69
+ resolve(1);
70
+ }
71
+ else {
72
+ reject(err);
73
+ }
74
+ };
75
+ spawnCmd(command, args, options);
76
+ });
77
+ }
@@ -1,47 +1,47 @@
1
- /**
2
- * 数据验证器
3
- */
4
- interface RuleItem {
5
- rule: RegExp | ((v: any) => boolean) | 'required';
6
- message: string;
7
- sameKey?: string;
8
- }
9
- export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
10
- rule: string | RegExp | ((v: any) => boolean);
11
- message?: string;
12
- });
13
- export interface SchemaType {
14
- key: string;
15
- required?: boolean;
16
- type?: string | ((v: any) => void);
17
- rules: RuleType[];
18
- message?: string;
19
- }
20
- /**
21
- * 数据验证器,除了进行数据验证外,还可以同时进行数据转化
22
- */
23
- declare class Validator {
24
- rules: {
25
- [index: string]: RuleItem[];
26
- };
27
- /**
28
- * 构造数据验证转换器
29
- * @param schemas 配置验证转换规则
30
- */
31
- constructor(schemas: SchemaType[]);
32
- /**
33
- * 进行数据验证
34
- * @param data 待验证的数据
35
- * @returns
36
- */
37
- validate(data: any): Promise<boolean>;
38
- /**
39
- * 只验证指定 key 的数据格式
40
- * @param key 指定待验证的 key
41
- * @param value 待验证的数据
42
- */
43
- validateKey(key: string, value: any, data?: any): Promise<boolean>;
44
- private _validateRule;
45
- private _parseStringRule;
46
- }
47
- export default Validator;
1
+ /**
2
+ * 数据验证器
3
+ */
4
+ interface RuleItem {
5
+ rule: RegExp | ((v: any) => boolean) | 'required';
6
+ message: string;
7
+ sameKey?: string;
8
+ }
9
+ export type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
10
+ rule: string | RegExp | ((v: any) => boolean);
11
+ message?: string;
12
+ });
13
+ export interface SchemaType {
14
+ key: string;
15
+ required?: boolean;
16
+ type?: string | ((v: any) => void);
17
+ rules?: RuleType[];
18
+ message?: string;
19
+ }
20
+ /**
21
+ * 数据验证器,除了进行数据验证外,还可以同时进行数据转化
22
+ */
23
+ declare class Validator {
24
+ rules: {
25
+ [index: string]: RuleItem[];
26
+ };
27
+ /**
28
+ * 构造数据验证转换器
29
+ * @param schemas 配置验证转换规则
30
+ */
31
+ constructor(schemas: SchemaType[]);
32
+ /**
33
+ * 进行数据验证
34
+ * @param data 待验证的数据
35
+ * @returns
36
+ */
37
+ validate(data: any): Promise<boolean>;
38
+ /**
39
+ * 只验证指定 key 的数据格式
40
+ * @param key 指定待验证的 key
41
+ * @param value 待验证的数据
42
+ */
43
+ validateKey(key: string, value: any, data?: any): Promise<boolean>;
44
+ private _validateRule;
45
+ private _parseStringRule;
46
+ }
47
+ export default Validator;