Duplicate run function and use in bin.js

This commit is contained in:
Jeroen Engels 2021-10-01 22:59:13 +02:00
parent 84e9c0f95d
commit fd4ef3f055
2 changed files with 118 additions and 6 deletions

View File

@ -1,6 +1,7 @@
// tslint:disable-next-line no-require-imports no-var-requires
import program from 'commander';
import chalk from 'chalk';
import { run } from './index';
import { run } from './run';
const { version } = require('../package.json');
// import * as BenchInit from './benchmark/init'
// import * as Benchmark from './benchmark/benchmark';
@ -28,8 +29,11 @@ Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and the
.parse(process.argv);
const { output, optimizeSpeed } = program.opts();
run({
inputFilePath: program.args[0],
outputFilePath: output,
optimizeSpeed
}).catch((e) => console.error(e));
run(
{
inputFilePath: program.args[0],
outputFilePath: output,
optimizeSpeed
},
console.log.bind(console),
).catch((e) => console.error(e));

108
src/run.ts Normal file
View File

@ -0,0 +1,108 @@
// tslint:disable-next-line no-require-imports no-var-requires
import program from 'commander';
import * as path from 'path';
import * as Transform from './transform';
import { toolDefaults } from './types';
import { compileToStringSync } from 'node-elm-compiler';
import * as fs from 'fs';
// import * as BenchInit from './benchmark/init'
// import * as Benchmark from './benchmark/benchmark';
// import * as Reporting from './benchmark/reporting';
export async function run(
options: {
inputFilePath: string | undefined,
outputFilePath: string | null,
optimizeSpeed: boolean
},
log: (message?: any, ...optionalParams: any[]) => void
) {
const dirname = process.cwd();
let jsSource: string = '';
let elmFilePath = undefined;
const replacements = null;
const inputFilePath = options.inputFilePath;
const o3Enabled = options.optimizeSpeed;
// if (program.initBenchmark) {
// console.log(`Initializing benchmark ${program.initBenchmark}`)
// BenchInit.generate(program.initBenchmark)
// process.exit(0)
// }
// if (program.benchmark) {
// const options = {
// compile: true,
// gzip: true,
// minify: true,
// verbose: true,
// assetSizes: true,
// runBenchmark: [
// {
// browser: Browser.Chrome,
// headless: true,
// }
// ],
// transforms: benchmarkDefaults(o3Enabled, replacements),
// };
// const report = await Benchmark.run(options, [
// {
// name: 'Benchmark',
// dir: program.benchmark,
// elmFile: 'V8/Benchmark.elm',
// }
// ]);
// console.log(Reporting.terminal(report));
// // fs.writeFileSync('./results.markdown', Reporting.markdownTable(result));
// process.exit(0)
// }
if (inputFilePath && inputFilePath.endsWith('.js')) {
jsSource = fs.readFileSync(inputFilePath, 'utf8');
log('Optimizing existing JS...');
} else if (inputFilePath && inputFilePath.endsWith('.elm')) {
elmFilePath = inputFilePath;
jsSource = compileToStringSync([inputFilePath], {
output: 'output/elm.opt.js',
cwd: dirname,
optimize: true,
processOpts:
// ignore stdout
{
stdio: ['inherit', 'ignore', 'inherit'],
},
});
if (jsSource != '') {
log('Compiled, optimizing JS...');
} else {
process.exit(1)
}
} else {
console.error('Please provide a path to an Elm file.');
program.outputHelp();
return;
}
if (jsSource != '') {
const transformed = await Transform.transform(
dirname,
jsSource,
elmFilePath,
false,
toolDefaults(o3Enabled, replacements),
);
// Make sure all the folders up to the output file exist, if not create them.
// This mirrors elm make behavior.
const outputDirectory = path.dirname(program.output);
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory, { recursive: true });
}
fs.writeFileSync(program.output, transformed);
const fileName = path.basename(inputFilePath);
log('Success!');
log('');
log(` ${fileName} ───> ${program.output}`);
log('');
}
}