Skip to content

Commit ac3ad45

Browse files
authored
feat: op global package cache (#168)
* feat: op global package cache * feat: npm package info cache use detail version number
1 parent d60cd71 commit ac3ad45

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

.changeset/lucky-pots-sleep.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@modern-js/codesmith": patch
3+
---
4+
5+
feat: op global package cache
6+
7+
feat: 优化 global 包缓存逻辑,在版本不变时,缓存一直有效

packages/core/src/utils/downloadPackage.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ export async function getGeneratorVersion(
5757
return version;
5858
}
5959

60-
async function isValidCache(cacheDir: string) {
60+
async function isValidCache(cacheDir: string, pkgName: string) {
6161
/* generator cache can use
6262
* 1. .codesmith.completed exist
6363
* 2. cache time is within the validity period
64+
* 3. 对于 @modern-js/codesmith-global 包,缓存一直有效
6465
*/
6566
if (await fsExists(`${cacheDir}/.codesmith.completed`)) {
67+
if (pkgName === '@modern-js/codesmith-global') {
68+
return true;
69+
}
6670
const preCacheTimeStr = await fs.readFile(
6771
`${cacheDir}/.codesmith.completed`,
6872
{
@@ -163,7 +167,7 @@ export async function downloadPackage(
163167
logger?.debug?.(
164168
`💡 [Download Generator Package]: ${pkgName}@${version} to ${targetDir}`,
165169
);
166-
if ((await fsExists(targetDir)) && (await isValidCache(targetDir))) {
170+
if ((await fsExists(targetDir)) && (await isValidCache(targetDir, pkgName))) {
167171
return targetDir;
168172
}
169173
await fs.remove(targetDir);

packages/core/src/utils/getNpmPackageInfo.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function getNpmPackageInfo(
2121
pkgVersion: string,
2222
options?: Options,
2323
): Promise<PackageInfo> {
24-
const packageName = `${pkgName}/${pkgVersion}`;
24+
const packageName = `${pkgName}@${pkgVersion}`;
2525
const packageInfo = NpmPackageInfoCache.get(packageName);
2626
if (packageInfo) {
2727
return packageInfo;
@@ -36,7 +36,9 @@ export async function getNpmPackageInfo(
3636
`Get npm package info of '${pkgName}'`,
3737
);
3838

39-
NpmPackageInfoCache.set(packageName, response.data);
39+
const { version } = response.data;
40+
41+
NpmPackageInfoCache.set(`${pkgName}@${version || pkgVersion}`, response.data);
4042

4143
return response.data;
4244
}

0 commit comments

Comments
 (0)