Skip to content

Commit

Permalink
Add support for URL to filePath, ignorePath, output, rcPath
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 18, 2023
1 parent d6c6555 commit 4c53efa
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
13 changes: 10 additions & 3 deletions lib/file-pipeline/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import fs from 'node:fs'
import path from 'node:path'
import {fileURLToPath} from 'node:url'
import createDebug from 'debug'

const debug = createDebug('unified-engine:file-pipeline:copy')
Expand All @@ -28,21 +29,27 @@ export function copy(context, file, next) {
const output = context.settings.output
const currentPath = file.path

if (typeof output !== 'string' || file.data.unifiedEngineIgnored) {
if (
output === undefined ||
typeof output === 'boolean' ||
file.data.unifiedEngineIgnored
) {
debug('Not copying')
next()
return
}

const outpath = path.resolve(context.settings.cwd, output)
const outputPath = typeof output === 'object' ? fileURLToPath(output) : output

const outpath = path.resolve(context.settings.cwd, outputPath)

debug('Copying `%s`', currentPath)

fs.stat(outpath, function (error, stats) {
if (error) {
if (
error.code !== 'ENOENT' ||
output.charAt(output.length - 1) === path.sep
outputPath.charAt(outputPath.length - 1) === path.sep
) {
return next(new Error('Cannot read output folder', {cause: error}))
}
Expand Down
10 changes: 8 additions & 2 deletions lib/find-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Configuration.
* @property {string} cwd
* Base.
* @property {string | undefined} filePath
* @property {URL | string | undefined} filePath
* File path of a given file.
* @property {boolean | undefined} [detect=false]
* Whether to detect files (default: `false`).
Expand All @@ -51,6 +51,7 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import {fileURLToPath} from 'node:url'
import createDebug from 'debug'
import {wrap} from 'trough'

Expand Down Expand Up @@ -81,7 +82,12 @@ export class FindUp {

/** @type {string | undefined} */
this.givenFilePath = options.filePath
? path.resolve(options.cwd, options.filePath)
? path.resolve(
options.cwd,
typeof options.filePath === 'object'
? fileURLToPath(options.filePath)
: options.filePath
)
: undefined

/** @type {Array<Callback<Value>> | Error | Value | undefined} */
Expand Down
2 changes: 1 addition & 1 deletion lib/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Whether to detect ignore files.
* @property {string | undefined} ignoreName
* Basename of ignore files.
* @property {string | undefined} ignorePath
* @property {URL | string | undefined} ignorePath
* Explicit path to an ignore file.
* @property {ResolveFrom | undefined} ignorePathResolveFrom
* How to resolve.
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* Search for files with these extensions, when folders are passed
* (optional); generated files are also given the first extension if `treeIn`
* is on and `output` is on or points to a folder.
* @property {string | undefined} [filePath]
* @property {URL | string | undefined} [filePath]
* File path to process the given file on `streamIn` as (optional).
* @property {Array<URL | VFile | string> | undefined} [files]
* Paths or globs to files and folders, or virtual files, to process
Expand All @@ -74,7 +74,7 @@
* (default: `false`).
* @property {string | undefined} [ignoreName]
* Name of ignore files to load (optional).
* @property {string | undefined} [ignorePath]
* @property {URL | string | undefined} [ignorePath]
* Filepath to an ignore file to load (optional).
* @property {ResolveFrom | undefined} [ignorePathResolveFrom]
* Resolve patterns in `ignorePath` from the current working
Expand All @@ -91,7 +91,7 @@
* `false`).
* @property {boolean | undefined} [out=false]
* Whether to write the processed file to `streamOut` (default: `false`).
* @property {boolean | string | undefined} [output=false]
* @property {URL | boolean | string | undefined} [output=false]
* Whether to write successfully processed files, and where to (default:
* `false`).
*
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Configuration (TypeScript type).
— search for files with these extensions, when folders are passed;
generated files are also given the first extension if `treeIn` is on and
`output` is on or points to a folder
* `filePath` (`string`, optional)
* `filePath` (`URL` or `string`, optional)
— file path to process the given file on `streamIn` as
* `files` (`Array<URL | VFile | string>`, optional)
— paths or [globs][node-glob] to files and folder, or virtual files, to
Expand All @@ -304,7 +304,7 @@ Configuration (TypeScript type).
— call back with an unsuccessful (`1`) code on warnings as well as errors
* `ignoreName` (`string`, optional)
— name of ignore files to load
* `ignorePath` (`string`, optional)
* `ignorePath` (`URL` or `string`, optional)
— filepath to an ignore file to load
* `ignorePathResolveFrom` ([`ResolveFrom`][api-resolve-from], default:
`'dir'`)
Expand All @@ -320,7 +320,7 @@ Configuration (TypeScript type).
— whether to output a formatted syntax tree for debugging
* `out` (`boolean`, default: `false`)
— whether to write the processed file to `streamOut`
* `output` (`boolean` or `string`, default: `false`)
* `output` (`URL`, `boolean` or `string`, default: `false`)
— whether to write successfully processed files, and where to; when `true`,
overwrites the given files, when `false`, does not write to the file system;
when pointing to an existing folder, files are written to that folder and
Expand All @@ -338,7 +338,7 @@ Configuration (TypeScript type).
— do not report successful files; given to the reporter
* `rcName` (`string`, optional)
— name of configuration files to load
* `rcPath` (`string`, optional)
* `rcPath` (`URL` or `string`, optional)
— filepath to a configuration file to load
* `reporter` ([`VFileReporter`][api-vfile-reporter] or `string`, default:
`vfile-reporter`)
Expand Down
46 changes: 25 additions & 21 deletions test/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,33 @@ test('ignore', async function (t) {
}
)

await t.test('should support custom ignore files', async function () {
const stderr = spy()
await t.test(
'should support custom ignore files and support URLs',
async function () {
const cwd = new URL('ignore-file/', fixtures)
const stderr = spy()

const code = await run({
cwd: new URL('ignore-file/', fixtures),
detectIgnore: false,
extensions: ['txt'],
files: ['.'],
ignorePath: '.fooignore',
processor: noop,
streamError: stderr.stream
})
const code = await run({
cwd,
detectIgnore: false,
extensions: ['txt'],
files: ['.'],
ignorePath: new URL('.fooignore', cwd),
processor: noop,
streamError: stderr.stream
})

assert.equal(code, 0)
assert.equal(
stderr(),
[
'nested' + sep + 'three.txt: no issues found',
'one.txt: no issues found',
''
].join('\n')
)
})
assert.equal(code, 0)
assert.equal(
stderr(),
[
'nested' + sep + 'three.txt: no issues found',
'one.txt: no issues found',
''
].join('\n')
)
}
)

await t.test('should support searching ignore files', async function () {
const stderr = spy()
Expand Down
4 changes: 2 additions & 2 deletions test/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ test('output', async function (t) {
assert.equal(output, 'two')
})

await t.test('should write to folders', async function () {
await t.test('should write to folders and support URLs', async function () {
const cwd = new URL('simple-structure/', fixtures)
const stderr = spy()

const code = await run({
cwd,
extensions: ['txt'],
files: ['one.txt'],
output: 'nested/',
output: new URL('nested/', cwd),
processor: noop().use(
/** @type {import('unified').Plugin<[], Literal>} */
function () {
Expand Down

0 comments on commit 4c53efa

Please sign in to comment.