@jakerluo/utils
Advanced tools
Comparing version 0.0.2-next.3 to 0.0.2-next.4
"use strict"; | ||
const axios = require("axios"); | ||
const urlJoin = require("url-join"); | ||
const semver = require("semver"); | ||
const npmInfo = require("./npmInfo"); | ||
const log = require("./log"); | ||
function getNpmInfo(npmName, registry) { | ||
if (!npmName) { | ||
return null; | ||
} | ||
const registryUrl = registry || getDefaultRegistry(); | ||
const npmInfoUrl = urlJoin(registryUrl, npmName); | ||
return axios | ||
.get(npmInfoUrl) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
return response.data; | ||
} | ||
return null; | ||
}) | ||
.catch((err) => { | ||
return Promise.reject(err); | ||
}); | ||
} | ||
function getDefaultRegistry(isOriginal = false) { | ||
return isOriginal | ||
? "https://registry.npmjs.org" | ||
: "https://registry.npm.taobao.org"; | ||
} | ||
async function getNpmVersions(npmName, registry) { | ||
const data = await getNpmInfo(npmName, registry); | ||
if (data) { | ||
return Object.keys(data.versions); | ||
} else { | ||
return []; | ||
} | ||
} | ||
function getSemverVersions(baseVersion, versions) { | ||
return versions | ||
.filter((version) => semver.satisfies(version, `>${baseVersion}`)) | ||
.sort((a, b) => (semver.gt(b, a) ? 1 : -1)); | ||
} | ||
async function getNpmSemverVersion(baseVersion, npmName, registry) { | ||
const versions = await getNpmVersions(npmName, registry); | ||
const newVersions = getSemverVersions(baseVersion, versions); | ||
if (newVersions && newVersions.length > 0) { | ||
return newVersions[0]; | ||
} | ||
return null; | ||
} | ||
async function getNpmLatestVersion(npmName, registry) { | ||
let versions = await getNpmVersions(npmName, registry); | ||
if (versions) { | ||
return versions.sort((a, b) => semver.gt(b, a))[0]; | ||
} | ||
return null; | ||
} | ||
module.exports = { | ||
getNpmInfo, | ||
getNpmVersions, | ||
getNpmSemverVersion, | ||
getDefaultRegistry, | ||
getNpmLatestVersion, | ||
...npmInfo, | ||
log, | ||
}; |
{ | ||
"name": "@jakerluo/utils", | ||
"version": "0.0.2-next.3", | ||
"version": "0.0.2-next.4", | ||
"description": "utils", | ||
@@ -31,6 +31,7 @@ "keywords": [ | ||
"axios": "^0.21.4", | ||
"npmlog": "^5.0.1", | ||
"semver": "^7.3.5", | ||
"url-join": "^4.0.1" | ||
}, | ||
"gitHead": "233169a664bbdba36a709dd24172dad4ea867255" | ||
"gitHead": "16e3b61726afee62e8e11418c55bf953d40539c5" | ||
} |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
3787
5
95
4
1
+ Addednpmlog@^5.0.1
+ Addedansi-regex@5.0.1(transitive)
+ Addedaproba@2.0.0(transitive)
+ Addedare-we-there-yet@2.0.0(transitive)
+ Addedcolor-support@1.1.3(transitive)
+ Addedconsole-control-strings@1.1.0(transitive)
+ Addeddelegates@1.0.0(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedgauge@3.0.2(transitive)
+ Addedhas-unicode@2.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addednpmlog@5.0.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwide-align@1.1.5(transitive)