-
Notifications
You must be signed in to change notification settings - Fork 577
/
Copy pathgulpfile.babel.js
208 lines (181 loc) · 4.62 KB
/
gulpfile.babel.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
/* eslint max-len: 0 */
import gulp from 'gulp';
import babel from 'gulp-babel';
import server from 'gulp-server-livereload';
import { exec } from 'child_process';
import dotenv from 'dotenv';
import sassVariables from 'gulp-sass-variables';
import { removeSync } from 'fs-extra';
import kebabCase from 'kebab-case';
import hexRgb from 'hex-rgb';
import ts from 'gulp-typescript';
import terser from 'gulp-terser';
import config from './package.json';
import * as rawStyleConfig from './src/theme/default/legacy.js';
const tsProject = ts.createProject('./tsconfig.json');
const sass = require('gulp-sass')(require('sass'));
dotenv.config();
const styleConfig = Object.keys(rawStyleConfig).map((key) => {
const isHex = /^#[0-9A-F]{6}$/i.test(rawStyleConfig[key]);
return ({ [`$raw_${kebabCase(key)}`]: isHex ? hexRgb(rawStyleConfig[key], { format: 'array' }).splice(0, 3).join(',') : rawStyleConfig[key] });
});
const paths = {
src: 'src',
dest: 'build',
tmp: '.tmp',
package: `out/${config.version}`,
html: {
src: 'src/**/*.html',
dest: 'build/',
watch: 'src/**/*.html',
},
styles: {
src: 'src/styles/main.scss',
dest: 'build/styles',
watch: 'src/styles/**/*.scss',
},
scripts: {
src: 'src/**/*.js',
dest: 'build/',
watch: [
// 'packages/**/*.js',
'src/**/*.js',
],
},
tsScripts: {
src: 'src/**/*.ts',
dest: 'build/',
watch: [
// 'packages/**/*.js',
'src/**/*.ts',
],
},
packages: {
watch: 'packages/**/*',
// dest: 'build/',
// watch: [
// // 'packages/**/*.js',
// 'src/**/*.js',
// ],
},
};
function _shell(cmd, cb) {
console.log('executing', cmd);
exec(cmd, {
cwd: paths.dest,
}, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
cb();
});
}
const clean = (done) => {
removeSync(paths.tmp);
removeSync(paths.dest);
done();
};
export { clean };
export function mvSrc() {
return gulp.src(
[
`${paths.src}/*`,
`${paths.src}/*/**`,
`!${paths.scripts.watch[1]}`,
`!${paths.src}/styles/**`,
`!${paths.src}/**/*.js`,
`!${paths.src}/**/*.ts`,
], { since: gulp.lastRun(mvSrc) },
)
.pipe(gulp.dest(paths.dest));
}
export function mvPackageJson() {
return gulp.src(
[
'./package.json',
],
)
.pipe(gulp.dest(paths.dest));
}
export function mvLernaPackages() {
return gulp.src(
[
'packages/**',
],
)
.pipe(gulp.dest(`${paths.dest}/packages`));
}
export function html() {
return gulp.src(paths.html.src, { since: gulp.lastRun(html) })
.pipe(gulp.dest(paths.html.dest));
}
export function styles() {
return gulp.src(paths.styles.src)
.pipe(sassVariables(Object.assign({
$env: process.env.NODE_ENV === 'development' ? 'development' : 'production',
}, ...styleConfig)))
.pipe(sass({
includePaths: [
'./node_modules',
'../node_modules',
],
}).on('error', sass.logError))
.pipe(gulp.dest(paths.styles.dest));
}
export function typescript() {
return gulp.src(paths.tsScripts.src, { since: gulp.lastRun(typescript) })
.pipe(tsProject())
.pipe(gulp.dest(paths.tsScripts.dest));
}
export function scripts() {
return gulp.src(paths.scripts.src, { since: gulp.lastRun(scripts) })
.pipe(babel({
comments: false,
}))
.pipe(gulp.dest(paths.scripts.dest));
}
export function minify() {
return gulp.src(`${paths.dest}/**/*.js`)
.pipe(terser())
.pipe(gulp.dest(paths.dest));
}
export function watch() {
gulp.watch(paths.packages.watch, mvLernaPackages);
gulp.watch(paths.styles.watch, styles);
gulp.watch([
paths.src,
`${paths.scripts.src}`,
`${paths.scripts.src}`,
`${paths.styles.src}`,
], mvSrc);
gulp.watch(paths.scripts.watch, scripts);
gulp.watch(paths.tsScripts.watch, typescript);
}
export function webserver() {
gulp.src([
paths.dest,
])
.pipe(server({
livereload: true,
}));
}
export function sign(done) {
_shell(`codesign --verbose=4 --deep --strict --force --sign "${process.env.SIGNING_IDENTITY}" "${__dirname}/node_modules/electron/dist/Electron.app"`, done);
}
const build = gulp.series(
clean,
gulp.parallel(typescript, mvSrc, mvPackageJson, mvLernaPackages),
gulp.parallel(html, scripts, styles),
minify,
);
export { build };
const devBuild = gulp.series(
clean,
gulp.parallel(typescript, mvSrc, mvPackageJson, mvLernaPackages),
gulp.parallel(html, scripts, styles),
);
const dev = gulp.series(devBuild, gulp.parallel(webserver, watch));
export { dev };