Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Replace text in one or more files or globs.

License

NotificationsYou must be signed in to change notification settings

wj42ftns/replace-in-files

Repository files navigation

Replace text in one or more files or globs. Works asynchronously with promises.

npm versiondependencies StatusBuild StatusCoverage Status

Installation

# Using npmnpm install replace-in-files# Using yarnyarn add replace-in-files

Usage

Specify options

constreplaceInFiles=require('replace-in-files');constoptions={// See more: https://www.npmjs.com/package/globby// Single file or globfiles:'path/to/file',// Multiple files or globsfiles:['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// Replacementfrom:/foo/g,// string or regexto:'bar',// string or fn  (fn: carrying last argument - path to replaced file)// See more: https://www.npmjs.com/package/globoptionsForFiles:{// 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 filesencoding: 'utf8',// defaultshouldSkipBinaryFiles:true,// defaultonlyFindPathsWithoutReplace:false// defaultreturnPaths:true// defaultreturnCountOfMatchesByPaths:true// default};

Replacing multiple occurrences

Please note that the value specified in thefrom parameter is passed straight to the nativeString replace method. As such, if you pass a string as thefrom parameter, it willonly replace the first occurrence.

To replace multiple occurrences at once, you must use a regular expression for thefrom parameter with the global flag enabled, e.g./foo/g.

Asynchronous replacement with promises

constreplaceInFiles=require('replace-in-files');// ...replaceInFiles(options).then(({ changedFiles, countOfMatchesByPaths})=>{console.log('Modified files:',changedFiles);console.log('Count of matches by paths:',countOfMatchesByPaths);console.log('was called with:',options);}).catch(error=>{console.error('Error occurred:',error);});

Asynchronous replacement with co yield

constreplaceInFiles=require('replace-in-files');constco=require('co');// ...co(function*(){const{    changedFiles,    countOfMatchesByPaths,    replaceInFilesOptions}=yieldreplaceInFiles(options);console.log('Modified files:',changedFiles);console.log('Count of matches by paths:',countOfMatchesByPaths);console.log('was called with:',replaceInFilesOptions);}).catch((error)=>{console.log('Error occurred:',error);});

Asynchronous replacement with async await (node 8+)

constreplaceInFiles=require('replace-in-files');// ...asyncfunctionmain(){try{const{      changedFiles,      countOfMatchesByPaths,      replaceInFilesOptions}=awaitreplaceInFiles(options);console.log('Modified files:',changedFiles);console.log('Count of matches by paths:',countOfMatchesByPaths);console.log('was called with:',replaceInFilesOptions);}catch(error){console.log('Error occurred:',error);}}main();

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)

constreplaceInFiles=require('replace-in-files');// ...asyncfunctionmain(){try{const{      changedFiles,      countOfMatchesByPaths,      replaceInFilesOptions}=awaitreplaceInFiles(options).pipe({from:'foo',to:'bar'}).pipe({from:'first',to:'second'}).pipe({from:/const/g,to:()=>'var'});console.log('Modified files:',changedFiles);console.log('Count of matches by paths:',countOfMatchesByPaths);console.log('was called with:',replaceInFilesOptions);}catch(error){console.log('Error occurred:',error);}}main();

Return value

The return value of the library is an object with: countOfMatchesByPaths and paths

For example:

constreplaceInFiles=require('replace-in-files');constdata=replaceInFiles({files:'path/to/files/*.html',from:'a',to:'b',});// 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)

About

Replace text in one or more files or globs.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp