Skip to content
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

feat: support glob patterns #151

Merged
merged 4 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
test-react-native-android:
runs-on: macos-latest
needs: check
continue-on-error: true
steps:
- uses: actions/checkout@v2
- run: npm install
Expand Down
126 changes: 33 additions & 93 deletions src/files/glob-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,131 +7,71 @@ const Path = require('path')
const errCode = require('err-code')

/**
* Create an async iterator that yields paths that match requested file paths.
* Create an async iterator that yields paths that match requested glob pattern
*
* @param {Iterable<string> | AsyncIterable<string> | string} paths - File system path(s) to glob from
* @param {string} pattern - Glob pattern to match
* @param {Object} [options] - Optional options
* @param {boolean} [options.recursive] - Recursively glob all paths in directories
* @param {string} [options.cwd] - The directory to start matching the pattern in
* @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
* @param {boolean} [options.preserveMode] - preserve mode
* @param {boolean} [options.preserveMtime] - preserve mtime
* @param {number} [options.mode] - mode to use - if preserveMode is true this will be ignored
* @param {import('ipfs-unixfs').MtimeLike} [options.mtime] - mtime to use - if preserveMtime is true this will be ignored
* @yields {Object} File objects in the form `{ path: String, content: AsyncIterator<Buffer> }`
*/
module.exports = async function * globSource (paths, options) {
module.exports = async function * globSource (pattern, options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be tempted to make this take cwd, pattern, options to avoid some of the cwd messing around bits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I'm, always wondering where the output path will start.

options = options || {}

if (typeof paths === 'string') {
paths = [paths]
if (typeof pattern !== 'string') {
throw errCode(
new Error('Pattern must be a string'),
'ERR_INVALID_PATH',
{ pattern }
)
}

const globSourceOptions = {
recursive: options.recursive,
glob: {
dot: Boolean(options.hidden),
ignore: Array.isArray(options.ignore) ? options.ignore : [],
follow: options.followSymlinks != null ? options.followSymlinks : true
let cwd = options.cwd

if (!cwd) {
if (Path.isAbsolute(pattern)) {
cwd = Path.dirname(pattern)
pattern = pattern.replace(cwd + Path.sep, '')
} else {
cwd = process.cwd()
}
}

// 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 }
)
}
if (pattern.startsWith('.' + Path.sep)) {
pattern = pattern.replace('.' + Path.sep, '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this bit guard against?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With hindsight I don't think it's necessary, I've removed it.

}

const absolutePath = Path.resolve(process.cwd(), path)
const stat = await fsp.stat(absolutePath)
const prefix = Path.dirname(absolutePath)
const globOptions = Object.assign({}, pattern, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern is a string no? Why assigning options to it?

Copy link
Member Author

@achingbrain achingbrain Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point.

cwd,
nodir: false,
realpath: false,
absolute: true,
dot: Boolean(options.hidden),
follow: options.followSymlinks != null ? options.followSymlinks : true
})

for await (const p of glob(cwd, pattern, globOptions)) {
const stat = await fsp.stat(p)

let mode = options.mode

if (options.preserveMode) {
// @ts-ignore
mode = stat.mode
}

let mtime = options.mtime

if (options.preserveMtime) {
// @ts-ignore
mtime = stat.mtime
}

if (stat.isDirectory()) {
yield {
path: `/${Path.basename(path)}`,
mode,
mtime
}
}

yield * toGlobSource({
path,
type: stat.isDirectory() ? 'dir' : 'file',
prefix,
mode,
mtime,
preserveMode: options.preserveMode,
preserveMtime: options.preserveMtime
}, globSourceOptions)
}
}

// @ts-ignore
async function * toGlobSource ({ path, type, prefix, mode, mtime, preserveMode, preserveMtime }, 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)),
mode,
mtime
}

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: false,
realpath: false,
absolute: true
})

for await (const p of glob(path, '**/*', globOptions)) {
const stat = await fsp.stat(p)

if (preserveMode || preserveMtime) {
if (preserveMode) {
mode = stat.mode
}

if (preserveMtime) {
mtime = stat.mtime
}
}

yield {
path: toPosix(p.replace(prefix, '')),
path: toPosix(p.replace(cwd, '')),
content: stat.isFile() ? fs.createReadStream(p) : undefined,
mode,
mtime
Expand Down
Loading