accounts-frontend/packages/scripts/build-dll.ts

65 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-env node */
/* eslint-disable no-console */
import type { Stats } from 'fs';
import fs from 'fs';
import webpack, { MultiCompiler } from 'webpack';
import chalk from 'chalk';
import webpackConfig from './../../webpack.dll.config';
// @ts-ignore
const compiler: MultiCompiler = webpack(webpackConfig);
2020-05-24 04:38:24 +05:30
Promise.all([stat(`${__dirname}/../../yarn.lock`), stat(`${__dirname}/../../dll/vendor.json`)])
.then(([lockFileStats, dllFileStats]) => {
const lockFile = new Date(lockFileStats.mtime);
const dll = new Date(dllFileStats.mtime);
if (dll < lockFile) {
return Promise.reject({
code: 'OUTDATED',
});
}
2020-05-24 04:38:24 +05:30
logResult(chalk.green('Current dlls are up to date!'));
})
.catch((err) => {
if (err.code !== 'ENOENT' && err.code !== 'OUTDATED') {
return Promise.reject(err);
}
2020-05-24 04:38:24 +05:30
console.log('Rebuilding dlls...');
2020-05-24 04:38:24 +05:30
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
return reject(err);
}
2020-05-24 04:38:24 +05:30
logResult(chalk.green('Dll was successfully build in %s ms'), stats.endTime! - stats.startTime!);
2020-05-24 04:38:24 +05:30
resolve();
});
});
})
.catch((err) => {
logResult(chalk.red('Unexpected error checking dll state'), err);
process.exit(1);
});
function logResult(...args: any[]): void {
2020-05-24 04:38:24 +05:30
console.log('\n');
console.log(...args);
console.log('\n');
}
function stat(path: string): Promise<Stats> {
2020-05-24 04:38:24 +05:30
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
err ? reject(err) : resolve(stats);
});
});
}