t-comm
Version:
专业、稳定、纯粹的工具库
97 lines (96 loc) • 3.48 kB
TypeScript
/**
* 判断包是否是腾讯内网包(@tencent 开头)
* @param name 包名
* @returns 是否是 @tencent/xxx 风格的内网包
*/
export declare function isTencentInternalPackage(name: string): boolean;
/**
* 解析 npm 包详情页地址。
* - `@tencent/xxx`:腾讯内网镜像 detail 页(mirrors.tencent.com 私有 npm,repo_id=537)
* - 其他:npmjs.com 公网详情页
*
* @param name 包名
* @returns 完整 URL
* @example
*
* resolveNpmLink('t-comm')
* // 'https://mirrors.tencent.com/#/private/npm/detail?repo_id=537&project_name=%40tencent%2Ft-comm'
*
* resolveNpmLink('lodash')
* // 'https://npmjs.com/package/lodash'
*/
export declare function resolveNpmLink(name: string): string;
/**
* 通过 `npm view <name> time --json` 拿到所有版本的发布时间,
* 按时间倒序返回最近的 N 个版本号(跳过 created/modified 两个非版本号 key)。
*
* 这样做的原因:
* - version-tip 是在「发布完成后」调用的,registry 上已经存在新版本;
* - `time` 字段记录每个版本的 ISO 发布时间,倒序就能依次拿到「新版本」「上一个版本」。
*
* @param {string} name 包名
* @param {number} limit 取最近的几个版本,默认 2(新、旧各一个)
* @returns {string[]} 版本号数组,按时间倒序;失败返回空数组
*/
export declare function getRecentVersions(name: string, limit?: number): string[];
/**
* 通过 `npm pack --dry-run --json` 获取「即将发布」的新包体积。
*
* 注意事项:
* - 必须在项目根目录或正确的 cwd 下执行,否则会发错包。
* - 文件多时输出会撑爆默认 `maxBuffer`(1MB),这里设为 64MB。
* - `--loglevel=error` 用于压住 npm 警告,进一步减少缓冲区占用。
*
* @param options.cwd 工作目录,默认 `process.cwd()`
* @returns 新包体积。失败时返回全 0。
*/
export declare function getNewPackageSize(options?: {
cwd?: string;
}): {
tarballSize: number;
unpackedSize: number;
fileCount: number;
};
/**
* 通过 registry 接口拿指定版本的包体积。
* 兼容字段:
* - 腾讯源 mirrors.tencent.com:`dist.packageSize`(tarball 字节,可能无 unpackedSize)
* - 公网 npm:`dist.size`(tarball) + `dist.unpackedSize`(解包大小) + `dist.fileCount`
*/
export declare function getPackageSizeByVersion(name: string, version: string, options?: {
registry?: string;
timeout?: number;
}): Promise<{
tarballSize: number;
unpackedSize: number;
fileCount: number;
}>;
/**
* 收集 `PackageSizeInfo`:
* - 从 `npm view <name> time` 拿到最近两个版本(新、旧)
* - 分别请求 registry 拿两个版本的体积,对比生成 PackageSizeInfo
*
* 注意:本方法依赖「发布完成后」调用,新版本必须已经在 registry 上。
*
* @param {object} appInfo package.json 内容,至少包含 name
* @param {object} [options]
* @param {string} [options.registry] 自定义 registry
* @returns {Promise<import('t-comm').PackageSizeInfo>}
*/
export declare function collectPackageSize(appInfo: {
name: string;
}, options?: {
registry?: string;
}): Promise<{
newSize: number;
oldSize?: undefined;
oldUnpacked?: undefined;
newUnpacked?: undefined;
fileCount?: undefined;
} | {
oldSize: number;
newSize: number;
oldUnpacked: number;
newUnpacked: number;
fileCount: number;
}>;