-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpubTool.js
executable file
·88 lines (79 loc) · 2.66 KB
/
pubTool.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
#!/usr/bin/env node
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const { uploadOSS, uploadRelay, uploadPosts } = require("./aliyunOSS.js");
const { genTagslist } = require("./genTagslist.js");
const { blogRootPath, sourcePath, sourceDocPath, relaySourcePath } = require("./config.js");
const argv = process.argv.slice(2);
// 第一个参数是命令, 后面跟命令的参数
// 只实现 commandName arg1 arg2 arg3 ... 的形式
const [commandName, ...args] = argv;
// save to repository
const cSaveToRepo = () => {
console.log("----------------------------------------");
console.log("正在保存文章至远端仓库...");
try {
const add = "git add .",
commit = `git commit -m "update posts..."`,
push = "git push";
console.log(add);
execSync(add, { cwd: sourcePath });
console.log(commit);
execSync(commit, { cwd: sourcePath });
console.log(push);
execSync(push, { cwd: sourcePath });
} catch (e) {
console.log("推送失败!" + e.stdout);
}
};
// 把博文上传至 OSS
const cPublish = () => {
const push = () =>
// 生成 tags
genTagslist(sourceDocPath).then(tagsList => {
fs.writeFile(
path.format({
dir: sourceDocPath,
base: "/postList.json",
}),
JSON.stringify(tagsList, null, " "),
"utf8",
err => {
if (err) throw err;
console.log("postList created success...");
console.log("----------------------------------------");
console.log("正在推送文章...");
try {
// 上传静态资源
uploadPosts(sourceDocPath);
} catch (e) {
console.log("推送失败!" + e.stdout);
}
}
);
});
push();
};
const commandList = {
s: cSaveToRepo,
p: cPublish,
uo: () => uploadOSS(blogRootPath),
sp: () => (cSaveToRepo(), cPublish()),
relay: () => uploadRelay(relaySourcePath),
};
console.log(`
----------------------------------------
Ksana 博客自用工具
----------------------------------------
s := 保存文档至仓库
p := 发布博文
sp := 保存文档并发布博文
uo := 博客静态资源更新至 OSS
relay := 更新 Relay 静态资源到 OSS
`);
// 执行命令
if (!!commandName) {
const command = commandList[commandName];
command && command(args);
}