This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
forked from jumbojett/c9.ide.theme.jett
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-theme.js
executable file
·404 lines (329 loc) · 12 KB
/
build-theme.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"use strict";
main.consumes = ["Plugin", "connect.static"];
main.provides = [];
module.exports = main;
/**
* Server-side plugin for Jett theme. Mounts url routes and builds theme CSS.
* @method main
* @param {} options
* @param {} imports
* @param {} register
* @return
*/
function main(options, imports, register) {
var Plugin = imports.Plugin;
var connectStatic = imports["connect.static"];
var os = require("os");
var fs = require("fs");
var path = require("path");
var error = require("http-error");
var atomic = require("c9/atomic");
var themeName = "jett";
var themeCSSPath = __dirname + '/css/compile_' + themeName + '.css';
options.version = 'standalone';
options.cache = path.normalize(path.join(options.pathRoot + '/../build'));
options.baseUrl = '';
options.virtual = undefined;
options.config = 'standalone';
/**
* Wraps architect build
* @method build
* @return
*/
var build = function() {
// delay loading of architect build for faster startup
// todo should we disable this plugin on local instead?
build = require(options.pathRoot + "/../node_modules/architect-build/build");
build.apply(null, arguments);
};
var cache;
/***** Initialization *****/
var compress = options.compress || true;
var obfuscate = options.obfuscate || false;
var keepLess = options.keepLess || false;
var config = options.config || "ide";
var settings = options.settings || "standalone";
var cacheDir = path.resolve(options.cache || os.tmpdir() + "/cdn");
var staticsConfig;
/**
* *** Register and define API ****
* @method init
* @return
*/
function init() {
/* Add our static content */
connectStatic.addStatics([{
path: __dirname + "/fonts",
mount: "/fonts"
}]);
getPathConfig('standalone', function(err, pathConfig) {
if (err) {
console.error(err, err.stack);
process.exit(1);
}
/* If our custom ACE themes aren't installed then install them */
var themePath = path.join(pathConfig.root, "plugins/c9.ide.ace/themes.json");
fs.readFile(themePath, "utf8", function(err, content) {
if (err) {
console.error(err, err.stack);
process.exit(1);
}
var themes = JSON.parse(content);
var save = false;
[themeName].every(function(name) {
var themeName = capitalizeFirstLetter(name);
if (!themes.hasOwnProperty(themeName)) {
themes[themeName] = "ace/theme/" + name;
// Copy our theme to the directory
["js", "css"].every(function(ext) {
fs.createReadStream(__dirname + '/ace.themes/' + name + '.' + ext)
.pipe(fs.createWriteStream(__dirname + "/../../node_modules/ace/lib/ace/theme/" + name + "." + ext));
return true;
});
save = true;
}
return true;
});
// If we had to insert our own config then save the new config
if (save) {
atomic.writeFile(themePath, JSON.stringify(themes, null, 4), "utf8", function(err) {
if (err)
return console.error("Updating themes ", themePath, "failed", err);
var mtime = Math.floor(Date.now() / 1000) * 1000;
// set utime to have consistent etag
fs.utimes(themePath, mtime / 1000, mtime / 1000, function(e) {
if (e) console.error(e);
});
});
}
});
if (!fileExists(themeCSSPath))
buildSkin('default', 'compile_' + themeName, pathConfig, function(err, result) {
if (err) console.log(err);
//callback(err, result && result.code || "");
var mtime = Math.floor(Date.now() / 1000) * 1000;
atomic.writeFile(themeCSSPath, result.code, "utf8", function(err) {
if (err)
return console.error("Caching file", themeCSSPath, "failed", err);
console.log("File cached at", themeCSSPath);
// set utime to have consistent etag
fs.utimes(themeCSSPath, mtime / 1000, mtime / 1000, function(e) {
if (e) console.error(e);
});
});
});
});
}
/**
* Helper
* @method capitalizeFirstLetter
* @param {} string
* @return BinaryExpression
*/
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Helper
* @method fileExists
* @param {} filePath
* @return
*/
function fileExists(filePath) {
try {
return fs.statSync(filePath).isFile();
}
catch (err) {
return false;
}
}
/**
* Modified function present in C9's build.js
* @method readConfig
* @param {} config
* @return ObjectExpression
*/
function readConfig(config) {
if (config == "full") {
var plugins = [];
["default-local", "ssh", "default"].forEach(function(n) {
plugins.push.apply(plugins, readConfig(n).config);
});
return {
config: plugins
};
}
if (config[0] != "/")
config = path.join(options.pathRoot, "client-" + config);
if (config.slice(-3) !== ".js")
config += ".js";
var settings;
try {
settings = require(options.pathRoot + "/../settings/standalone");
config = require(config);
}
catch (e) {
if (e.code == "MODULE_NOT_FOUND")
e = new error.NotFound();
return {
error: e
};
}
settings = settings();
settings.packaging = true;
return {
config: config(settings)
};
}
/**
* Modified function present in C9's build.js
* @method buildSkin
* @param {} config
* @param {} color
* @param {} pathConfig
* @param {} callback
* @return
*/
function buildSkin(config, color, pathConfig, callback) {
var data = readConfig(config);
if (data.error)
return callback(data.error);
var plugins = data.config.concat([
"lib/architect/architect"
]);
var lessLibs = [];
fs.readFile(path.join(pathConfig.root, "plugins/c9.ide.layout.classic/less/lesshat.less"), "utf8", function(err, lesshat) {
if (err) return callback(err);
lessLibs.push(lesshat);
// 372 kb avgerage
fs.readFile(path.join(__dirname, "less/variables.less"), "utf8", function(err, theme) {
if (err) return callback(err);
lessLibs.push(theme);
lessLibs.staticPrefix = "plugins/c9.ide.theme." + themeName;
var themeCss = [{
id: "text!" + path.join(__dirname, "less/overrides.less"),
parent: {}
}];
build(plugins, {
cache: cache,
pathConfig: pathConfig,
enableBrowser: true,
includeConfig: false,
noArchitect: true,
compress: compress,
filter: [],
ignore: [],
withRequire: false,
compileLess: true,
lessLibs: lessLibs,
lessLibCacheKey: color,
basepath: pathConfig.root,
additional: themeCss
}, callback);
});
});
}
/**
* Modified function present in C9's build.js
* @method getStaticsConfig
* @param {} callback
* @return
*/
function getStaticsConfig(callback) {
if (staticsConfig)
return callback(null, staticsConfig);
tryGetConfig(null, connectStatic);
if (staticsConfig)
return callback(null, staticsConfig);
var dir = path.join(cacheDir, options.version);
console.log("Linking static files to ", dir, settings);
require("../../scripts/makestatic.js")(config, settings, {
dest: dir + "/static",
symlink: false,
compress: options.compress,
getMounts: !options.link,
saveRjsConfig: false,
}, function(err, connectStatic) {
tryGetConfig(err, connectStatic);
return callback(err, staticsConfig);
});
/**
* Modified function present in C9's build.js
* @method tryGetConfig
* @param {} err
* @param {} connectStatic
* @return
*/
function tryGetConfig(err, connectStatic) {
if (err) {
console.error(err, err.stack);
process.exit(1);
}
if (!connectStatic || options.link)
return;
var mounts = connectStatic.getMounts();
var rjsConfig = connectStatic.getRequireJsConfig();
if (!mounts || !mounts[0] || !mounts[0].mount)
return;
var pathMap = Object.create(null);
mounts.forEach(function(mount) {
pathMap[mount.mount] = mount.path;
});
staticsConfig = {
pathMap: pathMap,
rjsConfig: JSON.parse(JSON.stringify(rjsConfig))
};
}
}
/**
* Modified function present in C9's build.js
* @method getPathConfig
* @param {} hash
* @param {} callback
* @return
*/
function getPathConfig(hash, callback) {
if (!options.link) {
getStaticsConfig(function(err, config) {
if (err) return callback(err);
var pathMap = config.pathMap;
var pathConfig = config.rjsConfig;
pathConfig.hash = hash;
pathConfig.root = path.normalize(path.join(options.pathRoot + '/../'));
var baseUrl = pathConfig.baseUrl || "";
for (var p in pathConfig.paths) {
var url = pathConfig.paths[p];
if (typeof url === "string" && url.substring(0, baseUrl.length) == baseUrl)
pathConfig.paths[p] = url.substring(baseUrl.length);
}
pathConfig.pathMap = pathMap;
callback(null, pathConfig);
});
}
else {
var root = path.resolve(path.join(cacheDir, hash));
var rjsConfigPath = path.join(root, "/static/requirejs-config.json");
fs.readFile(rjsConfigPath, "utf8", function(err, pathConfig) {
if (err) {
if (err.code == "ENOENT")
return callback(new error.NotFound());
else
return callback(err);
}
try {
pathConfig = JSON.parse(pathConfig);
}
catch (e) {
return callback(e);
}
pathConfig.root = path.join(root, pathConfig.baseUrl);
for (var p in pathConfig.paths) {
pathConfig.paths[p] = path.join(root, pathConfig.paths[p]);
}
callback(null, pathConfig);
});
}
}
init();
register(null, {});
};