js2bin-version-info
Advanced tools
Comparing version 1.0.1 to 1.1.0
86
index.js
@@ -64,3 +64,14 @@ /** | ||
class VersionInfo { | ||
constructor() { | ||
/** | ||
* constructor | ||
* | ||
* @param {object} [opt] - options | ||
* @param {boolean} [opt.active] - for 'ci', get only latest active version | ||
* @param {boolean} [opt.current] - for 'ci', include current nodejs release | ||
* @param {number} [opt.timeout] - timeout on fetch, in milliseconds | ||
*/ | ||
constructor(opt = {}) { | ||
const { active, current, timeout } = opt; | ||
this._active = !!active; | ||
this._current = !!current; | ||
this._js2bin = { | ||
@@ -75,5 +86,37 @@ latest: null, | ||
}; | ||
this._timeout = TIMEOUT_FETCH; | ||
this._timeout = | ||
Number.isInteger(timeout) && timeout >= 0 ? timeout : TIMEOUT_FETCH; | ||
this._url = { | ||
js2binRelease: JS2BIN_RELEASE, | ||
nodeRelease: NODEJS_RELEASE, | ||
nodeSchedule: NODEJS_SCHEDULE | ||
}; | ||
} | ||
get active() { | ||
return !!this._active; | ||
} | ||
set active(bool) { | ||
this._active = !!bool; | ||
} | ||
get current() { | ||
return !!this._current; | ||
} | ||
set current(bool) { | ||
this._current = !!bool; | ||
} | ||
get timeout() { | ||
return this._timeout; | ||
} | ||
set timeout(ms) { | ||
if (Number.isInteger(ms) && ms >= 0) { | ||
this._timeout = ms; | ||
} | ||
} | ||
/** | ||
@@ -101,3 +144,2 @@ * get version list | ||
} else { | ||
// only latest version of each LTS | ||
name === 'nodelts' && versionList.add(value.latest); | ||
@@ -116,3 +158,3 @@ } | ||
async _setJs2binVersions() { | ||
const res = await fetchJson(JS2BIN_RELEASE, this._timeout); | ||
const res = await fetchJson(this._url.js2binRelease, this._timeout); | ||
if (Array.isArray(res)) { | ||
@@ -123,3 +165,4 @@ const [{ assets }] = res; | ||
const { name } = asset; | ||
if (REG_PLATFORM.test(name) && REG_SEMVER.test(name)) { | ||
if (isString(name) && | ||
REG_PLATFORM.test(name) && REG_SEMVER.test(name)) { | ||
const [, platform] = REG_PLATFORM.exec(name); | ||
@@ -148,3 +191,3 @@ const [, version] = REG_SEMVER.exec(name); | ||
async _setNodeltsCodenames() { | ||
const stats = await fetchJson(NODEJS_SCHEDULE, this._timeout); | ||
const stats = await fetchJson(this._url.nodeSchedule, this._timeout); | ||
if (stats && Object.keys(stats).every(key => REG_NODEJS_KEY.test(key))) { | ||
@@ -155,8 +198,12 @@ const now = Date.now(); | ||
const { codename, end, start } = value; | ||
const isActive = new Date(end) > now && new Date(start) < now; | ||
// only if LTS is active or still maintained | ||
if (codename && isActive) { | ||
this._nodelts[codename] = new VersionContainer(); | ||
if (codename) { | ||
const isActive = new Date(end) > now && new Date(start) < now; | ||
if (isActive) { | ||
this._nodelts[codename] = new VersionContainer(); | ||
} | ||
} | ||
} | ||
if (this._current) { | ||
this._nodelts.current = new VersionContainer(); | ||
} | ||
} | ||
@@ -171,7 +218,8 @@ } | ||
async _setNodeltsVersions() { | ||
const res = await fetchJson(NODEJS_RELEASE, this._timeout); | ||
const res = await fetchJson(this._url.nodeRelease, this._timeout); | ||
if (Array.isArray(res)) { | ||
await this._setNodeltsCodenames(); | ||
for (const item of res) { | ||
const { lts: codename, version: nodeVersion } = item; | ||
const { lts, version: nodeVersion } = item; | ||
const codename = lts || (this._current && 'current'); | ||
if (codename && this._nodelts[codename] && | ||
@@ -199,3 +247,4 @@ REG_SEMVER.test(nodeVersion)) { | ||
* @param {object} [opt] - options | ||
* @param {boolean} [opt.active] - opt for 'ci', get only active LTS version | ||
* @param {boolean} [opt.active] - for 'ci', get only latest active version | ||
* @param {boolean} [opt.current] - for 'ci', include current nodejs release | ||
* @param {number} [opt.timeout] - timeout on fetch, in milliseconds | ||
@@ -211,4 +260,7 @@ * @returns {(string|Array|null)} - result | ||
let res; | ||
const { active, timeout } = opt; | ||
if (Number.isInteger(timeout)) { | ||
const { active, current, timeout } = opt; | ||
if (typeof current === 'boolean') { | ||
this._current = current; | ||
} | ||
if (Number.isInteger(timeout) && timeout >= 0) { | ||
this._timeout = timeout; | ||
@@ -220,4 +272,4 @@ } | ||
await this._setNodeltsVersions(); | ||
if (active) { | ||
const latest = this._nodelts.latest; | ||
if (active || this._active) { | ||
const { latest } = this._nodelts; | ||
if (latest && !js2binList.includes(latest)) { | ||
@@ -224,0 +276,0 @@ res = latest; |
@@ -24,6 +24,6 @@ { | ||
"chai": "^4.2.0", | ||
"eslint": "^7.13.0", | ||
"eslint-config-standard": "^16.0.1", | ||
"eslint": "^7.15.0", | ||
"eslint-config-standard": "^16.0.2", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jsdoc": "^30.7.7", | ||
"eslint-plugin-jsdoc": "^30.7.8", | ||
"eslint-plugin-node": "^11.1.0", | ||
@@ -35,3 +35,3 @@ "eslint-plugin-promise": "^4.2.1", | ||
}, | ||
"version": "1.0.1" | ||
"version": "1.1.0" | ||
} |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://travis-ci.com/asamuzaK/js2bin-version-info.svg?branch=main)](https://travis-ci.com/asamuzaK/js2bin-version-info) | ||
[![build](https://github.com/asamuzaK/js2bin-version-info/workflows/build/badge.svg)](https://github.com/asamuzaK/js2bin-version-info/actions?query=workflow%3Abuild) | ||
[![npm version](https://badge.fury.io/js/js2bin-version-info.svg)](https://badge.fury.io/js/js2bin-version-info) | ||
@@ -35,4 +35,36 @@ | ||
### Options | ||
You can give options either when you create an instance or when you get versions. | ||
``` | ||
const info = new VersionInfo(opt); | ||
``` | ||
* @param {object} [opt] - options | ||
* @param {boolean} [opt.active] - for 'ci', get only the latest active version | ||
* @param {boolean} [opt.current] - for 'ci', include current nodejs release | ||
* @param {number} [opt.timeout] - timeout on fetch, in milliseconds | ||
``` | ||
const getVersionsForCi = async () => { | ||
const info = new VersionInfo({ | ||
current: true, | ||
timeout: 10000 | ||
}); | ||
const versions = await info.get('ci'); | ||
return versions; | ||
}; | ||
const getVersionForBuild = async () => { | ||
const info = new VersionInfo(); | ||
const version = await info.get('build', { | ||
timeout: 10000 | ||
}); | ||
return version; | ||
}; | ||
``` | ||
## Demo | ||
Run `npm run demo` for the live demo. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13967
6
324
70