-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgulpfile.js
171 lines (149 loc) · 5.13 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
const fs = require('fs');
const { spawn, exec } = require('child_process');
const request = require('sync-request');
const gulp = require('gulp');
const gutil = require('gulp-util');
const jsonEditor = require('gulp-json-editor');
const webpack = require('webpack');
const app = require('./scripts/server-app');
const mergeTransifex = require('./webpack/gulp-merge-transifex-translations');
const webpackConfig = require('./webpack/config');
const buildConfig = require('./config-build');
const merge = require('gulp-merge-json');
const RelayCommand = [
'relay-compiler',
'--src',
'src/app',
'--schema',
'relay.json',
];
gulp.task('relay:copy', (callback) => {
if (buildConfig.relay.startsWith('http')) {
const res = request('GET', buildConfig.relay);
if (res.statusCode < 300) {
fs.writeFile('./relay.json', res.getBody(), callback);
}
} else {
fs.writeFile('./relay.json', fs.readFileSync(buildConfig.relay), callback);
}
});
gulp.task('webpack:build:web', (callback) => {
const prodConfig = {
...webpackConfig,
mode: 'production',
};
webpack(prodConfig, (err, stats) => {
if (err) {
gutil.log(err.message);
process.exit(1);
}
gutil.log('[webpack:build:web]', stats.toString({ colors: true, chunks: false }));
callback();
});
});
function copy_build_web_assets() {
return gulp.src('./src/assets/**/*').pipe(gulp.dest('./build/web'));
}
function copy_build_web_config_js() {
return gulp.src('./config.js').pipe(gulp.dest('./build/web/js'));
}
function copy_build_web_uptime_js() {
return gulp.src('./uptime.js').pipe(gulp.dest('./build/web/js'));
}
gulp.task('copy:build:web', gulp.series(copy_build_web_assets, copy_build_web_config_js, copy_build_web_uptime_js));
gulp.task('transifex:translations', () => gulp.src('./localization/translations/**/*.json').pipe(mergeTransifex(buildConfig)).pipe(gulp.dest('./localization/translations/')));
gulp.task('transifex:prepare:merge', () => gulp.src('./localization/react-intl/**/*').pipe(jsonEditor((inputJson) => {
const outputJson = {};
inputJson.forEach((entry) => {
outputJson[entry.id] = {
string: entry.defaultMessage,
developer_comment: entry.description,
};
});
return outputJson;
}))
.pipe(merge({ fileName: 'WebStructured.json' }))
.pipe(gulp.dest('./localization/transifex/')));
gulp.task('transifex:prepare:sort', () => gulp.src('./localization/transifex/WebStructured.json').pipe(jsonEditor((inputJson) => {
const orderedResource = Object.keys(inputJson).sort().reduce(
(obj, key) => {
obj[key] = inputJson[key];
return obj;
},
{}
);
return orderedResource;
})).pipe(gulp.dest('./localization/transifex/')));
gulp.task('transifex:prepare', gulp.series('transifex:prepare:merge', 'transifex:prepare:sort'));
function spawnPromise(cmd) {
return new Promise((resolve, reject) => {
const childProcess = spawn(cmd[0], cmd.slice(1), { stdio: 'inherit' });
childProcess.on('error', reject);
childProcess.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`${cmd[0]} exited with code ${code}`));
} else {
resolve();
}
});
});
}
gulp.task('react-relay:build', () => spawnPromise(RelayCommand));
gulp.task('build:web', gulp.series('relay:copy', 'react-relay:build', 'webpack:build:web', 'copy:build:web'));
gulp.task('react-relay:build:watch', () => spawnPromise([...RelayCommand, '--watch']));
gulp.task('clean:build:web', (cb) => {
exec('rm -rf build/web/*', (err, stdout, stderr) => {
cb();
});
});
// Dev mode — with 'watch' enabled for faster builds
// Webpack only — without the rest of the web build steps.
gulp.task('webpack:build:web:dev', (callback) => {
const devConfig = {
...webpackConfig,
bail: (process.env.MODE === 'test'), // Exit on error if on test mode
mode: 'development', // FIXME: Need to change to "production" to work around Relay update issues related to https://github.com/facebook/relay/issues/2049
watch: true,
};
webpack(devConfig, (err, stats) => {
if (err) {
return callback(new gutil.PluginError('webpack:build', err));
}
gutil.log('[webpack:build:web:dev]', stats.toString({
colors: true,
hash: false,
version: false,
timings: true,
assets: true,
chunks: false,
modules: false,
reasons: false,
children: false,
source: false,
errors: true,
errorDetails: true,
warnings: true,
publicPath: false,
}));
return null; // keep eslint happy
});
// never call callback()
});
gulp.task(
'build:web:dev',
gulp.series(
'clean:build:web',
'relay:copy',
'copy:build:web',
'react-relay:build', // before Webpack -- for first run to succeed and not race
gulp.parallel('react-relay:build:watch', 'webpack:build:web:dev'),
),
);
gulp.task('serve:server', (callback) => { // eslint-disable-line no-unused-vars
const port = process.env.SERVER_PORT || 8000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`); // eslint-disable-line no-console
// never call callback()
});
});
gulp.task('serve:dev', gulp.parallel('build:web:dev', 'serve:server'));