-
Notifications
You must be signed in to change notification settings - Fork 6
/
scaffold.js
63 lines (55 loc) · 1.54 KB
/
scaffold.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
const fs = require("hexo-fs");
const path = require("path");
let hexo = null;
let SCAFFOLD_DIR = null;
function setup(hexoInstance) {
hexo = hexoInstance;
SCAFFOLD_DIR = hexo.scaffold_dir;
}
function getAllScaffoldNames() {
let allScaffolds = [];
const files = fs.readdirSync(SCAFFOLD_DIR, { withFileTypes: true });
files.forEach((file) => {
if (file.isFile()) {
allScaffolds.push(path.basename(file.name, ".md"));
}
});
return allScaffolds;
}
function update(name, content) {
return fs.writeFileSync(path.join(SCAFFOLD_DIR, name + ".md"), content);
}
function getAllScaffoldData() {
const allScaffoldNames = getAllScaffoldNames();
let allScaffolds = [];
allScaffoldNames.forEach((scaffoldName) => {
let fileName = path.join(SCAFFOLD_DIR, scaffoldName + ".md");
allScaffolds.push({
name: scaffoldName,
fileName: fileName,
content: fs.readFileSync(fileName),
});
});
return allScaffolds;
}
function rename(oldName, newName) {
const oldPath = path.join(SCAFFOLD_DIR, oldName + ".md");
const newPath = path.join(SCAFFOLD_DIR, newName + ".md");
return fs.renameSync(oldPath, newPath);
}
function create(name) {
return update(name, "---\ntitle: {{ title }}\ntags:\n---");
}
function deleteScaffold(name) {
const scaffoldPath = path.join(SCAFFOLD_DIR, name + ".md");
return fs.unlinkSync(scaffoldPath);
}
module.exports = {
setup: setup,
getNames: getAllScaffoldNames,
getAllData: getAllScaffoldData,
update: update,
rename: rename,
create: create,
delete: deleteScaffold,
};