forked from hebcal/hebcal-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
82 lines (75 loc) · 2.64 KB
/
compile.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
var browserify = require('browserify'),
path = require('path'),
fs = require('fs'),
exorcist = require('exorcist'),
uglifyjs = require('uglify-js'),
marked = require('marked'),
version = require('./package.json').version;
var github = 'https://github.com/hebcal/hebcal-js/tree/v'+version+'/';
var minHeader = "/*\n Hebcal - A Jewish Calendar Generator\n\n https://github.com/hebcal/hebcal-js\n\n Licensed GPLv3\n */\n"
// the commands
normal();
noloc();
readme();
// all the functions get hoisted
function compile(inFile, outFile, sourceMap, cb) {
console.log('Compiling ' + inFile + ' to ' + outFile);
var outStream = fs.createWriteStream(outFile, 'utf8');
outStream.on('close', cb);
browserify({debug: true})
.require(inFile, { entry: true })
.bundle()
.pipe(exorcist(sourceMap))
.pipe(outStream);
}
function minify(inFile, outFile, inSourceMap, outSourceMap) {
console.log('Minifying ' + inFile + ' to ' + outFile);
var result = uglifyjs.minify(inFile, {
inSourceMap: inSourceMap,
outSourceMap: outSourceMap,
sourceRoot: github,
mangle: true,
warnings: true
});
fs.writeFileSync(outFile, minHeader + removeLocalPath(result.code));
fs.writeFileSync(outSourceMap, removeLocalPath(result.map));
function removeLocalPath(input) {
return input.replace(new RegExp(__dirname, 'g'), github)
}
}
function normal() {
var sourceFile = 'src/client.js';
var fullOutput = 'client/hebcal.js';
var fullMap = 'client/hebcal.js.map';
var minOutput = 'client/hebcal.min.js';
var minMap = 'client/hebcal.min.js.map';
compile(sourceFile, fullOutput, fullMap, function () {
minify(fullOutput, minOutput, fullMap, minMap);
});
}
function noloc() {
var sourceFile = 'src/noloc.js';
var fullOutput = 'client/hebcal.noloc.js';
var fullMap = 'client/hebcal.noloc.js.map';
var minOutput = 'client/hebcal.noloc.min.js';
var minMap = 'client/hebcal.noloc.min.js.map';
compile(sourceFile, fullOutput, fullMap, function () {
minify(fullOutput, minOutput, fullMap, minMap);
});
}
function readme() {
fs.readFile('README.md', function(err, md){
if (err) {
console.error('Could not open README.md');
return;
}
var output = marked(md.toString(), {gfm: true, breaks: true});
fs.writeFile('README.html', output, function(err){
if (err) {
console.error('Could not write README.html');
} else {
console.log('Created README.html');
}
});
});
}