ph-utils 0.2.22 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.md +11 -11
- package/lib/date.d.ts +55 -34
- package/lib/date.js +203 -126
- package/lib/dom.d.ts +92 -92
- package/lib/dom.js +190 -190
- package/lib/file.d.ts +31 -34
- package/lib/file.js +96 -99
- package/lib/index.d.ts +97 -61
- package/lib/index.js +171 -82
- package/lib/server.d.ts +32 -39
- package/lib/server.js +77 -93
- package/lib/validator.d.ts +47 -47
- package/lib/validator.js +212 -215
- package/lib/web.d.ts +13 -55
- package/lib/web.js +57 -141
- package/package.json +10 -9
- package/lib/date_m.d.ts +0 -34
- package/lib/date_m.js +0 -119
- package/lib/index_m.d.ts +0 -61
- package/lib/index_m.js +0 -82
- package/lib/validator_m.d.ts +0 -47
- package/lib/validator_m.js +0 -210
package/lib/server.js
CHANGED
@@ -1,93 +1,77 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
/**
|
4
|
-
*
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
+
}
|
package/lib/validator.d.ts
CHANGED
@@ -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
|
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
|
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;
|