From b76f3718b3793effc8676706db86785f5afebaa1 Mon Sep 17 00:00:00 2001 From: Robin Heggelund Hansen Date: Fri, 20 Aug 2021 10:14:46 +0200 Subject: [PATCH] Use function wrapper replacements by default. --- src/fs_util.ts | 27 +++++++++++++++++++++++++++ src/index.ts | 33 ++++++--------------------------- src/types.ts | 6 ++++-- 3 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 src/fs_util.ts diff --git a/src/fs_util.ts b/src/fs_util.ts new file mode 100644 index 0000000..3cc4b6d --- /dev/null +++ b/src/fs_util.ts @@ -0,0 +1,27 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +export function readFilesSync(dir: string): {[key: string]: string} | null { + let foundAnything = false + const files: {[key: string]: string} = {}; + + fs.readdirSync(dir).forEach(filename => { + const name = path.parse(filename).name; + const filepath = path.resolve(dir, filename); + const stat = fs.statSync(filepath); + const isFile = stat.isFile(); + + if (isFile) { + const content = fs.readFileSync(path.join(dir, filename)) + files[name] = content.toString() + foundAnything = true + } + }); + + if (foundAnything) { + return files; + } else { + return null + } + +} diff --git a/src/index.ts b/src/index.ts index 6721bd3..45cfb8e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ const { version } = require('../package.json'); import * as BenchInit from './benchmark/init' import * as Benchmark from './benchmark/benchmark'; import * as Reporting from './benchmark/reporting'; +import { readFilesSync } from './fs_util'; program .version(version) @@ -38,8 +39,12 @@ async function run(inputFilePath: string | undefined) { const replacementDir = hasReplacements(process.argv) let replacements = null - if (replacementDir){ + if (replacementDir) { replacements = readFilesSync(replacementDir) + } else if (program.benchmark) { + replacements = benchmarkDefaults.replacements; + } else { + replacements = toolDefaults.replacements; } if (program.initBenchmark) { @@ -147,30 +152,4 @@ function hasReplacements(args: string[]){ return dir } - -function readFilesSync(dir: string): {[key: string]: string} | null { - let foundAnything = false - const files: {[key: string]: string} = {}; - - fs.readdirSync(dir).forEach(filename => { - const name = path.parse(filename).name; - const ext = path.parse(filename).ext; - const filepath = path.resolve(dir, filename); - const stat = fs.statSync(filepath); - const isFile = stat.isFile(); - - if (isFile) { - const content = fs.readFileSync(path.join(dir, filename)) - files[name] = content.toString() - foundAnything = true - } - }); - if (foundAnything) { - return files; - } else { - return null - } - -} - run(program.args[0]).catch((e) => console.error(e)); diff --git a/src/types.ts b/src/types.ts index fed094d..5c4e8a4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import { readFilesSync } from './fs_util'; + export enum Mode { Prod = 'prod', Dev = 'dev', @@ -87,7 +89,7 @@ export const toolDefaults: Transforms = { replaceStringFunctions: false, recordUpdates: false, v8Analysis: false, - replacements: null + replacements: readFilesSync(__dirname + '/replacements/faster-function-wrappers') }; @@ -107,5 +109,5 @@ export const benchmarkDefaults: Transforms = { replaceStringFunctions: true, recordUpdates: false, v8Analysis: true, - replacements: null + replacements: readFilesSync(__dirname + '/replacements/faster-function-wrappers') };