Skip to content

Commit

Permalink
fix: parse version and date from different search result formats
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Oct 25, 2022
1 parent 90be5a3 commit 4acb09a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/cmd-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,19 @@ const {
} = require("./core");
const { isConnectionError, is404Error } = require("./error-handler");

const searchEndpoint = async function(keyword, registry) {
const searchEndpoint = async function (keyword, registry) {
if (!registry) registry = env.registry;
try {
const results = await npmSearch(keyword, getNpmFetchOptions());
log.verbose("npmsearch", results);
return results.map(x => {
return [
x.name,
x["dist-tags"] ? x["dist-tags"].latest : "",
x.time && x.time.modified ? x.time.modified.split("T")[0] : "",
""
];
});
return results.map(getTableRow);
} catch (err) {
if (!is404Error(err)) log.error("", err.message);
log.warn("", "fast search endpoint is not available, using old search.");
}
};

const searchOld = async function(keyword) {
const searchOld = async function (keyword) {
// all endpoint
try {
const results = await npmFetch.json("/-/all", getNpmFetchOptions());
Expand All @@ -48,12 +41,7 @@ const searchOld = async function(keyword) {
}
// prepare rows
const rows = objects.map(pkg => {
let name = pkg.name;
let version = getLatestVersion(pkg);
let date =
pkg.time && pkg.time.modified ? pkg.time.modified.split("T")[0] : "";
let item = [name, version, date, ""];
return item;
return getTableRow(pkg);
});
// filter keyword
const klc = keyword.toLowerCase();
Expand All @@ -66,15 +54,27 @@ const searchOld = async function(keyword) {
}
};

const getTable = function() {
const getTable = function () {
var table = new Table({
head: ["Name", "Version", "Date"],
colWidths: [42, 20, 12]
});
return table;
};

module.exports = async function(keyword, options) {
const getTableRow = function (pkg) {
const name = pkg.name;
const version = getLatestVersion(pkg);
let date = "";
if (pkg.time && pkg.time.modified) date = pkg.time.modified.split("T")[0];
if (pkg.date) {
date = pkg.date.toISOString().slice(0, 10)
}
const row = [name, version, date, ""];
return row
}

module.exports = async function (keyword, options) {
// parse env
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
Expand Down
2 changes: 2 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ const getLatestVersion = function(pkgInfo) {
return Object.keys(pkgInfo.versions).find(
key => pkgInfo.versions[key] == "latest"
);
else if (pkgInfo.version)
return pkgInfo.version
};

// Load manifest json file
Expand Down

0 comments on commit 4acb09a

Please sign in to comment.