├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── api.d.ts ├── api.js ├── cli.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | os: 16 | - ubuntu-latest 17 | - macos-latest 18 | - windows-latest 19 | steps: 20 | - uses: actions/checkout@v4 21 | - uses: actions/setup-node@v4 22 | with: 23 | node-version: ${{ matrix.node-version }} 24 | - run: npm install 25 | - run: npm test 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /api.d.ts: -------------------------------------------------------------------------------- 1 | export default function replaceInFiles( 2 | path: string | string[], 3 | options: { 4 | find: Array; 5 | replacement: string; 6 | ignoreCase?: boolean; 7 | glob?: boolean; 8 | }, 9 | ): Promise; 10 | -------------------------------------------------------------------------------- /api.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import path from 'node:path'; 3 | import {promises as fsPromises} from 'node:fs'; 4 | import normalizePath_ from 'normalize-path'; 5 | import writeFileAtomic from 'write-file-atomic'; 6 | import escapeStringRegexp from 'escape-string-regexp'; 7 | import {globby} from 'globby'; 8 | 9 | const normalizePath = process.platform === 'win32' ? normalizePath_ : x => x; 10 | 11 | // TODO(sindresorhus): I will extract this to a separate module at some point when it's more mature. 12 | // `find` is expected to be `Array` 13 | // The `ignoreCase` option overrides the `i` flag for regexes in `find` 14 | export default async function replaceInFiler(filePaths, {find, replacement, ignoreCase, glob} = {}) { 15 | filePaths = [filePaths].flat(); 16 | 17 | if (filePaths.length === 0) { 18 | return; 19 | } 20 | 21 | if (find.length === 0) { 22 | throw new Error('Expected at least one `find` pattern'); 23 | } 24 | 25 | if (replacement === undefined) { 26 | throw new Error('The `replacement` option is required'); 27 | } 28 | 29 | // Replace the replacement string with the string unescaped (only one backslash) if it's escaped 30 | replacement = replacement 31 | .replaceAll('\\n', '\n') 32 | .replaceAll('\\r', '\r') 33 | .replaceAll('\\t', '\t'); 34 | 35 | // TODO: Drop the `normalizePath` call when `convertPathToPattern` from `fast-glob` is added to globby. 36 | filePaths = glob ? await globby(filePaths.map(filePath => normalizePath(filePath))) : [...new Set(filePaths.map(filePath => normalizePath(path.resolve(filePath))))]; 37 | 38 | find = find.map(element => { 39 | const iFlag = ignoreCase ? 'i' : ''; 40 | 41 | if (typeof element === 'string') { 42 | return new RegExp(escapeStringRegexp(element), `g${iFlag}`); 43 | } 44 | 45 | return new RegExp(element.source, `${element.flags.replace('i', '')}${iFlag}`); 46 | }); 47 | 48 | await Promise.all(filePaths.map(async filePath => { 49 | const string = await fsPromises.readFile(filePath, 'utf8'); 50 | 51 | let newString = string; 52 | for (const pattern of find) { 53 | newString = newString.replace(pattern, replacement); 54 | } 55 | 56 | if (newString === string) { 57 | return; 58 | } 59 | 60 | await writeFileAtomic(filePath, newString); 61 | })); 62 | } 63 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import process from 'node:process'; 3 | import meow from 'meow'; 4 | import replaceInFiles from './api.js'; 5 | 6 | const cli = meow(` 7 | Usage 8 | $ replace-in-files 9 | 10 | Options 11 | --regex Regex pattern to find (Can be set multiple times) 12 | --string String to find (Can be set multiple times) 13 | --replacement Replacement string (Required) 14 | --ignore-case Search case-insensitively 15 | --no-glob Disable globbing 16 | 17 | Examples 18 | $ replace-in-files --string='horse' --regex='unicorn|rainbow' --replacement='🦄' foo.md 19 | $ replace-in-files --regex='v\\d+\\.\\d+\\.\\d+' --replacement=v$npm_package_version foo.css 20 | $ replace-in-files --string='blob' --replacement='blog' 'some/**/[gb]lob/*' '!some/glob/foo' 21 | 22 | You can use the same replacement patterns as with \`String#replace()\`, like \`$&\`. 23 | `, { 24 | importMeta: import.meta, 25 | flags: { 26 | regex: { 27 | type: 'string', 28 | isMultiple: true, 29 | }, 30 | string: { 31 | type: 'string', 32 | isMultiple: true, 33 | }, 34 | replacement: { 35 | type: 'string', 36 | isRequired: true, 37 | }, 38 | ignoreCase: { 39 | type: 'boolean', 40 | default: false, 41 | }, 42 | glob: { 43 | type: 'boolean', 44 | default: true, 45 | }, 46 | }, 47 | }); 48 | 49 | if (cli.input.length === 0) { 50 | console.error('Specify one or more file paths'); 51 | process.exit(1); 52 | } 53 | 54 | if (!cli.flags.regex && !cli.flags.string) { 55 | console.error('Specify at least `--regex` or `--string`'); 56 | process.exit(1); 57 | } 58 | 59 | await replaceInFiles(cli.input, { 60 | find: [ 61 | ...cli.flags.string, 62 | ...cli.flags.regex.map(regexString => new RegExp(regexString, 'g')), 63 | ], 64 | replacement: cli.flags.replacement, 65 | ignoreCase: cli.flags.ignoreCase, 66 | glob: cli.flags.glob, 67 | }); 68 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "replace-in-files-cli", 3 | "version": "3.0.0", 4 | "description": "Replace matching strings and regexes in files", 5 | "license": "MIT", 6 | "repository": "sindresorhus/replace-in-files-cli", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./api.d.ts", 16 | "default": "./api.js" 17 | }, 18 | "bin": { 19 | "replace-in-files": "./cli.js" 20 | }, 21 | "sideEffects": false, 22 | "engines": { 23 | "node": ">=18" 24 | }, 25 | "scripts": { 26 | "test": "xo && ava" 27 | }, 28 | "files": [ 29 | "cli.js", 30 | "api.js", 31 | "api.d.ts" 32 | ], 33 | "keywords": [ 34 | "cli-app", 35 | "cli", 36 | "replace", 37 | "matching", 38 | "match", 39 | "matches", 40 | "find", 41 | "search", 42 | "string", 43 | "regex", 44 | "regexp", 45 | "pattern", 46 | "files", 47 | "file", 48 | "text", 49 | "contents" 50 | ], 51 | "dependencies": { 52 | "escape-string-regexp": "^5.0.0", 53 | "globby": "^14.0.1", 54 | "meow": "^13.2.0", 55 | "normalize-path": "^3.0.0", 56 | "write-file-atomic": "^5.0.1" 57 | }, 58 | "devDependencies": { 59 | "ava": "^6.1.3", 60 | "execa": "^9.3.0", 61 | "temp-write": "^5.0.0", 62 | "xo": "^0.58.0" 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # replace-in-files-cli 2 | 3 | > Replace matching strings and regexes in files 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install --global replace-in-files-cli 9 | ``` 10 | 11 | ## Usage 12 | 13 | ``` 14 | $ replace-in-files --help 15 | 16 | Usage 17 | $ replace-in-files 18 | 19 | Options 20 | --regex Regex pattern to find (Can be set multiple times) 21 | --string String to find (Can be set multiple times) 22 | --replacement Replacement string (Required) 23 | --ignore-case Search case-insensitively 24 | --no-glob Disable globbing 25 | 26 | Examples 27 | $ replace-in-files --string='horse' --regex='unicorn|rainbow' --replacement='🦄' foo.md 28 | $ replace-in-files --regex='v\d+\.\d+\.\d+' --replacement=v$npm_package_version foo.css 29 | $ replace-in-files --string='blob' --replacement='blog' 'some/**/[gb]lob/*' '!some/glob/foo' 30 | 31 | You can use the same replacement patterns as with `String#replace()`, like `$&`. 32 | ``` 33 | 34 | Real-world use-case: [Bumping version number in a file when publishing to npm](https://github.com/sindresorhus/modern-normalize/commit/c1d65e3f7daba2b695ccf837d2aef19d586d1ca6) 35 | 36 | The regex should be [JavaScript flavor](https://www.regular-expressions.info/javascript.html). 37 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import path from 'node:path'; 3 | import process from 'node:process'; 4 | import test from 'ava'; 5 | import {execa} from 'execa'; 6 | import tempWrite from 'temp-write'; 7 | 8 | test('--string', async t => { 9 | const filePath = await tempWrite('foo bar foo'); 10 | await execa('./cli.js', ['--string=bar', '--replacement=foo', filePath]); 11 | t.is(fs.readFileSync(filePath, 'utf8'), 'foo foo foo'); 12 | }); 13 | 14 | test('--regex', async t => { 15 | const filePath = await tempWrite('foo bar foo'); 16 | await execa('./cli.js', ['--regex=\\bb.*?\\b', '--replacement=foo', filePath]); 17 | t.is(fs.readFileSync(filePath, 'utf8'), 'foo foo foo'); 18 | }); 19 | 20 | test('newlines and tabs', async t => { 21 | const filePath = await tempWrite('a,b,c'); 22 | await execa('./cli.js', ['--string=,', '--replacement=\\n', filePath]); 23 | t.is(fs.readFileSync(filePath, 'utf8'), 'a\nb\nc'); 24 | 25 | const filePath2 = await tempWrite('a,b,c'); 26 | await execa('./cli.js', ['--string=,', '--replacement=\\t', filePath2]); 27 | t.is(fs.readFileSync(filePath2, 'utf8'), 'a\tb\tc'); 28 | 29 | const filePath3 = await tempWrite('a,b,c'); 30 | await execa('./cli.js', ['--string=,', '--replacement=\\r', filePath3]); 31 | t.is(fs.readFileSync(filePath3, 'utf8'), 'a\rb\rc'); 32 | }); 33 | 34 | test('multiple newlines and tabs', async t => { 35 | const filePath = await tempWrite('a,b,c'); 36 | await execa('./cli.js', ['--string=,', '--replacement=\\n\\n\\t\\r', filePath]); 37 | t.is(fs.readFileSync(filePath, 'utf8'), 'a\n\n\t\rb\n\n\t\rc'); 38 | }); 39 | 40 | test('globs', async t => { 41 | const filePaths = [await tempWrite('foo bar foo', 'a.glob'), await tempWrite('foo bar foo', 'b.glob')]; 42 | const dirnames = filePaths.map(filePath => path.dirname(filePath)); 43 | 44 | await execa('./cli.js', ['--string=bar', '--replacement=foo', path.join(dirnames[0], '*.glob'), path.join(dirnames[1], '*.glob')]); 45 | t.is(fs.readFileSync(filePaths[0], 'utf8'), 'foo foo foo'); 46 | t.is(fs.readFileSync(filePaths[1], 'utf8'), 'foo foo foo'); 47 | }); 48 | 49 | test('no globs', async t => { 50 | const filePaths = [await tempWrite('foo bar foo', process.platform === 'win32' ? 'STAR.glob' : '*.glob'), await tempWrite('foo bar foo', 'foo.glob')]; 51 | const dirnames = filePaths.map(filePath => path.dirname(filePath)); 52 | 53 | await t.throwsAsync( 54 | execa('./cli.js', [ 55 | '--string=bar', 56 | '--replacement=foo', 57 | '--no-glob', 58 | path.join(dirnames[0], '*.glob'), 59 | path.join(dirnames[1], '*.glob'), 60 | ]), 61 | { 62 | message: /ENOENT/, 63 | }, 64 | ); 65 | }); 66 | --------------------------------------------------------------------------------