replace-in-files
Replace text in one or more files or globs. Works asynchronously with promises.
Installation
# Using npm npm install replace-in-files # Using yarn yarn add replace-in-files
Usage
Specify options
const replaceInFiles = ; const options = // See more: https://www.npmjs.com/package/globby // Single file or glob files: 'path/to/file' // Multiple files or globs files: 'path/to/file' 'path/to/other/file' 'path/to/files/*.html' 'another/**/*.path' // See more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace // Replacement from: /foo/g // string or regex to: 'bar' // string or fn (fn: carrying last argument - path to replaced file) // See more: https://www.npmjs.com/package/glob optionsForFiles: // default "ignore": "**/node_modules/**" // format: `${fileName}-${year}-${month}-${day}_${hour}:${minute}:${second}.{fileExtension}` // fileName-2017-11-01_21:29:55.js // date of createFile old file or last modificate (if not find create date) saveOldFile: false // default //Character encoding for reading/writing files encoding: 'utf8' // default shouldSkipBinaryFiles: true // default onlyFindPathsWithoutReplace: false // default returnPaths: true // default returnCountOfMatchesByPaths: true // default;
Replacing multiple occurrences
Please note that the value specified in the from
parameter is passed straight to the native String replace method. As such, if you pass a string as the from
parameter, it will only replace the first occurrence.
To replace multiple occurrences at once, you must use a regular expression for the from
parameter with the global flag enabled, e.g. /foo/g
.
Asynchronous replacement with promises
const replaceInFiles = ; // ... ;
Asynchronous replacement with co yield
const replaceInFiles = ;const co = ; // ... ;
Asynchronous replacement with async await (node 8+)
const replaceInFiles = ; // ... { try const changedFiles countOfMatchesByPaths replaceInFilesOptions = await ; console; console; console; } catch error console; } ;
Sequentially replacement
use .pipe - will be replaced with only files found at first replacement
.pipe supported only: { from, to } (the other options will be received from options in the first replacement)
const replaceInFiles = ; // ... { try const changedFiles countOfMatchesByPaths replaceInFilesOptions = await ; console; console; console; } catch error console; } ;
Return value
The return value of the library is an object with: countOfMatchesByPaths and paths
For example:
const replaceInFiles = ; const data = ; // data could like: countOfMatchesByPaths: 'path/to/files/file1.html': 5 'path/to/files/file3.html': 1 'path/to/files/file5.html': 3 paths: 'path/to/files/file1.html' 'path/to/files/file3.html' 'path/to/files/file5.html' replaceInFilesOptions: files: 'path/to/files/*.html' from: 'a' to: 'b' // if empty: countOfMatchesByPaths: {} paths: // if used 2 .pipe countOfMatchesByPaths: 'path/to/files/file1.html': 5 'path/to/files/file3.html': 1 'path/to/files/file5.html': 3 'path/to/files/file5.html': 4 'path/to/files/file1.html': 2 'path/to/files/file5.html': 4 paths: 'path/to/files/file1.html' 'path/to/files/file3.html' 'path/to/files/file5.html' replaceInFilesOptions: files: 'path/to/files/*.html' from: 'a' to: 'b' from: 'c' to: 'd' from: 'e' to: 'f'
Version information
Replace in files requires Node 12 or higher. (v.3.0.0 +) potentially still supported earlier node, but in pipeline eslint required node >=12 Replace in files requires Node 8 or higher. (v.2.0.3) - potentially still supported node 6, but in pipeline eslint required node 8 Replace in files requires Node 6 or higher. (v.1.1.4)
License
(MIT License)