-
Notifications
You must be signed in to change notification settings - Fork 602
/
Jakefile.js
175 lines (147 loc) · 5.26 KB
/
Jakefile.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
var fs = require("fs");
var indexFileName = "index.html";
var srcDir = "src/";
var publishDir = "dist/";
var scriptFilename = "script.js";
var scriptDir = "js/";
var regexScriptSection = /<!-- JS:LIB:BEGIN -->([\s\S]*?)<!-- JS:LIB:END -->/;
var excludeFiles = [ ".gitignore", ".git", "dist", "test", ".settings", "build",
".project", "README.md", "*psd", "*.psd", "*libs" ];
var indexFile = fs.readFileSync(srcDir + indexFileName, "utf8");
// find the scripts in index.html
function extractScriptNames() {
console.log("Extracting script file names from index.html");
var regexScriptName = /<script src="(.*?)"><\/script>/g;
var scriptSection = regexScriptSection.exec(indexFile)[1];
// extract script names
var names = [];
var match;
while ((match = regexScriptName.exec(scriptSection)) != null) {
var script = match[1];
names.push(script);
}
return names;
}
// run all scripts through uglifyJS and write to new dest
function minifyScripts(scriptNames) {
console.log("Minifying and concatting scripts.");
var UglifyJS = require("uglify-js");
var regexMinifed = /min.js$/;
var regexCopyright = /^\/\*![\s\S]*?\*\//m;
var buffer = [];
scriptNames.forEach(function(script) {
var scriptFile = fs.readFileSync(srcDir + script, "utf8");
var copyright = regexCopyright.exec(scriptFile);
if (copyright) {
buffer.push(copyright);
}
// check if file is already minified
if (!regexMinifed.test(script)) {
var result = UglifyJS.minify(scriptFile);
if (result.error) {
throw new Error(error);
}
scriptFile = result.code;
} else {
console.log("> Skipping: " + script + " is already minified.");
}
buffer.push(scriptFile + ";");
});
var combined = buffer.join("\n");
fs.writeFileSync(publishDir + scriptDir + scriptFilename, combined);
console.log("Combined all scripts into " + scriptFilename);
}
desc("Build project");
task("build", function() {
// Clean old build directory
if (fs.existsSync(publishDir)) {
console.log("Deleting old dist directory");
jake.rmRf(publishDir);
}
// create new publish dir
console.log("Creating new dist directory");
jake.mkdirP(publishDir + scriptDir);
// minify js
var scriptNames = extractScriptNames();
minifyScripts(scriptNames);
// insert minified js into index.html
console.log("Replacing script files with minified version in index.html");
indexFile = indexFile.replace(regexScriptSection, '<script src="js/'
+ scriptFilename + '"></script>');
// remove debug statements
console.log("Removing IF DEBUG statements in index.html");
var regexDebug = /<!-- DEBUG -->[\s\S]*?<!-- \/DEBUG -->/gmi;
indexFile = indexFile.replace(regexDebug, "");
// insert production statements
var regexProduction = /<!-- PRODUCTION([\s\S]*?)\/PRODUCTION -->/gmi;
indexFile = indexFile.replace(regexProduction, "$1");
// copy index file
console.log("Copying index.html to /dist");
fs.writeFileSync(publishDir + indexFileName, indexFile);
// copy other files
console.log("Copying all other files into /dist");
function createExludeRegex() {
// exclude files that get optimization treatment
excludeFiles.push(indexFileName);
excludeFiles.push.apply(excludeFiles, scriptNames);
// convert wildcard notation to proper regex
// *foo.jpg becomes ^.*foo\.jpg$
excludeFiles = excludeFiles.map(function(file) {
file = file.replace(/\./g, "\\.").replace("*", ".*", "g");
file = "^" + file + "$";
return file;
});
return new RegExp(excludeFiles.join("|"));
}
var regexExcludeFiles = createExludeRegex();
copyFiles("");
/**
* Recursively copies all files that dont match the exclude filter from the
* base directory to the publish directory.
*/
function copyFiles(dir) {
var files = fs.readdirSync(srcDir + dir);
files.forEach(function(file) {
var currentDir = dir + file;
if (!regexExcludeFiles.test(currentDir)) {
var stats = fs.statSync(srcDir + currentDir);
if (stats.isDirectory()) {
if (!fs.existsSync(publishDir + currentDir)) {
fs.mkdirSync(publishDir + currentDir);
}
copyFiles(currentDir + "/");
} else if (stats.isFile()) {
var contents = fs.readFileSync(srcDir + currentDir);
fs.writeFileSync(publishDir + currentDir, contents);
}
}
});
}
// update manifest, put new timestamp
var fileDir = publishDir + "cache.appcache";
var contents = fs.readFileSync(fileDir, "utf8");
contents = contents.replace("{{timestamp}}", Date.now());
fs.writeFileSync(fileDir, contents);
console.log("Project built.");
});
desc("default");
task("default", [ "build" ], function() {
// build on default
});
desc("Generate JSDoc");
task("generate-docs", function() {
console.log("Creating project documentation");
var exec = require('child_process').exec;
var command = "docs/generate.sh";
exec(command, function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
console.log("STDOUT:\n" + stdout);
console.log("Created documentation");
}
if (stderr) {
console.log("STDERR: " + stderr);
}
});
});