Comparing version 1.0.8 to 2.0.0
99
index.js
@@ -5,3 +5,23 @@ "use strict"; | ||
const path = require('path'); | ||
const nodefs = require('fs'); | ||
const WRITEOPERATION = new Set([ | ||
'write', | ||
'writeSync', | ||
'writeFile', | ||
'writeFileSync', | ||
'writev', | ||
'writevSync', | ||
'appendFileSync', | ||
'appendFile', | ||
'rmdir', | ||
'rmdirSync', | ||
'mkdir', | ||
'mkdirSync' | ||
]); | ||
const READDIR = new Set([ | ||
'readdirSync', | ||
'readdir' | ||
]); | ||
function getRootAndPrefix(tree) { | ||
@@ -38,2 +58,36 @@ let root = ''; | ||
this._dirList = Array.isArray(trees) ? trees : [trees]; | ||
let self = this; | ||
this.fs = new Proxy(nodefs, { | ||
get(target, propertyName) { | ||
if(!WRITEOPERATION.has(propertyName)) { | ||
if (READDIR.has(propertyName)) { | ||
return function() { | ||
let [relativePath] = arguments; | ||
if (path.isAbsolute(relativePath) && relativePath.trim() != '/') { | ||
return target[propertyName](...arguments); | ||
} | ||
return self[propertyName](...arguments); | ||
} | ||
} | ||
return function() { | ||
let [relativePath] = arguments; | ||
let { _dirList } = self; | ||
let fullPath = relativePath; | ||
if (!path.isAbsolute(relativePath)) { | ||
for (let i=0; i < _dirList.length; i++) { | ||
let { root } = getRootAndPrefix(_dirList[i]); | ||
let tempPath = root + '/' + relativePath; | ||
if(fs.existsSync(tempPath)) { | ||
fullPath = tempPath; | ||
} | ||
} | ||
} | ||
arguments[0] = fullPath; | ||
return target[propertyName](...arguments); | ||
} | ||
} else { | ||
throw new Error(`Operation ${propertyName} is a write operation, not allowed with FSMerger.fs`); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -99,16 +153,55 @@ | ||
readDirSync(dirPath, options) { | ||
readdirSync(dirPath, options) { | ||
let { _dirList } = this; | ||
let result = []; | ||
let result = [], errorCount = 0; | ||
let fullDirPath = ''; | ||
for (let i=0; i < _dirList.length; i++) { | ||
let { root } = getRootAndPrefix(_dirList[i]); | ||
let fullDirPath = root + '/' + dirPath; | ||
fullDirPath = root + '/' + dirPath; | ||
fullDirPath = fullDirPath.replace(/(\/|\/\/)$/, ''); | ||
if(fs.existsSync(fullDirPath)) { | ||
result.push.apply(result, fs.readdirSync(fullDirPath, options)); | ||
} else { | ||
errorCount += 1; | ||
} | ||
} | ||
if (errorCount == _dirList.length) { | ||
fs.readdirSync(fullDirPath); | ||
} | ||
return [...new Set(result)]; | ||
} | ||
readdir(dirPath, callback) { | ||
let result = []; | ||
let { _dirList } = this; | ||
let fullDirPath = ''; | ||
let existingPath = []; | ||
for (let i=0; i < _dirList.length; i++) { | ||
let { root } = getRootAndPrefix(_dirList[i]); | ||
fullDirPath = root + '/' + dirPath; | ||
fullDirPath = fullDirPath.replace(/(\/|\/\/)$/, ''); | ||
if(fs.existsSync(fullDirPath)) { | ||
existingPath.push(fullDirPath); | ||
} | ||
} | ||
if (!existingPath.length) { | ||
fs.readdir(fullDirPath, callback); | ||
} | ||
let readComplete = 0; | ||
for (let i = 0; i < existingPath.length; i++) { | ||
fs.readdir(existingPath[i], (err, list) => { | ||
readComplete += 1; | ||
result.push.apply(result, list); | ||
if (readComplete == existingPath.length || err) { | ||
if (err) { | ||
result = undefined; | ||
} else { | ||
result = [...new Set(result)]; | ||
} | ||
callback(err, result); | ||
} | ||
}); | ||
} | ||
} | ||
entries(dirPath = '', options) { | ||
@@ -115,0 +208,0 @@ let { _dirList } = this; |
{ | ||
"name": "fs-merger", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"description": "Reads files from a real location", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,3 @@ "use strict"; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
@@ -27,2 +28,5 @@ describe('fs-reader', function () { | ||
} | ||
}, | ||
'test-sub-2': { | ||
} | ||
@@ -47,21 +51,21 @@ }, | ||
describe('Reads file from given location', function() { | ||
let fs = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']); | ||
let fsMerger = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']); | ||
it('a.txt', function () { | ||
let content = fs.readFileSync('a.txt', 'utf-8'); | ||
let content = fsMerger.fs.readFileSync('a.txt', 'utf-8'); | ||
expect(content).to.be.equal('this is same other'); | ||
}); | ||
it('c.txt', function () { | ||
let content = fs.readFileSync('c.txt', 'utf-8'); | ||
let content = fsMerger.fs.readFileSync('c.txt', 'utf-8'); | ||
expect(content).to.be.equal('this is new file'); | ||
}); | ||
it('test-1/b.txt', function () { | ||
let content = fs.readFileSync('c.txt', 'utf-8'); | ||
let content = fsMerger.fs.readFileSync('c.txt', 'utf-8'); | ||
expect(content).to.be.equal('this is new file'); | ||
}); | ||
it('test-1/b.txt', function () { | ||
let content = fs.readFileSync('test-1/b.txt', 'utf-8'); | ||
let content = fsMerger.fs.readFileSync('test-1/b.txt', 'utf-8'); | ||
expect(content).to.be.equal('b contains text'); | ||
}); | ||
it('test-1/b.txt', function () { | ||
let content = fs.readFileSync('test-sub-1/test-sub-sub-1/sub-sub-c.txt', 'utf-8'); | ||
let content = fsMerger.fs.readFileSync('test-sub-1/test-sub-sub-1/sub-sub-c.txt', 'utf-8'); | ||
expect(content).to.be.equal('this is inside of test-sub-sub-1'); | ||
@@ -111,21 +115,155 @@ }); | ||
}); | ||
describe('Reads contents of the folder from location', function() { | ||
let fs = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']); | ||
it('test-1', function() { | ||
let content = fs.readDirSync('test-1'); | ||
expect(content).to.be.deep.equal(['b.txt']); | ||
let fsMerger = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']); | ||
describe('read test-1 folder', function() { | ||
it('readdirSync', function() { | ||
let content = fsMerger.fs.readdirSync('test-1'); | ||
expect(content).to.be.deep.equal(['b.txt']); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('test-1', function (err, content) { | ||
expect(content).to.have.all.members(['b.txt']); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('test-sub-1', function() { | ||
let content = fs.readDirSync('test-sub-1'); | ||
expect(content).to.be.deep.equal([ 'sub-b.txt', 'test-sub-sub-1', 'sub-c.txt' ]); | ||
describe('read test-sub-1 sub-folder', function() { | ||
it('readdirSync', function() { | ||
let content = fsMerger.fs.readdirSync('test-sub-1'); | ||
expect(content).to.be.deep.equal([ 'sub-b.txt', 'test-sub-sub-1', 'sub-c.txt' ]); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('test-sub-1', function (err, content) { | ||
expect(content).to.have.all.members([ 'sub-b.txt', 'test-sub-sub-1', 'sub-c.txt' ]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('test-sub-1/test-sub-sub-1', function() { | ||
let content = fs.readDirSync('test-sub-1/test-sub-sub-1'); | ||
expect(content).to.be.deep.equal([ 'sub-sub-b.txt', 'sub-sub-c.txt' ]); | ||
describe('read test-sub-1/test-sub-sub-1 folder', function() { | ||
it('readdirSync', function() { | ||
let content = fsMerger.fs.readdirSync('test-sub-1/test-sub-sub-1'); | ||
expect(content).to.be.deep.equal([ 'sub-sub-b.txt', 'sub-sub-c.txt' ]); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('test-sub-1/test-sub-sub-1', function (err, content) { | ||
expect(content).to.have.all.members([ 'sub-sub-b.txt', 'sub-sub-c.txt' ]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('/', function() { | ||
let content = fs.readDirSync('/'); | ||
expect(content).to.be.deep.equal([ 'a.txt', 'test-1', 'x.txt','c.txt', 'test-sub-1', 'b.txt', 'd.txt']); | ||
describe('read / folder', function() { | ||
it('readdirsync', function() { | ||
let content = fsMerger.fs.readdirSync('/'); | ||
expect(content).to.be.deep.equal([ 'a.txt', 'test-1', 'x.txt','c.txt', 'test-sub-1', 'test-sub-2', 'b.txt', 'd.txt']); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('/', function (err, content) { | ||
expect(content).to.have.all.members([ 'a.txt', 'test-1', 'x.txt','c.txt', 'test-sub-1', 'test-sub-2', 'b.txt', 'd.txt']); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('reading folder invalid folder will throw with absolute path', function() { | ||
it('readdirsync', function() { | ||
expect(() => { | ||
fsMerger.fs.readdirSync('/sfsd') | ||
}).throw(/ENOENT\: no such file or directory, scandir.*/); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('/sfsd', function (err) { | ||
expect(err.message).to.be.contains(`ENOENT: no such file or directory, scandir`); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('reading folder invalid folder will throw', function() { | ||
it('readdirsync', function() { | ||
expect(() => { | ||
fsMerger.fs.readdirSync('/sfsd') | ||
}).throw(/ENOENT\: no such file or directory, scandir.*/); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('sfsd', function (err) { | ||
expect(err.message).to.be.contains(`ENOENT: no such file or directory, scandir `); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe(`shouldn't throw error when folder exist but empty`, function() { | ||
it('readdirSync', function() { | ||
let content = fsMerger.fs.readdirSync('test-sub-2'); | ||
expect(content).to.be.deep.equal([]); | ||
}); | ||
it('readdir', function(done) { | ||
fsMerger.fs.readdir('test-sub-2', function (err, content) { | ||
expect(content).to.have.all.members([]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe(`error from fs.readdir matches error from fsMerger.fs.readdir`, function() { | ||
it('readdir', function(done) { | ||
let fsError = ''; | ||
fs.readdir('fixtures/test-1/test-1', (err, list) => { | ||
try { | ||
fs.readFileSync(list[0]); | ||
}catch (error) { | ||
fsError = error; | ||
} | ||
}); | ||
fsMerger.fs.readdir('test-1', (err, list) => { | ||
try { | ||
fs.readFileSync(list[0]); | ||
} catch (error) { | ||
expect(error.syscall).to.be.equal(fsError.syscall); | ||
expect(error.errno).to.be.equal(fsError.errno); | ||
expect(error.message).to.be.equal(fsError.message); | ||
expect(error.code).to.be.equal(fsError.code); | ||
expect(error.path).to.be.equal(fsError.path); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Verify few fs operations', function() { | ||
let fsMerger = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']); | ||
it('existsSync works', function() { | ||
let content = fsMerger.fs.existsSync('test-1'); | ||
expect(content).to.be.true; | ||
}); | ||
it('exists works', function(done) { | ||
fsMerger.fs.exists('test-1', function (content) { | ||
expect(content).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
it('absolute path works', function() { | ||
let exist = fsMerger.fs.existsSync(`${__dirname}/../fixtures/test-1`); | ||
expect(exist).to.be.true; | ||
}); | ||
it('writeFileSync operation must throw error', function () { | ||
let fsMerger = new FSMerge(['fixtures/test-1']); | ||
expect(()=>{ | ||
fsMerger.fs.writeFileSync('read.md', 'test'); | ||
}).to.throw(`Operation writeFileSync is a write operation, not allowed with FSMerger.fs`) | ||
}); | ||
}); | ||
describe('Returns entries for', function() { | ||
@@ -136,3 +274,3 @@ let fs = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']); | ||
let fileList = []; | ||
let walkList = ['a.txt', 'b.txt', 'c.txt', 'd.txt', 'test-1/', 'test-1/b.txt', 'test-sub-1/', 'test-sub-1/sub-b.txt', 'test-sub-1/sub-c.txt', 'test-sub-1/test-sub-sub-1/', 'test-sub-1/test-sub-sub-1/sub-sub-b.txt', 'test-sub-1/test-sub-sub-1/sub-sub-c.txt', 'x.txt' ]; | ||
let walkList = ['a.txt', 'b.txt', 'c.txt', 'd.txt', 'test-1/', 'test-1/b.txt', 'test-sub-1/', 'test-sub-1/sub-b.txt', 'test-sub-1/sub-c.txt', 'test-sub-1/test-sub-sub-1/', 'test-sub-1/test-sub-sub-1/sub-sub-b.txt', 'test-sub-1/test-sub-sub-1/sub-sub-c.txt', 'test-sub-2/' ,'x.txt']; | ||
fsEntries.forEach(entry => { | ||
@@ -139,0 +277,0 @@ fileList.push(entry.relativePath); |
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
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
27961
653
9