Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds
getNodeVersion
util (#1002)
* feat: add `getNodeVersion` util * chore: remove ESLint comment * chore: add more unit tests
- Loading branch information
1 parent
ccb1e4a
commit b66177d
Showing
8 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// eslint-disable-next-line no-magic-numbers | ||
type SupportedVersionNumbers = 8 | 10 | 12 | 14 | ||
type NodeVersion = `${SupportedVersionNumbers}.x` | `nodejs${SupportedVersionNumbers}.x` | ||
|
||
// Must match the default version used in Bitballoon. | ||
const DEFAULT_NODE_VERSION = 14 | ||
const VERSION_REGEX = /(nodejs)?(\d+)\.x/ | ||
|
||
const getNodeVersion = (configVersion?: string) => parseVersion(configVersion) ?? DEFAULT_NODE_VERSION | ||
|
||
// Takes a string in the format defined by the `NodeVersion` type and returns | ||
// the numeric major version (e.g. "nodejs14.x" => 14). | ||
const parseVersion = (input: string | undefined) => { | ||
if (input === undefined) { | ||
return | ||
} | ||
|
||
const match = input.match(VERSION_REGEX) | ||
|
||
if (match === null) { | ||
return | ||
} | ||
|
||
const version = Number.parseInt(match[2]) | ||
|
||
if (Number.isNaN(version)) { | ||
return | ||
} | ||
|
||
return version | ||
} | ||
|
||
export { DEFAULT_NODE_VERSION, getNodeVersion, parseVersion, NodeVersion } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const test = require('ava') | ||
|
||
const { | ||
DEFAULT_NODE_VERSION, | ||
getNodeVersion, | ||
parseVersion, | ||
} = require('../../../../../dist/runtimes/node/utils/node_version') | ||
|
||
test('getNodeVersion', (t) => { | ||
t.is(getNodeVersion('nodejs12.x'), 12) | ||
t.is(getNodeVersion('nodejs8.x'), 8) | ||
t.is(getNodeVersion('12.x'), 12) | ||
t.is(getNodeVersion('8.x'), 8) | ||
t.is(getNodeVersion('node14'), DEFAULT_NODE_VERSION) | ||
t.is(getNodeVersion(':shrug:'), DEFAULT_NODE_VERSION) | ||
}) | ||
|
||
test('parseVersion', (t) => { | ||
t.is(parseVersion('nodejs12.x'), 12) | ||
t.is(parseVersion('nodejs8.x'), 8) | ||
t.is(parseVersion('12.x'), 12) | ||
t.is(parseVersion('8.x'), 8) | ||
t.is(parseVersion('node14'), undefined) | ||
t.is(parseVersion(':shrug:'), undefined) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b66177d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results
largeDepsEsbuild: 6.8s
largeDepsNft: 32.3s
largeDepsZisi: 58.4s