Skip to content

Commit

Permalink
Support the cwd option (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyunhe authored Feb 10, 2024
1 parent d4be48a commit 468f601
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ The default export is a recma plugin.

### Options

- `name` (`string`, default: `'filepath'`) — The name to export the file path as.
- `absolute` (`boolean`, default: `false`) — If true, use an absolute path. By default a relative
path is used.
- `cwd` (`string`) The current working directory to use when generating a relative file path.
- `name` (`string`, default: `'filepath'`) — The name to export the file path as.

## Compatibility

Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { relative } from 'node:path'
import { relative, resolve } from 'node:path'

import { name as isIdentifierName } from 'estree-util-is-identifier-name'
import normalizePath from 'normalize-path'
Expand All @@ -7,6 +7,8 @@ import normalizePath from 'normalize-path'
* @typedef RecmaExportFilepathOptions
* @property {boolean} [absolute=false]
* If true, use an absolute path. By default a relative path is used.
* @property {string} [cwd]
* The current working directory to use when generating a relative file path.
* @property {string} [name='filepath']
* The name to export the file path as.
*/
Expand All @@ -16,7 +18,8 @@ import normalizePath from 'normalize-path'
*
* @type {import('unified').Plugin<[RecmaExportFilepathOptions?], import('estree').Program>}
*/
export default function recmaExportFilepath({ absolute = false, name = 'filepath' } = {}) {
export default function recmaExportFilepath(options = {}) {
const { absolute = false, cwd, name = 'filepath' } = options
if (!isIdentifierName(name)) {
throw new Error(`Name this should be a valid identifier, got: ${JSON.stringify(name)}`)
}
Expand All @@ -25,11 +28,9 @@ export default function recmaExportFilepath({ absolute = false, name = 'filepath
let value = file.path

if (value) {
const cwd = normalizePath(file.cwd)
value = normalizePath(value)

if (!absolute) {
value = relative(cwd, value)
value = relative(cwd ? resolve(file.cwd, cwd) : normalizePath(file.cwd), value)
}
} else {
const message = file.message('Missing file.path', {
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ test('absolute path', () => {
assert.equal(String(result), 'export const filepath = "/home/user/example.js";\n')
})

test('cwd (absolute)', () => {
const result = recma().use(recmaExportFilepath, { cwd: '/home/user/docs/' }).processSync({
cwd: '/home/user',
path: '/home/user/docs/example.md',
value: ''
})

assert.equal(String(result), 'export const filepath = "example.md";\n')
})

test('cwd (relative)', () => {
const result = recma().use(recmaExportFilepath, { cwd: 'docs' }).processSync({
cwd: '/home/user',
path: '/home/user/docs/example.md',
value: ''
})

assert.equal(String(result), 'export const filepath = "example.md";\n')
})

test('cwd (relative with parent dir)', () => {
const result = recma().use(recmaExportFilepath, { cwd: '..' }).processSync({
cwd: '/home/user',
path: '/home/user/docs/example.md',
value: ''
})

assert.equal(String(result), 'export const filepath = "user/docs/example.md";\n')
})

test('insert as first statement', () => {
const result = recma().use(recmaExportFilepath).processSync({
cwd: '/home/user',
Expand Down

0 comments on commit 468f601

Please sign in to comment.