forked from magnusstubman/perspective-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.js
More file actions
executable file
·243 lines (183 loc) · 5.73 KB
/
make.js
File metadata and controls
executable file
·243 lines (183 loc) · 5.73 KB
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
#!/usr/bin/env node
require('shelljs/make');
require('colors');
var _ = require('underscore');
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var mustache = require('mustache');
var moment = require('moment');
/*** CONFIG ********/
var term = process.env.TERM;
var version = process.env.VERSION || moment().format('YYYYMMDD');
var targetDir = process.env.OUTPUT_DIR || path.join('target', 'frontend-build');
var appDir = path.join('src/static');
var indexFile = path.join('config', 'index.mustache');
var mainLessFile = path.join(appDir, 'css', 'main.less');
var devLessFile = path.join('libraries', 'less.js', 'dist', 'less-1.5.0.js');
var devRequireFile = path.join('libraries', 'requirejs', 'require.js');
var devMainLessFile = path.join('css', 'main.less');
var viewsDir = path.join(targetDir, 'views');
var staticDir = path.join(targetDir, 'static');
var jsFileName = 'app-' + version + '.js';
var jsFile = path.join(staticDir, jsFileName);
var cssFileName = 'style-' + version + '.css';
var cssFile = path.join(staticDir, cssFileName);
var rjsConfig = path.join('config', 'build-config.js');
var jshintConfig = path.join('config', 'jshint.json');
var karmaConfig = path.join('config', 'karma.conf.js');
/*** TARGETS ********/
target.all = function() {
target.bower();
target.convert();
target.check();
target.jshint();
target.test();
target.build();
};
target.bower = function() {
section('Installing dependencies');
bin('bower', 'install');
};
target.convert = function() {
var coreDir = 'src/static/libraries/perspective-core-rjs';
var webSocketDir = 'src/static/libraries/perspective-core-web-socket-helper-rjs';
if (test('-d', coreDir) && test('-d', webSocketDir)) {
return;
}
section('Converting Common JS modules');
npmBin('r.js', '-convert', 'src/static/libraries/perspective-core', coreDir);
npmBin('r.js', '-convert', 'src/static/libraries/perspective-core-web-socket-helper', webSocketDir);
};
target.jshint = function() {
var srcFiles = glob.sync(path.join(appDir, 'js', '**', '*.js'));
var testFiles = glob.sync(path.join('test', 'js', '**', '*.js'));
section('Running JSHint');
npmBin('jshint', '--config ' + jshintConfig, srcFiles.concat(testFiles).join(' '));
};
target.test = function() {
section('Running JavaScript tests');
npmBin('karma', 'start', karmaConfig, '--browsers PhantomJS', '--single-run');
};
target.wtest = function() {
section('Running JavaScript tests');
npmBin('karma', 'start', karmaConfig, '--browsers PhantomJS', '--auto-watch');
};
target.build = function() {
createCleanDir(targetDir);
mkdir('-p', viewsDir);
buildIndexHtml();
buildJavaScript();
buildCss();
optimizeImages();
echo();echo();
success("Build succeeded!");
};
target.check = function() {
failIfOnlySubsetOfTestsAreRunning();
};
/*** APP FUNCTIONS ********/
var buildIndexHtml = function() {
var htmlProductionFile = path.join(viewsDir, 'index.html');
var htmlDevFile = path.join('src', 'views', 'index.html');
section('Building HTML for production → ' + htmlProductionFile);
renderAndWriteTemplate(indexFile, htmlProductionFile, {
cssFile: cssFileName,
jsFile: jsFileName,
prod: true,
config: '{{{config}}}'
});
section('Building HTML for development → ' + htmlDevFile);
renderAndWriteTemplate(indexFile, htmlDevFile, {
devMainLessFile: devMainLessFile,
devLess: devLessFile,
devRequire: devRequireFile,
prod: false,
config: '{{{config}}}'
});
};
var buildJavaScript = function() {
section('Building JavaScript → ' + jsFile);
npmBin('r.js', '-o ' + rjsConfig, 'out=' + jsFile);
};
var buildCss = function() {
section('Copying all .css files');
var from = path.join(appDir, 'css/*.css');
cp('-R', from, staticDir);
section('Copying all font files');
from = path.join(appDir, 'css/fonts/');
var to = path.join(staticDir, 'fonts');
cp('-R', from, to);
section('Building Less → ' + cssFile);
npmBin('lessc', mainLessFile, cssFile);
};
var optimizeImages = function() {
var pngs = glob.sync(path.join(appDir, 'images', '*.png'));
if (pngs.length === 0) {
return;
}
section('Optimizing pngs');
var to = path.join(targetDir, 'images');
npmBin('optipng', '-strip all', '-dir ' + to, pngs.join(' '))
};
var renderAndWriteTemplate = function(from, to, data) {
var content = fs.readFileSync(from).toString();
var template = mustache.compile(content);
var html = template(data);
fs.writeFileSync(to, html);
success();
};
var failIfOnlySubsetOfTestsAreRunning = function() {
var specs = glob.sync(path.join(appDir, 'js', '**', '*Spec.js'));
section('Checking for "ddescribe" and "iit" in tests');
var ddescribe = grep("ddescribe", specs);
var iit = grep("iit", specs);
if (ddescribe === '' && iit === '') {
success();
} else {
fail();
}
};
/*** HELPER FUNCTIONS ********/
var bin = function(name) {
var res = exec(name + ' ' + _.rest(arguments).join(' '));
done(res);
};
var npmBin = function(name) {
var bin = path.join('node_modules', '.bin', name);
if (term === 'cygwin')
bin = bin+'.cmd';
if (!test('-e', bin)) {
echo('Binary does not exist: ' + bin);
exit(1);
}
var res = exec(bin + ' ' + _.rest(arguments).join(' '));
done(res);
};
var createCleanDir = function(dir) {
if (test('-d', dir)) {
rm('-rf', dir);
}
mkdir('-p', dir);
return dir;
};
var section = function(header) {
echo();
echo(' ' + header.bold);
};
var done = function(res) {
if (res.code === 0) {
success();
} else {
fail();
}
};
var success = function(text) {
text = text || 'done';
echo(' ' + '✓'.green + ' ' + text.green);
};
var fail = function(text) {
text = text || 'failed';
echo(' ' + '✘'.red + ' ' + text.red);
exit(1);
};