Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
Promisify options.delFn, update tested versions, use consistent formatting, and make use of const/let.
  • Loading branch information
jakezatecky committed Jan 22, 2019
1 parent 3e6352a commit c58cf53
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 78 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- '7'
- '6'
- '4'
- '11'
- '10'
- '8'
- '6'
95 changes: 54 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
'use strict';

var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var del = require('del');
var through = require('through2');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const del = require('del');
const through = require('through2');

function revDel(options, cb) {
if (!_.isObject(options)) {
options = { oldManifest: options };
}

// Useful when testing
options.delFn = options.delFn || del;
const delFn = options.delFn ? (files, deleteOptions) => {
return new Promise((resolve, reject) => {
options.delFn(files, deleteOptions, (err, filesDeleted) => {
if (err) {
reject(err);
}

resolve(filesDeleted);
});
});
} : del;
options.dest = options.dest || '.';

options.suppress = (options.suppress !== false);

if (options.newManifest) {
options.oldManifest = options.oldManifest || path.join(options.dest, 'rev-manifest.json');

var oldManifest = getManifest(options.oldManifest, options.suppress);
var newManifest = getManifest(options.newManifest);
var oldFiles = getChanged(oldManifest, newManifest);
const oldManifest = getManifest(options.oldManifest, options.suppress);
const newManifest = getManifest(options.newManifest);
let oldFiles = getChanged(oldManifest, newManifest);

if (options.base) {
oldFiles = _.map(oldFiles, function (file) {
oldFiles = _.map(oldFiles, (file) => {
return path.join(options.dest || options.base, file);
});
}

if(options.deleteMapExtensions){

var extCheckPath;
if (options.deleteMapExtensions) {
let extCheckPath;

oldFiles.forEach(function (oldFile){
extCheckPath = oldFile+'.map';
oldFiles.forEach((oldFile) => {
extCheckPath = oldFile + '.map';
try {
fs.statSync(extCheckPath);
oldFiles.push(extCheckPath);
} catch (errA){
var oldFileCheck = path.relative(options.dest || options.base, oldFile);
var foundOrigKey = false;

for (var manifestKey in oldManifest) {
if (oldManifest.hasOwnProperty(manifestKey) && oldManifest[manifestKey] === oldFileCheck) {
foundOrigKey = manifestKey;
break;
}
}

if (foundOrigKey!==false && Object.keys(newManifest).indexOf(foundOrigKey)===-1) {
extCheckPath = path.join(options.dest || options.base, foundOrigKey+'.map');
oldFiles.push(extCheckPath);
}
}
fs.statSync(extCheckPath);
oldFiles.push(extCheckPath);
} catch (errA) {
const oldFileCheck = path.relative(options.dest || options.base, oldFile);
let foundOrigKey = false;

for (const manifestKey in oldManifest) {
if (oldManifest.hasOwnProperty(manifestKey) && oldManifest[manifestKey] === oldFileCheck) {
foundOrigKey = manifestKey;
break;
}
}

if (foundOrigKey !== false && Object.keys(newManifest).indexOf(foundOrigKey) === -1) {
extCheckPath = path.join(options.dest || options.base, foundOrigKey + '.map');
oldFiles.push(extCheckPath);
}
}
});
}

return options.delFn(oldFiles, { force: options.force }, cb);

return delFn(oldFiles, { force: options.force })
.then((filesDeleted) => {
return cb(null, filesDeleted);
})
.catch((err) => {
return cb(err);
});
}

// newManifest isn't specified, return a stream
return through.obj(function (file, enc, cb) {
return through.obj((file, enc, cb) => {
if (!options.base && file.base) {
options.base = file.base;
}
Expand All @@ -79,19 +92,19 @@ function revDel(options, cb) {
return cb(e);
}

revDel(options, function (err, filesDeleted) {
revDel(options, (err, filesDeleted) => {
if (err) {
return cb(err);
}

file.revDeleted = filesDeleted;
cb(null, file);
});
});
}

function getChanged(oldObject, newObject) {
return _.reduce(oldObject, function (result, fingerprinted, path) {
return _.reduce(oldObject, (result, fingerprinted, path) => {
if (newObject[path] !== fingerprinted) {
result.push(fingerprinted);
}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
},
"homepage": "https://github.com/callumacrae/rev-del",
"dependencies": {
"del": "^1.1.1",
"lodash": ">=3.1 <5",
"mocha": "^2.1.0",
"should": "^4.6.5",
"through2": "^0.6.3"
"del": "^3.0.0",
"lodash": "^4.17.11",
"mocha": "^5.2.0",
"should": "^13.2.3",
"through2": "^3.0.0"
},
"devDependencies": {
"vinyl": "^0.4.6"
"vinyl": "^2.2.0"
}
}
56 changes: 28 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
'use strict';

var path = require('path');
var _ = require('lodash');
var File = require('vinyl');
var revDel = require('./');
var should = require('should');
const path = require('path');
const _ = require('lodash');
const File = require('vinyl');
const revDel = require('./');
const should = require('should');

var oldManifest = {
const oldManifest = {
'foo.js': 'foo-abc.js',
'bar.js': 'bar-abc.js',
'hello': 'world'
};

var newManifest = {
const newManifest = {
'foo.js': 'foo-def.js',
'bar.js': 'bar-abc.js',
'hello': 'world2'
};

var manifests = {
const manifests = {
oldManifest: oldManifest,
newManifest: newManifest,
delFn: function (files, options, cb) {
delFn: (files, options, cb) => {
cb(null, files);
}
};

it('should work out which files to delete', function (cb) {
it('should work out which files to delete', (cb) => {
revDel(manifests, function (err, files) {
files.length.should.equal(2);
files.should.eql(['foo-abc.js', 'world']);
Expand All @@ -35,8 +35,8 @@ it('should work out which files to delete', function (cb) {
});
});

it('should read from JSON files', function (cb) {
var manifestsString = _.clone(manifests);
it('should read from JSON files', (cb) => {
const manifestsString = _.clone(manifests);
manifestsString.oldManifest = 'test.json';

revDel(manifestsString, function (err, files) {
Expand All @@ -47,15 +47,15 @@ it('should read from JSON files', function (cb) {
});
});

it('should handle streams', function (cb) {
var stream = revDel({
it('should handle streams', (cb) => {
const stream = revDel({
oldManifest: 'test.json',
delFn: function (files, options, cb) {
cb(null, files);
}
});

stream.on('data', function (file) {
stream.on('data', (file) => {
file.revDeleted.length.should.equal(2);
file.revDeleted.should.eql(['foo-abc.js', 'world']);

Expand All @@ -68,14 +68,14 @@ it('should handle streams', function (cb) {
stream.end();
});

it('should get the file path from gulp-rev', function (cb) {
var stream = revDel({
it('should get the file path from gulp-rev', (cb) => {
const stream = revDel({
delFn: function (files, options, cb) {
cb(null, files);
}
});

stream.on('data', function (file) {
stream.on('data', (file) => {
file.revDeleted.length.should.equal(2);
file.revDeleted.should.eql(['foo-abc.js', 'world']);

Expand All @@ -89,36 +89,36 @@ it('should get the file path from gulp-rev', function (cb) {
stream.end();
});

it('should not explode when rev-manifest.json is not found', function (cb) {
var manifestsString = _.clone(manifests);
it('should not explode when rev-manifest.json is not found', (cb) => {
const manifestsString = _.clone(manifests);
manifestsString.oldManifest = 'doesntexist.json';

// We're literally just testing that it doesn't throw an error
revDel(manifestsString, function () {
revDel(manifestsString, () => {
cb();
});
});

it('should explode when suppress is set to false says to', function () {
var manifestsString = _.clone(manifests);
it('should explode when suppress is set to false says to', () => {
const manifestsString = _.clone(manifests);
manifestsString.oldManifest = 'doesntexist.json';
manifestsString.suppress = false;

should.throws(function () {
revDel(manifestsString, function () {});
should.throws(() => {
revDel(manifestsString, () => {});
});
});

it('should accept dest', function (cb) {
var stream = revDel({
it('should accept dest', (cb) => {
const stream = revDel({
delFn: function (files, options, cb) {
cb(null, files);
},
dest: 'test',
oldManifest: 'test.json'
});

stream.on('data', function (file) {
stream.on('data', (file) => {
file.revDeleted.length.should.equal(2);
file.revDeleted.should.eql([path.normalize('test/foo-abc.js'), path.normalize('test/world')]);

Expand Down

0 comments on commit c58cf53

Please sign in to comment.