-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (64 loc) · 1.78 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
const fs = require("fs");
const http = require("http");
const execSync = require("child_process").execSync;
const path = require("path");
const watchFileName = "TiddlyWiki.html";
const watchDir = path.resolve(process.env.HOME, "Downloads");
const watchFilepath = path.resolve(watchDir, watchFileName);
const root = path.dirname(__filename);
const serverPort = 8848;
const rootWikiPath = path.resolve(root, watchFileName);
const commitScriptPath = path.resolve(root, "script", "commit.sh");
const config = {
watchFileName,
watchDir,
watchFilepath,
root,
serverPort,
rootWikiPath,
commitScriptPath,
};
console.log(`current config \n ${JSON.stringify(config, null, "\t")}\n`);
http
.createServer(function (req, res) {
if (!execSync("git status", {
cwd: root
}).toString().includes("Your branch is up to date with")) {
execSync(`git pull`, {
cwd: root
})
execSync(`git push`, {
cwd: root
})
}
if (req.url !== `/${watchFileName}`) {
res.writeHead(302, {
Location: `http://127.0.0.1:${serverPort}/${watchFileName}`,
});
res.end();
return;
}
fs.readFile(rootWikiPath, function (_, data) {
res.writeHead(200, {
"Content-Type": "text/html",
"Content-Length": data.length,
});
res.write(data);
res.end();
});
})
.listen(serverPort);
console.log(`wiki start at http://127.0.0.1:${serverPort}/${watchFileName}`);
fs.watch(watchDir, function (_, filename) {
if (filename !== watchFileName) {
return;
}
const exist = fs.existsSync(watchFilepath);
if (!exist) {
return;
}
console.log("copy file");
fs.renameSync(watchFilepath, rootWikiPath);
execSync(`/bin/sh ${commitScriptPath}`, () => { });
});
console.log(`wiki watch ${watchDir} now`);