Skip to content

Introduce version alias for current node version and project pined version #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
[![Twitter](https://img.shields.io/badge/%E2%80%8B-twitter-4cc61e.svg?logo=twitter)](https://twitter.com/intent/follow?screen_name=ehmicky)
[![Medium](https://img.shields.io/badge/%E2%80%8B-medium-4cc61e.svg?logo=medium)](https://medium.com/@ehmicky)

Normalize and validate Node.js versions
Normalize and validate Node.js versions.

Can guess the current project's version using its `.nvmrc` if you use supported
[aliases](#supported-aliases).

# Example

Expand All @@ -21,6 +24,8 @@ await normalizeNodeVersion('v8.5.0') // '8.5.0'
await normalizeNodeVersion('8.5.2') // Error: Invalid Node version
await normalizeNodeVersion('<7') // '6.17.1'
await normalizeNodeVersion('*') // Latest Node version, e.g. '12.8.0'
await normalizeNodeVersion('_') // Node version used by current process
await normalizeNodeVersion('.') // Node version from a '.nvmrc', '.node-version' or '.naverc' file in the current directory or any parent directory
await normalizeNodeVersion('not_a_version') // Error: Invalid Node version

// All available options
Expand Down Expand Up @@ -64,6 +69,24 @@ Base URL. Can be customized (for example `https://npm.taobao.org/mirrors/node`).
The following environment variables can also be used: `NODE_MIRROR`,
`NVM_NODEJS_ORG_MIRROR`, `N_NODE_MIRROR` or `NODIST_NODE_MIRROR`.

#### cwd

_Type_: `string`\
_Default_: `process.cwd()`

Start from this directory when looking for a Node.js version file when using the
`.` alias (`.node-version`, `.nvmrc` or `.naverc`).

### Supported aliases

`normalizeNodeVersion` supports some Node version aliases you can use as
`versionRange`:

- `_` : Node version used by the current process
- `.` : Node version from a `.nvmrc`, `.node-version` or `.naverc` file in the
current directory or any parent directory. If no version file is found, it
will default to the current process version.

# See also

- [`nve`](https://github.com/ehmicky/nve): Run a specific Node.js version (CLI)
Expand Down
152 changes: 98 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"dependencies": {
"all-node-versions": "^6.0.1",
"core-js": "^3.6.4",
"find-up": "^4.1.0",
"global-cache-dir": "^2.0.0",
"semver": "^7.1.3",
"write-file-atomic": "^3.0.3"
Expand Down
32 changes: 32 additions & 0 deletions src/aliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { promises as fs } from 'fs'
import { version as processVersion } from 'process'

import findUp from 'find-up'

const NODE_VERSION_ALIAS = '.'
const NODE_VERSION_FILES = ['.node-version', '.nvmrc', '.naverc']

const CURRENT_NODE_ALIAS = '_'

const resolveNodeVersionAlias = async ({ cwd }) => {
const nodeVersionFile = await findUp(NODE_VERSION_FILES, { cwd })
if (nodeVersionFile === undefined) return

const nodeVersionFileContent = await fs.readFile(nodeVersionFile, 'utf-8')
return nodeVersionFileContent.trim()
}

export const resolveVersionRangeAlias = async function (
versionRange,
{ cwd } = {},
) {
if (versionRange === CURRENT_NODE_ALIAS) return processVersion

if (versionRange === NODE_VERSION_ALIAS) {
const resolvedVersion = await resolveNodeVersionAlias({ cwd })

return resolvedVersion || processVersion
}

return versionRange
}
9 changes: 7 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import allNodeVersions from 'all-node-versions'
import { maxSatisfying } from 'semver'

import { resolveVersionRangeAlias } from './aliases.js'
import { getCachedVersions, cacheVersions } from './cache.js'
import { handleOfflineError } from './offline.js'

// Retrieve the Node version matching a specific `versionRange`
const normalizeNodeVersion = async function (versionRange, opts) {
const versions = await getVersions(versionRange, opts)
const resolvedVersionRange = await resolveVersionRangeAlias(
versionRange,
opts,
)
const versions = await getVersions(resolvedVersionRange, opts)

const version = maxSatisfying(versions, versionRange)
const version = maxSatisfying(versions, resolvedVersionRange)

if (version === null) {
throw new Error(`Invalid Node version: ${versionRange}`)
Expand Down
Loading