-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
79 lines (70 loc) · 1.93 KB
/
utils.js
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
import { fileURLToPath } from 'node:url';
import { basename, dirname, join, resolve } from 'node:path';
import { readdirSync } from 'node:fs';
export function fnFilename(metaUrl) {
return fileURLToPath(metaUrl);
}
export function fnPureFilename(metaUrl) {
return basename(fnFilename(metaUrl)).split('.')[0];
}
export function fnDirname(metaUrl) {
const filename = fileURLToPath(metaUrl);
return dirname(filename);
}
export function fnCliDir() {
return join(fnDirname(import.meta.url));
}
export function fnAppDir(workdir) {
return workdir ? resolve(process.cwd(), workdir) : process.cwd();
}
// 获取file协议的路径
export function fnFileProtocolPath(_path) {
if (_path.startsWith('file:')) {
return _path;
} else {
return 'file:///' + _path.replace(/\\+/gi, '/');
}
}
/**
* 可控导入
* @param {String} path 导入路径
* @param {Any} default 默认值
*/
export async function fnImport(path, name, defaultValue) {
try {
const data = await import(path);
return data;
} catch (err) {
console.log('🚀 ~ fnImport ~ err:', err);
return {
[name]: defaultValue
};
}
}
/**
* 获取所有环境变量.env文件的文件名组成的数组
* @returns array 环境变量数组
*/
export function fnGetEnvNames(promptParams, appDir) {
const files = readdirSync(resolve(appDir, 'src', 'env'));
const envFiles = files
.filter((file) => {
return /\.env\.[\da-z]+/.test(file);
})
.map((file) => {
return file.replace('.env.', '');
});
return envFiles;
}
// 排除掉无用的属性
export function fnOmit(obj, exclude = []) {
const obj2 = {};
for (let prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
if (exclude.includes(prop) === false) {
obj2[prop] = obj[prop];
}
}
}
return obj2;
}