-
Notifications
You must be signed in to change notification settings - Fork 2
/
compile-tiddlywiki-production.js
100 lines (87 loc) · 4.26 KB
/
compile-tiddlywiki-production.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
89
90
91
92
93
94
95
96
97
98
99
100
const fs = require('fs');
const path = require('path');
const crypto = require("crypto");
const cp = require("child_process");
//compiles plugins as JSON header plus plain text body
if (!process.argv[2] || !process.argv[3] || !process.argv[4] || !process.argv[5])
throw "Please specify source, destination, version, and type";
const oldFolder = path.resolve(process.argv[2]);
const newFolder = path.resolve(process.argv[3]);
const pkg = require(path.resolve(oldFolder, "package.json"));
const type = process.argv[5];
if(["client", "server", "both"].indexOf(type) === -1)
throw "the fourth argument must be 'client', 'server', or 'both'";
if (pkg.version !== process.argv[4])
throw "Mismatch with package.json version";
fs.mkdirSync(newFolder);
var $tw = require(oldFolder).TiddlyWiki();
$tw.boot.argv = [oldFolder + '/editions/empty'];
const execute = $tw.boot.executeNextStartupTask;
$tw.boot.executeNextStartupTask = function () {
if (!execute()) complete();
return true;
}
$tw.boot.boot();
function complete() {
const hashes = {};
['plugins', 'themes'].map(folder => path.join(oldFolder, folder))
.reduce((n, folder) => n.concat(fs.readdirSync(folder).filter(sub => !sub.startsWith(".")).map(sub => path.join(folder, sub))), [])
.concat([path.join(oldFolder, 'languages')])
.reduce((n, folder) => n.concat(fs.readdirSync(folder).filter(sub => !sub.startsWith(".")).map(sub => path.join(folder, sub))), [])
.concat([path.join(oldFolder, 'core')])
.forEach(oldPath => {
const plugin = $tw.loadPluginFolder(oldPath)
const relPath = path.normalize(path.relative(oldFolder, oldPath));
console.log(relPath);
const newPath = path.join(newFolder, relPath);
// console.log(newPath);
const splitPath = relPath.split(path.sep);
for (let i = 0; i < splitPath.length; i++) {
const curPath = path.join(newFolder, splitPath.slice(0, i + 1).join(path.sep));
if (!fs.existsSync(curPath)) fs.mkdirSync(curPath);
}
if (plugin) {
plugin.text = JSON.stringify(JSON.parse(plugin.text));
if(type === "server"){
plugin.tiddlers = JSON.parse(plugin.text).tiddlers;
delete plugin.text;
fs.writeFileSync(path.join(newPath, "plugin.info"), JSON.stringify(plugin));
} else if (type === "client"){
let js = Buffer.from(`$tw.preloadTiddler(${JSON.stringify(plugin)});`, "utf8");
let hash = crypto.createHash("sha384").update(js).digest("base64");
fs.writeFileSync(path.join(newPath, "plugin.info.js"), js);
hashes[relPath] = "sha384-" + hash;
}
} else console.log(oldPath);
});
// console.log(hashes);
if(type === "client") {
fs.writeFileSync(path.join(newFolder,"hashes.json"),JSON.stringify(hashes,null,2));
}
// fs.mkdirSync(path.join(newFolder, 'bin'));
// fs.mkdirSync(path.join(newFolder, 'boot'));
// fs.mkdirSync(path.join(newFolder, 'licenses'));
// const files = fs.readdirSync(path.join(oldFolder, 'boot')).map(e => 'boot/' + e)
// .concat(fs.readdirSync(path.join(oldFolder, 'licenses')).map(e => 'licenses/' + e))
// .concat(['tiddlywiki.js', 'license']);
// add package.json with a few changes
/** @type {import("./TiddlyWiki5-Jermolene/package.json")} */
let pkg = JSON.parse(fs.readFileSync(path.join(oldFolder, "package.json"), "utf8"));
pkg.name += "-production-" + type;
pkg.preferGlobal = false;
fs.writeFileSync(path.join(newFolder, "package.json"), JSON.stringify(pkg, null, 2));
// console.log("The 'bin' and 'editions' folders are not required " +
// "for TiddlyWiki to work correctly as a module. They are both included in a zip file.");
// let rel = path.relative(path.relative(__dirname, process.argv[2]), newFolder);
// let cmd = `(cd ${process.argv[2]} && zip -r ${rel}/editions.zip editions bin)`;
// console.log("\nRunning %s", cmd);
// let res = cp.spawnSync(cmd, { shell: true });
// let output = Buffer.concat(res.output.filter(e => e)).toString("utf8");
// let outputArr = output.split("\n").map(e => e.trimLeft().startsWith("adding") ? true : e);
// let count = outputArr.filter(e => e === true).length;
// let other = outputArr.filter(e => e !== true).join('\n');
// console.log("Zip finished with status %s and signal %s", res.status, res.signal);
// console.log(res.stderr.toString("utf8"));
// console.log("Added %s files, and printed the following lines as well", count);
// console.log(other);
}