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 3.3.0 to 3.4.0

5

lib/helpers/get-paths-async.js

@@ -12,3 +12,3 @@ 'use strict';

module.exports = function getPathsAsync(
patterns, ignore, disableGlobs, allowEmptyPaths
patterns, ignore, disableGlobs, allowEmptyPaths, cfg
) {

@@ -23,4 +23,5 @@

return Promise
.all(patterns.map(pattern => globAsync(pattern, ignore, allowEmptyPaths)))
.all(patterns
.map(pattern => globAsync(pattern, ignore, allowEmptyPaths, cfg)))
.then(paths => [].concat.apply([], paths));
};

7

lib/helpers/get-paths-sync.js

@@ -11,3 +11,3 @@ 'use strict';

*/
module.exports = function getPathsSync(patterns, ignore, disableGlobs) {
module.exports = function getPathsSync(patterns, ignore, disableGlobs, cfg) {

@@ -19,5 +19,8 @@ //Not using globs?

//Prepare glob config
cfg = Object.assign({ignore}, cfg, {nodir: true});
//Get paths
const paths = patterns
.map(pattern => glob.sync(pattern, {ignore, nodir: true}));
.map(pattern => glob.sync(pattern, cfg));

@@ -24,0 +27,0 @@ //Return flattened

@@ -11,5 +11,10 @@ 'use strict';

*/
module.exports = function globAsync(pattern, ignore, allowEmptyPaths) {
module.exports = function globAsync(pattern, ignore, allowEmptyPaths, cfg) {
//Prepare glob config
cfg = Object.assign({ignore}, cfg, {nodir: true});
//Return promise
return new Promise((resolve, reject) => {
glob(pattern, {ignore, nodir: true}, (error, files) => {
glob(pattern, cfg, (error, files) => {

@@ -16,0 +21,0 @@ //istanbul ignore if: hard to make glob error

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

dry: false,
glob: {},
};

@@ -27,4 +28,7 @@

//Fix glob
config.glob = config.glob || {};
//Extract data
const {files, from, to, ignore, encoding} = config;
const {files, from, to, ignore, encoding, glob} = config;

@@ -41,2 +45,5 @@ //Validate values

}
if (typeof glob !== 'object') {
throw new Error('Invalid glob config');
}

@@ -43,0 +50,0 @@ //Ensure arrays

@@ -32,6 +32,7 @@ 'use strict';

files, from, to, encoding, ignore, allowEmptyPaths, disableGlobs,
dry, verbose,
dry, verbose, glob,
} = config;
//Dry run?
//istanbul ignore if: No need to test console logs
if (dry && verbose) {

@@ -42,3 +43,3 @@ console.log(chalk.yellow('Dry run, not making actual changes'));

//Find paths
return getPathsAsync(files, ignore, disableGlobs, allowEmptyPaths)
return getPathsAsync(files, ignore, disableGlobs, allowEmptyPaths, glob)

@@ -86,8 +87,9 @@ //Make replacements

const {
files, from, to, encoding, ignore, disableGlobs, dry, verbose,
files, from, to, encoding, ignore, disableGlobs, dry, verbose, glob,
} = config;
const paths = getPathsSync(files, ignore, disableGlobs);
const paths = getPathsSync(files, ignore, disableGlobs, glob);
const changedFiles = [];
//Dry run?
//istanbul ignore if: No need to test console logs
if (dry && verbose) {

@@ -94,0 +96,0 @@ console.log(chalk.yellow('Dry run, not making any changes'));

@@ -71,2 +71,11 @@ 'use strict';

it('should throw an error when invalid `glob` config defined', () => {
return expect(replace({
files: 'test1',
from: /re\splace/g,
to: 'test',
glob: 'invalid',
})).to.eventually.be.rejectedWith(Error);
});
it('should replace contents in a single file with regex', done => {

@@ -93,3 +102,3 @@ replace({

},
to: 'b'
to: 'b',
}).then(() => {

@@ -349,2 +358,31 @@ const test1 = fs.readFileSync('test1', 'utf8');

});
it('should accept glob configuration', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
allowEmptyPaths: true,
glob: {
ignore: ['test1'],
},
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
expect(test1).to.equal('a re place c');
done();
});
});
it('should ignore empty glob configuration', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
glob: null,
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
expect(test1).to.equal('a b c');
done();
});
});
});

@@ -422,3 +460,3 @@

},
to: 'b'
to: 'b',
}, () => {

@@ -756,3 +794,3 @@ const test1 = fs.readFileSync('test1', 'utf8');

},
to: 'b'
to: 'b',
});

@@ -759,0 +797,0 @@ const test1 = fs.readFileSync('test1', 'utf8');

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

@@ -34,3 +34,3 @@ "homepage": "https://github.com/adamreisnz/replace-in-file#readme",

"dependencies": {
"chalk": "^2.3.0",
"chalk": "^2.3.2",
"glob": "^7.1.2",

@@ -47,3 +47,3 @@ "yargs": "^11.0.0"

"istanbul": "^1.0.0-alpha.2",
"mocha": "^5.0.0"
"mocha": "^5.0.4"
},

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

@@ -32,2 +32,3 @@ # Replace in file

- [Disable globs](#disable-globs)
- [Specify glob configuration](#glob-configuration)
- [Specify character encoding](#specify-character-encoding)

@@ -267,2 +268,15 @@ - [Dry run](#dry-run)

### Specify glob configuration
Specify configuration passed to the `glob` call:
```js
const options = {
glob: {
//Glob settings here
},
};
```
Please note that the setting `nodir` will always be passed as `false`.
### Specify character encoding

@@ -269,0 +283,0 @@ Use a different character encoding for reading/writing files. Defaults to `utf-8`.

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