CLI functionality

This commit is contained in:
Adam Reis
2017-02-13 18:29:46 +13:00
parent 59f5b17621
commit db4b760d9e
7 changed files with 459 additions and 153 deletions

View File

@@ -5,6 +5,7 @@
*/
const fs = require('fs');
const glob = require('glob');
const chalk = require('chalk');
/**
* Defaults
@@ -19,24 +20,42 @@ const defaults = {
*/
function parseConfig(config) {
//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';
}
//Merge config with defaults
return Object.assign({}, defaults, config);
@@ -58,21 +77,21 @@ function getReplacement(replace, isArray, i) {
/**
* Helper to make replacements
*/
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) {
return;
}
@@ -88,13 +107,13 @@ function makeReplacements(contents, find, replace) {
/**
* Helper to replace in a single file (sync)
*/
function replaceSync(file, find, replace, enc) {
function replaceSync(file, from, to, enc) {
//Read contents
const contents = fs.readFileSync(file, enc);
//Replace contents and check if anything changed
const newContents = makeReplacements(contents, find, replace);
const newContents = makeReplacements(contents, from, to);
if (newContents === contents) {
return false;
}
@@ -107,7 +126,7 @@ function replaceSync(file, find, replace, enc) {
/**
* Helper to replace in a single file (async)
*/
function replaceAsync(file, find, replace, enc) {
function replaceAsync(file, from, to, enc) {
return new Promise((resolve, reject) => {
fs.readFile(file, enc, (error, contents) => {
//istanbul ignore if
@@ -116,7 +135,7 @@ function replaceAsync(file, find, replace, enc) {
}
//Replace contents and check if anything changed
let newContents = makeReplacements(contents, find, replace);
let newContents = makeReplacements(contents, from, to);
if (newContents === contents) {
return resolve({file, hasChanged: false});
}
@@ -173,7 +192,7 @@ function replaceInFile(config, cb) {
}
//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];
//Find files
@@ -185,7 +204,7 @@ function replaceInFile(config, cb) {
//Make replacements
.then(files => Promise.all(files.map(file => {
return replaceAsync(file, find, replace, encoding);
return replaceAsync(file, from, to, encoding);
})))
//Convert results to array of changed files
@@ -223,7 +242,7 @@ replaceInFile.sync = function(config) {
config = parseConfig(config);
//Get config and globs
const {files, find, replace, encoding} = config;
const {files, from, to, encoding} = config;
const globs = Array.isArray(files) ? files : [files];
const changedFiles = [];
@@ -232,7 +251,7 @@ replaceInFile.sync = function(config) {
glob
.sync(pattern, {nodir: true})
.forEach(file => {
if (replaceSync(file, find, replace, encoding)) {
if (replaceSync(file, from, to, encoding)) {
changedFiles.push(file);
}
});

672
lib/replace-in-file.spec.js Normal file
View File

@@ -0,0 +1,672 @@
'use strict';
/**
* Dependencies
*/
const replace = require('../lib/replace-in-file');
const fs = require('fs');
const writeFile = Promise.promisify(fs.writeFile);
const deleteFile = Promise.promisify(fs.unlink);
/**
* Specifications
*/
describe('Replace in file', () => {
//Test JSON
const testData = 'a re place c';
/**
* Prepare test files
*/
beforeEach(() => Promise.all([
writeFile('test1', testData, 'utf8'),
writeFile('test2', testData, 'utf8'),
writeFile('test3', 'nope', 'utf8'),
]));
/**
* Clean up test files
*/
afterEach(() => Promise.all([
deleteFile('test1'),
deleteFile('test2'),
deleteFile('test3'),
]));
/**
* Async with promises
*/
describe('Async with promises', () => {
it('should throw an error when no config provided', () => {
return expect(replace()).to.eventually.be.rejectedWith(Error);
});
it('should throw an error when invalid config provided', () => {
return expect(replace(42)).to.eventually.be.rejectedWith(Error);
});
it('should throw an error when no `files` defined', () => {
return expect(replace({
from: /re\splace/g,
to: 'b',
})).to.eventually.be.rejectedWith(Error);
});
it('should throw an error when no `from` defined', () => {
return expect(replace({
files: 'test1',
to: 'b',
})).to.eventually.be.rejectedWith(Error);
});
it('should throw an error when no `to` defined', () => {
return expect(replace({
files: 'test1',
from: /re\splace/g,
})).to.eventually.be.rejectedWith(Error);
});
it('should replace contents in a single file with regex', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal(testData);
done();
});
});
it('should replace contents with a string replacement', done => {
replace({
files: 'test1',
from: 're place',
to: 'b',
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
expect(test1).to.equal('a b c');
done();
});
});
it('should replace contents in a an array of files', done => {
replace({
files: ['test1', 'test2'],
from: /re\splace/g,
to: 'b',
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
done();
});
});
it('should expand globs', done => {
replace({
files: 'test*',
from: /re\splace/g,
to: 'b',
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
done();
});
});
it('should fulfill the promise on success', () => {
return replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}).should.be.fulfilled;
});
it('should reject the promise with an error on failure', () => {
return expect(replace({
files: 'nope',
from: /re\splace/g,
to: 'b',
})).to.eventually.be.rejectedWith(Error);
});
it('should not reject the promise if allowEmptyPaths is true', () => {
return replace({
files: 'nope',
allowEmptyPaths: true,
from: /re\splace/g,
to: 'b',
}).should.be.fulfilled;
});
it('should return a changed files array', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}).then(changedFiles => {
expect(changedFiles).to.be.instanceof(Array);
done();
});
});
it('should return in files if something was replaced', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}).then(changedFiles => {
expect(changedFiles).to.have.length(1);
expect(changedFiles[0]).to.equal('test1');
done();
});
});
it('should not return in files if nothing replaced', done => {
replace({
files: 'test1',
from: 'nope',
to: 'b',
}).then(changedFiles => {
expect(changedFiles).to.have.length(0);
done();
});
});
it('should return changed files for multiple files', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: /re\splace/g,
to: 'b',
}).then(changedFiles => {
expect(changedFiles).to.have.length(2);
expect(changedFiles).to.contain('test1');
expect(changedFiles).to.contain('test2');
done();
});
});
it('should make multiple replacements with the same string', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: 'b',
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b b c');
expect(test2).to.equal('a b b c');
done();
});
});
it('should make multiple replacements with different strings', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: ['b', 'e'],
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b e c');
expect(test2).to.equal('a b e c');
done();
});
});
it('should not replace with missing replacement values', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: ['b'],
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b place c');
expect(test2).to.equal('a b place c');
done();
});
});
});
/**
* Async with callback
*/
describe('Async with callback', () => {
it('should throw an error when no config provided', done => {
replace(null, (error) => {
expect(error).to.be.instanceof(Error);
done();
});
});
it('should throw an error when invalid config provided', done => {
replace(42, (error) => {
expect(error).to.be.instanceof(Error);
done();
});
});
it('should throw an error when no `files` defined', done => {
replace({
from: /re\splace/g,
to: 'b',
}, (error) => {
expect(error).to.be.instanceof(Error);
done();
});
});
it('should throw an error when no `from` defined', done => {
replace({
files: 'test1',
to: 'b',
}, (error) => {
expect(error).to.be.instanceof(Error);
done();
});
});
it('should throw an error when no `to` defined', done => {
replace({
files: 'test1',
from: /re\splace/g,
}, (error) => {
expect(error).to.be.instanceof(Error);
done();
});
});
it('should replace contents in a single file with regex', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal(testData);
done();
});
});
it('should replace contents with a string replacement', done => {
replace({
files: 'test1',
from: 're place',
to: 'b',
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
expect(test1).to.equal('a b c');
done();
});
});
it('should replace contents in a an array of files', done => {
replace({
files: ['test1', 'test2'],
from: /re\splace/g,
to: 'b',
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
done();
});
});
it('should expand globs', done => {
replace({
files: 'test*',
from: /re\splace/g,
to: 'b',
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
done();
});
});
it('should not return an error on success', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}, (error) => {
expect(error).to.equal(null);
done();
});
});
it('should return an error on failure', done => {
replace({
files: 'nope',
from: /re\splace/g,
to: 'b',
}, (error) => {
expect(error).to.be.instanceof(Error);
done();
});
});
it('should not return an error if allowEmptyPaths is true', done => {
replace({
files: 'nope',
allowEmptyPaths: true,
from: /re\splace/g,
to: 'b',
}, (error) => {
expect(error).to.equal(null);
done();
});
});
it('should return a changed files array', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}, (error, changedFiles) => {
expect(changedFiles).to.be.instanceof(Array);
done();
});
});
it('should return in files if something was replaced', done => {
replace({
files: 'test1',
from: /re\splace/g,
to: 'b',
}, (error, changedFiles) => {
expect(changedFiles).to.have.length(1);
expect(changedFiles[0]).to.equal('test1');
done();
});
});
it('should not return in files if nothing replaced', done => {
replace({
files: 'test1',
from: 'nope',
to: 'b',
}, (error, changedFiles) => {
expect(changedFiles).to.have.length(0);
done();
});
});
it('should return changed files for multiple files', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: /re\splace/g,
to: 'b',
}, (error, changedFiles) => {
expect(changedFiles).to.have.length(2);
expect(changedFiles).to.contain('test1');
expect(changedFiles).to.contain('test2');
done();
});
});
it('should make multiple replacements with the same string', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: 'b',
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b b c');
expect(test2).to.equal('a b b c');
done();
});
});
it('should make multiple replacements with different strings', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: ['b', 'e'],
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b e c');
expect(test2).to.equal('a b e c');
done();
});
});
it('should not replace with missing replacement values', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: ['b'],
}, () => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b place c');
expect(test2).to.equal('a b place c');
done();
});
});
});
/**
* Sync
*/
describe('Sync', () => {
it('should throw an error when no config provided', () => {
expect(function() {
replace.sync();
}).to.throw(Error);
});
it('should throw an error when invalid config provided', () => {
expect(function() {
replace.sync(42);
}).to.throw(Error);
});
it('should throw an error when no `files` defined', () => {
expect(function() {
replace.sync({
from: /re\splace/g,
to: 'b',
});
}).to.throw(Error);
});
it('should throw an error when no `from` defined', () => {
expect(function() {
replace.sync({
files: 'test1',
to: 'b',
});
}).to.throw(Error);
});
it('should throw an error when no `to` defined', () => {
expect(function() {
replace.sync({
files: 'test1',
from: /re\splace/g,
});
}).to.throw(Error);
});
it('should support `replace` and `with` params for now', () => {
expect(function() {
replace.sync({
files: 'test1',
replace: /re\splace/g,
with: 'b',
});
}).to.not.throw(Error);
});
it('should support the encoding parameter', () => {
expect(function() {
replace.sync({
files: 'test1',
from: /re\splace/g,
to: 'b',
encoding: 'utf-8',
});
}).to.not.throw(Error);
});
it('should fall back to utf-8 encoding with invalid configuration', () => {
expect(function() {
replace.sync({
files: 'test1',
from: /re\splace/g,
to: 'b',
encoding: '',
});
}).to.not.throw(Error);
expect(function() {
replace.sync({
files: 'test1',
from: /re\splace/g,
to: 'b',
encoding: null,
});
}).to.not.throw(Error);
});
it('should replace contents in a single file with regex', function() {
replace.sync({
files: 'test1',
from: /re\splace/g,
to: 'b',
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal(testData);
});
it('should replace contents with a string replacement', function() {
replace.sync({
files: 'test1',
from: 're place',
to: 'b',
});
const test1 = fs.readFileSync('test1', 'utf8');
expect(test1).to.equal('a b c');
});
it('should replace contents in a an array of files', function() {
replace.sync({
files: ['test1', 'test2'],
from: /re\splace/g,
to: 'b',
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
});
it('should expand globs', function() {
replace.sync({
files: 'test*',
from: /re\splace/g,
to: 'b',
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
});
it('should return a changed files array', function() {
const changedFiles = replace.sync({
files: 'test1',
from: /re\splace/g,
to: 'b',
});
expect(changedFiles).to.be.instanceof(Array);
});
it('should return in changed files if something was replaced', function() {
const changedFiles = replace.sync({
files: 'test1',
from: /re\splace/g,
to: 'b',
});
expect(changedFiles).to.have.length(1);
expect(changedFiles[0]).to.equal('test1');
});
it('should not return in changed files if nothing replaced', function() {
const changedFiles = replace.sync({
files: 'test1',
from: 'nope',
to: 'b',
});
expect(changedFiles).to.have.length(0);
});
it('should return changed files for multiple files', function() {
const changedFiles = replace.sync({
files: ['test1', 'test2', 'test3'],
from: /re\splace/g,
to: 'b',
});
expect(changedFiles).to.have.length(2);
expect(changedFiles).to.contain('test1');
expect(changedFiles).to.contain('test2');
});
it('should make multiple replacements with the same string', () => {
replace.sync({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: 'b',
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b b c');
expect(test2).to.equal('a b b c');
});
it('should make multiple replacements with different strings', () => {
replace.sync({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: ['b', 'e'],
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b e c');
expect(test2).to.equal('a b e c');
});
it('should not replace with missing replacement values', () => {
replace.sync({
files: ['test1', 'test2', 'test3'],
from: [/re/g, /place/g],
to: ['b'],
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a b place c');
expect(test2).to.equal('a b place c');
});
});
});