Skip to content

Repository files navigation

tiny-replace-files


Like vscode, simple utility to quickly replace text in one or more files.

Travis (.org)npm bundle sizenpmGitHub issuesGitHub starsGitHub license

📦 Getting Started

install

# npm 
npm install --save tiny-replace-files fast-glob
# yarn
yarn add tiny-replace-files fast-glob
# pnpm
pnpm install --save tiny-replace-files fast-glob

usage

Sync

import{replaceStringInFilesSync}from'tiny-replace-files'constoptions={files: 'src/targets/index.js',from: 'test-plugin',to: 'self-name',}
# awaitconstresult=replaceStringInFilesSync(options)console.info(result)/**[ { file: './ques2.md', changed: true, matchCounts: 1, replaceCounts: 1 }]*/

Async

importreplaceStringInFilesfrom'tiny-replace-files'constoptions={files: 'src/targets/index.js',from: 'test-plugin',to: 'self-name',}
# awaitconstresult=awaitreplaceStringInFiles(options)console.info(result)
# promisereplaceStringInFiles(options).then((res)=>{console.info(res)}).catch(err=>{console.info(err)})

Advanced usage

Replace a single file or glob

constoptions={files: 'file',};

Replace multiple files or globs

constoptions={files: ['file1','file2','file3',],};constoptions={files: ['file1','file2/**',]};

Replace by regex

constoptions={from: /foo/g,to: 'bar',};

from callback

constoptions={files: 'file',from: (file)=>newRegExp(file,'g'),to: 'bar',};

to callback

constoptions={files: './ques2.md',from: 'quest 2',to: (match: string)=>match.toUpperCase(),countMatches: true}

Ignore file(s) or glob

constoptions={ignore: './ignored/file',};constoptions={ignore: ['path/**','path2/index.html',],};

Disable globs

do not use fast-glob to get path. if you config this, you can uninstall fast-glob for reduce pkg size.

constoptions={disableGlobs: true,};

glob configuration

API passed to the fast-glob:

constoptions={glob: {//Glob settings heredot: true,//E.g. to include file names starting with a dot},};

Character encoding

Use a different character encoding for reading/writing files. Defaults to utf-8.

constoptions={encoding: 'utf8',};

freeze Run

freeze mode will do not replace & change, just run the process.

constoptions={freeze: true,};

⚙️ Changelog

See CHANGELOG.

LICENSE

MIT

✈️ TODO

  • init lib
  • support replaceStringInFiles
  • support replaceStringInFilesSync
  • support test

About

🎉🎉🎉like vcsode, string/texts can be replaced in file(s).

Topics

Resources

Contributing

Stars

18 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages