/
start.js
112 lines (94 loc) · 2.4 KB
/
start.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
'use strict';
var Q = require('q');
var electron = require('electron');
var path = require('path');
var childProcess = require('child_process');
var kill = require('tree-kill');
var util = require('util');
var conf = require('./conf');
var utils = require('./utils');
var env = utils.getEnvName();
var watch;
var browserSync = require('browser-sync').create();
var browserSyncSpa = require('browser-sync-spa');
browserSync.use(browserSyncSpa({selector: '[ng-app]'}));
function browserSyncInit(baseDir, callback) {
var routes = null;
if (baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
routes = {
'/bower_components': 'bower_components'
};
}
var server = {
baseDir: baseDir,
routes: routes
};
browserSync.init({
browser: 'default',
files: [
path.join(conf.paths.tmp, '/serve/app/index.css'),
path.join(conf.paths.tmp, '/serve/app/index.module.js'),
path.join(conf.paths.tmp, '/serve/app/templateCacheHtml.js')
],
open: false,
server: server,
startPath: '/',
ui: false,
watchOptions: {
awaitWriteFinish: true,
ignoreInitial: true
}
}, callback);
}
var gulpPath = path.resolve('./node_modules/.bin/gulp');
if (process.platform === 'win32') {
gulpPath += '.cmd';
}
var runBuild = function () {
var deferred = Q.defer();
var build = childProcess.spawn(gulpPath, [
'build',
'--env=' + env,
'--color'
], {
stdio: 'inherit'
});
build.on('close', function (/*code*/) {
deferred.resolve();
});
return deferred.promise;
};
var runGulpWatch = function () {
watch = childProcess.spawn(gulpPath, [
'watch',
'--env=' + env,
'--color'
], {
stdio: 'inherit'
});
watch.on('close', function (/*code*/) {
// Gulp watch exits when error occurred during build.
// Just respawn it then.
runGulpWatch();
});
};
var runElectronApp = function () {
var app = childProcess.spawn(electron, ['./build'], {
stdio: 'inherit'
});
app.on('close', function (/*code*/) {
// User closed the app. Kill the host process.
kill(watch.pid, 'SIGKILL', function () {
process.exit();
});
});
};
var runApp = function () {
env == 'development'
? browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src], runElectronApp)
: runElectronApp();
};
runBuild().then(function () {
runGulpWatch();
runApp();
});