-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (80 loc) · 2.12 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { readFileSync, readFile } from 'fs';
import { join } from 'path';
import { cpus } from 'os';
import fg from 'fast-glob';
import pMap from 'p-map';
import arrify from 'arrify';
/**
* await fastGitignore({topic, templatesDir, custom});
*
* @param {String | Array} topic - .gitignore 主题集合
* @param {Array} templatesDir - 模板库磁盘位置
* @param {String | Array} custom - 自定义规则
*/
export async function fastGitignore({
topic = [],
templatesDir = '.',
custom = undefined,
}) {
const GLOB = `+(${arrify(topic).join('|')})`;
const TPL_PATHS = await fg([join(templatesDir, `${GLOB}.gitignore`)]);
const gotGitignore = await pMap(
TPL_PATHS,
(filePath) =>
new Promise((resolve, reject) => {
readFile(filePath, 'utf-8', (err, data) => {
if (err) {
reject(err);
return;
}
resolve({ [filePath]: data });
});
}),
{ concurrency: cpus().length },
);
const PAYLOAD = gotGitignore.reduce((acc, cur) => {
Object.assign(acc, cur);
return acc;
}, {});
if (custom) {
Object.assign(
PAYLOAD,
arrify(custom).reduce((acc, cur, idx) => {
acc[`custom-${idx}`] = cur;
return acc;
}, {}),
);
}
// 缓存文件路径:意义不大,本来就是本地的
// 缓存文件内容:
return PAYLOAD;
}
/**
* fastGitignoreSync({topic, templatesDir, custom});
*
* @param {String | Array} topic - .gitignore 主题集合
* @param {Array} templatesDir - 模板库磁盘位置
* @param {String | Array} custom - 自定义规则,选填
*/
export function fastGitignoreSync({
topic = [],
templatesDir = '.',
custom = undefined,
}) {
const GLOB = `+(${arrify(topic).join('|')})`;
const TPL_PATHS = fg.sync([join(templatesDir, `${GLOB}.gitignore`)]);
const PAYLOAD = TPL_PATHS.reduce((acc, cur) => {
acc[cur] = readFileSync(cur, 'utf8');
return acc;
}, {});
if (custom) {
Object.assign(
PAYLOAD,
arrify(custom).reduce((acc, cur, idx) => {
acc[`custom-${idx}`] = cur;
return acc;
}, {}),
);
}
return PAYLOAD;
}