Files
pixel.horse/gulpfile.mjs
T

463 lines
14 KiB
JavaScript

/* eslint-disable */
import 'source-map-support/register.js';
import fs from 'fs';
import path from 'path';
import gulp from 'gulp';
import _ from 'lodash';
import gulpif from 'gulp-if';
import rev from 'gulp-rev';
import gulpSass from 'gulp-sass';
import * as sassEmbedded from 'sass-embedded';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import imagemin, { mozjpeg, optipng, gifsicle, svgo } from 'gulp-imagemin';
import sizereport from 'gulp-sizereport';
import { spawn } from 'child_process';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import { deleteAsync } from 'del';
import config from './config.json' with { type: 'json' };
let development = true;
const stamp = Math.floor(Math.random() * 0xffffffff);
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
const HASH = _.range(0, 10).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
const sass = gulpSass(sassEmbedded);
const argv = yargs(hideBin(process.argv))
.option('fast', { type: 'boolean', default: false })
.option('noserver', { type: 'boolean', default: false })
.option('adm', { type: 'boolean', default: false })
.option('debug', { type: 'boolean', default: false })
.option('main', { type: 'boolean', default: false })
.option('beta', { type: 'boolean', default: false })
.option('timing', { type: 'boolean', default: false })
.option('coverage', { type: 'boolean', default: false })
.option('tests', { type: 'boolean', default: false })
.option('sprites', { type: 'boolean', default: false })
.parse();
function swallowError(e) {
console.log(e.message);
this.emit('end');
}
const runAsync = (command, args) => new Promise((resolve, reject) => {
const proc = spawn(command, args)
.on('error', reject)
.on('exit', code => code ? reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`)) : resolve());
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
});
const readFile = src => fs.readFileSync(src, { encoding: 'utf-8' });
const lintCode = code => (code.trim() + '\n')
.replace(/\r\n/g, '\n')
.replace(/ /g, '\t')
.replace(/"/g, "'");
const npmScript = (name, args = []) => {
const func = () => runAsync(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', name, ...args]);
func.displayName = `npm run ${name}`;
return func;
};
const clean = () => deleteAsync([
'dist/**',
'!dist/node',
'!dist/node/**',
'!dist/pony*',
'temp/*',
'!temp/.gitignore',
]);
const cleanAdmin = () => deleteAsync([
'dist/browser/assets-admin',
]);
const cleanAll = () => deleteAsync([
'node_modules',
'dist',
'dist/**',
'build',
'build/**',
'build-snapshot',
'build-snapshot/**',
'logs',
'logs/**',
'settings',
'settings/**',
'.cache',
'.cache/**',
'src/ts/generated/*',
'!src/ts/generated/gamepad-template.ts',
'!src/ts/generated/gamepad-mappings.ts',
'assets/images/pony.png',
'assets/images/pony2.png',
'assets/images/pony2a.png',
'config.json',
'.idea',
'.idea/**',
'.vs',
'.vs/**',
]);
const manifest = cb => {
const json = {
name: config.title,
short_name: config.title,
start_url: '/',
scope: '/',
theme_color: '#5b6ee1',
background_color: '#5b6ee1',
display: 'standalone',
icons: [
{
src: '/android-chrome-192x192.png',
sizes: '192x192',
type: 'image/png'
},
{
src: '/android-chrome-512x512.png',
sizes: '512x512',
type: 'image/png'
}
]
};
fs.writeFile('src/ts/generated/changelog.ts', JSON.stringify(json, null, 2), 'utf8', cb);
};
const sprites = () => Promise.resolve() // del(['tools/output/images/*'])
.then(() => runAsync('node', ['dist/node/tools/create-sprites.js']))
.then(() => gulp.src('tools/output/images/*', { encoding: false })
.pipe(gulpif(!argv.fast, imagemin()))
.pipe(gulp.dest('assets/images')));
function parseChangelog(markdown) {
const versions = [];
for (const line of markdown.split(/\r?\n/)) {
const heading = /^#+\s+(.*)$/.exec(line);
const item = /^\s*[-*+]\s+(.*)$/.exec(line);
const current = versions[versions.length - 1];
if (heading) {
versions.push({ version: heading[1].trim(), changes: [] });
}
else if (!current || !line.trim()) {
continue;
}
else if (item || !current.changes.length) {
current.changes.push((item ? item[1] : line).trim());
}
else {
current.changes[current.changes.length - 1] += `\n${line.trim()}`;
}
}
return versions;
}
const changelog = cb => {
const object = config.nochangelog ? [] : parseChangelog(readFile('CHANGELOG.md')).map(({ version, changes }) => ({
version,
changes: changes.map(x => x.replace(/^\[test\]/, '<span class="badge badge-secondary">test</span>')),
}));
const type = `{ version: string; changes: string[]; }[]`;
const code = `/* eslint-disable */\n\nexport const CHANGELOG: ${type} = ${JSON.stringify(object, null, 2)};\n`;
fs.writeFile('src/ts/generated/changelog.ts', code, 'utf8', cb);
};
const icons = cb => {
const fontPaths = [
path.join('node_modules', '@fortawesome', 'free-solid-svg-icons'),
path.join('node_modules', '@fortawesome', 'free-brands-svg-icons')
];
const parseIcon = pathString => {
const faCode = fs.readFileSync(pathString, 'utf8');
const exports = {};
const fakeRequire = relative => {
const splitPath = pathString.split(path.sep);
splitPath.pop();
const newPath = `${splitPath.join(path.sep)}${path.sep}${relative}.js`;
return parseIcon(newPath);
};
const fn = new Function('exports', 'require', faCode);
fn(exports, fakeRequire);
return exports;
};
const getIconCode = src => JSON.stringify(parseIcon(`./${src}`).definition);
const iconsTs = readFile('src/ts/client/icons.ts');
const matched = _.uniq(iconsTs.match(/\bfa[A-Z]\S*\b/g));
const icons = matched.map(m => {
const iconPath = (e) => path.join(e, `${m}.js`);
const paths = fontPaths.map(e => iconPath(e));
const found = paths.find(e => fs.existsSync(e));
if (!found) {
console.log(paths.join('\n'));
throw new Error(`Unable to find icon ${m}`);
}
const iconCode = getIconCode(found);
return {
name: m,
code: iconCode
};
}).sort((a, b) => a.name.localeCompare(b.name));
const code = `/* eslint-disable */\n\n${icons.map(({ name, code }) => `export const ${name} = ${code};`).join('\n')}`;
fs.writeFile('src/ts/generated/fa-icons.ts', lintCode(code), 'utf8', cb);
};
const shaders = cb => {
function getShaderCode(filePath) {
return fs.readFileSync(filePath, 'utf8')
.replace(/^\s*\n/gm, '').trim();
}
const dir = path.join('src', 'ts', 'graphics', 'shaders');
const code = '/* eslint-disable */\n\n' + fs.readdirSync(dir)
.map(file => [_.camelCase(file.replace(/\.glsl$/, '')), path.join(dir, file)])
.map(([name, filePath]) => `export const ${name}Shader = \`${getShaderCode(filePath)}\`;`)
.join('\n\n');
fs.writeFile('src/ts/generated/shaders.ts', lintCode(code), 'utf8', cb);
};
const hash = cb => {
const code = `export const HASH = '${HASH}';\nexport const STAMP = ${stamp};`;
fs.writeFileSync('src/ts/generated/hash.ts', lintCode(code), 'utf8');
fs.writeFile('src/ts/generated/hash.json', JSON.stringify({ hash: HASH, stamp }), 'utf8', cb);
};
const rollbar = cb => {
const { environment, clientToken } = config.rollbar || {};
const code = `export const ROLLBAR_ENV = '${environment}';\nexport const ROLLBAR_TOKEN = '${clientToken}';`;
fs.writeFile('src/ts/generated/rollbarConfig.ts', lintCode(code), 'utf8', cb);
};
const assetsRev = cb => {
const json = fs.readFileSync('dist/browser/rev-manifest.json', 'utf8');
const data = _.mapValues(JSON.parse(json), value => value.replace(/^\S+-([a-f0-9]{10})\.\S+$/, '$1'));
const code = `export const REV: { [key: string]: string; } = ${JSON.stringify(data, null, 2)};`;
fs.writeFile('src/ts/generated/rev.ts', lintCode(code), 'utf8', cb);
};
// images are only optimized for production builds
const assetsCopy = () => gulp.src('assets/**/*', { encoding: false })
.pipe(gulpif(!argv.fast && !development, imagemin([
mozjpeg(),
optipng({ optimizationLevel: 1 }),
gifsicle(),
svgo(),
])))
.pipe(rev())
.pipe(gulp.dest('dist/browser/assets'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist/browser'));
function buildSass(name, src, dest) {
const result = () => gulp.src([src], { base: 'src', encoding: false })
.pipe(sass({
loadPaths: ['src/styles/'],
quietDeps: true,
silenceDeprecations: ['import', 'global-builtin', 'color-functions', 'if-function', 'slash-div'],
}).on('error', sass.logError))
.pipe(postcss(() => ({
plugins: [
autoprefixer({ overrideBrowserslist: ['last 2 versions'] }),
...(development ? [] : [cssnano({ preset: ['default', { discardComments: { removeAll: true } }] })]),
],
})))
.pipe(gulpif(!development, rev()))
.pipe(gulp.dest(dest));
result.displayName = `sass (${name})`;
return result;
}
const sassMain = buildSass('main', 'src/styles/style.scss', 'dist/browser/assets');
const sassInline = buildSass('inline', 'src/styles/style-inline.scss', 'dist/browser/assets');
const sassTools = buildSass('tools', 'src/styles/style-tools.scss', 'dist/browser/assets');
const sassAdmin = buildSass('admin', 'src/styles/style-admin.scss', 'dist/browser/assets-admin');
const sassTasks = gulp.parallel(sassMain, sassInline, sassTools, sassAdmin);
const testScripts = [
'dist/node/tests/**/*.js'
];
const ts = npmScript('ts');
const mochaBin = process.platform === 'win32' ? 'mocha.cmd' : 'mocha';
const c8Bin = process.platform === 'win32' ? 'c8.cmd' : 'c8';
const mochaArgs = [...testScripts, '--reporter', 'progress', '--exit', '--timeout', '10000'];
const tests = () => runAsync(mochaBin, mochaArgs)
.catch(e => console.log(e.message));
const covTmp = 'coverage/.tmp';
const coverage = () => runAsync(c8Bin, [
'--clean=false', `--temp-directory=${covTmp}`, '--reporter=json', '--report-dir=coverage', '--include=dist/node',
mochaBin, ...mochaArgs,
])
.catch(e => console.log(e.message));
const remap = () => runAsync(c8Bin, [
`--temp-directory=${covTmp}`, '--reporter=html', '--reporter=text', '--report-dir=coverage-remapped', '--include=dist/node',
'report',
]);
const size = () => gulp.src([
'dist/browser/assets/*.js',
'dist/browser/assets/scripts/*.js',
'dist/browser/assets-admin/scripts/*.js',
'dist/browser/assets/styles/*.css',
'dist/browser/assets-admin/styles/*.css',
]).pipe(sizereport({ gzip: true, total: true }));
const music = async () => {
const dir = path.join(config.assetsPath, 'assets', 'music');
const files = fs.readdirSync(dir).filter(file => file.endsWith('.wav'));
for (const file of files) {
const out = ext => path.join('assets', 'music', path.basename(file, '.wav') + ext);
await runAsync('ffmpeg', ['-y', '-i', path.join(dir, file), '-acodec', 'libmp3lame', out('.mp3')]);
await runAsync('ffmpeg', ['-y', '-i', path.join(dir, file), '-acodec', 'libvorbis', out('.webm')]);
}
};
const serverDev = cb => {
if (!argv.noserver) {
const serverPath = path.join('dist', 'node', 'server', 'server.js');
const options = { env: { NODE_OPTIONS: '--inspect' } };
const commonArgs = [serverPath, '--inspect', '--color', '--beta', '--admin'];
const args = argv.adm ? [...commonArgs, '--adm'] : [...commonArgs, '--login', '--game', '--tools'];
let server;
const start = () => {
server = spawn(process.execPath, args, { stdio: 'inherit', env: { ...process.env, ...options.env } });
};
const restart = cb => {
if (server && server.exitCode === null && server.signalCode === null) {
server.once('exit', () => {
start();
cb();
});
server.kill();
}
else {
start();
cb();
}
};
start();
process.on('exit', () => server && server.kill());
gulp.watch([
'config.json',
'src/maps/*.json',
'dist/node/common/**/*.js',
'dist/node/generated/**/*.js',
'dist/node/graphics/**/*.js',
'dist/node/server/**/*.js',
'views/index.pug',
], { debounceDelay: 1000 }, restart);
}
cb();
};
const patchWebWorkerTask = cb => {
const t = './dist/browser/sw.min.js';
if (fs.existsSync(t)) {
fs.renameSync(t, t.replace('.min', ''));
}
const a = './dist/browser/sw.min.js.map';
if (fs.existsSync(a)) {
fs.unlinkSync(a);
}
cb();
};
const clientArgs = ['--', ...['debug', 'beta', 'timing', 'main'].filter(arg => argv[arg]).map(arg => `--${arg}`)];
const clientProd = npmScript('client', clientArgs);
const clientAdmin = npmScript('client-admin');
const sw = npmScript('sw');
const assets = gulp.series(assetsCopy, assetsRev);
const codegen = gulp.parallel(gulp.series(manifest, changelog), hash, rollbar, icons, shaders);
// sass-embedded compiles in a separate process, so it does not block the event loop
const common = gulp.parallel(codegen, assets, sassTasks);
const covRemap = gulp.series(coverage, remap);
const watch = cb => {
gulp.watch(['CHANGELOG.md'], changelog);
gulp.watch(['src/ts/client/icons.ts'], icons);
gulp.watch(['src/styles/**/*.scss'], sassTasks);
gulp.watch(['src/ts/graphics/shaders/*.glsl'], shaders);
if (argv.coverage || argv.tests) {
gulp.watch(['dist/node/**/*.js'], { debounceDelay: 1000 }, argv.coverage ? covRemap : tests);
}
cb();
};
const watchTools = cb => {
if (argv.sprites) {
// gulp.watch(['src/scripts/tools/**/*.js'], { debounceDelay: 1000 }, sprites);
gulp.watch(['src/ts/tools/trigger.txt'], sprites);
gulp.watch(['assets/**/*'], { debounceDelay: 1000, readDelay: 1000 }, assets);
// gulp.watch([path.join(config.assetsPath, 'assets/**/*')], { debounceDelay: 1000, readDelay: 1000 }, sprites);
}
cb();
};
// const watchTests = cb => {
// const task = argv.coverage ? covRemap : tests;
// gulp.watch([
// 'dist/node/**/*.js',
// 'src/tests/**/*.txt',
// 'src/tests/**/*.png'
// ], { debounceDelay: 1000 }, task);
// cb();
// };
const setProd = cb => {
development = false;
cb();
};
const empty = cb => cb();
//const tsTools = gulp.series(npmScript('ts-tools'));
const spritesTask = argv.sprites ? sprites : empty;
//const buildSprites = gulp.series(tsTools, sprites);
const build = gulp.series(clean, setProd, gulp.parallel(
gulp.series(gulp.parallel(codegen, assets), gulp.parallel(ts, clientProd)),
sassTasks,
), sw, size);
const admin = gulp.series(cleanAdmin, setProd, sassAdmin, ts, clientAdmin);
const dev = gulp.series(clean, spritesTask, common, gulp.parallel(serverDev, watch, watchTools));
export {
music,
admin,
build,
cleanAll as clean,
dev,
sprites,
patchWebWorkerTask as patchWebWorker,
tests as test
};