Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 72d34ea

Browse files
author
Alan Shaw
committed
refactor: use it-glob
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 5ab9c36 commit 72d34ea

File tree

3 files changed

+40
-56
lines changed

3 files changed

+40
-56
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"err-code": "^2.0.0",
5252
"explain-error": "^1.0.4",
5353
"flatmap": "0.0.3",
54+
"fs-extra": "^8.1.0",
5455
"glob": "^7.1.3",
5556
"ipfs-block": "~0.8.1",
5657
"ipfs-utils": "~0.0.3",
@@ -62,7 +63,7 @@
6263
"is-stream": "^2.0.0",
6364
"iso-stream-http": "~0.1.2",
6465
"iso-url": "~0.4.6",
65-
"it-pushable": "^1.2.1",
66+
"it-glob": "0.0.1",
6667
"it-to-stream": "^0.1.1",
6768
"iterable-ndjson": "^1.1.0",
6869
"just-kebab-case": "^1.1.0",

src/add-from-fs/glob-source.js

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
22

3-
const Fs = require('fs')
3+
const fs = require('fs-extra')
4+
const glob = require('it-glob')
45
const Path = require('path')
5-
const glob = require('glob')
6-
const pushable = require('it-pushable')
76
const errCode = require('err-code')
87

98
/**
@@ -16,9 +15,9 @@ const errCode = require('err-code')
1615
* @param {Boolean} [options.hidden] Include .dot files in matched paths
1716
* @param {Array<String>} [options.ignore] Glob paths to ignore
1817
* @param {Boolean} [options.followSymlinks] follow symlinks
19-
* @returns {AsyncIterable}
18+
* @returns {Iterable} source iterable
2019
*/
21-
module.exports = (...args) => (async function * () {
20+
module.exports = async function * globSource (...args) {
2221
const options = typeof args[args.length - 1] === 'string' ? {} : args.pop()
2322
const paths = args
2423

@@ -32,63 +31,49 @@ module.exports = (...args) => (async function * () {
3231
}
3332

3433
// Check the input paths comply with options.recursive and convert to glob sources
35-
const results = await Promise.all(paths.map(pathAndType))
36-
const globSources = results.map(r => toGlobSource(r, globSourceOptions))
37-
38-
for (const globSource of globSources) {
39-
for await (const { path, contentPath } of globSource) {
40-
yield { path, content: Fs.createReadStream(contentPath) }
41-
}
34+
for (const path of paths) {
35+
const stat = await fs.stat(path)
36+
const prefix = Path.dirname(path)
37+
yield * toGlobSource({ path, type: stat.isDirectory() ? 'dir' : 'file', prefix }, globSourceOptions))
4238
}
43-
})()
44-
45-
function toGlobSource ({ path, type }, options) {
46-
return (async function * () {
47-
options = options || {}
39+
}
4840

49-
const baseName = Path.basename(path)
41+
async function * toGlobSource ({ path, type, prefix }, options) {
42+
options = options || {}
5043

51-
if (type === 'file') {
52-
yield { path: baseName, contentPath: path }
53-
return
54-
}
44+
const baseName = Path.basename(path)
5545

56-
if (type === 'dir' && !options.recursive) {
57-
throw errCode(
58-
new Error(`'${path}' is a directory and recursive option not set`),
59-
'ERR_DIR_NON_RECURSIVE',
60-
{ path }
46+
if (type === 'file') {
47+
yield {
48+
path: baseName.replace(prefix, ''),
49+
content: fs.createReadStream(
50+
Path.isAbsolute(path) ? path : Path.join(process.cwd(), path)
6151
)
6252
}
53+
return
54+
}
6355

64-
const globOptions = Object.assign({}, options.glob, {
65-
cwd: path,
66-
nodir: true,
67-
realpath: false,
68-
absolute: false
69-
})
70-
71-
// TODO: want to use pull-glob but it doesn't have the features...
72-
const pusher = pushable()
56+
if (type === 'dir' && !options.recursive) {
57+
throw errCode(
58+
new Error(`'${path}' is a directory and recursive option not set`),
59+
'ERR_DIR_NON_RECURSIVE',
60+
{ path }
61+
)
62+
}
7363

74-
glob('**/*', globOptions)
75-
.on('match', m => pusher.push(m))
76-
.on('end', () => pusher.end())
77-
.on('abort', () => pusher.end())
78-
.on('error', err => pusher.end(err))
64+
const globOptions = Object.assign({}, options.glob, {
65+
cwd: path,
66+
nodir: true,
67+
realpath: false,
68+
absolute: false
69+
})
7970

80-
for await (const p of pusher) {
81-
yield {
82-
path: `${baseName}/${toPosix(p)}`,
83-
contentPath: Path.join(path, p)
84-
}
71+
for await (const p of glob(path, '**/*', globOptions)) {
72+
yield {
73+
path: toPosix(p.replace(prefix, '')),
74+
content: fs.createReadStream(p)
8575
}
86-
})()
87-
}
88-
89-
async function pathAndType (path) {
90-
const stat = await Fs.promises.stat(path)
91-
return { path, type: stat.isDirectory() ? 'dir' : 'file' }
76+
}
9277
}
9378

9479
const toPosix = path => path.replace(/\\/g, '/')

src/add-from-url.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ module.exports = configure(({ ky }) => {
1717
content: toIterable(body)
1818
}
1919

20-
for await (const file of add(input, options)) {
21-
yield file
22-
}
20+
yield * add(input, options)
2321
})()
2422
})

0 commit comments

Comments
 (0)