@arkxio/ark-dev-utils 0.1.6 → 0.1.9

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.
@@ -1,124 +1,124 @@
1
- /** @typedef {import('../../typings').FileDesc} FileDesc*/
2
- import * as fs from 'fs';
3
- import cst from '../configs/consts';
4
-
5
- /**
6
- * 递归获得某个目录下的所有文件绝对路径
7
- * @param {string} dirPath 形如:/user/zzk/log/build
8
- * @return {string[]} filePathList
9
- * 形如 ['/user/zzk/log/build/js/xx.js', '/user/zzk/log/build/img/xx.png']
10
- */
11
- export function getAllFilePath(dirPath) {
12
- const _getAllFilePath = (dirPath, filePathList) => {
13
- const names = fs.readdirSync(dirPath);
14
- names.forEach((name) => {
15
- const stats = fs.statSync(`${dirPath}/${name}`);
16
- if (stats.isDirectory()) {
17
- _getAllFilePath(`${dirPath}/${name}`, filePathList);
18
- } else {
19
- filePathList.push(`${dirPath}/${name}`);
20
- }
21
- });
22
- };
23
-
24
- const filePathList = [];
25
- _getAllFilePath(dirPath, filePathList);
26
- return filePathList;
27
- }
28
-
29
- /**
30
- * 生成符合上传cos的文件描述对象列表
31
- * @return {FileDesc[]} fileDescList
32
- */
33
- export function makeFileDescList(fileFullPathList, appHomePage, splitStrForFilePathUnderBuild = 'build/') {
34
- const fileDescList = [];
35
- const appVersion = getAppVersionFromHomePage(appHomePage);
36
- const zoneName = getZoneNameFromHomePage(appHomePage);
37
-
38
- fileFullPathList.forEach((fileAbsolutePath) => {
39
- // 获取文件处于build目录下的相对路径,形如:
40
- // /static/js/runtime-main.66c45929.js
41
- // /asset-manifest.json
42
- const filePathUnderBuild = fileAbsolutePath.split(splitStrForFilePathUnderBuild)[1];
43
-
44
- fileDescList.push({
45
- fileAbsolutePath,
46
- fileWebPathWithoutHost: `${zoneName}/${appVersion}/${filePathUnderBuild}`,
47
- fileWebPath: `${appHomePage}/${filePathUnderBuild}`,
48
- });
49
- });
50
-
51
- return fileDescList;
52
- }
53
-
54
- export function verbose(...args) {
55
- console.log('[Ark-verbose:] ', ...args);
56
- }
57
-
58
- /**
59
- * verbose with handler
60
- * @param {} argHandler
61
- * @param {...any} args
62
- */
63
- export function verboseH(argHandler, ...args) {
64
- const handledArgs = args.map((arg, idx) => argHandler(arg, idx));
65
- verbose(...handledArgs);
66
- }
67
-
68
- /**
69
- * stringify obj in args
70
- * @param {...any} args
71
- */
72
- export function verboseObj(...args) {
73
- verboseH((arg) => {
74
- if (typeof arg === 'object') return JSON.stringify(arg);
75
- return arg;
76
- }, ...args);
77
- }
78
-
79
- /**
80
- * input : https://xxxxx.yy.com/<zoneName>/<relativeDirName>
81
- * output: https://xxxxx.yy.com
82
- * @param {string} homePage
83
- */
84
- export function getCdnHostFromHomePage(homePage) {
85
- const [protocolStr, restStr] = homePage.split('//');
86
- const [hostName] = restStr.split('/');
87
- return `${protocolStr}//${hostName}`;
88
- }
89
-
90
- /**
91
- * input : https://xxxxx.yy.com/<zoneName>/<relativeDirName>
92
- * output: <relativeDirName>
93
- * @param {string} homePage
94
- * @return {string} appVersion
95
- */
96
- export function getAppVersionFromHomePage(homePage) {
97
- const arr = homePage.split('/');
98
- return arr[arr.length - 1];
99
- }
100
-
101
- /**
102
- * input : https://xxxxx.yy.com/<zoneName>/<relativeDirName>
103
- * output: <zoneName>
104
- * @param {string} homePage
105
- * @return {string} appVersion
106
- */
107
- export function getZoneNameFromHomePage(homePage) {
108
- const arr = homePage.split('/');
109
- return arr[arr.length - 2];
110
- }
111
-
112
- const cdnType2host = {
113
- unpkg: 'https://unpkg.com',
114
- jsdelivr: 'https://cdn.jsdelivr.net/npm',
115
- };
116
-
117
- export function getNpmCdnHomePage(packageJson, options) {
118
- const { npmCdnType = cst.DEFAULT_NPM_CDN_TYPE, distDir = cst.ARK_DIST_DIR, homePage } = options;
119
- const { name, version } = packageJson;
120
- // 优先考虑用户透传的 homePage,表示用户部署了 unpkg 私服
121
- const unpkgHost = homePage || cdnType2host[npmCdnType] || '';
122
- // TODO,未来考虑更多类型的 cdn,如:jsdelivr
123
- return `${unpkgHost}/${name}@${version}/${distDir}`;
124
- }
1
+ /** @typedef {import('../../typings').FileDesc} FileDesc*/
2
+ import * as fs from 'fs';
3
+ import cst from '../configs/consts';
4
+
5
+ /**
6
+ * 递归获得某个目录下的所有文件绝对路径
7
+ * @param {string} dirPath 形如:/user/zzk/log/build
8
+ * @return {string[]} filePathList
9
+ * 形如 ['/user/zzk/log/build/js/xx.js', '/user/zzk/log/build/img/xx.png']
10
+ */
11
+ export function getAllFilePath(dirPath) {
12
+ const _getAllFilePath = (dirPath, filePathList) => {
13
+ const names = fs.readdirSync(dirPath);
14
+ names.forEach((name) => {
15
+ const stats = fs.statSync(`${dirPath}/${name}`);
16
+ if (stats.isDirectory()) {
17
+ _getAllFilePath(`${dirPath}/${name}`, filePathList);
18
+ } else {
19
+ filePathList.push(`${dirPath}/${name}`);
20
+ }
21
+ });
22
+ };
23
+
24
+ const filePathList = [];
25
+ _getAllFilePath(dirPath, filePathList);
26
+ return filePathList;
27
+ }
28
+
29
+ /**
30
+ * 生成符合上传cos的文件描述对象列表
31
+ * @return {FileDesc[]} fileDescList
32
+ */
33
+ export function makeFileDescList(fileFullPathList, appHomePage, splitStrForFilePathUnderBuild = 'build/') {
34
+ const fileDescList = [];
35
+ const appVersion = getAppVersionFromHomePage(appHomePage);
36
+ const zoneName = getZoneNameFromHomePage(appHomePage);
37
+
38
+ fileFullPathList.forEach((fileAbsolutePath) => {
39
+ // 获取文件处于build目录下的相对路径,形如:
40
+ // /static/js/runtime-main.66c45929.js
41
+ // /asset-manifest.json
42
+ const filePathUnderBuild = fileAbsolutePath.split(splitStrForFilePathUnderBuild)[1];
43
+
44
+ fileDescList.push({
45
+ fileAbsolutePath,
46
+ fileWebPathWithoutHost: `${zoneName}/${appVersion}/${filePathUnderBuild}`,
47
+ fileWebPath: `${appHomePage}/${filePathUnderBuild}`,
48
+ });
49
+ });
50
+
51
+ return fileDescList;
52
+ }
53
+
54
+ export function verbose(...args) {
55
+ console.log('[Ark-verbose:] ', ...args);
56
+ }
57
+
58
+ /**
59
+ * verbose with handler
60
+ * @param {} argHandler
61
+ * @param {...any} args
62
+ */
63
+ export function verboseH(argHandler, ...args) {
64
+ const handledArgs = args.map((arg, idx) => argHandler(arg, idx));
65
+ verbose(...handledArgs);
66
+ }
67
+
68
+ /**
69
+ * stringify obj in args
70
+ * @param {...any} args
71
+ */
72
+ export function verboseObj(...args) {
73
+ verboseH((arg) => {
74
+ if (typeof arg === 'object') return JSON.stringify(arg);
75
+ return arg;
76
+ }, ...args);
77
+ }
78
+
79
+ /**
80
+ * input : https://xxxxx.yy.com/<zoneName>/<relativeDirName>
81
+ * output: https://xxxxx.yy.com
82
+ * @param {string} homePage
83
+ */
84
+ export function getCdnHostFromHomePage(homePage) {
85
+ const [protocolStr, restStr] = homePage.split('//');
86
+ const [hostName] = restStr.split('/');
87
+ return `${protocolStr}//${hostName}`;
88
+ }
89
+
90
+ /**
91
+ * input : https://xxxxx.yy.com/<zoneName>/<relativeDirName>
92
+ * output: <relativeDirName>
93
+ * @param {string} homePage
94
+ * @return {string} appVersion
95
+ */
96
+ export function getAppVersionFromHomePage(homePage) {
97
+ const arr = homePage.split('/');
98
+ return arr[arr.length - 1];
99
+ }
100
+
101
+ /**
102
+ * input : https://xxxxx.yy.com/<zoneName>/<relativeDirName>
103
+ * output: <zoneName>
104
+ * @param {string} homePage
105
+ * @return {string} appVersion
106
+ */
107
+ export function getZoneNameFromHomePage(homePage) {
108
+ const arr = homePage.split('/');
109
+ return arr[arr.length - 2];
110
+ }
111
+
112
+ const cdnType2host = {
113
+ unpkg: 'https://unpkg.com',
114
+ jsdelivr: 'https://cdn.jsdelivr.net/npm',
115
+ };
116
+
117
+ export function getNpmCdnHomePage(packageJson, options) {
118
+ const { npmCdnType = cst.DEFAULT_NPM_CDN_TYPE, distDir = cst.ARK_DIST_DIR, homePage } = options;
119
+ const { name, version } = packageJson;
120
+ // 优先考虑用户透传的 homePage,表示用户部署了 unpkg 私服
121
+ const unpkgHost = homePage || cdnType2host[npmCdnType] || '';
122
+ // TODO,未来考虑更多类型的 cdn,如:jsdelivr
123
+ return `${unpkgHost}/${name}@${version}/${distDir}`;
124
+ }
@@ -1,45 +1,45 @@
1
- export function okeys(obj) {
2
- return Object.keys(obj);
3
- }
4
-
5
- /**
6
- *
7
- * @param value
8
- * @param {{nullValues: any[], isEmptyObjNull: boolean, isEmptyArrNull: boolean}} nullDef - 空定义
9
- */
10
- export function isNull(value, nullDef = {}) {
11
- const { nullValues = [null, undefined, ''], isEmptyObjNull = true, isEmptyArrNull = true } = nullDef;
12
-
13
- const inNullValues = nullValues.includes(value);
14
- if (inNullValues) {
15
- return true;
16
- }
17
-
18
- if (Array.isArray(value)) {
19
- if (isEmptyArrNull) return value.length === 0;
20
- return false;
21
- }
22
-
23
- if (typeof value === 'object') {
24
- if (isEmptyObjNull) return okeys(value).length === 0;
25
- return false;
26
- }
27
-
28
- return false;
29
- }
30
-
31
- /**
32
- *
33
- * @param {{[key:string]: any}} obj
34
- * @param {(value:any, key:string)=>boolean} judgeValueValid - 判断value有效的函数
35
- */
36
- export function purify(obj, judgeValueValid) {
37
- // isValidVal or isNull
38
- const isValid = judgeValueValid || ((value) => !isNull(value));
39
- const pureObj = {};
40
- okeys(obj).forEach((key) => {
41
- const value = obj[key];
42
- if (isValid(value, key)) pureObj[key] = value;
43
- });
44
- return pureObj;
45
- }
1
+ export function okeys(obj) {
2
+ return Object.keys(obj);
3
+ }
4
+
5
+ /**
6
+ *
7
+ * @param value
8
+ * @param {{nullValues: any[], isEmptyObjNull: boolean, isEmptyArrNull: boolean}} nullDef - 空定义
9
+ */
10
+ export function isNull(value, nullDef = {}) {
11
+ const { nullValues = [null, undefined, ''], isEmptyObjNull = true, isEmptyArrNull = true } = nullDef;
12
+
13
+ const inNullValues = nullValues.includes(value);
14
+ if (inNullValues) {
15
+ return true;
16
+ }
17
+
18
+ if (Array.isArray(value)) {
19
+ if (isEmptyArrNull) return value.length === 0;
20
+ return false;
21
+ }
22
+
23
+ if (typeof value === 'object') {
24
+ if (isEmptyObjNull) return okeys(value).length === 0;
25
+ return false;
26
+ }
27
+
28
+ return false;
29
+ }
30
+
31
+ /**
32
+ *
33
+ * @param {{[key:string]: any}} obj
34
+ * @param {(value:any, key:string)=>boolean} judgeValueValid - 判断value有效的函数
35
+ */
36
+ export function purify(obj, judgeValueValid) {
37
+ // isValidVal or isNull
38
+ const isValid = judgeValueValid || ((value) => !isNull(value));
39
+ const pureObj = {};
40
+ okeys(obj).forEach((key) => {
41
+ const value = obj[key];
42
+ if (isValid(value, key)) pureObj[key] = value;
43
+ });
44
+ return pureObj;
45
+ }
@@ -1,75 +1,75 @@
1
- /**
2
- * for getting pretty format multi line string when use \`...\`
3
- * this function will remove indent of every line automatically
4
- * @param {string} mayLineBreakStr
5
- * @param {'MULTI' | 'ONE'} [mode='MULTI']
6
- * @returns
7
- * ```
8
- * // usage 1, process multi lines with mode='MULTI' by default
9
- * pfstr(`
10
- * line1 line1 line1,
11
- * line2 line2 line2.
12
- * `);
13
- * // pass to console.log will print:
14
- * line1 line1 line1,
15
- * line2 line2 line2.
16
- * // attention: end <br/> will be removed automatically in MULTI mode
17
- * pfstr(`
18
- * line1 line1 line1,<br/>
19
- * line2 line2 line2.
20
- * `);
21
- * // pass to console.log will print:
22
- * line1 line1 line1,
23
- * line2 line2 line2.
24
- *
25
- * // usage 2, set mode='ONE' to get no line break string
26
- * pfstr(`
27
- * line1 line1 line1,
28
- * line2 line2 line2.
29
- * `, 'ONE');
30
- * // pass to console.log will print:
31
- * line1 line1 line1, line2 line2 line2.
32
- *
33
- * // usage 3, add <br/> to control line break
34
- * pfstr(`
35
- * line1 line1 line1,
36
- * line2 line2 line2,<br/>
37
- * line3 line3 line3.
38
- * `, 'ONE');
39
- * // pass to console.log will print:
40
- * line1 line1 line1, line2 line2 line2,
41
- * line3 line3 line3.
42
- * ```
43
- */
44
- export function pfstr(/** @type string */ mayLineBreakStr, mode = 'MULTI') {
45
- // MULTI ONE
46
- const lines = mayLineBreakStr.split('\n');
47
- const lastIdx = lines.length - 1;
48
-
49
- const formatLine = lines
50
- .map((line, idx) => {
51
- if (!line && (idx === 0 || idx === lastIdx)) {
52
- return '';
53
- }
54
- const replaceBr = (/** @type string */ line, hasBrStr, noBrStr = '') => {
55
- let result = line;
56
- if (line.endsWith('<br/>')) {
57
- result = line.substring(0, result.length - 5);
58
- return `${result}${hasBrStr}`;
59
- }
60
- return `${result}${noBrStr}`;
61
- };
62
-
63
- // 此处暂时规避可选链写法,因 rollup 对此未处理,编译后上层使用会报错
64
- // SyntaxError: Unexpected token '.'
65
- const { trimStart } = line;
66
- let result = trimStart ? line.trimStart() : line; // 去头部所有空格
67
- if (mode === 'MULTI') {
68
- return `${replaceBr(result, '')}\n`;
69
- }
70
- result = replaceBr(result, '\n', ' ');
71
- return result;
72
- })
73
- .join('');
74
- return formatLine;
75
- }
1
+ /**
2
+ * for getting pretty format multi line string when use \`...\`
3
+ * this function will remove indent of every line automatically
4
+ * @param {string} mayLineBreakStr
5
+ * @param {'MULTI' | 'ONE'} [mode='MULTI']
6
+ * @returns
7
+ * ```
8
+ * // usage 1, process multi lines with mode='MULTI' by default
9
+ * pfstr(`
10
+ * line1 line1 line1,
11
+ * line2 line2 line2.
12
+ * `);
13
+ * // pass to console.log will print:
14
+ * line1 line1 line1,
15
+ * line2 line2 line2.
16
+ * // attention: end <br/> will be removed automatically in MULTI mode
17
+ * pfstr(`
18
+ * line1 line1 line1,<br/>
19
+ * line2 line2 line2.
20
+ * `);
21
+ * // pass to console.log will print:
22
+ * line1 line1 line1,
23
+ * line2 line2 line2.
24
+ *
25
+ * // usage 2, set mode='ONE' to get no line break string
26
+ * pfstr(`
27
+ * line1 line1 line1,
28
+ * line2 line2 line2.
29
+ * `, 'ONE');
30
+ * // pass to console.log will print:
31
+ * line1 line1 line1, line2 line2 line2.
32
+ *
33
+ * // usage 3, add <br/> to control line break
34
+ * pfstr(`
35
+ * line1 line1 line1,
36
+ * line2 line2 line2,<br/>
37
+ * line3 line3 line3.
38
+ * `, 'ONE');
39
+ * // pass to console.log will print:
40
+ * line1 line1 line1, line2 line2 line2,
41
+ * line3 line3 line3.
42
+ * ```
43
+ */
44
+ export function pfstr(/** @type string */ mayLineBreakStr, mode = 'MULTI') {
45
+ // MULTI ONE
46
+ const lines = mayLineBreakStr.split('\n');
47
+ const lastIdx = lines.length - 1;
48
+
49
+ const formatLine = lines
50
+ .map((line, idx) => {
51
+ if (!line && (idx === 0 || idx === lastIdx)) {
52
+ return '';
53
+ }
54
+ const replaceBr = (/** @type string */ line, hasBrStr, noBrStr = '') => {
55
+ let result = line;
56
+ if (line.endsWith('<br/>')) {
57
+ result = line.substring(0, result.length - 5);
58
+ return `${result}${hasBrStr}`;
59
+ }
60
+ return `${result}${noBrStr}`;
61
+ };
62
+
63
+ // 此处暂时规避可选链写法,因 rollup 对此未处理,编译后上层使用会报错
64
+ // SyntaxError: Unexpected token '.'
65
+ const { trimStart } = line;
66
+ let result = trimStart ? line.trimStart() : line; // 去头部所有空格
67
+ if (mode === 'MULTI') {
68
+ return `${replaceBr(result, '')}\n`;
69
+ }
70
+ result = replaceBr(result, '\n', ' ');
71
+ return result;
72
+ })
73
+ .join('');
74
+ return formatLine;
75
+ }