This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
build.js
executable file
·118 lines (95 loc) · 3.53 KB
/
build.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
var hostname = require('os').hostname();
var panda = require("panda-docs");
var Smasher = require('asset-smasher').Smasher;
var argv = process.argv.splice(2);
var fs = require("fs");
var development = argv[0];
var sm = new Smasher({
paths:['./assets'],
only:['styles.css.mf', 'javascripts.js.mf'],
compress: true,
outputTo: './out/assets/min'
});
var buildOptions = {
title: "Cloud9 User Documentation",
assets: "./assets",
development: development,
resources: "./resources",
baseUrl: "https://docs.c9.io",
json: true,
tocId: "doc-nav",
contentId: "content",
safeWords: ["GitHub", "Cloud9"]
};
panda.make([
"./src/"
], buildOptions, function(err, cbReturn) {
if (err) console.error(err);
// for docs.c9.io, rely on the written filename, since
// sm force updates all files, thus deleting any mtime info
if (/\w+-\d+-\d+-\d+-\d+/.test(hostname)) {
var recentFiles = JSON.parse(fs.readFileSync("recentFiles.json"));
}
else {
// in case the index was updated recently, remove it
var recentFiles = cbReturn["files"].sort(function(a,b) { return b.mtime - a.mtime } ).splice(0, 6).filter(function (file) {
return file.filename !== "index";
});
// in case the index WASN'T updated, drop the last item
if (recentFiles.length > 5)
recentFiles.pop();
recentFiles = recentFiles.map(function (recentFile) {
delete recentFile.contents;
return recentFile;
});
fs.writeFileSync("recentFiles.json", JSON.stringify(recentFiles));
}
addRecentChanges(recentFiles);
compileAssets();
fixJson(recentFiles);
});
function addRecentChanges(recentFiles) {
fs.readFile(__dirname + "/out/index.html", "utf8", function(err, contents) {
if (err) console.error(err);
contents = replaceRecentFiles(recentFiles, contents);
fs.writeFile(__dirname + "/out/index.html", contents, "utf8", function(err, contents) {
if (err) console.error(err);
});
});
}
function compileAssets() {
if (!development) {
console.log("Compiling assets...");
sm.compileAssets(function(err) {
if(err) {
console.error("An asset compilation error occurred", err);
} else {
console.log("Compilation done!");
}
return;
});
}
}
function fixJson(recentFiles) {
fs.readFile(__dirname + "/out/cloud9-user-documentation.json", "utf8", function(err, contents) {
if (err) console.error(err);
var docsJson = JSON.parse(contents);
docsJson["files"] = docsJson["files"].map(function(file) {
file.contents = file.contents.replace("\n <div id=\"disqus_thread\"></div>\n ", "").replace("\n <div id=\"toh_btn\" onclick=\"toggleTOH(this)\"></div>\n ", "");
if (file.filename === "index")
file.contents = replaceRecentFiles(recentFiles, contents);
return file;
});
docsJson.toc = docsJson.toc.replace(new RegExp('<i class=\"icon-chevron-down tocNavArrow\"></i><i class=\"icon-chevron-up tocNavArrow\"></i>', "g"), "");
fs.writeFile(__dirname + "/out/cloud9-user-documentation.json", JSON.stringify(docsJson, null, " "), "utf8", function(err, contents) {
if (err) console.error(err);
});
});
}
function replaceRecentFiles(recentFiles, contents) {
recentFiles.forEach(function (recentFile, i) {
contents = contents.replace("%placeholder" + (i+1) + "%", "<a href='" + recentFile.filename + ".html'>" + recentFile.pageTitle + "</a>");
});
return contents;
}