Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

replace-in-file

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replace-in-file - npm Package Compare versions

Comparing version 2.2.2 to 2.3.0

bin/cli.js

61

lib/replace-in-file.js

@@ -8,2 +8,3 @@ 'use strict';

const glob = require('glob');
const chalk = require('chalk');

@@ -23,20 +24,38 @@ /**

//Validate input
//Validate config
if (typeof config !== 'object' || config === null) {
throw new Error('Must specify configuration object');
}
//Backwards compatibilility
if (typeof config.replace !== 'undefined' &&
typeof config.from === 'undefined') {
console.log(
chalk.yellow('Option `replace` is deprecated. Use `from` instead.')
);
config.from = config.replace;
}
if (typeof config.with !== 'undefined' &&
typeof config.to === 'undefined') {
console.log(
chalk.yellow('Option `with` is deprecated. Use `to` instead.')
);
config.to = config.with;
}
//Validate values
if (typeof config.files === 'undefined') {
throw new Error('Must specify file or files');
}
if (typeof config.replace === 'undefined') {
if (typeof config.from === 'undefined') {
throw new Error('Must specify string or regex to replace');
}
if (typeof config.with === 'undefined') {
if (typeof config.to === 'undefined') {
throw new Error('Must specify a replacement (can be blank string)');
}
//Use different naming internally as we can't use `with` as a variable name
config.find = config.replace;
config.replace = config.with;
delete config.with;
//Use default encoding if invalid
if (typeof config.encoding !== 'string' || config.encoding === '') {
config.encoding = 'utf-8';
}

@@ -63,17 +82,17 @@ //Merge config with defaults

*/
function makeReplacements(contents, find, replace) {
function makeReplacements(contents, from, to) {
//Turn into array
if (!Array.isArray(find)) {
find = [find];
if (!Array.isArray(from)) {
from = [from];
}
//Check if replace value is an array
const isReplaceArray = Array.isArray(replace);
const isArray = Array.isArray(to);
//Make replacements
find.forEach((item, i) => {
from.forEach((item, i) => {
//Get replacement value
const replacement = getReplacement(replace, isReplaceArray, i);
const replacement = getReplacement(to, isArray, i);
if (replacement === null) {

@@ -94,3 +113,3 @@ return;

*/
function replaceSync(file, find, replace, enc) {
function replaceSync(file, from, to, enc) {

@@ -101,3 +120,3 @@ //Read contents

//Replace contents and check if anything changed
const newContents = makeReplacements(contents, find, replace);
const newContents = makeReplacements(contents, from, to);
if (newContents === contents) {

@@ -115,3 +134,3 @@ return false;

*/
function replaceAsync(file, find, replace, enc) {
function replaceAsync(file, from, to, enc) {
return new Promise((resolve, reject) => {

@@ -125,3 +144,3 @@ fs.readFile(file, enc, (error, contents) => {

//Replace contents and check if anything changed
let newContents = makeReplacements(contents, find, replace);
let newContents = makeReplacements(contents, from, to);
if (newContents === contents) {

@@ -183,3 +202,3 @@ return resolve({file, hasChanged: false});

//Get config and globs
const {files, find, replace, allowEmptyPaths, encoding} = config;
const {files, from, to, allowEmptyPaths, encoding} = config;
const globs = Array.isArray(files) ? files : [files];

@@ -196,3 +215,3 @@

.then(files => Promise.all(files.map(file => {
return replaceAsync(file, find, replace, encoding);
return replaceAsync(file, from, to, encoding);
})))

@@ -235,3 +254,3 @@

//Get config and globs
const {files, find, replace, encoding} = config;
const {files, from, to, encoding} = config;
const globs = Array.isArray(files) ? files : [files];

@@ -245,3 +264,3 @@ const changedFiles = [];

.forEach(file => {
if (replaceSync(file, find, replace, encoding)) {
if (replaceSync(file, from, to, encoding)) {
changedFiles.push(file);

@@ -248,0 +267,0 @@ }

{
"name": "replace-in-file",
"version": "2.2.2",
"version": "2.3.0",
"description": "A simple utility to quickly replace text in one or more files.",

@@ -26,5 +26,6 @@ "homepage": "https://github.com/adamreisnz/replace-in-file#readme",

"main": "index.js",
"bin": "./bin/cli.js",
"scripts": {
"lint": "eslint . --fix",
"istanbul": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha test/**/*.spec.js",
"istanbul": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha lib/**/*.spec.js",
"test": "npm run istanbul -s",

@@ -41,3 +42,3 @@ "postversion": "git push && git push --tags && npm publish",

"dirty-chai": "^1.2.2",
"eslint": "^3.14.0",
"eslint": "^3.15.0",
"istanbul": "^1.0.0-alpha.2",

@@ -47,3 +48,5 @@ "mocha": "^3.2.0"

"dependencies": {
"glob": "^7.1.1"
"chalk": "^1.1.3",
"glob": "^7.1.1",
"yargs": "^6.6.0"
},

@@ -50,0 +53,0 @@ "browserify": {

@@ -40,12 +40,12 @@ # Replace in file

//Replacement to make (string or regex)
replace: /foo/g,
with: 'bar',
from: /foo/g,
to: 'bar',
//Multiple replacements with the same string (replaced sequentially)
replace: [/foo/g, /baz/g],
with: 'bar',
from: [/foo/g, /baz/g],
to: 'bar',
//Multiple replacements with different strings (replaced sequentially)
replace: [/foo/g, /baz/g],
with: ['bar', 'bax'],
from: [/foo/g, /baz/g],
to: ['bar', 'bax'],

@@ -96,2 +96,11 @@ //Specify if empty/invalid file paths are allowed (defaults to false)

Via CLI:
```sh
replace-in-file from to some/file.js,some/**/glob.js
```
The options `allowEmptyPaths` and `encoding` are supported in the CLI as well.
In addition, the CLI supports the `verbose` option to list the changed files.
## License

@@ -98,0 +107,0 @@ (MIT License)

'use strict';
//Load dependencies
let Promise = require('bluebird');
let chai = require('chai');
let dirtyChai = require('dirty-chai');
let chaiAsPromised = require('chai-as-promised');
const Promise = require('bluebird');
const chai = require('chai');
const dirtyChai = require('dirty-chai');
const chaiAsPromised = require('chai-as-promised');

@@ -9,0 +9,0 @@ //Enable should assertion style for usage with chai-as-promised

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc