Giter Site home page Giter Site logo

npx-bin / file-content-replacer Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 30 KB

A file content replacer utility. Replaces contents within files, recursively within a directory.

Home Page: https://www.npmjs.com/package/file-content-replacer

License: MIT License

JavaScript 100.00%
replace recursive directory folder files file content

file-content-replacer's Introduction

file-content-replacer

A file content replacer utility. Replaces contents within files, recursively within a directory.


Installation:

npm i file-content-replacer

Usage:

var fcr = require("file-content-replacer");
async function myFunc() {
    var result = await fcr(directoryPath, fileMatcher, replace, replaceWith, options).catch((err) => {
        console.log(err);
    });
    result && console.log(result.message);
    result && console.log(result.data);
}
myFunc();

/**
* 
* @param {*} directoryPath [type: string] - Path of the directory.
* @param {*} fileMatcher [type: function] - Predicate function for matching files.
* @param {*} replace [type: string|RegExp] - The substring to be replaced.
* @param {*} replaceWith [type: string|function] - The replacement which will replace the substring
* @param {?} options [type: Object] - Configuration object. This argument is OPTIONAL.
*
* return: Promise - The resolved object contains properties 'message' & 'data'.
*/

For the usage of replace and replaceWith parameters, the behavior is the same as String.prototype.replace:
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)

RegExp:
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)


A complex example:

The below example describes a scenario wherein we want to update all the ".css" files in the current directory(recursive) and replace the text "/ASSETS/" with "assets/".

async function myFunc() {
    var result = await fcr(".", function (filepath) { return filepath.endsWith(".css"); }, /\/ASSETS\//gi, function (match, offset, string) {
        return match.toLowerCase().substr(1);
    }).catch((err) => {
        console.log(err);
    });
    result && console.log(result.message);
    result && console.log(result.data);
}
myFunc();
  • You can provide a directoryPath (i.e. first argument)
  • You can control the files to be matched via a fileMatcher (i.e. second argument) predicate function.
    Note: If you want to lookup file(s) only at a specific directory, then use the filepath argument and return true with the appropriate comparison.
    The predicate function gives you the flexibility to determine which exact file(s) should be matched.
  • Provide a string or RegExp as the replace (i.e. third argument) as in this case i.e. /\/ASSETS\//gi --> match global, case-insensitive occurences of the substring "/ASSETS/"
  • Provide a string or function as the replaceWith (i.e. fourth argument)
  • Additionally you can also provide a fifth argument options of type Object.
    The valid properties in the options object are:
    timeoutMillis - specify the timeout in milliseconds
    e.g.: fcr(directoryPath, fileMatcher, replace, replaceWith, {timeoutMillis: 5000});
    This would mean that if the replacement operation exceeds 5 seconds, then it will timeout.
    The default value for timeoutMillis is 20000 i.e. 20 seconds and will be used if the options argument is missing or options.timeoutMillis is not specified.

A simple example:

The below example describes a scenario wherein we want to update all the ".css" files in the current directory(recursive) and replace the text "/assets/" with "assets/".

async function myFunc() {
    var result = await fcr(".", function (filepath) { return filepath.endsWith(".css"); }, "/assets/", "assets/").catch((err) => {
        console.log(err);
    });
    result && console.log(result.message);
    result && console.log(result.data);
}
myFunc();
  • In the above simple example, we will replace the substring "/assets/" with "assets/" in all the ".css" files in the current directory(recursive).
    Note: Since we provided the third argument as a string and not a RegExp, it would only replace the first matching occurence in each file.
    The purpose of this example is just to demonstrate the flexibility offered by the library.

file-content-replacer's People

Contributors

kcak11 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.