forked from thomaschampagne/elevate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
367 lines (291 loc) · 11.2 KB
/
gulpfile.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
/**
* * * * * * * * *
* TASKS GRAPH
* * * * * * * * *
* clean => cleanPackage => cleanDistAll => cleanExtNodeModules
* cleanAll => cleanRootNodeModules => clean
* build => writeManifest => tsCompile => npmInstall
* specs => buildSpecs
* buildSpecs => build
* makeArchive => build
* package => clean => makeArchive
*
* * * * * * * * *
* COMMANDS
* * * * * * * * *
* gulp clean
* gulp build
* gulp specs
* gulp package
*/
/**
* Required node module for running gulp tasks
*/
var fs = require('fs');
var _ = require('underscore');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var util = require('gulp-util');
var runSequence = require('run-sequence');
var exec = require('child_process').exec;
var options = require('gulp-options');
var ftp = require('vinyl-ftp');
var git = require('gulp-git');
var jeditor = require("gulp-json-editor");
var typeScript = require("gulp-typescript");
var tsProject = typeScript.createProject("tsconfig.json");
var karmaServer = require('karma').Server;
/**
* Global folder variable
*/
var ROOT_FOLDER = __dirname;
var EXT_FOLDER = ROOT_FOLDER + '/plugin/';
var DIST_FOLDER = ROOT_FOLDER + '/dist/';
var PACKAGE_FOLDER = ROOT_FOLDER + '/package/';
var SPECS_FOLDER = ROOT_FOLDER + '/specs/';
var PACKAGE_NAME = null; // No value at the moment, dynamically set by "package" task
var CURRENT_COMMIT = null;
/**
* Global folder variable
*/
var PLUGIN_TYPESCRIPT_SCRIPTS = ['plugin/**/*.ts']; // CORE & OPTIONS
var CORE_JAVASCRIPT_SCRIPTS = [
'plugin/core/config/env.js',
'plugin/core/modules/*.js',
'plugin/core/scripts/**/*.js', // This shouldn't copy js files to destination because of TypeScript (No JS files written anymore). Keep it in case of JavaScript files used by the way.
'plugin/node_modules/geodesy/dms.js',
'plugin/node_modules/geodesy/latlon-spherical.js',
'plugin/node_modules/chart.js/dist/Chart.bundle.js',
'plugin/node_modules/qrcode-js-package/qrcode.min.js',
'plugin/node_modules/fancybox/dist/js/jquery.fancybox.pack.js',
'plugin/node_modules/underscore/underscore-min.js',
'plugin/node_modules/jquery/dist/jquery.js',
];
var CORE_STYLESHEETS = [
'plugin/node_modules/fancybox/dist/css/jquery.fancybox.css',
'plugin/core/css/core.css'
];
var MANIFEST = ['plugin/manifest.json'];
var CORE_RESOURCES = [
'plugin/core/icons/*',
'plugin/node_modules/fancybox/dist/img/*.*',
];
var OPTIONS_FILES = [
'plugin/node_modules/angular-material/angular-material.css',
'plugin/node_modules/angular-material-icons/angular-material-icons.css',
'plugin/node_modules/angular-material-data-table/dist/md-data-table.min.css',
'plugin/node_modules/nvd3/build/nv.d3.min.css',
'plugin/node_modules/angular/angular.js',
'plugin/node_modules/angular-route/angular-route.js',
'plugin/node_modules/angular-sanitize/angular-sanitize.js',
'plugin/node_modules/angular-animate/angular-animate.js',
'plugin/node_modules/angular-aria/angular-aria.js',
'plugin/node_modules/angular-messages/angular-messages.js',
'plugin/node_modules/angular-material/angular-material.js',
'plugin/node_modules/angular-material-icons/angular-material-icons.js',
'plugin/node_modules/angular-material-data-table/dist/md-data-table.min.js',
'plugin/node_modules/q/q.js',
'plugin/node_modules/d3/d3.js',
'plugin/node_modules/nvd3/build/nv.d3.min.js',
'plugin/node_modules/angular-nvd3/dist/angular-nvd3.min.js',
'plugin/node_modules/moment/moment.js',
'plugin/node_modules/angular-moment/angular-moment.js',
'plugin/node_modules/file-saver/FileSaver.min.js',
'plugin/options/**/*',
'!plugin/options/**/*.ts' // Do not copy TypeScripts script using "!". They are compiled to JS files which are already copied to destination folder. (@see PLUGIN_TYPESCRIPT_SCRIPTS var)
];
/**
* Gulp Tasks
*/
gulp.task('tsCompile', ['npmInstall'], function () { // Compile Typescript and copy them to DIST_FOLDER
util.log('Start TypeScript compilation... then copy files to destination folder.');
return gulp.src(PLUGIN_TYPESCRIPT_SCRIPTS, {
base: 'plugin/'
}).pipe(typeScript(tsProject)).pipe(gulp.dest(DIST_FOLDER));
});
gulp.task('writeManifest', ['tsCompile'], function (done) {
// Handle manifest file, if preview mode or not... if preview then: version name change to short sha1 HEAD commit and version = 0
if (options.has('preview')) {
util.log('Generating preview build.');
git.revParse({
args: '--short HEAD',
quiet: true
}, function (err, sha1Short) {
if (err) {
throw new Error(err);
}
CURRENT_COMMIT = sha1Short
gulp.src(MANIFEST, {
base: 'plugin/'
}).pipe(jeditor({
'version': '0',
'version_name': 'preview@' + sha1Short
})).pipe(gulp.dest(DIST_FOLDER)).on('end', function () {
util.log('HEAD commit short sha1 is: ' + sha1Short + '. Version name will be: preview@' + sha1Short);
done();
});
});
} else {
gulp.src(MANIFEST, {
base: 'plugin/'
}).pipe(gulp.dest(DIST_FOLDER)).on('end', function () {
done();
});
}
});
gulp.task('build', ['writeManifest'], function () {
util.log('Building destination folder with others files: core js scripts, stylesheets, common resources, options files');
return gulp.src(_.union(CORE_JAVASCRIPT_SCRIPTS, CORE_STYLESHEETS, CORE_RESOURCES, OPTIONS_FILES), {
base: 'plugin/'
}).pipe(gulp.dest(DIST_FOLDER));
});
gulp.task('npmInstall', function (initDone) {
util.log('Installing extension NPM dependencies');
// Switch to ./plugin folder
process.chdir(EXT_FOLDER);
exec('npm install', function (error, stdout, stderr) {
if (error) {
util.log(error);
util.log(stderr);
} else {
if (stdout) {
util.log(stdout);
util.log('Install done.');
} else {
util.log('Nothing to install');
}
util.log('Use generated "dist/" folder as chrome unpacked extension folder. You will have to execute "gulp build" command before. Helper: "gulp watch" command will automatically trigger "gulp build" command on a file change event.');
// Switch back to ./plugin/core/../.. aka "root" folder
process.chdir(ROOT_FOLDER);
initDone();
}
});
});
gulp.task('makeArchive', ['build'], function () {
var version;
if (options.has('preview')) {
version = options.get('preview') ? options.get('preview') : (CURRENT_COMMIT) ? CURRENT_COMMIT : 'preview';
} else {
version = 'v' + JSON.parse(fs.readFileSync(DIST_FOLDER + '/manifest.json')).version
}
PACKAGE_NAME = 'stravistix_' + version + '_' + (new Date().toISOString().replace(/T/, '_').replace(/\..+/, '').replace(/:/g, '.')) + '.zip';
util.log('Now creating package archive: ' + PACKAGE_NAME);
return gulp.src(DIST_FOLDER + '/**')
.pipe(plugins.zip(PACKAGE_NAME))
.pipe(gulp.dest(PACKAGE_FOLDER));
});
gulp.task('buildSpecs', ['build'], function () {
util.log('Compile TypeScript specs to JS for Karma testing');
return gulp.src([SPECS_FOLDER + '/**/*.ts'], {
base: './'
}).pipe(typeScript(tsProject)).pipe(gulp.dest('./'));
});
gulp.task('specs', ['buildSpecs'], function () {
util.log('Running jasmine tests through Karma server');
new karmaServer({
configFile: __dirname + '/karma.conf.js'
}, function (hasError) {
if (!hasError) {
util.log('Cleaning compiled JS files inside ' + SPECS_FOLDER + ' folder');
return gulp.src([
SPECS_FOLDER + '/**/*.js'
]).pipe(plugins.clean({
force: true
}));
} else {
process.exit(1);
}
}).start();
});
gulp.task('cleanDistAll', function () {
util.log('Cleaning dist/ folder completly');
return gulp.src(DIST_FOLDER)
.pipe(plugins.clean({
force: true
}));
});
gulp.task('cleanPackage', ['cleanDistAll'], function () {
util.log('Cleaning package/ folder');
return gulp.src(PACKAGE_FOLDER).pipe(plugins.clean({
force: true
}));
});
gulp.task('cleanExtNodeModules', ['cleanDistAll'], function () {
util.log('Cleaning extension node_modules/ folder');
return gulp.src('plugin/node_modules/')
.pipe(plugins.clean({
force: true
}));
});
gulp.task('cleanRootNodeModules', ['clean'], function () {
util.log('Cleaning root extension node_modules/ folder');
return gulp.src('node_modules/')
.pipe(plugins.clean({
force: true
}));
});
/**
* Defining tasks
*/
// Do init install and build to dist/
gulp.task('default', ['build']);
// Result in a zip file into builds/
gulp.task('package', function (done) {
runSequence('clean', 'makeArchive', function () {
done();
});
});
gulp.task('watch', function () {
gulp.watch([
'plugin/**/*',
'!plugin/node_modules/**/*',
], ['build']);
});
// Clean dist/, package/, plugin/core/node_modules/
gulp.task('clean', ['cleanPackage']);
gulp.task('cleanAll', ['cleanRootNodeModules']);
// FTP publish
gulp.task('ftpPublish', ['package'], function () {
if (PACKAGE_NAME) {
util.log('FTP Publish of ' + PACKAGE_NAME);
var ftpConfig = {
host: 'yours',
user: 'yours',
pass: 'yours',
remotePath: '/'
};
if (!options.has('env') && !options.has('json')) {
throw new Error('Make sure to specify option "--json" or "--env"');
} else if (options.has('json')) {
if (fs.existsSync('./ftpConfig.json')) {
util.log('Using ftp config from ./ftpConfig.json file');
ftpConfig = JSON.parse(fs.readFileSync('./ftpConfig.json'));
} else {
throw new Error('Make sure to create ./ftpConfig.json with following config: ' + JSON.stringify(ftpConfig));
}
} else if (options.has('env')) {
if (process.env.FTP_HOST && process.env.FTP_USER && process.env.FTP_PASSWORD) {
ftpConfig.host = process.env.FTP_HOST;
ftpConfig.user = process.env.FTP_USER;
ftpConfig.pass = process.env.FTP_PASSWORD;
ftpConfig.remotePath = process.env.FTP_REMOTE_PATH;
} else {
throw new Error('Missing FTP_HOST, FTP_USER or FTP_PASSWORD environnment variables. FTP_REMOTE_PATH can be also specified.');
}
}
util.log('FTP Upload in progress...');
var globs = [PACKAGE_FOLDER + '/' + PACKAGE_NAME];
var conn = ftp.create({
host: ftpConfig.host,
user: ftpConfig.user,
password: ftpConfig.pass,
log: util.log
});
return gulp.src(globs, {
base: './package/',
buffer: false
}).pipe(conn.dest(ftpConfig.remotePath));
} else {
throw new Error('No package name found. Unable to publish');
}
});