Skip to content

Commit 152c312

Browse files
committed
Improve option validation
1 parent 5ded244 commit 152c312

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 3.4.0
2+
3+
## Features
4+
5+
- Improve options validation
6+
17
# 3.3.0
28

39
## Features

src/dirs.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import { homedir as getHomeDir } from 'os'
2-
import { normalize, dirname } from 'path'
2+
import { dirname } from 'path'
33
import { env } from 'process'
4-
import { fileURLToPath } from 'url'
54

65
// Retrieve list of directories to search:
76
// - current directory
87
// - any parent directory
98
// - home directory
109
// If `global` option is `true`, only home directory is searched
11-
export const getSearchDirs = function ({ cwd, globalOpt }) {
10+
export const getSearchDirs = function (cwd, globalOpt) {
1211
// For tests only
1312
const homeDir = env.TEST_HOME_DIR === undefined ? HOME_DIR : env.TEST_HOME_DIR
1413

1514
if (globalOpt) {
1615
return [homeDir]
1716
}
1817

19-
const cwdA = normalizeCwd(cwd)
20-
const parentDirs = getParentDirs(cwdA)
18+
const parentDirs = getParentDirs(cwd)
2119

2220
if (parentDirs.includes(homeDir)) {
2321
return parentDirs
@@ -26,10 +24,6 @@ export const getSearchDirs = function ({ cwd, globalOpt }) {
2624
return [...parentDirs, homeDir]
2725
}
2826

29-
const normalizeCwd = function (cwd) {
30-
return cwd instanceof URL ? fileURLToPath(cwd) : normalize(cwd)
31-
}
32-
3327
const getParentDirs = function (dir) {
3428
const parentDir = dirname(dir)
3529
const parentDirs = parentDir === dir ? [] : getParentDirs(parentDir)

src/find.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { loadVersionFile } from './load.js'
33
import { getFilePath } from './path.js'
44

55
// Retrieve Node.js version before normalization
6-
export const findVersion = async function ({ cwd, globalOpt }) {
7-
const { filePath, rawVersion } = await getVersionFile({ cwd, globalOpt })
6+
export const findVersion = async function (cwd, globalOpt) {
7+
const { filePath, rawVersion } = await getVersionFile(cwd, globalOpt)
88

99
if (rawVersion !== undefined) {
1010
return { filePath, rawVersion }
@@ -14,8 +14,8 @@ export const findVersion = async function ({ cwd, globalOpt }) {
1414
}
1515

1616
// Retrieve Node.js version file
17-
const getVersionFile = async function ({ cwd, globalOpt }) {
18-
const filePath = await getFilePath({ cwd, globalOpt })
17+
const getVersionFile = async function (cwd, globalOpt) {
18+
const filePath = await getFilePath(cwd, globalOpt)
1919

2020
if (filePath === undefined) {
2121
return {}

src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { getOpts } from './options.js'
88
// `.nvmrc` (or similar files) or `package.json` `engines.node`.
99
export default async function preferredNodeVersion(opts) {
1010
const { cwd, globalOpt, nodeVersionAliasOpts } = getOpts(opts)
11-
const { filePath, envVariable, rawVersion } = await findVersion({
11+
const { filePath, envVariable, rawVersion } = await findVersion(
1212
cwd,
1313
globalOpt,
14-
})
14+
)
1515

1616
if (rawVersion === undefined) {
1717
return {}

src/options.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { normalize } from 'path'
2+
import { fileURLToPath } from 'url'
3+
14
import isPlainObj from 'is-plain-obj'
25

36
// Normalize options and assign default values
@@ -7,6 +10,21 @@ export const getOpts = function (opts = {}) {
710
}
811

912
const { cwd = '.', global: globalOpt = false, fetch: fetchOpt, mirror } = opts
13+
const cwdA = normalizeCwd(cwd)
1014
const nodeVersionAliasOpts = { fetch: fetchOpt, mirror }
11-
return { cwd, globalOpt, nodeVersionAliasOpts }
15+
return { cwd: cwdA, globalOpt, nodeVersionAliasOpts }
16+
}
17+
18+
const normalizeCwd = function (cwd) {
19+
if (objectToString.call(cwd) === '[object URL]') {
20+
return fileURLToPath(cwd)
21+
}
22+
23+
if (typeof cwd !== 'string') {
24+
throw new TypeError(`Option "cwd" must be a string or a URL: ${cwd}`)
25+
}
26+
27+
return normalize(cwd)
1228
}
29+
30+
const { toString: objectToString } = Object.prototype

src/path.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { loadVersionFile, NODE_VERSION_FILES } from './load.js'
88

99
// Find any file indicating the current directory's Node.js version
1010
// Use p-locate instead of find-up for performance (more parallelism)
11-
export const getFilePath = function ({ cwd, globalOpt }) {
12-
const searchFiles = getSearchFiles({ cwd, globalOpt })
11+
export const getFilePath = function (cwd, globalOpt) {
12+
const searchFiles = getSearchFiles(cwd, globalOpt)
1313
return pLocate(searchFiles, isNodeVersionFile)
1414
}
1515

16-
const getSearchFiles = function ({ cwd, globalOpt }) {
17-
const searchDirs = getSearchDirs({ cwd, globalOpt })
16+
const getSearchFiles = function (cwd, globalOpt) {
17+
const searchDirs = getSearchDirs(cwd, globalOpt)
1818
const searchFiles = searchDirs.flatMap(addNodeVersionFiles)
1919
return searchFiles
2020
}

0 commit comments

Comments
 (0)