Skip to content

Commit

Permalink
Add support for passing URLs in files
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 15, 2022
1 parent 7a37155 commit 9be24d1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
5 changes: 3 additions & 2 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ function done(error) {
## `options.files`
Paths or [globs][glob], or [vfile][]s to files and directories to process.
Paths or [globs][glob], or [vfile][]s or `URL`s to files and directories to
process.
Fileglobs (for example, `*.md`) can be given to add all matching files.
Directories and globs to directories can be given alongside
[`extensions`][extensions] to search directories for files matching an extension
Expand All @@ -106,7 +107,7 @@ Directories and globs to directories can be given alongside
This searching will not include `node_modules` or hidden directories (those
starting with a dot, `.`, like `.git`).
* Type: `Array<string>`
* Type: `Array<string|URL|VFile>`
* Default: `[]`
###### Example
Expand Down
12 changes: 8 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @typedef Settings
* @property {Options['processor']} processor
* @property {Exclude<Options['cwd'], undefined | URL>} cwd
* @property {Exclude<Options['files'], undefined>} files
* @property {Array<string|VFile>} files
* @property {Exclude<Options['extensions'], undefined>} extensions
* @property {Exclude<Options['streamIn'], undefined>} streamIn
* @property {Options['filePath']} filePath
Expand Down Expand Up @@ -65,7 +65,7 @@
* @property {string|URL} [cwd]
* Directory to search files in, load plugins from, and more.
* Defaults to `process.cwd()`.
* @property {Array<string|VFile>} [files]
* @property {Array<string|URL|VFile>} [files]
* Paths or globs to files and directories, or virtual files, to process.
* @property {Array<string>} [extensions]
* If `files` matches directories, include `files` with `extensions`
Expand Down Expand Up @@ -221,7 +221,11 @@ export function engine(options, callback) {
: options.cwd || process.cwd()

// Input.
settings.files = options.files || []
settings.files = (options.files || []).map((d) => {
return typeof d === 'object' && 'href' in d && !('path' in d)
? fileURLToPath(d)
: d
})
settings.extensions = (options.extensions || []).map((ext) =>
ext.charAt(0) === '.' ? ext : '.' + ext
)
Expand Down Expand Up @@ -338,7 +342,7 @@ export function engine(options, callback) {
settings.frail = options.frail

// Process.
fileSetPipeline.run({files: options.files || []}, settings, next)
fileSetPipeline.run({files: settings.files}, settings, next)

/**
* @param {Error|null} error
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ done.
— unified processor to transform files
* [`cwd`][cwd] (`string` or `URL`, default: `process.cwd()`)
— Directory to search files in, load plugins from, and more
* [`files`][files] (`Array<string|VFile>`, optional)
— Paths or globs to files and directories, or virtual files, to process
* [`files`][files] (`Array<string|URL|VFile>`, optional)
— Paths or globs to files and directories, virtual files, or URLs, to
process
* [`extensions`][extensions] (`Array<string>`, optional)
— If `files` matches directories, include files with `extensions`
* [`streamIn`][stream-in] (`ReadableStream`, default: `process.stdin`)
Expand Down
2 changes: 1 addition & 1 deletion test/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ test('ignore', (t) => {
processor: noop,
cwd,
streamError: stderr.stream,
files: [fileURLToPath(url)]
files: [url]
},
(error, code) => {
fs.unlinkSync(url)
Expand Down
19 changes: 8 additions & 11 deletions test/reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,13 @@ test('reporting', (t) => {

t.test('should support custom given reporters', (t) => {
const stderr = spy()
const root = fileURLToPath(new URL('two-files/', fixtures))

t.plan(1)

engine(
{
processor: noop(),
cwd: root,
cwd: new URL('two-files/', fixtures),
streamError: stderr.stream,
files: ['.'],
extensions: ['txt'],
Expand All @@ -174,7 +173,7 @@ test('reporting', (t) => {

t.test('should support custom reporters (without prefix)', (t) => {
const stderr = spy()
const root = fileURLToPath(new URL('two-files/', fixtures))
const cwd = new URL('two-files/', fixtures)

t.plan(1)

Expand All @@ -188,7 +187,7 @@ test('reporting', (t) => {
}
}
),
cwd: root,
cwd,
streamError: stderr.stream,
files: ['.'],
extensions: ['txt'],
Expand All @@ -202,13 +201,13 @@ test('reporting', (t) => {
[
{
path: 'one.txt',
cwd: root,
cwd: fileURLToPath(cwd),
history: ['one.txt'],
messages: []
},
{
path: 'two.txt',
cwd: root,
cwd: fileURLToPath(cwd),
history: ['two.txt'],
messages: [
{
Expand Down Expand Up @@ -238,7 +237,7 @@ test('reporting', (t) => {

t.test('should support custom reporters (with prefix)', (t) => {
const stderr = spy()
const root = fileURLToPath(new URL('two-files/', fixtures))
const cwd = new URL('two-files/', fixtures)

t.plan(1)

Expand All @@ -252,7 +251,7 @@ test('reporting', (t) => {
}
}
),
cwd: root,
cwd,
streamError: stderr.stream,
files: ['.'],
extensions: ['txt'],
Expand All @@ -273,14 +272,12 @@ test('reporting', (t) => {
})

t.test('should fail on an unfound reporter', (t) => {
const root = fileURLToPath(new URL('one-file/', fixtures))

t.plan(1)

engine(
{
processor: noop(),
cwd: root,
cwd: new URL('one-file/', fixtures),
files: ['.'],
extensions: ['txt'],
reporter: 'missing'
Expand Down

0 comments on commit 9be24d1

Please sign in to comment.