-
Notifications
You must be signed in to change notification settings - Fork 14
/
gulpfile.js
186 lines (167 loc) · 5.37 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
const gulp = require('gulp')
const changed = require('gulp-changed')
const browserSync = require('browser-sync').create()
const childProcesses = require('child_process')
const chalk = require('chalk')
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const webpackStream = require('webpack-stream')
const ManifestPlugin = require('webpack-manifest-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
// Define a global serverProcess that we can use to keep track of the running server instance
let serverProcess = null
// Whether our webpack task should be watching or not
let isStaticBuildWatching = false
const PROJECT_ROOT = __dirname;
const EXEC_PATH = './.build/debug/HaCWebsite'
const isProduction = process.env.NODE_ENV === 'production'
function setWatchOnBuildStatic () {
isStaticBuildWatching = true
return Promise.resolve()
}
function buildStatic () {
// To prevent lots of files and add ease to live reloading,
// We don't want to hash filenames in dev mode
const dotHash = isProduction ? '.[hash]' : ''
return gulp.src('static/src/main.js')
.pipe(
webpackStream({
watch: isStaticBuildWatching,
context: path.join(PROJECT_ROOT, 'static/src'),
entry: {
// Scripts entry points
main: './main.js',
// Styles entry points
'styles/main': './styles/main.styl',
// TODO: Ideally we want this to be a wildcard match of some form
// so we can automatically pick-up new custom styles.
'styles/custom/gamegig2017': './styles/custom/gamegig2017.styl'
},
output: {
filename: `[name]${dotHash}.js`,
publicPath: '/static/'
},
module: {
// We add rules explicitly for every file type
// If you need to support a new file extension, edit a rule below
// or add a new one
rules: [
{
test: /\.styl$/,
use: ExtractTextPlugin.extract({
use: 'css-loader!postcss-loader!stylus-loader',
}),
},
{
test: /\.(png|jpg|gif|woff|woff2|ttf|eot|svg)$/,
loader: 'file-loader',
options: {
name: `[path][name]${dotHash}.[ext]`,
}
}
]
},
resolve: {
extensions: ['.js', '.styl']
},
plugins: [
new ManifestPlugin({
fileName: 'manifest.json'
}),
new ExtractTextPlugin(`[name]${dotHash}.css`),
]
}, webpack)
)
.pipe(changed('static/dist', { hasChanged: changed.compareContents }))
.pipe(gulp.dest('static/dist/'))
.pipe(browserSync.stream())
}
// Hooks up the stdout adn stderr of a child process to the gulp output
function connectProcessOutput (process, prefix) {
const logPrefix = prefix == null ? '' : '[' + chalk.blue(prefix) + '] '
process.stdout.on('data', function (data) {
data.toString().split('\n').map(line => console.log(logPrefix, line));
})
process.stderr.on('data', function (data) {
data.toString().split('\n').map(line => console.log(logPrefix, chalk.red(line)))
})
}
// Runs the `swift build` command
function swiftBuild (done) {
var stream = fs.createWriteStream("swift_build.log");
stream.once('open', function(fd) {
const buildProcess = childProcesses.spawn('swift', ['build'])
buildProcess.stdout.on('data', data => {
stream.write(data);
});
buildProcess.stderr.on('data', data => {
stream.write(data);
});
const logPrefix = 'swift-build'
connectProcessOutput(buildProcess, logPrefix)
buildProcess.on('exit', function (code) {
stream.end();
if (code === 0) {
fs.unlink("swift_build.log");
done()
} else {
// The build failed
// Ring the console 'bell'
console.log('\u0007')
done()
}
});
});
}
// Starts our server process
function startServer (done) {
serverProcess = childProcesses.spawn(EXEC_PATH)
serverProcess.on('exit', function (code) {
serverProcess = null
})
connectProcessOutput(serverProcess, 'web-server')
// Wait for Kitura to tell us it's listening
serverProcess.stdout.on('data', function (data) {
if (data.toString().includes('Listening on port')) {
done()
}
})
}
// Starts our server process, killing it first if necessary
function reloadServer (done) {
if (serverProcess === null) {
startServer(done)
} else {
serverProcess.kill()
serverProcess.on('exit', function (code) {
// The process exited, restart it
startServer(done)
})
}
}
function reloadBrowser (done) {
browserSync.reload()
done()
}
function watch () {
browserSync.init({
proxy: 'http://localhost:8090',
notify: false,
open: false,
ghostMode: false
})
gulp.watch(['Sources/**/*', 'Package.swift'], gulp.series(swiftBuild, reloadServer, reloadBrowser))
}
const build = gulp.parallel(swiftBuild, buildStatic)
const serve = gulp.series(setWatchOnBuildStatic, swiftBuild, gulp.parallel(buildStatic, reloadServer, watch))
// Expose the serve and build tasks
gulp.task('static-build', buildStatic)
gulp.task('build', build)
gulp.task('serve', serve)
// When the gulp process is terminated, make sure to clean up
process.on('exit', function () {
if (serverProcess != null) {
serverProcess.kill()
}
})