Skip to content

Commit

Permalink
feat: op global package cache (#168)
Browse files Browse the repository at this point in the history
* feat: op global package cache

* feat: npm package info cache use detail version number
  • Loading branch information
caohuilin authored Oct 16, 2024
1 parent d60cd71 commit ac3ad45
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/lucky-pots-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@modern-js/codesmith": patch
---

feat: op global package cache

feat: 优化 global 包缓存逻辑,在版本不变时,缓存一直有效
8 changes: 6 additions & 2 deletions packages/core/src/utils/downloadPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ export async function getGeneratorVersion(
return version;
}

async function isValidCache(cacheDir: string) {
async function isValidCache(cacheDir: string, pkgName: string) {
/* generator cache can use
* 1. .codesmith.completed exist
* 2. cache time is within the validity period
* 3. 对于 @modern-js/codesmith-global 包,缓存一直有效
*/
if (await fsExists(`${cacheDir}/.codesmith.completed`)) {
if (pkgName === '@modern-js/codesmith-global') {
return true;
}
const preCacheTimeStr = await fs.readFile(
`${cacheDir}/.codesmith.completed`,
{
Expand Down Expand Up @@ -163,7 +167,7 @@ export async function downloadPackage(
logger?.debug?.(
`💡 [Download Generator Package]: ${pkgName}@${version} to ${targetDir}`,
);
if ((await fsExists(targetDir)) && (await isValidCache(targetDir))) {
if ((await fsExists(targetDir)) && (await isValidCache(targetDir, pkgName))) {
return targetDir;
}
await fs.remove(targetDir);
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/utils/getNpmPackageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function getNpmPackageInfo(
pkgVersion: string,
options?: Options,
): Promise<PackageInfo> {
const packageName = `${pkgName}/${pkgVersion}`;
const packageName = `${pkgName}@${pkgVersion}`;
const packageInfo = NpmPackageInfoCache.get(packageName);
if (packageInfo) {
return packageInfo;
Expand All @@ -36,7 +36,9 @@ export async function getNpmPackageInfo(
`Get npm package info of '${pkgName}'`,
);

NpmPackageInfoCache.set(packageName, response.data);
const { version } = response.data;

NpmPackageInfoCache.set(`${pkgName}@${version || pkgVersion}`, response.data);

return response.data;
}

0 comments on commit ac3ad45

Please sign in to comment.