-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
modifyTemplate.ts
63 lines (61 loc) · 1.42 KB
/
modifyTemplate.ts
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
import MetalSmith from 'metalsmith';
import { ejs } from 'consolidate';
import { promisify } from 'util';
import * as path from 'path';
/**
*
* @param {*} fromPath 源路径
* @param {*} toPath 目标路径
*/
async function handleTemplate(fromPath: string, toPath: string, config: any) {
await new Promise((resovle, reject) => {
MetalSmith(__dirname)
.source(fromPath)
.destination(path.join(path.resolve(), toPath))
.use(async (_files, metal, done) => {
let result = {
license: 'MIT',
version: '0.0.1',
...config,
};
const data = metal.metadata();
Object.assign(data, result);
//@ts-ignore
done();
})
.use((files, metal, done) => {
Object.keys(files).forEach(async (file) => {
let content = files[file].contents.toString();
if (
file.includes('.js') ||
file.includes('.json') ||
file.includes('.txt') ||
file.includes('.md')
) {
if (content.includes('<%')) {
let { render } = ejs;
//@ts-ignore
render = promisify(render);
try {
content = await render(content, metal.metadata());
} catch (e) {
console.log(e);
}
files[file].contents = Buffer.from(content);
}
}
});
//@ts-ignore
done();
})
.build((err) => {
if (!err) {
resovle(true);
} else {
console.log(err);
reject(false);
}
});
});
}
export default handleTemplate;