Skip to content

Commit

Permalink
Add support for passing URL as cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 15, 2022
1 parent af64b44 commit 7a37155
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 135 deletions.
5 changes: 2 additions & 3 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function done(error) {

Directory to search files in, load plugins from, and more.

* Type: `string`
* Type: `string` or `URL`
* Default: [`process.cwd()`][cwd]

###### Example
Expand All @@ -77,14 +77,13 @@ The following example reformats `readme.md`. The `doc` directory is used to
process from.

```js
import {fileURLToPath} from 'node:url'
import {engine} from 'unified-engine'
import {remark} from 'remark'

engine(
{
processor: remark(),
cwd: fileURLToPath(new URL('doc/', import.meta.url)),
cwd: new URL('doc/', import.meta.url),
files: ['readme.md'],
output: true
},
Expand Down
10 changes: 7 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @typedef Settings
* @property {Options['processor']} processor
* @property {Exclude<Options['cwd'], undefined>} cwd
* @property {Exclude<Options['cwd'], undefined | URL>} cwd
* @property {Exclude<Options['files'], undefined>} files
* @property {Exclude<Options['extensions'], undefined>} extensions
* @property {Exclude<Options['streamIn'], undefined>} streamIn
Expand Down Expand Up @@ -62,7 +62,7 @@
* Options for unified engine
* @property {() => Processor} processor
* Unified processor to transform files
* @property {string} [cwd]
* @property {string|URL} [cwd]
* Directory to search files in, load plugins from, and more.
* Defaults to `process.cwd()`.
* @property {Array<string|VFile>} [files]
Expand Down Expand Up @@ -172,6 +172,7 @@

import process from 'node:process'
import {PassThrough} from 'node:stream'
import {fileURLToPath} from 'node:url'
import {statistics} from 'vfile-statistics'
import {fileSetPipeline} from './file-set-pipeline/index.js'

Expand Down Expand Up @@ -214,7 +215,10 @@ export function engine(options, callback) {
settings.processor = options.processor

// Path to run as.
settings.cwd = options.cwd || process.cwd()
settings.cwd =
typeof options.cwd === 'object'
? fileURLToPath(options.cwd)
: options.cwd || process.cwd()

// Input.
settings.files = options.files || []
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ done.

* [`processor`][processor] ([`Processor`][unified-processor])
— unified processor to transform files
* [`cwd`][cwd] (`string`, default: `process.cwd()`)
* [`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
Expand Down
3 changes: 1 addition & 2 deletions test/color.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import process from 'node:process'
import {fileURLToPath} from 'node:url'
import test from 'tape'
import {engine} from '../index.js'
import {noop} from './util/noop-processor.js'
Expand All @@ -17,7 +16,7 @@ test('color', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('empty/', fixtures)),
cwd: new URL('empty/', fixtures),
streamError: stderr.stream,
files: ['readme.md'],
color: true
Expand Down
5 changes: 2 additions & 3 deletions test/completers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import fs from 'node:fs'
import {sep} from 'node:path'
import {fileURLToPath} from 'node:url'
import test from 'tape'
import {engine} from '../index.js'
import {noop} from './util/noop-processor.js'
Expand Down Expand Up @@ -52,7 +51,7 @@ test('completers', (t) => {
t.equal(set.add('two.txt'), set, 'should be able to `add` a file')
}
],
cwd: fileURLToPath(new URL('two-files/', fixtures)),
cwd: new URL('two-files/', fixtures),
files: ['one.txt']
},
(error, code) => {
Expand Down Expand Up @@ -108,7 +107,7 @@ test('completers', (t) => {
set.add('bar.text')
}
],
cwd: fileURLToPath(cwd),
cwd,
files: ['foo.txt'],
output: 'nested/'
},
Expand Down
5 changes: 2 additions & 3 deletions test/configuration-default.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {fileURLToPath} from 'node:url'
import test from 'tape'
import {engine} from '../index.js'
import {noop} from './util/noop-processor.js'
Expand All @@ -25,7 +24,7 @@ test('`defaultConfig`', (t) => {
Object.assign(this, {t})
}),
streamError: stderr.stream,
cwd: fileURLToPath(new URL('config-default/', fixtures)),
cwd: new URL('config-default/', fixtures),
files: ['.'],
packageField: 'bar',
extensions: ['txt'],
Expand All @@ -52,7 +51,7 @@ test('`defaultConfig`', (t) => {
Object.assign(this, {t})
}),
streamError: stderr.stream,
cwd: fileURLToPath(new URL('config-default/', fixtures)),
cwd: new URL('config-default/', fixtures),
files: ['.'],
packageField: 'foo',
extensions: ['txt'],
Expand Down
19 changes: 9 additions & 10 deletions test/configuration-plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {sep} from 'node:path'
import {fileURLToPath} from 'node:url'
import test from 'tape'
import {engine} from '../index.js'
import {noop} from './util/noop-processor.js'
Expand All @@ -21,7 +20,7 @@ test('configuration', (t) => {
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: fileURLToPath(new URL('config-plugins-cascade/', fixtures)),
cwd: new URL('config-plugins-cascade/', fixtures),
streamError: stderr.stream,
files: ['.'],
packageField: 'fooConfig',
Expand Down Expand Up @@ -49,7 +48,7 @@ test('configuration', (t) => {
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: fileURLToPath(new URL('config-plugins-esm-mjs/', fixtures)),
cwd: new URL('config-plugins-esm-mjs/', fixtures),
streamError: stderr.stream,
files: ['one.txt'],
rcName: '.foorc'
Expand All @@ -75,7 +74,7 @@ test('configuration', (t) => {
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: fileURLToPath(new URL('config-plugins-esm-mjs/', fixtures)),
cwd: new URL('config-plugins-esm-mjs/', fixtures),
streamError: stderr.stream,
files: ['one.txt'],
rcName: '.foorc'
Expand All @@ -98,7 +97,7 @@ test('configuration', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('malformed-plugin/', fixtures)),
cwd: new URL('malformed-plugin/', fixtures),
streamError: stderr.stream,
files: ['.'],
packageField: 'fooConfig',
Expand Down Expand Up @@ -127,7 +126,7 @@ test('configuration', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('plugin-without-default/', fixtures)),
cwd: new URL('plugin-without-default/', fixtures),
streamError: stderr.stream,
files: ['.'],
packageField: 'fooConfig',
Expand Down Expand Up @@ -156,7 +155,7 @@ test('configuration', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('missing-plugin/', fixtures)),
cwd: new URL('missing-plugin/', fixtures),
streamError: stderr.stream,
files: ['.'],
packageField: 'fooConfig',
Expand All @@ -183,7 +182,7 @@ test('configuration', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('not-a-plugin/', fixtures)),
cwd: new URL('not-a-plugin/', fixtures),
streamError: stderr.stream,
files: ['.'],
packageField: 'fooConfig',
Expand Down Expand Up @@ -211,7 +210,7 @@ test('configuration', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('throwing-plugin/', fixtures)),
cwd: new URL('throwing-plugin/', fixtures),
streamError: stderr.stream,
files: ['.'],
packageField: 'fooConfig',
Expand Down Expand Up @@ -239,7 +238,7 @@ test('configuration', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('one-file/', fixtures)),
cwd: new URL('one-file/', fixtures),
streamError: stderr.stream,
files: ['.'],
plugins: [
Expand Down
21 changes: 8 additions & 13 deletions test/configuration-presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @typedef {import('unist').Literal} Literal
*/

import {fileURLToPath} from 'node:url'
import test from 'tape'
import {engine} from '../index.js'
import {noop} from './util/noop-processor.js'
Expand All @@ -23,7 +22,7 @@ test('configuration-presets', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(root),
cwd: root,
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand Down Expand Up @@ -54,7 +53,7 @@ test('configuration-presets', (t) => {
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: fileURLToPath(new URL('config-presets-local/', fixtures)),
cwd: new URL('config-presets-local/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand All @@ -78,7 +77,7 @@ test('configuration-presets', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(new URL('config-presets-missing-plugin/', fixtures)),
cwd: new URL('config-presets-missing-plugin/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand Down Expand Up @@ -112,7 +111,7 @@ test('configuration-presets', (t) => {
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: fileURLToPath(new URL('config-plugins-reconfigure/', fixtures)),
cwd: new URL('config-plugins-reconfigure/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand All @@ -139,9 +138,7 @@ test('configuration-presets', (t) => {
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: fileURLToPath(
new URL('config-preset-plugins-reconfigure/', fixtures)
),
cwd: new URL('config-preset-plugins-reconfigure/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand All @@ -166,9 +163,7 @@ test('configuration-presets', (t) => {
engine(
{
processor: noop,
cwd: fileURLToPath(
new URL('config-plugins-reconfigure-off/', fixtures)
),
cwd: new URL('config-plugins-reconfigure-off/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand Down Expand Up @@ -204,7 +199,7 @@ test('configuration-presets', (t) => {

t.deepEqual(this.data('settings'), {alpha: true}, 'should configure')
}),
cwd: fileURLToPath(new URL('config-settings-reconfigure-a/', fixtures)),
cwd: new URL('config-settings-reconfigure-a/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand Down Expand Up @@ -240,7 +235,7 @@ test('configuration-presets', (t) => {
}
})
}),
cwd: fileURLToPath(new URL('config-settings-reconfigure-b/', fixtures)),
cwd: new URL('config-settings-reconfigure-b/', fixtures),
streamError: stderr.stream,
files: ['.'],
rcName: '.foorc',
Expand Down
3 changes: 1 addition & 2 deletions test/configuration-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @typedef {import('../index.js').Preset['plugins']} Plugins
*/

import {fileURLToPath} from 'node:url'
import test from 'tape'
import {engine} from '../index.js'
import {noop} from './util/noop-processor.js'
Expand All @@ -26,7 +25,7 @@ test('`configTransform`', (t) => {
Object.assign(this, {t})
}),
streamError: stderr.stream,
cwd: fileURLToPath(new URL('config-transform/', fixtures)),
cwd: new URL('config-transform/', fixtures),
files: ['.'],
packageField: 'foo',
configTransform(
Expand Down

0 comments on commit 7a37155

Please sign in to comment.