Skip to content

Commit

Permalink
feat: optimize get npm package info function (#170)
Browse files Browse the repository at this point in the history
* feat: optimize get npm package info function, use npm command as fallback

* feat: update lock file
  • Loading branch information
caohuilin authored Oct 17, 2024
1 parent 83c9120 commit 29343ff
Show file tree
Hide file tree
Showing 7 changed files with 5,463 additions and 4,388 deletions.
7 changes: 7 additions & 0 deletions .changeset/eleven-eyes-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@modern-js/codesmith": patch
---

feat: optimize get npm package info function, use npm command as fallback

feat: 优化获取 npm 包信息函数,使用 npm 命令兜底
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
},
"engines": {
"node": ">=14.17.6",
"pnpm": ">=8.0.0 <=9.0.0"
"pnpm": ">=8.0.0"
},
"packageManager": "pnpm@8.15.8",
"packageManager": "pnpm@9.12.2",
"husky": {
"hooks": {
"pre-commit": "lint-staged",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/downloadPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function getGeneratorVersion(
version = await getNpmVersion(pkgName, {
registryUrl,
version: pkgVersion,
logger,
});
} catch (error: any) {
throw new Error(
Expand Down Expand Up @@ -155,6 +156,7 @@ export async function downloadPackage(
version = await getNpmVersion(pkgName, {
registryUrl,
version: pkgVersion,
logger,
});
logger?.timing(`🕒 get ${pkgName} version`, true);
if (version === undefined) {
Expand All @@ -177,6 +179,7 @@ export async function downloadPackage(
// get package tarball
const tarballPkg = await getNpmTarballUrl(pkgName, version, {
registryUrl,
logger,
});
logger?.timing(`🕒 get ${pkgName}@${version} tarball url`, true);

Expand Down
67 changes: 57 additions & 10 deletions packages/core/src/utils/getNpmPackageInfo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { NPM_API_TIMEOUT } from '@/constants';
import { execa } from '@modern-js/codesmith-utils/execa';
import axios from 'axios';
import type { ILogger } from '../logger/constants';
import { getNpmRegistry } from './getNpmRegistry';
import { timeoutPromise } from './timeoutPromise';

interface Options {
registryUrl?: string;
logger?: ILogger;
}

interface PackageInfo {
Expand All @@ -16,6 +19,42 @@ interface PackageInfo {

const NpmPackageInfoCache = new Map<string, PackageInfo>();

async function getNpmPackageInfoWithCommand(
pkgName: string,
pkgVersion: string,
options?: Options,
): Promise<PackageInfo> {
const { registryUrl } = options || {};
const params = [
'view',
`${pkgName}@${pkgVersion}`,
'dist',
'version',
'--json',
];

if (registryUrl) {
params.push('--registry');
params.push(registryUrl);
}

const getPkgInfoPromise = execa('npm', params);
const { stdout } = await timeoutPromise(
getPkgInfoPromise,
NPM_API_TIMEOUT,
`Get npm tarball of '${pkgName}'`,
);

try {
const pkgDistInfo = JSON.parse(stdout);
return pkgDistInfo;
} catch (e) {
throw new Error(
`Version \`${pkgVersion}\` for package \`${pkgName}\` could not be found`,
);
}
}

export async function getNpmPackageInfo(
pkgName: string,
pkgVersion: string,
Expand All @@ -26,19 +65,27 @@ export async function getNpmPackageInfo(
if (packageInfo) {
return packageInfo;
}
const { registryUrl = await getNpmRegistry() } = options || {};
const { registryUrl = await getNpmRegistry(), logger } = options || {};

const url = `${registryUrl.replace(/\/$/, '')}/${pkgName}/${pkgVersion}`;
const url = `${registryUrl.replace(/\/$/, '')}/${pkgName}/${pkgVersion || 'latest'}`;

const response = await timeoutPromise(
axios.get(url),
NPM_API_TIMEOUT,
`Get npm package info of '${pkgName}'`,
);
let response: PackageInfo;
try {
response = (
await timeoutPromise(
axios.get(url),
NPM_API_TIMEOUT,
`Get npm package info of '${pkgName}'`,
)
).data;
} catch (e) {
logger?.error(e);
response = await getNpmPackageInfoWithCommand(pkgName, pkgVersion, options);
}

const { version } = response.data;
const { version } = response;

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

return response.data;
return response;
}
2 changes: 2 additions & 0 deletions packages/core/src/utils/getNpmTarballUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ILogger } from '../logger/constants';
import { getNpmPackageInfo } from './getNpmPackageInfo';

interface Options {
registryUrl?: string;
logger?: ILogger;
}

export async function getNpmTarballUrl(
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils/getNpmVersion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ILogger } from '../logger/constants';
import { getNpmPackageInfo } from './getNpmPackageInfo';

/**
* get package version
* @param {string} packageName
Expand All @@ -10,6 +10,7 @@ import { getNpmPackageInfo } from './getNpmPackageInfo';
interface Options {
registryUrl?: string;
version?: string;
logger?: ILogger;
}

export async function getNpmVersion(
Expand Down
Loading

0 comments on commit 29343ff

Please sign in to comment.