-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathfile.js
More file actions
44 lines (39 loc) · 930 Bytes
/
file.js
File metadata and controls
44 lines (39 loc) · 930 Bytes
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
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const log = require('./log');
const ejsCompile = (templatePath, data={}, options = {}) => {
return new Promise((resolve, reject) => {
ejs.renderFile(templatePath, {data}, options, (err, str) => {
if (err) {
reject(err);
return;
}
resolve(str);
})
})
}
const writeFile = (path, content) => {
if (fs.existsSync(path)) {
log.error("the file already exists~")
return;
}
return fs.promises.writeFile(path, content);
}
const mkdirSync = (dirname) => {
if (fs.existsSync(dirname)) {
return true
} else {
// 不存在,判断父亲文件夹是否存在?
if (mkdirSync(path.dirname(dirname))) {
// 存在父亲文件夹,就直接新建该文件夹
fs.mkdirSync(dirname)
return true
}
}
}
module.exports = {
ejsCompile,
writeFile,
mkdirSync
}