From 88d34d3a757fc36f80b019d1688ab1a2c57bf3dd Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 17:59:14 +0200 Subject: [PATCH 01/22] Pass options from program --- src/index.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7b3b912..1afe088 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,12 +32,11 @@ Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and the // .option('--replacements ', 'Replace stuff') .parse(process.argv); -async function run(inputFilePath: string | undefined) { +async function run(inputFilePath: string | undefined, options: { output: string | null, optimizeSpeed: boolean }) { const dirname = process.cwd(); let jsSource: string = ''; let elmFilePath = undefined; - const options = program.opts(); const replacements = null; const o3Enabled = options.optimizeSpeed; @@ -123,5 +122,5 @@ async function run(inputFilePath: string | undefined) { } } - -run(program.args[0]).catch((e) => console.error(e)); +const { output, optimizeSpeed } = program.opts(); +run(program.args[0], { output, optimizeSpeed }).catch((e) => console.error(e)); From 820141a6a017e7a69d884741682daccd846bfe4e Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:00:39 +0200 Subject: [PATCH 02/22] Rename field --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1afe088..f1b4879 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,7 @@ Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and the // .option('--replacements ', 'Replace stuff') .parse(process.argv); -async function run(inputFilePath: string | undefined, options: { output: string | null, optimizeSpeed: boolean }) { +async function run(inputFilePath: string | undefined, options: { outputFilePath: string | null, optimizeSpeed: boolean }) { const dirname = process.cwd(); let jsSource: string = ''; let elmFilePath = undefined; @@ -123,4 +123,4 @@ async function run(inputFilePath: string | undefined, options: { output: string } const { output, optimizeSpeed } = program.opts(); -run(program.args[0], { output, optimizeSpeed }).catch((e) => console.error(e)); +run(program.args[0], { outputFilePath: output, optimizeSpeed }).catch((e) => console.error(e)); From 209990083915c1d2a253040fd023ee99d652b321 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:01:44 +0200 Subject: [PATCH 03/22] Move inputFilePath into the options --- src/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1b4879..16d80c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,12 +32,17 @@ Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and the // .option('--replacements ', 'Replace stuff') .parse(process.argv); -async function run(inputFilePath: string | undefined, options: { outputFilePath: string | null, optimizeSpeed: boolean }) { +async function run(options: { + inputFilePath: string | undefined, + outputFilePath: string | null, + optimizeSpeed: boolean +}) { const dirname = process.cwd(); let jsSource: string = ''; let elmFilePath = undefined; const replacements = null; + const inputFilePath = options.inputFilePath; const o3Enabled = options.optimizeSpeed; // if (program.initBenchmark) { @@ -123,4 +128,8 @@ async function run(inputFilePath: string | undefined, options: { outputFilePath: } const { output, optimizeSpeed } = program.opts(); -run(program.args[0], { outputFilePath: output, optimizeSpeed }).catch((e) => console.error(e)); +run({ + inputFilePath: program.args[0], + outputFilePath: output, + optimizeSpeed +}).catch((e) => console.error(e)); From 37aefb39e9c45aed796fea4b77b2f0e0b0632b11 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:08:52 +0200 Subject: [PATCH 04/22] Add console.log argument --- src/index.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 16d80c3..4121afd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,6 +37,14 @@ async function run(options: { outputFilePath: string | null, optimizeSpeed: boolean }) { + return runWithLogger(options, console.log.bind(console)); +} + +async function runWithLogger(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; @@ -80,7 +88,7 @@ async function run(options: { if (inputFilePath && inputFilePath.endsWith('.js')) { jsSource = fs.readFileSync(inputFilePath, 'utf8'); - console.log('Optimizing existing JS...'); + log('Optimizing existing JS...'); } else if (inputFilePath && inputFilePath.endsWith('.elm')) { elmFilePath = inputFilePath; jsSource = compileToStringSync([inputFilePath], { @@ -94,7 +102,7 @@ async function run(options: { }, }); if (jsSource != '') { - console.log('Compiled, optimizing JS...'); + log('Compiled, optimizing JS...'); } else { process.exit(1) } @@ -120,10 +128,10 @@ async function run(options: { } fs.writeFileSync(program.output, transformed); const fileName = path.basename(inputFilePath); - console.log('Success!'); - console.log(''); - console.log(` ${fileName} ───> ${program.output}`); - console.log(''); + log('Success!'); + log(''); + log(` ${fileName} ───> ${program.output}`); + log(''); } } From f4e923fdaba0e5518d287a89cd3ff695fc860cda Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:13:01 +0200 Subject: [PATCH 05/22] Formatting --- src/index.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4121afd..9e06e32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,14 +37,20 @@ async function run(options: { outputFilePath: string | null, optimizeSpeed: boolean }) { - return runWithLogger(options, console.log.bind(console)); + return runWithLogger( + options, + console.log.bind(console) + ); } -async function runWithLogger(options: { - inputFilePath: string | undefined, - outputFilePath: string | null, - optimizeSpeed: boolean -}, log: (message?: any, ...optionalParams: any[]) => void) { +async function runWithLogger( + 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; From ea0cb5ea78c70b312b941175315fed6202939a74 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:22:49 +0200 Subject: [PATCH 06/22] Expose run --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 9e06e32..64ee023 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,7 @@ Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and the // .option('--replacements ', 'Replace stuff') .parse(process.argv); -async function run(options: { +export async function run(options: { inputFilePath: string | undefined, outputFilePath: string | null, optimizeSpeed: boolean From aca18d1433720863b09e29e5ee6a7612f94ea4ce Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:31:43 +0200 Subject: [PATCH 07/22] Add bin.js --- bin/elm-optimize-level-2.js | 2 +- src/bin.ts | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/bin.ts diff --git a/bin/elm-optimize-level-2.js b/bin/elm-optimize-level-2.js index df107c6..260d4f2 100755 --- a/bin/elm-optimize-level-2.js +++ b/bin/elm-optimize-level-2.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -const index = require('../dist/index.js'); +const index = require('../dist/bin.js'); diff --git a/src/bin.ts b/src/bin.ts new file mode 100644 index 0000000..92cd0ee --- /dev/null +++ b/src/bin.ts @@ -0,0 +1,41 @@ +// 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 chalk from 'chalk'; +const { version } = require('../package.json'); +// import * as BenchInit from './benchmark/init' +// import * as Benchmark from './benchmark/benchmark'; +// import * as Reporting from './benchmark/reporting'; + +program + .version(version) + .description( + `${chalk.yellow('Elm Optimize Level 2!')} + +This applies a second level of optimization to the javascript that Elm creates. + +Make sure you're familiar with Elm's built-in optimization first: ${chalk.cyan( + 'https://guide.elm-lang.org/optimization/asset_size.html' + )} + +Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and then I'll make some more optimizations!` + ) + .usage('[options] ') + .option('--output ', 'the javascript file to create.', 'elm.js') + .option('-O3, --optimize-speed', 'Enable optimizations that likely increases asset size', false) + // .option('--init-benchmark ', 'Generate some files to help run benchmarks') + // .option('--benchmark ', 'Run the benchmark in the given directory.') + // .option('--replacements ', 'Replace stuff') + .parse(process.argv); + +const { output, optimizeSpeed } = program.opts(); +console.log(program.opts()); +// run({ +// inputFilePath: program.args[0], +// outputFilePath: output, +// optimizeSpeed +// }).catch((e) => console.error(e)); From 84e9c0f95dd6a54bf473b8b904839b48a5cac1e2 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 18:35:15 +0200 Subject: [PATCH 08/22] Remove binary related things from index.js, start execution from bin.js --- src/bin.ts | 18 ++++------- src/index.ts | 90 ++++++++++++++++++---------------------------------- 2 files changed, 36 insertions(+), 72 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 92cd0ee..5a34005 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -1,11 +1,6 @@ -// 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 chalk from 'chalk'; +import { run } from './index'; const { version } = require('../package.json'); // import * as BenchInit from './benchmark/init' // import * as Benchmark from './benchmark/benchmark'; @@ -33,9 +28,8 @@ 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(); -console.log(program.opts()); -// run({ -// inputFilePath: program.args[0], -// outputFilePath: output, -// optimizeSpeed -// }).catch((e) => console.error(e)); +run({ + inputFilePath: program.args[0], + outputFilePath: output, + optimizeSpeed +}).catch((e) => console.error(e)); diff --git a/src/index.ts b/src/index.ts index 64ee023..e0f9cad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,33 +5,10 @@ import * as Transform from './transform'; import { toolDefaults } from './types'; import { compileToStringSync } from 'node-elm-compiler'; import * as fs from 'fs'; -import chalk from 'chalk'; -const { version } = require('../package.json'); // import * as BenchInit from './benchmark/init' // import * as Benchmark from './benchmark/benchmark'; // import * as Reporting from './benchmark/reporting'; -program - .version(version) - .description( - `${chalk.yellow('Elm Optimize Level 2!')} - -This applies a second level of optimization to the javascript that Elm creates. - -Make sure you're familiar with Elm's built-in optimization first: ${chalk.cyan( - 'https://guide.elm-lang.org/optimization/asset_size.html' - )} - -Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and then I'll make some more optimizations!` - ) - .usage('[options] ') - .option('--output ', 'the javascript file to create.', 'elm.js') - .option('-O3, --optimize-speed', 'Enable optimizations that likely increases asset size', false) - // .option('--init-benchmark ', 'Generate some files to help run benchmarks') - // .option('--benchmark ', 'Run the benchmark in the given directory.') - // .option('--replacements ', 'Replace stuff') - .parse(process.argv); - export async function run(options: { inputFilePath: string | undefined, outputFilePath: string | null, @@ -65,32 +42,32 @@ async function runWithLogger( // 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 (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'); @@ -102,10 +79,10 @@ async function runWithLogger( cwd: dirname, optimize: true, processOpts: - // ignore stdout - { - stdio: ['inherit', 'ignore', 'inherit'], - }, + // ignore stdout + { + stdio: ['inherit', 'ignore', 'inherit'], + }, }); if (jsSource != '') { log('Compiled, optimizing JS...'); @@ -140,10 +117,3 @@ async function runWithLogger( log(''); } } - -const { output, optimizeSpeed } = program.opts(); -run({ - inputFilePath: program.args[0], - outputFilePath: output, - optimizeSpeed -}).catch((e) => console.error(e)); From fd4ef3f055d41058871fbee9279d2766517ad25d Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 22:59:13 +0200 Subject: [PATCH 09/22] Duplicate run function and use in bin.js --- src/bin.ts | 16 +++++--- src/run.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 src/run.ts diff --git a/src/bin.ts b/src/bin.ts index 5a34005..eb1ff76 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -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)); diff --git a/src/run.ts b/src/run.ts new file mode 100644 index 0000000..2ec84fa --- /dev/null +++ b/src/run.ts @@ -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(''); + } +} From 39d83a07517e8e8ee2272a3b360f99a4a5d19a17 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:00:55 +0200 Subject: [PATCH 10/22] Export a simplified run function in the index.js --- src/index.ts | 113 ++------------------------------------------------- 1 file changed, 3 insertions(+), 110 deletions(-) diff --git a/src/index.ts b/src/index.ts index e0f9cad..7e228ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,119 +1,12 @@ -// 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'; +import * as Run from './run'; export async function run(options: { inputFilePath: string | undefined, outputFilePath: string | null, optimizeSpeed: boolean }) { - return runWithLogger( + return Run.run( options, - console.log.bind(console) + () => { } ); } - -async function runWithLogger( - 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(''); - } -} From 1bb4a13ce0a5acf4bd2a21dfcc0c08180b65b59b Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:05:42 +0200 Subject: [PATCH 11/22] Exit with error code when something goes wrong --- src/bin.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bin.ts b/src/bin.ts index eb1ff76..c875289 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -36,4 +36,7 @@ run( optimizeSpeed }, console.log.bind(console), -).catch((e) => console.error(e)); +).catch((e) => { + console.error(e); + process.exit(1); +}); From 09027a383475bd5c1c2566c58c325159bb0c1bba Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:07:20 +0200 Subject: [PATCH 12/22] Don't print the stack trace when errors happen --- src/bin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin.ts b/src/bin.ts index c875289..0cbfc67 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -37,6 +37,6 @@ run( }, console.log.bind(console), ).catch((e) => { - console.error(e); + console.error(e.toString()); process.exit(1); }); From 6e36e786372f703caed9593dc43bd9fab42286f7 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:10:59 +0200 Subject: [PATCH 13/22] Add a verbose mode --- src/bin.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 0cbfc67..9d088fe 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -23,12 +23,13 @@ Give me an Elm file, I'll compile it behind the scenes using Elm 0.19.1, and the .usage('[options] ') .option('--output ', 'the javascript file to create.', 'elm.js') .option('-O3, --optimize-speed', 'Enable optimizations that likely increases asset size', false) + .option('--verbose', 'Show more error details, useful to provide better bug reports', false) // .option('--init-benchmark ', 'Generate some files to help run benchmarks') // .option('--benchmark ', 'Run the benchmark in the given directory.') // .option('--replacements ', 'Replace stuff') .parse(process.argv); -const { output, optimizeSpeed } = program.opts(); +const { output, optimizeSpeed, verbose } = program.opts(); run( { inputFilePath: program.args[0], @@ -37,6 +38,10 @@ run( }, console.log.bind(console), ).catch((e) => { - console.error(e.toString()); + if (verbose) { + console.error(e); + } else { + console.error(e.toString()); + } process.exit(1); }); From c33c280e096165c7917f5ea826e99de512e6a479 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:14:26 +0200 Subject: [PATCH 14/22] Throw an error when input is not a JS/Elm file --- src/run.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/run.ts b/src/run.ts index 2ec84fa..d2077e1 100644 --- a/src/run.ts +++ b/src/run.ts @@ -79,9 +79,7 @@ export async function run( process.exit(1) } } else { - console.error('Please provide a path to an Elm file.'); - program.outputHelp(); - return; + throw new Error('Please provide a path to an Elm file.\n' + program.helpInformation()); } if (jsSource != '') { const transformed = await Transform.transform( From 4845e5d9ac601272cd7ab52133979e369ed3ac38 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:18:34 +0200 Subject: [PATCH 15/22] Throw an error instead of using process.exit --- src/run.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/run.ts b/src/run.ts index d2077e1..c55e32e 100644 --- a/src/run.ts +++ b/src/run.ts @@ -76,7 +76,7 @@ export async function run( if (jsSource != '') { log('Compiled, optimizing JS...'); } else { - process.exit(1) + throw new Error('An error occurred when compiling your application with Elm 0.19.1.'); } } else { throw new Error('Please provide a path to an Elm file.\n' + program.helpInformation()); From bd7926257d28c9f8ded16a15062e934a043b1868 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:23:50 +0200 Subject: [PATCH 16/22] Use verbose mode for transforms when --verbose is used --- src/bin.ts | 3 ++- src/index.ts | 2 +- src/run.ts | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 9d088fe..cab1082 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -34,7 +34,8 @@ run( { inputFilePath: program.args[0], outputFilePath: output, - optimizeSpeed + optimizeSpeed, + verbose, }, console.log.bind(console), ).catch((e) => { diff --git a/src/index.ts b/src/index.ts index 7e228ce..34bebbd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export async function run(options: { optimizeSpeed: boolean }) { return Run.run( - options, + { ...options, verbose: false }, () => { } ); } diff --git a/src/run.ts b/src/run.ts index c55e32e..f8fad1c 100644 --- a/src/run.ts +++ b/src/run.ts @@ -13,7 +13,8 @@ export async function run( options: { inputFilePath: string | undefined, outputFilePath: string | null, - optimizeSpeed: boolean + optimizeSpeed: boolean, + verbose: boolean, }, log: (message?: any, ...optionalParams: any[]) => void ) { @@ -86,7 +87,7 @@ export async function run( dirname, jsSource, elmFilePath, - false, + options.verbose, toolDefaults(o3Enabled, replacements), ); From 908cec2fee861f3587c3ca5ccd4d43afe14c3af1 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:41:48 +0200 Subject: [PATCH 17/22] Extract processOpts to be an argument --- src/bin.ts | 1 + src/index.ts | 11 +++++++++-- src/run.ts | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index cab1082..e72e547 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -36,6 +36,7 @@ run( outputFilePath: output, optimizeSpeed, verbose, + processOpts: { stdio: ['inherit', 'ignore', 'inherit'] }, }, console.log.bind(console), ).catch((e) => { diff --git a/src/index.ts b/src/index.ts index 34bebbd..45b4c98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,10 +3,17 @@ import * as Run from './run'; export async function run(options: { inputFilePath: string | undefined, outputFilePath: string | null, - optimizeSpeed: boolean + optimizeSpeed: boolean, + processOpts: { + stdio: [string, string, string], + } | null, }) { return Run.run( - { ...options, verbose: false }, + { + ...options, + verbose: false, + processOpts: options.processOpts || { stdio: ['inherit', 'ignore', 'inherit'] }, + }, () => { } ); } diff --git a/src/run.ts b/src/run.ts index f8fad1c..71a38b6 100644 --- a/src/run.ts +++ b/src/run.ts @@ -15,6 +15,7 @@ export async function run( outputFilePath: string | null, optimizeSpeed: boolean, verbose: boolean, + processOpts: { stdio: [string, string, string] }, }, log: (message?: any, ...optionalParams: any[]) => void ) { From ac3d1349af065fc754eaec3ddfbd10af3f132817 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:44:38 +0200 Subject: [PATCH 18/22] Return the outputFilePath and require it in the lib --- src/index.ts | 2 +- src/run.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 45b4c98..fd190a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import * as Run from './run'; export async function run(options: { inputFilePath: string | undefined, - outputFilePath: string | null, + outputFilePath: string, optimizeSpeed: boolean, processOpts: { stdio: [string, string, string], diff --git a/src/run.ts b/src/run.ts index 71a38b6..42b5281 100644 --- a/src/run.ts +++ b/src/run.ts @@ -12,7 +12,7 @@ import * as fs from 'fs'; export async function run( options: { inputFilePath: string | undefined, - outputFilePath: string | null, + outputFilePath: string, optimizeSpeed: boolean, verbose: boolean, processOpts: { stdio: [string, string, string] }, @@ -94,15 +94,16 @@ export async function run( // 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); + const outputDirectory = path.dirname(options.outputFilePath); if (!fs.existsSync(outputDirectory)) { fs.mkdirSync(outputDirectory, { recursive: true }); } - fs.writeFileSync(program.output, transformed); + fs.writeFileSync(options.outputFilePath, transformed); const fileName = path.basename(inputFilePath); log('Success!'); log(''); - log(` ${fileName} ───> ${program.output}`); + log(` ${fileName} ───> ${options.outputFilePath}`); log(''); + return options.outputFilePath; } } From 07dd5d2fe9438f3683af344da69b501389d0420d Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:46:13 +0200 Subject: [PATCH 19/22] Throw an error if outputFilePath is not defined --- src/run.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/run.ts b/src/run.ts index 42b5281..4d20a62 100644 --- a/src/run.ts +++ b/src/run.ts @@ -19,6 +19,10 @@ export async function run( }, log: (message?: any, ...optionalParams: any[]) => void ) { + if (!options.outputFilePath) { + throw new Error('Missing an output file path'); + } + const dirname = process.cwd(); let jsSource: string = ''; let elmFilePath = undefined; From 89100d5ae96f0110a7b9b2f8edf774af00cb7e47 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:50:07 +0200 Subject: [PATCH 20/22] Get help information from arguments --- src/bin.ts | 1 + src/index.ts | 1 + src/run.ts | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index e72e547..664a305 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -38,6 +38,7 @@ run( verbose, processOpts: { stdio: ['inherit', 'ignore', 'inherit'] }, }, + program.helpInformation(), console.log.bind(console), ).catch((e) => { if (verbose) { diff --git a/src/index.ts b/src/index.ts index fd190a9..74c0594 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ export async function run(options: { verbose: false, processOpts: options.processOpts || { stdio: ['inherit', 'ignore', 'inherit'] }, }, + '', () => { } ); } diff --git a/src/run.ts b/src/run.ts index 4d20a62..9c8052f 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,5 +1,4 @@ // 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'; @@ -17,6 +16,7 @@ export async function run( verbose: boolean, processOpts: { stdio: [string, string, string] }, }, + helpInformation: string, log: (message?: any, ...optionalParams: any[]) => void ) { if (!options.outputFilePath) { @@ -85,7 +85,7 @@ export async function run( throw new Error('An error occurred when compiling your application with Elm 0.19.1.'); } } else { - throw new Error('Please provide a path to an Elm file.\n' + program.helpInformation()); + throw new Error(`Please provide a path to an Elm file.\n${helpInformation}`.trim()); } if (jsSource != '') { const transformed = await Transform.transform( From 5d43f746a125295a1ff3be291ab8126fa2af5e86 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:51:47 +0200 Subject: [PATCH 21/22] Throw an error if JS is empty --- src/run.ts | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/run.ts b/src/run.ts index 9c8052f..f80604a 100644 --- a/src/run.ts +++ b/src/run.ts @@ -87,27 +87,30 @@ export async function run( } else { throw new Error(`Please provide a path to an Elm file.\n${helpInformation}`.trim()); } - if (jsSource != '') { - const transformed = await Transform.transform( - dirname, - jsSource, - elmFilePath, - options.verbose, - 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(options.outputFilePath); - if (!fs.existsSync(outputDirectory)) { - fs.mkdirSync(outputDirectory, { recursive: true }); - } - fs.writeFileSync(options.outputFilePath, transformed); - const fileName = path.basename(inputFilePath); - log('Success!'); - log(''); - log(` ${fileName} ───> ${options.outputFilePath}`); - log(''); - return options.outputFilePath; + if (jsSource == '') { + throw new Error('Target JS file is empty.'); } + + const transformed = await Transform.transform( + dirname, + jsSource, + elmFilePath, + options.verbose, + 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(options.outputFilePath); + if (!fs.existsSync(outputDirectory)) { + fs.mkdirSync(outputDirectory, { recursive: true }); + } + fs.writeFileSync(options.outputFilePath, transformed); + const fileName = path.basename(inputFilePath); + log('Success!'); + log(''); + log(` ${fileName} ───> ${options.outputFilePath}`); + log(''); + return options.outputFilePath; } From fb6decc343f8eca48103a01c8f2e577f0e29bd15 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 1 Oct 2021 23:53:35 +0200 Subject: [PATCH 22/22] Remove unused linter disable comments --- src/bin.ts | 1 - src/run.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 664a305..95162d1 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -1,4 +1,3 @@ -// tslint:disable-next-line no-require-imports no-var-requires import program from 'commander'; import chalk from 'chalk'; import { run } from './run'; diff --git a/src/run.ts b/src/run.ts index f80604a..963e395 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,4 +1,3 @@ -// tslint:disable-next-line no-require-imports no-var-requires import * as path from 'path'; import * as Transform from './transform'; import { toolDefaults } from './types';