Skip to content

Commit

Permalink
As found
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Aug 14, 2024
1 parent f235367 commit f322ff2
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 79 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../types/rexreplace.d.ts" />

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';
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/env/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion test/cli/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ reset() {


echo
echo '>' Happy path
echo '>' Plain
reset
rexreplace x x my.file
assert "cat my.file" "foobar"
Expand Down
11 changes: 11 additions & 0 deletions test/manual.js
Original file line number Diff line number Diff line change
@@ -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);

17 changes: 17 additions & 0 deletions test/plain.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

138 changes: 69 additions & 69 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,74 +103,74 @@
slash "3.0.0"
source-map "^0.7.3"

"@swc/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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/[email protected].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"
Expand Down Expand Up @@ -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"
Expand All @@ -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" "*"

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f322ff2

Please sign in to comment.