🚀 Socket Launch Week 🚀 Day 3: Socket Acquires Coana.Learn More
Socket
Sign inDemoInstall
Socket

tiny-replace-files

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-replace-files

tiny replace string/regex in files.

1.0.2
latest
Source
npm
Version published
Weekly downloads
282
-54.15%
Maintainers
1
Weekly downloads
 
Created
Source

tiny-replace-files


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

Travis (.org) npm bundle size npm GitHub issues GitHub stars GitHub 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'

const options = {
  files: 'src/targets/index.js',
  from: 'test-plugin',
  to: 'self-name',
}

# await
const result = replaceStringInFilesSync(options)
console.info(result)
/**
[
  {
    file: './ques2.md',
    changed: true,
    matchCounts: 1,
    replaceCounts: 1
  }
]
*/

Async

import replaceStringInFiles from 'tiny-replace-files'

const options = {
  files: 'src/targets/index.js',
  from: 'test-plugin',
  to: 'self-name',
}

# await
const result = await replaceStringInFiles(options)
console.info(result)

# promise
replaceStringInFiles(options).then((res)=>{
    console.info(res)
}).catch(err=>{
    console.info(err)
})

Advanced usage

Replace a single file or glob

const options = {
  files: 'file',
};

Replace multiple files or globs

const options = {
  files: [
    'file1',
    'file2',
    'file3',
  ],
};

const options = {
  files: [
    'file1',
    'file2/**',
  ]
};

Replace by regex

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

from callback

const options = {
  files: 'file',
  from: (file) => new RegExp(file, 'g'),
  to: 'bar',
};

to callback

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

Ignore file(s) or glob

const options = {
  ignore: './ignored/file',
};

const options = {
  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.

const options = {
  disableGlobs: true,
};

glob configuration

API passed to the fast-glob:

const options = {
  glob: {
    //Glob settings here
    dot: 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.

const options = {
  encoding: 'utf8',
};

freeze Run

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

const options = {
  freeze: true,
};

⚙️ Changelog

See CHANGELOG.

LICENSE

MIT

✈️ TODO

  • init lib
  • 完成 replaceStringInFiles 函数开发
  • 完成同步函数开发
  • 完成测试
  • 生命周期??
  • cli???

Keywords

js-npm-template

FAQs

Package last updated on 21 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts