Skip to content

Commit

Permalink
feat(cli): remove the '--clean-cache' parameter from the search command
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed May 8, 2022
1 parent c51dfb5 commit 7cf2cf0
Show file tree
Hide file tree
Showing 7 changed files with 579 additions and 846 deletions.
1 change: 0 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ program
program
.command("search <keyword>")
.aliases(["s", "se", "find"])
.option("--clean-cache", "clean local cache before search")
.description("Search package by keyword")
.action(async function(keyword, options) {
const retCode = await search(keyword, options);
Expand Down
32 changes: 4 additions & 28 deletions lib/cmd-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const Table = require("cli-table");
const { log } = require("./logger");
const {
env,
cleanCache,
getCache,
getLatestVersion,
getNpmFetchOptions,
parseEnv
Expand All @@ -32,26 +30,10 @@ const searchEndpoint = async function(keyword, registry) {
}
};

const searchOld = async function(keyword, shouldCleanCache) {
// clean cache if needed
if (shouldCleanCache) cleanCache();
// load cache
let cache = getCache();
let cacheKey = env.namespace + ".all";
if (process.env.NODE_ENV == "test") cacheKey = "test." + cacheKey;
cacheKey = cacheKey + ".json";
let cached = cache[cacheKey] || {};
let cachedLen = (cached.objects || []).length;
log.verbose(
"cache",
`has ${cachedLen} package(s), updated at ${cached._updated}`
);
const searchOld = async function(keyword) {
// all endpoint
let serverUrl = cached._updated
? `/-/all/since?stale=update_after&startkey=${cached._updated}`
: `/-/all`;
try {
const results = await npmFetch.json(serverUrl, getNpmFetchOptions());
const results = await npmFetch.json("/-/all", getNpmFetchOptions());
log.verbose("endpoint.all", results);
let objects = [];
if (results) {
Expand All @@ -64,14 +46,8 @@ const searchOld = async function(keyword, shouldCleanCache) {
objects = Object.values(results);
}
}
// contact cached objects and remote objects
const allObjects = objects.concat(cached.objects || []);
// save to cache
cached.objects = allObjects;
cached._updated = Math.round(new Date().getTime());
cache[cacheKey] = cached;
// prepare rows
const rows = allObjects.map(pkg => {
const rows = objects.map(pkg => {
let name = pkg.name;
let version = getLatestVersion(pkg);
let date =
Expand Down Expand Up @@ -107,7 +83,7 @@ module.exports = async function(keyword, options) {
let results = await searchEndpoint(keyword);
// search old search
if (results === undefined) {
results = (await searchOld(keyword, options.cleanCache)) || [];
results = (await searchOld(keyword)) || [];
}
// search upstream
// if (env.upstream) {
Expand Down
22 changes: 0 additions & 22 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const chalk = require("chalk");
const mkdirp = require("mkdirp");
const net = require('node:net');
const isWsl = require("is-wsl");
const keyFileStorage = require("key-file-storage").default;
const TOML = require("@iarna/toml");
const yaml = require("yaml");

Expand Down Expand Up @@ -336,25 +335,6 @@ const saveManifest = function(data) {
}
};

// Return cache object
const getCache = function() {
const homeDir = os.homedir();
const openupmDir = path.join(homeDir, ".openupm");
try {
fs.mkdirSync(openupmDir);
} catch (err) {
if (err.code != "EEXIST") throw err;
}
const kfs = keyFileStorage(openupmDir);
return kfs;
};

// Clean cache
const cleanCache = function() {
const cache = getCache();
delete cache["*"];
};

// Get .upmconfig.toml directory
const getUpmConfigDir = async function() {
let dirPath = "";
Expand Down Expand Up @@ -486,12 +466,10 @@ const isInternalPackage = function(name) {
};

module.exports = {
cleanCache,
compareEditorVersion,
env,
fetchPackageDependencies,
fetchPackageInfo,
getCache,
getLatestVersion,
getNpmFetchOptions,
getUpmConfigDir,
Expand Down
Loading

0 comments on commit 7cf2cf0

Please sign in to comment.