Skip to content

Commit 4801df0

Browse files
committed
feat(utils): 添加检查包版本的工具函数
- 新增 checkPkgVersion 函数,用于查询 npm 注册表中的包版本 - 函数支持指定包名、dist 标签和注册表地址 - 默认使用 'latest' 标签和官方 npm 注册表 - 添加了相关的单元测试用例
1 parent be6afd1 commit 4801df0

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/utils.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type ChildProcess, type ExecOptions, exec } from 'node:child_process';
22
import fs from 'node:fs';
3+
import path from 'node:path';
34

45
export function normalizePath(path: string) {
56
return path.replace(/\\/g, '/');
@@ -30,3 +31,32 @@ export async function execCommand(
3031
});
3132
});
3233
}
34+
35+
export type CheckPkgUpdate = {
36+
/**
37+
* Package name
38+
*/
39+
name: string;
40+
/**
41+
* Dist tag
42+
* @default 'latest'
43+
* @see https://docs.npmjs.com/cli/dist-tag
44+
*/
45+
distTag?: string;
46+
/**
47+
* NPM registry
48+
* @default 'https://registry.npmjs.org'
49+
*/
50+
registry?: string;
51+
};
52+
53+
export async function checkPkgVersion(pkg: CheckPkgUpdate) {
54+
const url = new URL(pkg.registry || 'https://registry.npmjs.org');
55+
url.pathname = path.join(pkg.name, pkg.distTag || 'latest');
56+
url.searchParams.set('t', process.env.TEST ? '1234567890' : Date.now().toString());
57+
58+
const resp = await fetch(url.toString());
59+
const { version } = (await resp.json()) as { version: string };
60+
61+
return version;
62+
}

test/utils.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
4-
import { execCommand, isDirectory, isFile, normalizePath } from '../src/utils';
4+
import { checkPkgVersion, execCommand, isDirectory, isFile, normalizePath } from '../src/utils';
55
import { testRoot } from './helpers';
66

77
let tempDir: string;
@@ -100,3 +100,37 @@ describe('execCommand', () => {
100100
expect(result.stdout.trim()).toBe('test-value');
101101
});
102102
});
103+
104+
describe('checkPkgVersion', () => {
105+
it('should return package version from npm registry', async () => {
106+
const mockResponse = { version: '1.2.3' };
107+
const mockFetch = vi.fn().mockResolvedValue({
108+
json: () => Promise.resolve(mockResponse),
109+
});
110+
vi.stubGlobal('fetch', mockFetch);
111+
112+
const version = await checkPkgVersion({
113+
name: 'test-package',
114+
distTag: 'xxx',
115+
registry: 'https://registry.npmjs.org',
116+
});
117+
118+
expect(version).toBe('1.2.3');
119+
expect(mockFetch).toHaveBeenCalledWith('https://registry.npmjs.org/test-package/xxx?t=1234567890');
120+
});
121+
122+
it('should use default distTag and registry when not provided', async () => {
123+
const mockResponse = { version: '1.0.0' };
124+
const mockFetch = vi.fn().mockResolvedValue({
125+
json: () => Promise.resolve(mockResponse),
126+
});
127+
vi.stubGlobal('fetch', mockFetch);
128+
129+
const version = await checkPkgVersion({
130+
name: 'test-package',
131+
});
132+
133+
expect(version).toBe('1.0.0');
134+
expect(mockFetch).toHaveBeenCalledWith('https://registry.npmjs.org/test-package/latest?t=1234567890');
135+
});
136+
});

0 commit comments

Comments
 (0)