replace-in-file
Advanced tools
Comparing version 2.2.2 to 2.3.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
126091
16
888
108
3
1
+ Addedchalk@^1.1.3
+ Addedyargs@^6.6.0
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedcamelcase@3.0.0(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcliui@3.2.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-caller-file@1.0.3(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedinvert-kv@1.0.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.16.1(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedlcid@1.0.0(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedos-locale@1.4.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@1.0.1(transitive)
+ Addedresolve@1.22.10(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
+ Addedwhich-module@1.0.0(transitive)
+ Addedwrap-ansi@2.1.0(transitive)
+ Addedy18n@3.2.2(transitive)
+ Addedyargs@6.6.0(transitive)
+ Addedyargs-parser@4.2.1(transitive)