From f322ff27f276d2f3a07c6242b1ef4ac0edf65d1c Mon Sep 17 00:00:00 2001 From: Mathias Wulff Date: Wed, 14 Aug 2024 16:30:06 +1000 Subject: [PATCH] As found --- README.md | 10 ++-- src/cli.ts | 6 +- src/env/deno.ts | 2 +- test/cli/run.sh | 2 +- test/manual.js | 11 ++++ test/plain.test.js | 17 ++++++ yarn.lock | 138 ++++++++++++++++++++++----------------------- 7 files changed, 107 insertions(+), 79 deletions(-) create mode 100644 test/manual.js create mode 100644 test/plain.test.js diff --git a/README.md b/README.md index 87834b3c..42e6a720 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ [![FOSSA Status](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmathiasrw%2Frexreplace?ref=badge_shield) [![OPEN open source software](https://img.shields.io/badge/Open--OSS-%E2%9C%94-brightgreen.svg)](http://open-oss.com) -RexReplace is a versatile tool to search and replace text in files from the command line. It's inspired by how developers often need to do quick fixes or one-liners for build scripts. +> RexReplace is a versatile tool to search and replace text in files from the command line. It's inspired by how developers often need to do quick fixes or one-liners for build scripts. **Key features**: @@ -294,11 +294,11 @@ Reading many files multiple times is far more time-consuming than creating a com > **Anecdote time** > -> Imagine you need to duplicate a set of files, but each duplicate must have unique keys. To achieve this, you can append `_v2` to each key in the duplicated files. +> Imagine you need to duplicate a set of files, but each duplicate must have unique keys. To achieve this, you can append `_v2` to each key in the duplicated files. Running a separate `rexreplace` command for each key is a reasoable approach. > -> Running a separate `rexreplace` command for each key is a logical approach. However, in a real-world scenario with 5,000 keys across 10,000 files, this method took **57 minutes**. The bottleneck was identifying the 10,000 files 5,000 times, opening, and reading each one repeatedly, plus the startup time for node 5,000 times. +> However, in a real-world scenario with 5,000 keys across 10,000 files, this approach took **57 minutes**. The bottleneck was identifying the 10,000 files 5,000 times, open + read each file 5000 times and the startup time of node x 5,000. > -> Alternatively, bundling all keys into a single regex search pattern like `(key1|key2|key3|...)'` reduced the runtime to surprising **2.14 seconds**! Although this resulted in a 300Kb regex string, it was significantly faster. +> Seeking to optimize performance we bundled all keys into a single regex search pattern like `(key1|key2|key3|...)'`. This resulted in a nasty 300KB one-line command but it reduced the runtime to **2.14 seconds**! > > ```sh > # Separate commands for each key @@ -314,7 +314,7 @@ Reading many files multiple times is far more time-consuming than creating a com > # Total time: 2.14 seconds > ``` > -> Moral: Choosing the right approach can save you a significant amount of time. +> Look, not all problems can be solved like this, but the moral is stil valid: Choosing the right approach can save you a significant amount of time. ## Rumours diff --git a/src/cli.ts b/src/cli.ts index e1946e66..617ef569 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,7 +1,7 @@ /// import yargs from 'yargs'; -import argMate, {helpText} from 'argmate'; +import argMate, {paramInfo} from 'argmate'; import * as rexreplace from './engine.ts'; import {chat, debug, die, warn, info, outputConfig, step} from './output.ts'; @@ -337,10 +337,10 @@ outro: ); // All options into one big config object for the rexreplace engine - let conf: any = {}; + let conf: any = argv; conf.showHelp = () => { - console.log(helpText()); + console.log('helpText()'); process.exit(); }; conf.needHelp = needHelp; diff --git a/src/env/deno.ts b/src/env/deno.ts index 103805ad..7fc51dc8 100644 --- a/src/env/deno.ts +++ b/src/env/deno.ts @@ -8,7 +8,7 @@ import fs from 'fs-extra'; import {cli2conf, executeReplacement} from '../cli.ts'; -const runtime: Runtime = { +export const runtime: Runtime = { fileReadSync: (path, encoding = 'utf8') => fs.readFileSync(path, {encoding}), async fileReadAsync(path, encoding = 'utf8') { diff --git a/test/cli/run.sh b/test/cli/run.sh index ace8b297..8c048386 100644 --- a/test/cli/run.sh +++ b/test/cli/run.sh @@ -48,7 +48,7 @@ reset() { echo -echo '>' Happy path +echo '>' Plain reset rexreplace x x my.file assert "cat my.file" "foobar" diff --git a/test/manual.js b/test/manual.js new file mode 100644 index 00000000..eb8ff2dc --- /dev/null +++ b/test/manual.js @@ -0,0 +1,11 @@ +// https://bun.sh/docs/test/writing + +// @ts-ignore +import { expect, test, describe } from 'bun:test'; + +import { cli2conf, executeReplacement } from '../src/cli.ts'; + +import { runtime } from '../src/env/deno.ts' + +executeReplacement(runtime, conf, pipeData); + diff --git a/test/plain.test.js b/test/plain.test.js new file mode 100644 index 00000000..54d45cc3 --- /dev/null +++ b/test/plain.test.js @@ -0,0 +1,17 @@ +// https://bun.sh/docs/test/writing + +// @ts-ignore +import { expect, test, describe } from 'bun:test'; + +import { cli2conf, executeReplacement } from '../src/cli.ts'; + +import { runtime } from '../src/env/bun.ts' + +executeReplacement(runtime, conf, pipeData); + +describe('Plain', () => { + test('Happy path', () => { + expect(1).toBe(1) + }) +}) + diff --git a/yarn.lock b/yarn.lock index ee52b104..08572865 100644 --- a/yarn.lock +++ b/yarn.lock @@ -103,74 +103,74 @@ slash "3.0.0" source-map "^0.7.3" -"@swc/core-darwin-arm64@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.91.tgz#3bb751899cd208be261c48190b75c87171be79b4" - integrity sha512-7kHGiQ1he5khcEeJuHDmLZPM3rRL/ith5OTmV6bOPsoHi46kLeixORW+ts1opC3tC9vu6xbk16xgX0QAJchc1w== - -"@swc/core-darwin-x64@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-darwin-x64/-/core-darwin-x64-1.3.91.tgz#9860087421ac258f59c5aaede4b76dfe680bb28d" - integrity sha512-8SpU18FbFpZDVzsHsAwdI1thF/picQGxq9UFxa8W+T9SDnbsqwFJv/6RqKJeJoDV6qFdl2OLjuO0OL7xrp0qnQ== - -"@swc/core-linux-arm-gnueabihf@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.3.91.tgz#ed321346fea49cc07cdb1217b3ae5c7f3f8ba7ae" - integrity sha512-fOq4Cy8UbwX1yf0WB0d8hWZaIKCnPtPGguRqdXGLfwvhjZ9SIErT6PnmGTGRbQCNCIkOZWHKyTU0r8t2dN3haQ== - -"@swc/core-linux-arm64-gnu@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.3.91.tgz#bdabe0364f2851f7c1446a5da82b2ccc2a021f65" - integrity sha512-fki4ioRP/Esy4vdp8T34RCV+V9dqkRmOt763pf74pdiyFV2dPLXa5lnw/XvR1RTfPGknrYgjEQLCfZlReTryRw== - -"@swc/core-linux-arm64-musl@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.3.91.tgz#e9e14a8f228bddc68ffc5ab4495000ae8a56ced2" - integrity sha512-XrG+DUUqNtfVLcJ20imby7fpBwQNG5VsEQBzQndSonPyUOa2YkTbBb60YDondfQGDABopuHH8gHN8o2H2/VCnQ== - -"@swc/core-linux-x64-gnu@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.3.91.tgz#283baa984b370944cec8fe4a3a64a756cc40cccc" - integrity sha512-d11bYhX+YPBr/Frcjc6eVn3C0LuS/9U1Li9EmQ+6s9EpYtYRl2ygSlC8eueLbaiazBnCVYFnc8bU4o0kc5B9sw== - -"@swc/core-linux-x64-musl@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.3.91.tgz#18d3e773d150061aa2210149d4abff56ff1465fc" - integrity sha512-2SRp5Dke2P4jCQePkDx9trkkTstnRpZJVw5r3jvYdk0zeO6iC4+ZPvvoWXJLigqQv/fZnIiSUfJ6ssOoaEqTzQ== - -"@swc/core-win32-arm64-msvc@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.3.91.tgz#85a1d2b5c6292c98865ebbc2ad07be23e6869005" - integrity sha512-l9qKXikOxj42UIjbeZpz9xtBmr736jOMqInNP8mVF2/U+ws5sI8zJjcOFFtfis4ru7vWCXhB1wtltdlJYO2vGA== - -"@swc/core-win32-ia32-msvc@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.3.91.tgz#023501d012e3993933e06a9874e5568d38fbdb24" - integrity sha512-+s+52O0QVPmzOgjEe/rcb0AK6q/J7EHKwAyJCu/FaYO9df5ovE0HJjSKP6HAF0dGPO5hkENrXuNGujofUH9vtQ== - -"@swc/core-win32-x64-msvc@1.3.91": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.3.91.tgz#a70898460d3a91e59d14c4a0a1e772fb00643d11" - integrity sha512-7u9HDQhjUC3Gv43EFW84dZtduWCSa4MgltK+Sp9zEGti6WXqDPu/ESjvDsQEVYTBEMEvZs/xVAXPgLVHorV5nQ== +"@swc/core-darwin-arm64@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.92.tgz#0498d3584cf877e39107c94705c38fa4a8c04789" + integrity sha512-v7PqZUBtIF6Q5Cp48gqUiG8zQQnEICpnfNdoiY3xjQAglCGIQCjJIDjreZBoeZQZspB27lQN4eZ43CX18+2SnA== + +"@swc/core-darwin-x64@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-darwin-x64/-/core-darwin-x64-1.3.92.tgz#1728e7ebbfe37b56c07d99e29dde78bfa90cf8d1" + integrity sha512-Q3XIgQfXyxxxms3bPN+xGgvwk0TtG9l89IomApu+yTKzaIIlf051mS+lGngjnh9L0aUiCp6ICyjDLtutWP54fw== + +"@swc/core-linux-arm-gnueabihf@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.3.92.tgz#6f7c20833b739f8911c936c9783976ded2c449dc" + integrity sha512-tnOCoCpNVXC+0FCfG84PBZJyLlz0Vfj9MQhyhCvlJz9hQmvpf8nTdKH7RHrOn8VfxtUBLdVi80dXgIFgbvl7qA== + +"@swc/core-linux-arm64-gnu@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.3.92.tgz#bb01dd9b922b0c076c38924013bd10036ce39c7c" + integrity sha512-lFfGhX32w8h1j74Iyz0Wv7JByXIwX11OE9UxG+oT7lG0RyXkF4zKyxP8EoxfLrDXse4Oop434p95e3UNC3IfCw== + +"@swc/core-linux-arm64-musl@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.3.92.tgz#0070165eed2805475c98eb732bab8bdca955932e" + integrity sha512-rOZtRcLj57MSAbiecMsqjzBcZDuaCZ8F6l6JDwGkQ7u1NYR57cqF0QDyU7RKS1Jq27Z/Vg21z5cwqoH5fLN+Sg== + +"@swc/core-linux-x64-gnu@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.3.92.tgz#d9785f93b9121eeef0f54e8d845dd216698e0115" + integrity sha512-qptoMGnBL6v89x/Qpn+l1TH1Y0ed+v0qhNfAEVzZvCvzEMTFXphhlhYbDdpxbzRmCjH6GOGq7Y+xrWt9T1/ARg== + +"@swc/core-linux-x64-musl@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.3.92.tgz#8fe5cf244695bf4f0bc7dc7df450a9bd1bfccc2b" + integrity sha512-g2KrJ43bZkCZHH4zsIV5ErojuV1OIpUHaEyW1gf7JWKaFBpWYVyubzFPvPkjcxHGLbMsEzO7w/NVfxtGMlFH/Q== + +"@swc/core-win32-arm64-msvc@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.3.92.tgz#d6150785455c813a8e62f4e4b0a22773baf398eb" + integrity sha512-3MCRGPAYDoQ8Yyd3WsCMc8eFSyKXY5kQLyg/R5zEqA0uthomo0m0F5/fxAJMZGaSdYkU1DgF73ctOWOf+Z/EzQ== + +"@swc/core-win32-ia32-msvc@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.3.92.tgz#8142166bceafbaa209d440b36fdc8cd4b4f82768" + integrity sha512-zqTBKQhgfWm73SVGS8FKhFYDovyRl1f5dTX1IwSKynO0qHkRCqJwauFJv/yevkpJWsI2pFh03xsRs9HncTQKSA== + +"@swc/core-win32-x64-msvc@1.3.92": + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.3.92.tgz#4ba542875fc690b579232721ccec7873e139646a" + integrity sha512-41bE66ddr9o/Fi1FBh0sHdaKdENPTuDpv1IFHxSg0dJyM/jX8LbkjnpdInYXHBxhcLVAPraVRrNsC4SaoPw2Pg== "@swc/core@^1.3.90": - version "1.3.91" - resolved "https://registry.yarnpkg.com/@swc/core/-/core-1.3.91.tgz#4d7978846da67bfbe20486574d07f204d6891805" - integrity sha512-r950d0fdlZ8qbSDyvApn3HyCojiZE8xpgJzQvypeMi32dalYwugdJKWyLB55JIGMRGJ8+lmVvY4MPGkSR3kXgA== + version "1.3.92" + resolved "https://registry.yarnpkg.com/@swc/core/-/core-1.3.92.tgz#f51808cdb6cbb90b0877b9a51806eea9a70eafca" + integrity sha512-vx0vUrf4YTEw59njOJ46Ha5i0cZTMYdRHQ7KXU29efN1MxcmJH2RajWLPlvQarOP1ab9iv9cApD7SMchDyx2vA== dependencies: "@swc/counter" "^0.1.1" "@swc/types" "^0.1.5" optionalDependencies: - "@swc/core-darwin-arm64" "1.3.91" - "@swc/core-darwin-x64" "1.3.91" - "@swc/core-linux-arm-gnueabihf" "1.3.91" - "@swc/core-linux-arm64-gnu" "1.3.91" - "@swc/core-linux-arm64-musl" "1.3.91" - "@swc/core-linux-x64-gnu" "1.3.91" - "@swc/core-linux-x64-musl" "1.3.91" - "@swc/core-win32-arm64-msvc" "1.3.91" - "@swc/core-win32-ia32-msvc" "1.3.91" - "@swc/core-win32-x64-msvc" "1.3.91" + "@swc/core-darwin-arm64" "1.3.92" + "@swc/core-darwin-x64" "1.3.92" + "@swc/core-linux-arm-gnueabihf" "1.3.92" + "@swc/core-linux-arm64-gnu" "1.3.92" + "@swc/core-linux-arm64-musl" "1.3.92" + "@swc/core-linux-x64-gnu" "1.3.92" + "@swc/core-linux-x64-musl" "1.3.92" + "@swc/core-win32-arm64-msvc" "1.3.92" + "@swc/core-win32-ia32-msvc" "1.3.92" + "@swc/core-win32-x64-msvc" "1.3.92" "@swc/counter@^0.1.1": version "0.1.2" @@ -237,9 +237,9 @@ "@types/node" "*" "@types/node@*", "@types/node@^20.6.2": - version "20.8.2" - resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.2.tgz#d76fb80d87d0d8abfe334fc6d292e83e5524efc4" - integrity sha512-Vvycsc9FQdwhxE3y3DzeIxuEJbWGDsnrxvMADzTDF/lcdR9/K+AQIeAghTQsHtotg/q0j3WEOYS/jQgSdWue3w== + version "20.8.3" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.3.tgz#c4ae2bb1cfab2999ed441a95c122bbbe1567a66d" + integrity sha512-jxiZQFpb+NlH5kjW49vXxvxTjeeqlbsnTAdBTKpzEdPs9itay7MscYXz3Fo9VYFEsfQ6LJFitHad3faerLAjCw== "@types/responselike@^1.0.0": version "1.0.1" @@ -254,9 +254,9 @@ integrity sha512-axdPBuLuEJt0c4yI5OZssC19K2Mq1uKdrfZBzuxLvaztgqUtFYZUNw7lETExPYJR9jdEoIg4mb7RQKRQzOkeGQ== "@types/yargs@^17.0.24": - version "17.0.26" - resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.26.tgz#388e5002a8b284ad7b4599ba89920a6d74d8d79a" - integrity sha512-Y3vDy2X6zw/ZCumcwLpdhM5L7jmyGpmBCTYMHDLqT2IKVMYRRLdv6ZakA+wxhra6Z/3bwhNbNl9bDGXaFU+6rw== + version "17.0.28" + resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.28.tgz#d106e4301fbacde3d1796ab27374dd16588ec851" + integrity sha512-N3e3fkS86hNhtk6BEnc0rj3zcehaxx8QWhCROJkqpl5Zaoi7nAic3jH8q94jVD3zu5LGk+PUB6KAiDmimYOEQw== dependencies: "@types/yargs-parser" "*" @@ -313,7 +313,7 @@ arg@^4.1.0: integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== argmate@../argmate: - version "0.3.0" + version "0.4.1-beta" argparse@^2.0.1: version "2.0.1"