-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (128 loc) · 4.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import gracefulfs from 'graceful-fs'
import { promisify } from 'util'
import path from 'path'
import { Readable } from 'stream'
/**
* @typedef {Pick<File, 'stream'|'name'|'size'>} FileLike
* @typedef {{
* name: string
* isFile: () => boolean
* isDirectory: () => boolean
* }} Dirent
* @typedef {{
* readdir: (path: string, options: { withFileTypes: true }) => Promise<Dirent[]>
* }} DirReader
* @typedef {{
* size: number
* isFile: () => boolean
* isDirectory: () => boolean
* }} Stats
* @typedef {{ stat: (path: string) => Promise<Stats> }} StatGetter
* @typedef {{
* createReadStream: (path: string) => import('node:fs').ReadStream
* promises: DirReader & StatGetter
* }} FileSystem
*/
const defaultfs = {
createReadStream: gracefulfs.createReadStream,
promises: {
// https://github.com/isaacs/node-graceful-fs/issues/160
stat: promisify(gracefulfs.stat),
readdir: promisify(gracefulfs.readdir)
}
}
/**
* @param {string | Iterable<string>} paths
* @param {object} [options]
* @param {boolean} [options.hidden]
* @param {boolean} [options.sort] Sort by path. Default: true.
* @param {FileSystem} [options.fs] Custom FileSystem implementation.
* @returns {Promise<FileLike[]>}
*/
export async function filesFromPaths (paths, options) {
if (typeof paths === 'string') {
paths = [paths]
}
/** @type {string[]|undefined} */
let commonParts
const files = []
for (const p of paths) {
for await (const file of filesFromPath(p, options)) {
files.push(file)
const nameParts = file.name.split(path.sep)
if (commonParts == null) {
commonParts = nameParts.slice(0, -1)
continue
}
for (let i = 0; i < commonParts.length; i++) {
if (commonParts[i] !== nameParts[i]) {
commonParts = commonParts.slice(0, i)
break
}
}
}
}
const commonPath = `${(commonParts ?? []).join('/')}/`
const commonPathFiles = files.map(f => ({
...f,
// normalize file path on windows
name: path.sep === '\\'
? f.name.split(path.sep).join('/').slice(commonPath.length)
: f.name.slice(commonPath.length)
}))
return options?.sort == null || options?.sort === true
? commonPathFiles.sort((a, b) => a.name === b.name ? 0 : a.name > b.name ? 1 : -1)
: commonPathFiles
}
/**
* @param {string} filepath
* @param {object} [options]
* @param {boolean} [options.hidden]
* @param {FileSystem} [options.fs] Custom FileSystem implementation.
* @returns {AsyncIterableIterator<FileLike>}
*/
async function * filesFromPath (filepath, options) {
filepath = path.resolve(filepath)
const fs = options?.fs ?? defaultfs
const hidden = options?.hidden ?? false
/** @param {string} filepath */
const filter = filepath => {
if (!hidden && path.basename(filepath).startsWith('.')) return false
return true
}
const name = filepath
const stat = await fs.promises.stat(name)
if (!filter(name)) {
return
}
if (stat.isFile()) {
// @ts-expect-error node web stream not type compatible with web stream
yield { name, stream: () => Readable.toWeb(fs.createReadStream(name)), size: stat.size }
} else if (stat.isDirectory()) {
yield * filesFromDir(name, filter, options)
}
}
/**
* @param {string} dir
* @param {(name: string) => boolean} filter
* @param {object} [options]
* @param {FileSystem} [options.fs] Custom FileSystem implementation.
* @returns {AsyncIterableIterator<FileLike>}
*/
async function * filesFromDir (dir, filter, options) {
const fs = options?.fs ?? defaultfs
const entries = await fs.promises.readdir(path.join(dir), { withFileTypes: true })
for (const entry of entries) {
if (!filter(entry.name)) {
continue
}
if (entry.isFile()) {
const name = path.join(dir, entry.name)
const { size } = await fs.promises.stat(name)
// @ts-expect-error node web stream not type compatible with web stream
yield { name, stream: () => Readable.toWeb(fs.createReadStream(name)), size }
} else if (entry.isDirectory()) {
yield * filesFromDir(path.join(dir, entry.name), filter)
}
}
}