const { src, dest, watch, parallel } = require('gulp'); const ts = require('gulp-typescript'); const fs = require('fs'); const path = require('path'); const sourcemaps = require('gulp-sourcemaps'); const minimist = require('minimist'); const tsConfig = require('./tsconfig.json'); const webpack = require('webpack-stream'); const webpackConfig = require('./webpack.config'); const argv = minimist(process.argv); const tsProject = ts.createProject('tsconfig.json'); function deleteFolderRecursive(folder) { if (fs.existsSync(folder)) { fs.readdirSync(folder).forEach(function(file) { const currPath = path.resolve(folder, file); if (fs.lstatSync(currPath).isDirectory()) { deleteFolderRecursive(currPath); } else { fs.unlinkSync(currPath); } }); fs.rmdirSync(folder); } } function removeDistFolder() { const distDir = path.resolve('dist'); deleteFolderRecursive(distDir); } function removeFrontendFolder() { const migrationsDir = path.resolve('frontend'); deleteFolderRecursive(migrationsDir); } function buildFrontend() { removeFrontendFolder(); let config = webpackConfig; if (argv.dev) { config = { ...config, ...{ mode: 'development' } }; } if (argv.watch) { config = { ...config, ...{ watch: true } }; } return src(config.entry) .pipe(webpack(config)) .pipe(dest(config.output.path)); } function copyIndexHtml(cb) { let indexHtml = 'index.prod.html'; if (argv.dev) { indexHtml = 'index.dev.html'; } fs.copyFileSync(indexHtml, 'index.html'); cb(); } function buildBackend() { removeDistFolder(); return tsProject .src() .pipe(sourcemaps.init()) .pipe(tsProject()) .pipe(sourcemaps.write('.')) .pipe(dest(tsConfig.compilerOptions.outDir)); } function build(cb) { if (argv.watch) { watch(tsConfig.include, { ignoreInitial: false }, buildBackend); watch('index.*.html', { ignoreInitial: false }, copyIndexHtml); buildFrontend(); } else { parallel(buildBackend, copyIndexHtml, buildFrontend)(cb); } } build.flags = { '--dev': 'builds in development mode', '--watch': 'watches the files', }; exports.build = build;