diff options
author | muhamedsalih-tw <104364298+muhamedsalih-tw@users.noreply.github.com> | 2022-10-23 16:19:40 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-23 10:49:40 +0000 |
commit | c9f8a4e90beb48f96008f3417d603c8e35bc2985 (patch) | |
tree | 6161edd53c83df8030b4177fb4df462ed63b77ac /gulpfile.babel.js | |
parent | 6.2.1-nightly.26 [skip ci] (diff) | |
download | ferdium-app-c9f8a4e90beb48f96008f3417d603c8e35bc2985.tar.gz ferdium-app-c9f8a4e90beb48f96008f3417d603c8e35bc2985.tar.zst ferdium-app-c9f8a4e90beb48f96008f3417d603c8e35bc2985.zip |
chore: convert some js scripts & gulpfile into typescript (#693)
Diffstat (limited to 'gulpfile.babel.js')
-rw-r--r-- | gulpfile.babel.js | 262 |
1 files changed, 0 insertions, 262 deletions
diff --git a/gulpfile.babel.js b/gulpfile.babel.js deleted file mode 100644 index 447542f56..000000000 --- a/gulpfile.babel.js +++ /dev/null | |||
@@ -1,262 +0,0 @@ | |||
1 | import gulp from 'gulp'; | ||
2 | import gulpIf from 'gulp-if'; | ||
3 | import babel from 'gulp-babel'; | ||
4 | import dartSass from 'sass'; | ||
5 | import gulpSass from 'gulp-sass'; | ||
6 | import csso from 'gulp-csso'; | ||
7 | import terser from 'gulp-terser'; | ||
8 | import htmlMin from 'gulp-htmlmin'; | ||
9 | import connect from 'gulp-connect'; | ||
10 | import { exec } from 'child_process'; | ||
11 | import sassVariables from 'gulp-sass-variables'; | ||
12 | import { removeSync, outputJson } from 'fs-extra'; | ||
13 | import kebabCase from 'kebab-case'; | ||
14 | import hexRgb from 'hex-rgb'; | ||
15 | import ts from 'gulp-typescript'; | ||
16 | |||
17 | import * as buildInfo from 'preval-build-info'; | ||
18 | import config from './package.json'; | ||
19 | |||
20 | import * as rawStyleConfig from './scripts/theme/default/legacy'; | ||
21 | |||
22 | import 'dotenv/config'; | ||
23 | |||
24 | const sass = gulpSass(dartSass); | ||
25 | |||
26 | const isDevBuild = process.env.NODE_ENV === 'development'; | ||
27 | |||
28 | const getTargetEnv = isDevBuild ? 'development' : 'production'; | ||
29 | |||
30 | const tsProject = ts.createProject('tsconfig.json'); | ||
31 | |||
32 | const styleConfig = Object.keys(rawStyleConfig).map(key => { | ||
33 | const isHex = /^#[\da-f]{6}$/i.test(rawStyleConfig[key]); | ||
34 | return { | ||
35 | [`$raw_${kebabCase(key)}`]: isHex | ||
36 | ? hexRgb(rawStyleConfig[key], { format: 'array' }).splice(0, 3).join(',') | ||
37 | : rawStyleConfig[key], | ||
38 | }; | ||
39 | }); | ||
40 | |||
41 | const paths = { | ||
42 | src: 'src', | ||
43 | dest: 'build', | ||
44 | tmp: '.tmp', | ||
45 | dist: 'out', | ||
46 | package: `out/${config.version}`, | ||
47 | buildInfoDestFile: 'build/buildInfo.json', | ||
48 | recipes: { | ||
49 | src: 'recipes/archives/*.tar.gz', | ||
50 | dest: 'build/recipes/', | ||
51 | }, | ||
52 | recipeInfo: { | ||
53 | src: 'recipes/*.json', | ||
54 | dest: 'build/recipes/', | ||
55 | }, | ||
56 | html: { | ||
57 | src: 'src/**/*.html', | ||
58 | dest: 'build/', | ||
59 | watch: 'src/**/*.html', | ||
60 | }, | ||
61 | styles: { | ||
62 | src: [ | ||
63 | 'src/styles/main.scss', | ||
64 | 'src/styles/vertical.scss', | ||
65 | 'src/styles/animations.scss', | ||
66 | ], | ||
67 | dest: 'build/styles', | ||
68 | watch: 'src/styles/**/*.scss', | ||
69 | }, | ||
70 | javascripts: { | ||
71 | src: ['src/**/*.js', 'src/**/*.jsx'], | ||
72 | dest: 'build/', | ||
73 | watch: ['src/**/*.js', 'src/**/*.jsx'], | ||
74 | }, | ||
75 | typescripts: { | ||
76 | src: ['src/**/*.ts', 'src/**/*.tsx'], | ||
77 | dest: 'build/', | ||
78 | watch: ['src/**/*.ts', 'src/**/*.tsx'], | ||
79 | }, | ||
80 | }; | ||
81 | |||
82 | // eslint-disable-next-line no-unused-vars | ||
83 | function _shell(cmd, cb) { | ||
84 | console.log('executing', cmd); | ||
85 | exec( | ||
86 | cmd, | ||
87 | { | ||
88 | cwd: paths.dest, | ||
89 | }, | ||
90 | (error, stdout, stderr) => { | ||
91 | if (error) { | ||
92 | console.error(`exec error: ${error}`); | ||
93 | return; | ||
94 | } | ||
95 | console.log(`stdout: ${stdout}`); | ||
96 | console.log(`stderr: ${stderr}`); | ||
97 | |||
98 | cb(); | ||
99 | }, | ||
100 | ); | ||
101 | } | ||
102 | |||
103 | const clean = done => { | ||
104 | removeSync(paths.tmp); | ||
105 | removeSync(paths.dest); | ||
106 | removeSync(paths.dist); | ||
107 | |||
108 | done(); | ||
109 | }; | ||
110 | export { clean }; | ||
111 | |||
112 | export function mvSrc() { | ||
113 | return gulp | ||
114 | .src( | ||
115 | [ | ||
116 | `${paths.src}/*`, | ||
117 | `${paths.src}/*/**`, | ||
118 | `!${paths.javascripts.watch[0]}`, | ||
119 | `!${paths.typescripts.watch[0]}`, | ||
120 | `!${paths.src}/styles/**`, | ||
121 | ], | ||
122 | { since: gulp.lastRun(mvSrc) }, | ||
123 | ) | ||
124 | .pipe(gulp.dest(paths.dest)); | ||
125 | } | ||
126 | |||
127 | export function mvPackageJson() { | ||
128 | return gulp.src(['./package.json']).pipe(gulp.dest(paths.dest)); | ||
129 | } | ||
130 | |||
131 | export function exportBuildInfo() { | ||
132 | const buildInfoData = { | ||
133 | timestamp: buildInfo.timestamp, | ||
134 | gitHashShort: buildInfo.gitHashShort, | ||
135 | gitBranch: buildInfo.gitBranch, | ||
136 | }; | ||
137 | return outputJson(paths.buildInfoDestFile, buildInfoData); | ||
138 | } | ||
139 | |||
140 | export function html() { | ||
141 | return gulp | ||
142 | .src(paths.html.src, { since: gulp.lastRun(html) }) | ||
143 | .pipe( | ||
144 | gulpIf( | ||
145 | !isDevBuild, | ||
146 | htmlMin({ | ||
147 | // Only minify in production to speed up dev builds | ||
148 | collapseWhitespace: true, | ||
149 | removeComments: true, | ||
150 | }), | ||
151 | ), | ||
152 | ) | ||
153 | .pipe(gulp.dest(paths.html.dest)) | ||
154 | .pipe(connect.reload()); | ||
155 | } | ||
156 | |||
157 | export function styles() { | ||
158 | return gulp | ||
159 | .src(paths.styles.src) | ||
160 | .pipe( | ||
161 | sassVariables( | ||
162 | Object.assign( | ||
163 | { | ||
164 | $env: getTargetEnv, | ||
165 | }, | ||
166 | ...styleConfig, | ||
167 | ), | ||
168 | ), | ||
169 | ) | ||
170 | .pipe( | ||
171 | sass({ | ||
172 | includePaths: ['./node_modules', '../node_modules'], | ||
173 | }).on('error', sass.logError), | ||
174 | ) | ||
175 | .pipe( | ||
176 | gulpIf( | ||
177 | !isDevBuild, | ||
178 | csso({ | ||
179 | // Only minify in production to speed up dev builds | ||
180 | restructure: false, // Don't restructure CSS, otherwise it will break the styles | ||
181 | }), | ||
182 | ), | ||
183 | ) | ||
184 | .pipe(gulp.dest(paths.styles.dest)) | ||
185 | .pipe(connect.reload()); | ||
186 | } | ||
187 | |||
188 | export function processJavascripts() { | ||
189 | return gulp | ||
190 | .src(paths.javascripts.src, { since: gulp.lastRun(processJavascripts) }) | ||
191 | .pipe( | ||
192 | babel({ | ||
193 | comments: false, | ||
194 | }), | ||
195 | ) | ||
196 | .pipe(gulpIf(!isDevBuild, terser())) // Only uglify in production to speed up dev builds | ||
197 | .pipe(gulp.dest(paths.javascripts.dest)) | ||
198 | .pipe(connect.reload()); | ||
199 | } | ||
200 | |||
201 | export function processTypescripts() { | ||
202 | return gulp | ||
203 | .src(paths.typescripts.src, { since: gulp.lastRun(processTypescripts) }) | ||
204 | .pipe(tsProject()) | ||
205 | .js.pipe( | ||
206 | babel({ | ||
207 | comments: false, | ||
208 | }), | ||
209 | ) | ||
210 | .pipe(gulpIf(!isDevBuild, terser())) // Only uglify in production to speed up dev builds | ||
211 | .pipe(gulp.dest(paths.typescripts.dest)) | ||
212 | .pipe(connect.reload()); | ||
213 | } | ||
214 | |||
215 | export function watch() { | ||
216 | gulp.watch(paths.styles.watch, styles); | ||
217 | |||
218 | gulp.watch([paths.src], mvSrc); | ||
219 | |||
220 | gulp.watch(paths.javascripts.watch, processJavascripts); | ||
221 | gulp.watch(paths.typescripts.watch, processTypescripts); | ||
222 | } | ||
223 | |||
224 | export function webserver() { | ||
225 | connect.server({ | ||
226 | root: paths.dest, | ||
227 | livereload: true, | ||
228 | }); | ||
229 | } | ||
230 | |||
231 | export function recipes() { | ||
232 | return gulp | ||
233 | .src(paths.recipes.src, { since: gulp.lastRun(recipes) }) | ||
234 | .pipe(gulp.dest(paths.recipes.dest)); | ||
235 | } | ||
236 | |||
237 | export function recipeInfo() { | ||
238 | return gulp | ||
239 | .src(paths.recipeInfo.src, { since: gulp.lastRun(recipeInfo) }) | ||
240 | .pipe(gulp.dest(paths.recipeInfo.dest)); | ||
241 | } | ||
242 | |||
243 | const build = gulp.series( | ||
244 | clean, | ||
245 | gulp.parallel( | ||
246 | mvSrc, | ||
247 | mvPackageJson, | ||
248 | exportBuildInfo, | ||
249 | ), | ||
250 | gulp.parallel( | ||
251 | html, | ||
252 | processJavascripts, | ||
253 | processTypescripts, | ||
254 | styles, | ||
255 | recipes, | ||
256 | recipeInfo, | ||
257 | ), | ||
258 | ); | ||
259 | export { build }; | ||
260 | |||
261 | const dev = gulp.series(build, gulp.parallel(webserver, watch)); | ||
262 | export { dev }; | ||