-
Notifications
You must be signed in to change notification settings - Fork 31
feat: add glob-source from js-ipfs so it can be shared with the http client #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59f5e71
feat: add glob-source from js-ipfs
achingbrain 4b50299
chore: update test/files/glob-source.spec.js
achingbrain 70c74a3
chore: use env file instead of dep
achingbrain f792f8c
chore: fix another typo
achingbrain e56484a
test: add test for multiple paths
achingbrain 6d976fa
feat: make paths (async)iterable or string
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,93 @@ | ||
'use strict' | ||
|
||
const fs = require('fs-extra') | ||
const glob = require('it-glob') | ||
const Path = require('path') | ||
const errCode = require('err-code') | ||
const kindOf = require('kind-of') | ||
|
||
/** | ||
* Create an async iterator that yields paths that match requested file paths. | ||
* | ||
* @param {Iterable|AsyncIterable|String} paths File system path(s) to glob from | ||
* @param {Object} [options] Optional options | ||
* @param {Boolean} [options.recursive] Recursively glob all paths in directories | ||
* @param {Boolean} [options.hidden] Include .dot files in matched paths | ||
* @param {Array<String>} [options.ignore] Glob paths to ignore | ||
* @param {Boolean} [options.followSymlinks] follow symlinks | ||
* @yields {Object} File objects in the form `{ path: String, content: AsyncIterator<Buffer> }` | ||
*/ | ||
module.exports = async function * globSource (paths, options) { | ||
options = options || {} | ||
|
||
if (kindOf(paths) === 'string') { | ||
paths = [paths] | ||
} | ||
|
||
const globSourceOptions = { | ||
recursive: options.recursive, | ||
glob: { | ||
dot: Boolean(options.hidden), | ||
ignore: Array.isArray(options.ignore) ? options.ignore : [], | ||
follow: options.followSymlinks != null ? options.followSymlinks : true | ||
} | ||
} | ||
|
||
// Check the input paths comply with options.recursive and convert to glob sources | ||
for await (const path of paths) { | ||
if (typeof path !== 'string') { | ||
throw errCode( | ||
new Error(`Path must be a string`), | ||
'ERR_INVALID_PATH', | ||
{ path } | ||
) | ||
} | ||
|
||
const absolutePath = Path.resolve(process.cwd(), path) | ||
const stat = await fs.stat(absolutePath) | ||
const prefix = Path.dirname(absolutePath) | ||
|
||
for await (const entry of toGlobSource({ path, type: stat.isDirectory() ? 'dir' : 'file', prefix }, globSourceOptions)) { | ||
yield entry | ||
} | ||
} | ||
} | ||
|
||
async function * toGlobSource ({ path, type, prefix }, options) { | ||
options = options || {} | ||
|
||
const baseName = Path.basename(path) | ||
|
||
if (type === 'file') { | ||
yield { | ||
path: baseName.replace(prefix, ''), | ||
content: fs.createReadStream(Path.isAbsolute(path) ? path : Path.join(process.cwd(), path)) | ||
} | ||
|
||
return | ||
} | ||
|
||
if (type === 'dir' && !options.recursive) { | ||
throw errCode( | ||
new Error(`'${path}' is a directory and recursive option not set`), | ||
'ERR_DIR_NON_RECURSIVE', | ||
{ path } | ||
) | ||
} | ||
|
||
const globOptions = Object.assign({}, options.glob, { | ||
cwd: path, | ||
nodir: true, | ||
realpath: false, | ||
absolute: true | ||
}) | ||
|
||
for await (const p of glob(path, '**/*', globOptions)) { | ||
yield { | ||
path: toPosix(p.replace(prefix, '')), | ||
alanshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
content: fs.createReadStream(p) | ||
} | ||
} | ||
} | ||
|
||
const toPosix = path => path.replace(/\\/g, '/') |
This file contains hidden or 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,125 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const chaiAsPromised = require('chai-as-promised') | ||
const globSource = require('../../src/files/glob-source') | ||
const all = require('async-iterator-all') | ||
const path = require('path') | ||
const { | ||
isNode | ||
} = require('../../src/env') | ||
|
||
chai.use(dirtyChai) | ||
chai.use(chaiAsPromised) | ||
const expect = chai.expect | ||
|
||
describe('glob-source', () => { | ||
it('single file, relative path', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource(path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'file-0.html')))) | ||
|
||
expect(result.length).to.equal(1) | ||
expect(result[0].path).to.equal('file-0.html') | ||
}) | ||
|
||
it('directory, relative path', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource(path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { | ||
recursive: true | ||
})) | ||
|
||
expect(result.length).to.equal(3) | ||
expect(result[0].path).to.equal('/dir/file-1.txt') | ||
expect(result[1].path).to.equal('/dir/file-2.js') | ||
expect(result[2].path).to.equal('/dir/file-3.css') | ||
}) | ||
|
||
it('single file, absolute path', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'file-0.html')))) | ||
|
||
expect(result.length).to.equal(1) | ||
expect(result[0].path).to.equal('file-0.html') | ||
}) | ||
|
||
it('directory, relative path', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { | ||
recursive: true | ||
})) | ||
|
||
expect(result.length).to.equal(3) | ||
expect(result[0].path).to.equal('/dir/file-1.txt') | ||
expect(result[1].path).to.equal('/dir/file-2.js') | ||
expect(result[2].path).to.equal('/dir/file-3.css') | ||
}) | ||
|
||
it('directory, hidden files', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { | ||
recursive: true, | ||
hidden: true | ||
})) | ||
|
||
expect(result.length).to.equal(4) | ||
expect(result[0].path).to.equal('/dir/.hidden.txt') | ||
expect(result[1].path).to.equal('/dir/file-1.txt') | ||
expect(result[2].path).to.equal('/dir/file-2.js') | ||
expect(result[3].path).to.equal('/dir/file-3.css') | ||
}) | ||
|
||
it('directory, ignore files', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { | ||
recursive: true, | ||
ignore: ['**/file-1.txt'] | ||
})) | ||
|
||
expect(result.length).to.equal(2) | ||
expect(result[0].path).to.equal('/dir/file-2.js') | ||
expect(result[1].path).to.equal('/dir/file-3.css') | ||
}) | ||
|
||
it('multiple paths', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
const result = await all(globSource([ | ||
path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir', 'file-1.txt')), | ||
path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir', 'file-2.js')) | ||
])) | ||
|
||
expect(result.length).to.equal(2) | ||
expect(result[0].path).to.equal('file-1.txt') | ||
expect(result[1].path).to.equal('file-2.js') | ||
}) | ||
|
||
it('requires recursive flag for directory', async function () { | ||
if (!isNode) { | ||
return this.skip() | ||
} | ||
|
||
await expect(all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir'))))).to.be.rejectedWith(/recursive option not set/) | ||
}) | ||
}) |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.