-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from gemini-testing/collections
Get rid of q-io
- Loading branch information
Showing
7 changed files
with
129 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
*.log | ||
coverage | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const q = require('q'); | ||
const qfs = require('q-io/fs'); | ||
const Promise = require('bluebird'); | ||
const fs = Promise.promisifyAll(require('fs')); | ||
const path = require('path'); | ||
|
||
exports.asyncFilter = (items, cb) => { | ||
return _(items) | ||
.map((item) => cb(item).then((res) => res && item)) | ||
.thru(q.all) | ||
.value() | ||
.then(_.compact); | ||
exports.matchesFormats = (filePath, formats) => { | ||
return _.isEmpty(formats) || _.includes(formats, path.extname(filePath)); | ||
}; | ||
|
||
exports.matchesFormats = (path, formats) => { | ||
return _.isEmpty(formats) || _.includes(formats, qfs.extension(path)); | ||
}; | ||
exports.isFile = (path) => fs.statAsync(path).then((stat) => stat.isFile()); | ||
|
||
exports.getFilePaths = (basePath) => { | ||
function readDirFiles(basePath) { | ||
return fs.readdirAsync(basePath) | ||
.then((paths) => { | ||
return Promise.map(paths, (p) => exports.getFilePaths(path.join(basePath, p))) | ||
.then(_.flatten); | ||
}); | ||
} | ||
|
||
exports.isFile = (path) => qfs.stat(path).then((stat) => stat.isFile()); | ||
return exports.isFile(basePath) | ||
.then((isFile) => isFile ? [basePath] : readDirFiles(basePath)) | ||
.catch((err) => { | ||
throw new Error(`Error while reading path "${basePath}"\n${err.stack || err.message || err}`); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const utils = require('../lib/utils'); | ||
|
||
describe('utils', () => { | ||
const sandbox = sinon.sandbox.create(); | ||
|
||
afterEach(() => sandbox.restore()); | ||
|
||
describe('matchesFormats', () => { | ||
it('should return `true` if the formats option contain passed file format', () => { | ||
assert.isTrue(utils.matchesFormats('some/path/file.js', ['.js'])); | ||
}); | ||
|
||
it('should return `false` if the formats option does not contain passed file format', () => { | ||
assert.isFalse(utils.matchesFormats('some/path/file.js', ['.txt'])); | ||
}); | ||
}); | ||
|
||
describe('isFile', () => { | ||
beforeEach(() => { | ||
sandbox.stub(fs, 'statAsync'); | ||
}); | ||
|
||
it('should return `true` if the passed path is file', () => { | ||
fs.statAsync.resolves({isFile: () => true}); | ||
|
||
return assert.becomes(utils.isFile('some/path/file.js'), true); | ||
}); | ||
|
||
it('should return `false` if the passed path is directory', () => { | ||
fs.statAsync.resolves({isFile: () => false}); | ||
|
||
return assert.becomes(utils.isFile('some/path/dir'), false); | ||
}); | ||
}); | ||
|
||
describe('getFilePaths', () => { | ||
const createStatStub = (paths, opts) => { | ||
paths.forEach((path) => { | ||
fs.statAsync.withArgs(path).resolves({isFile: () => opts.isFile}); | ||
}); | ||
}; | ||
|
||
const createFiles = (...paths) => createStatStub(paths, {isFile: true}); | ||
|
||
const createDirs = (...paths) => createStatStub(paths, {isFile: false}); | ||
|
||
beforeEach(() => { | ||
sandbox.stub(fs, 'statAsync'); | ||
sandbox.stub(fs, 'readdirAsync'); | ||
}); | ||
|
||
it('should return file if argument is a file', () => { | ||
createFiles('file.js'); | ||
|
||
return assert.becomes(utils.getFilePaths('file.js'), ['file.js']); | ||
}); | ||
|
||
it('should return empty array if argument is an empty directory', () => { | ||
createDirs(false, 'dir'); | ||
fs.readdirAsync.withArgs('dir').resolves([]); | ||
|
||
return assert.becomes(utils.getFilePaths('dir'), []); | ||
}); | ||
|
||
it('should return only files from file system', () => { | ||
createDirs('root', 'root/subdir'); | ||
createFiles('root/file.js', 'root/subdir/file2.txt', 'root/subdir/file3.txt'); | ||
fs.readdirAsync.withArgs('root').resolves(['file.js', 'subdir']); | ||
fs.readdirAsync.withArgs('root/subdir').resolves(['file2.txt', 'file3.txt']); | ||
|
||
return assert.becomes(utils.getFilePaths('root'), ['root/file.js', 'root/subdir/file2.txt', 'root/subdir/file3.txt']); | ||
}); | ||
|
||
it('should throw an error if directory is not acceptable', () => { | ||
createDirs('root'); | ||
fs.readdirAsync.rejects('no-rights'); | ||
|
||
return assert.isRejected(utils.getFilePaths('root'), /no-rights/); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.