-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJakefile.js
242 lines (206 loc) · 6.59 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
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
// @author Kanstantsin Kamkou <2ka.by>
// MIT license
/* eslint-env node */
/* global desc, task, complete */
'use strict';
var uglify = require('uglify-js-harmony'),
fs = require('fs'),
path = require('path'),
wrench = require('wrench'),
_ = require('lodash'),
less = require('less'),
csso = require('csso'),
os = require('os');
// constants
var CONSTANTS = {
DIR_APP: path.join(__dirname, 'src', 'app'),
DIR_SRC: path.join(__dirname, 'src'),
DIR_VENDORS: path.join(__dirname, 'src', 'vendors'),
DIR_BUILD: path.join(__dirname, 'build'),
DIR_BUILD_APP: path.join(__dirname, 'build', 'app')
};
// internal functions
function manifestRead() {
return require(path.join(CONSTANTS.DIR_BUILD, 'manifest.json'));
}
function manifestUpdate(data) {
console.log('- updating the "manifest.json" file');
return fs.writeFileSync(
path.join(CONSTANTS.DIR_BUILD, 'manifest.json'),
JSON.stringify(data)
);
}
function minify(fileSet) {
let output = '';
fileSet.forEach(f => {
output += ~f.indexOf('/vendors/')
? fs.readFileSync(f)
: uglify.minify(f, {mangle: false}).code;
output += os.EOL;
});
return output;
}
function scripts() {
var templatePath = path.join(CONSTANTS.DIR_BUILD, 'views', 'default.html'),
body = fs.readFileSync(templatePath, {encoding: 'utf-8'}),
regex = new RegExp('<script.*src="(.+?)"><\/script>', 'g'),
fileSet = [],
match;
while ((match = regex.exec(body)) !== null) {
fileSet.push(path.join(CONSTANTS.DIR_SRC, match[1]));
}
return fileSet;
}
// default
desc('Default build action');
task('default', ['cleanup-pre', 'layout-modify', 'cleanup-post'], function () {
console.log('Done. Version:', process.env.version);
});
// pack-app
desc('Application scripts packing');
task('pack-app', ['copy-sources'], {async: true}, function () {
const files = [];
scripts().forEach(path => {
if (!~path.indexOf('vendors/')) {
files.push(path);
}
});
fs.writeFile(
path.join(CONSTANTS.DIR_BUILD_APP, 'app.js'),
uglify.minify(files, {mangle: false}).code,
function () {
console.log('- Packed to "app.js":', os.EOL, files);
complete();
}
);
});
// pack-vendors
desc('Vendors scripts packing');
task('pack-vendors', ['copy-sources'], {async: true}, function () {
const fileSet = [];
scripts().forEach(path => {
if (~path.indexOf('vendors/') && !~path.indexOf('less/')) {
fileSet.push(path);
}
});
fs.writeFile(
path.join(CONSTANTS.DIR_BUILD_APP, 'vendors.js'), minify(fileSet), () => {
console.log('- Packed to "vendors.js":', os.EOL, fileSet);
complete();
}
);
});
// pack-css
desc('Application styles packing');
task('pack-css', ['copy-sources'], {async: true}, function () {
less.render(
fs.readFileSync(path.join(CONSTANTS.DIR_APP, 'styles.less'), {encoding: 'utf-8'}),
{paths: [CONSTANTS.DIR_SRC]}
).then(function (output) {
fs.writeFileSync(
path.join(CONSTANTS.DIR_BUILD_APP, 'app.css'),
csso.minify(output.css).css
);
console.log('- Styles were packed');
complete();
});
});
// copy-sources
desc('Copies sources to the build folder');
task('copy-sources', function () {
wrench.copyDirSyncRecursive(
CONSTANTS.DIR_SRC, CONSTANTS.DIR_BUILD, {
forceDelete: true,
exclude: function (fileName, filePath) {
return fileName === 'variables.less'
|| filePath.indexOf(CONSTANTS.DIR_VENDORS) !== -1
|| filePath.indexOf(CONSTANTS.DIR_APP) !== -1;
}
}
);
console.log('- Sources were copied to the build folder');
});
// copy-metro
desc('Copying Metro-UI-CSS styles to the build folder');
task('copy-metro', ['copy-sources'], function () {
var pathMetroCssOut = path.join(CONSTANTS.DIR_BUILD_APP, 'metro.css'),
pathMetroCssIn = path.join(
CONSTANTS.DIR_VENDORS, 'Metro-UI-CSS', 'css', 'metro-bootstrap.css'
);
fs.writeFileSync(
pathMetroCssOut,
csso.minify(fs.readFileSync(pathMetroCssIn, {encoding: 'utf-8'})).css
);
console.log('- Metro styles were copied');
wrench.copyDirSyncRecursive(
path.join(CONSTANTS.DIR_VENDORS, 'Metro-UI-CSS', 'fonts'),
path.join(CONSTANTS.DIR_BUILD, 'fonts')
);
console.log('- Metro fonts were copied');
});
// copy-bootstrap-bg
desc('Copying bootstrap-bg.js to the build folder');
task('copy-bootstrap-bg', ['copy-sources'], {async: true}, function () {
var manifest = manifestRead(),
fileSet = [];
manifest.background.scripts.forEach(function (p) {
fileSet.push(CONSTANTS.DIR_SRC + p);
});
fs.writeFile(
path.join(CONSTANTS.DIR_BUILD_APP, 'bootstrap-bg.js'), minify(fileSet), function () {
console.log('- Packed to "bootstrap-bg.js":', os.EOL, fileSet);
manifest.background.scripts = ['app/bootstrap-bg.js'];
manifestUpdate(manifest);
complete();
}
);
});
// cleanup-pre
task('cleanup-pre', function () {
wrench.rmdirSyncRecursive(CONSTANTS.DIR_BUILD);
console.log('- "build" folder has been removed');
});
// cleanup-post
task('cleanup-post', {async: true}, function () {
fs.rmdir(path.join(CONSTANTS.DIR_BUILD, 'vendors'), function (err) {
if (err) { throw err; }
console.log('- "vendors" folder has been removed');
complete();
});
});
// layout-modify
desc('Replaces headers in the default layout');
task(
'layout-modify',
['copy-sources', 'pack-app', 'pack-vendors', 'pack-css', 'copy-metro',
'copy-bootstrap-bg', 'version-number'],
function () {
var templatePath = path.join(CONSTANTS.DIR_BUILD, 'views', 'default.html'),
body = fs.readFileSync(templatePath, {encoding: 'utf-8'});
body = body.replace(
/<!-- styles -->([^_]+?)<!-- \/styles -->/gm,
'<link rel="stylesheet" type="text/css" href="/app/metro.css">' +
'<link rel="stylesheet" type="text/css" href="/app/app.css">'
);
body = body.replace(
/<!-- scripts -->([^_]+?)<!-- \/scripts -->/gm,
'<script type="text/javascript" src="/app/vendors.js"></script>' +
'<script type="text/javascript" src="/app/app.js"></script>'
);
fs.writeFileSync(templatePath, body);
console.log('- "default.html" has been updated');
}
);
// version-number
desc('Adds a version number to the about template');
task('version-number', ['copy-sources'], function () {
var templatePath = path.join(CONSTANTS.DIR_BUILD, 'views', 'options-about.html');
fs.writeFileSync(
templatePath, fs.readFileSync(templatePath, {encoding: 'utf-8'})
.replace('##VERSION##', process.env.version)
);
console.log('- "options-about.html" has been changed');
var manifest = manifestRead();
manifest.version = process.env.version || '0.0';
manifestUpdate(manifest);
});