ws4kp-linhanced/gulp/publish-frontend.mjs

236 lines
6.6 KiB
JavaScript
Raw Normal View History

2025-05-14 09:42:31 -05:00
import 'dotenv/config';
2024-07-07 22:21:53 -05:00
import {
src, dest, series, parallel,
} from 'gulp';
import concat from 'gulp-concat';
import terser from 'gulp-terser';
import ejs from 'gulp-ejs';
import rename from 'gulp-rename';
2025-05-14 09:02:08 -05:00
import htmlmin from 'gulp-html-minifier-terser';
2024-07-07 22:21:53 -05:00
import { deleteAsync } from 'del';
2025-05-14 14:10:05 -05:00
import s3Upload from 'gulp-s3-uploader';
2024-07-07 22:21:53 -05:00
import webpack from 'webpack-stream';
import TerserPlugin from 'terser-webpack-plugin';
import { readFile } from 'fs/promises';
2025-03-27 10:23:27 -05:00
import file from 'gulp-file';
2025-05-23 22:14:48 -05:00
import { CloudFrontClient, CreateInvalidationCommand } from '@aws-sdk/client-cloudfront';
2025-11-10 04:52:13 +00:00
import log from 'fancy-log';
2025-11-30 04:16:02 +00:00
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import sourceMaps from 'gulp-sourcemaps';
2025-05-23 22:14:48 -05:00
import OVERRIDES from '../src/overrides.mjs';
2020-09-04 13:02:20 -05:00
2021-01-04 11:58:58 -06:00
// get cloudfront
2025-03-27 10:23:27 -05:00
import reader from '../src/playlist-reader.mjs';
2024-07-07 22:21:53 -05:00
2025-11-30 04:16:02 +00:00
const sass = gulpSass(dartSass);
2025-03-24 18:24:49 -05:00
const clean = () => deleteAsync(['./dist/**/*', '!./dist/readme.txt']);
2021-01-04 11:58:58 -06:00
2024-07-07 21:49:21 -05:00
const cloudfront = new CloudFrontClient({ region: 'us-east-1' });
const RESOURCES_PATH = './dist/resources';
2021-01-04 11:58:58 -06:00
// Data is now served as JSON files to avoid redundancy
2022-12-07 10:53:18 -06:00
const webpackOptions = {
mode: 'production',
output: {
filename: 'ws.min.js',
},
resolve: {
2024-07-07 22:21:53 -05:00
roots: ['./'],
2022-12-07 10:53:18 -06:00
},
2025-11-30 04:16:02 +00:00
devtool: 'source-map',
2022-12-07 10:53:18 -06:00
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
// sourceMap: true,
format: {
comments: false,
},
},
}),
],
},
};
const jsVendorSources = [
'server/scripts/vendor/auto/nosleep.js',
2020-10-16 15:52:56 -05:00
'server/scripts/vendor/auto/swiped-events.js',
'server/scripts/vendor/auto/suncalc.js',
2020-09-04 13:02:20 -05:00
];
2022-12-07 10:53:18 -06:00
2024-07-07 22:21:53 -05:00
const compressJsVendor = () => src(jsVendorSources)
2022-12-07 10:53:18 -06:00
.pipe(concat('vendor.min.js'))
2021-01-04 11:58:58 -06:00
.pipe(terser())
2024-07-07 22:21:53 -05:00
.pipe(dest(RESOURCES_PATH));
2020-09-04 13:02:20 -05:00
2022-12-07 10:53:18 -06:00
const mjsSources = [
'server/scripts/modules/currentweatherscroll.mjs',
2022-12-14 16:31:11 -06:00
'server/scripts/modules/hazards.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/currentweather.mjs',
'server/scripts/modules/almanac.mjs',
2025-05-15 16:04:57 -05:00
'server/scripts/modules/spc-outlook.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/icons.mjs',
'server/scripts/modules/extendedforecast.mjs',
'server/scripts/modules/hourly.mjs',
2022-12-07 15:36:02 -06:00
'server/scripts/modules/hourly-graph.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/latestobservations.mjs',
'server/scripts/modules/localforecast.mjs',
'server/scripts/modules/radar.mjs',
'server/scripts/modules/regionalforecast.mjs',
'server/scripts/modules/travelforecast.mjs',
'server/scripts/modules/progress.mjs',
2025-03-22 15:10:06 +01:00
'server/scripts/modules/media.mjs',
'server/scripts/modules/custom-scroll-text.mjs',
2026-04-06 18:50:03 -04:00
'server/scripts/modules/serverobservations.mjs',
'server/scripts/modules/linuxnews.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/index.mjs',
];
2024-07-07 22:21:53 -05:00
const buildJs = () => src(mjsSources)
2022-12-07 10:53:18 -06:00
.pipe(webpack(webpackOptions))
2024-07-07 22:21:53 -05:00
.pipe(dest(RESOURCES_PATH));
2022-12-07 10:53:18 -06:00
2021-01-04 11:58:58 -06:00
const cssSources = [
2025-11-30 04:16:02 +00:00
'server/styles/scss/**/*.scss',
2020-09-08 10:05:46 -05:00
];
2025-11-30 04:16:02 +00:00
const buildCss = () => src(cssSources)
.pipe(sourceMaps.init())
.pipe(sass({ style: 'compressed' }).on('error', sass.logError))
.pipe(rename({ suffix: '.min' }))
.pipe(sourceMaps.write('./'))
.pipe(dest(RESOURCES_PATH))
.pipe(dest('./server/styles'));
2020-09-04 13:02:20 -05:00
2021-01-04 11:58:58 -06:00
const htmlSources = [
2020-09-04 13:02:20 -05:00
'views/*.ejs',
];
const packageJson = await readFile('package.json');
let { version } = JSON.parse(packageJson);
const previewVersion = async () => {
// generate a relatively unique timestamp for cache invalidation of the preview site
const now = new Date();
const msNow = now.getTime() % 1_000_000;
version = msNow.toString();
2024-07-07 22:21:53 -05:00
};
2020-09-04 13:02:20 -05:00
const compressHtml = async () => src(htmlSources)
.pipe(ejs({
production: version,
serverAvailable: false,
version,
OVERRIDES,
query: {},
}))
.pipe(rename({ extname: '.html' }))
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(dest('./dist'));
2021-01-04 11:58:58 -06:00
const otherFiles = [
2020-09-04 13:02:20 -05:00
'server/robots.txt',
'server/manifest.json',
2025-03-27 10:23:27 -05:00
'server/music/**/*.mp3',
2020-09-04 13:02:20 -05:00
];
2025-03-24 18:24:49 -05:00
const copyOtherFiles = () => src(otherFiles, { base: 'server/', encoding: false })
2024-07-07 22:21:53 -05:00
.pipe(dest('./dist'));
2020-09-04 13:02:20 -05:00
// Copy JSON data files for static hosting
const copyDataFiles = () => src([
'datagenerators/output/travelcities.json',
'datagenerators/output/regionalcities.json',
'datagenerators/output/stations.json',
]).pipe(dest('./dist/data'));
2020-09-04 13:02:20 -05:00
const s3 = s3Upload({
useIAM: true,
2021-01-04 11:58:58 -06:00
}, {
2020-09-04 13:02:20 -05:00
region: 'us-east-1',
});
2021-01-04 11:58:58 -06:00
const uploadSources = [
2020-09-04 13:02:20 -05:00
'dist/**',
'!dist/images/**/*',
'!dist/fonts/**/*',
2020-09-04 13:02:20 -05:00
];
2025-06-26 22:30:42 -05:00
const uploadCreator = (bucket) => () => src(uploadSources, { base: './dist', encoding: false })
2021-01-04 11:58:58 -06:00
.pipe(s3({
2025-06-26 22:30:42 -05:00
Bucket: bucket,
2021-01-04 11:58:58 -06:00
StorageClass: 'STANDARD',
maps: {
CacheControl: (keyname) => {
if (keyname.indexOf('index.html') > -1) return 'max-age=300'; // 10 minutes
2025-03-27 10:23:27 -05:00
if (keyname.indexOf('.mp3') > -1) return 'max-age=31536000'; // 1 year for mp3 files
2021-01-04 11:58:58 -06:00
return 'max-age=2592000'; // 1 month
2020-09-04 13:02:20 -05:00
},
2021-01-04 11:58:58 -06:00
},
2024-07-07 22:21:53 -05:00
}));
2020-09-04 13:02:20 -05:00
2022-12-09 11:58:47 -06:00
const imageSources = [
'server/fonts/**',
'server/images/**',
2025-05-15 09:02:49 -05:00
'!server/images/gimp/**',
2022-12-09 11:58:47 -06:00
];
2025-06-26 22:30:42 -05:00
const upload = uploadCreator(process.env.BUCKET);
const uploadPreview = uploadCreator(process.env.BUCKET_PREVIEW);
const uploadImagesCreator = (bucket) => () => src(imageSources, { base: './server', encoding: false })
2022-12-09 11:58:47 -06:00
.pipe(
s3({
2025-06-26 22:30:42 -05:00
Bucket: bucket,
2022-12-09 11:58:47 -06:00
StorageClass: 'STANDARD',
2025-05-20 22:10:13 -05:00
maps: {
CacheControl: () => 'max-age=31536000',
},
2022-12-09 11:58:47 -06:00
}),
2024-07-07 22:21:53 -05:00
);
2022-12-09 11:58:47 -06:00
2025-06-26 22:30:42 -05:00
const uploadImages = uploadImagesCreator(process.env.BUCKET);
const uploadImagesPreview = uploadImagesCreator(process.env.BUCKET_PREVIEW);
const copyImageSources = () => src(imageSources, { base: './server', encoding: false })
.pipe(dest('./dist'));
2025-06-26 22:30:42 -05:00
const invalidateCreator = (distributionId) => () => cloudfront.send(new CreateInvalidationCommand({
DistributionId: distributionId,
2021-01-04 11:58:58 -06:00
InvalidationBatch: {
CallerReference: (new Date()).toLocaleString(),
Paths: {
Quantity: 1,
Items: ['/*'],
2020-09-04 13:02:20 -05:00
},
2021-01-04 11:58:58 -06:00
},
2024-07-07 22:21:53 -05:00
}));
2020-09-04 13:02:20 -05:00
2025-06-26 22:30:42 -05:00
const invalidate = invalidateCreator(process.env.DISTRIBUTION_ID);
const invalidatePreview = invalidateCreator(process.env.DISTRIBUTION_ID_PREVIEW);
2025-03-24 18:24:49 -05:00
const buildPlaylist = async () => {
const availableFiles = await reader();
const playlist = { availableFiles };
2025-03-27 10:23:27 -05:00
return file('playlist.json', JSON.stringify(playlist)).pipe(dest('./dist'));
};
2025-03-24 18:24:49 -05:00
2025-11-10 04:52:13 +00:00
const logVersion = async () => {
log(`Version Published: ${version}`);
};
2025-11-30 04:16:02 +00:00
const buildDist = series(clean, parallel(buildJs, compressJsVendor, buildCss, compressHtml, copyOtherFiles, copyDataFiles, copyImageSources, buildPlaylist));
2024-01-08 08:49:51 -06:00
2022-12-14 11:22:55 -06:00
// upload_images could be in parallel with upload, but _images logs a lot and has little changes
// by running upload last the majority of the changes will be at the bottom of the log for easy viewing
2025-11-10 04:52:13 +00:00
const publishFrontend = series(buildDist, uploadImages, upload, invalidate, logVersion);
const stageFrontend = series(previewVersion, buildDist, uploadImagesPreview, uploadPreview, invalidatePreview);
2024-07-07 22:21:53 -05:00
export default publishFrontend;
2025-03-24 18:24:49 -05:00
export {
buildDist,
2025-05-20 18:26:50 -05:00
invalidate,
2025-06-26 22:30:42 -05:00
stageFrontend,
2025-03-27 10:23:27 -05:00
};