Skip to content

Commit d313636

Browse files
committed
chore: option to fetch from npm registry
1 parent 0ff48dd commit d313636

4 files changed

Lines changed: 35 additions & 10 deletions

File tree

@iconify/tools/src/download/npm/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ interface IfModifiedSinceOption {
2020
* Options for downloadNPMPackage()
2121
*/
2222
export interface DownloadNPMPackageOptions
23-
extends ExportTargetOptions,
24-
Partial<IfModifiedSinceOption> {
23+
extends ExportTargetOptions, Partial<IfModifiedSinceOption> {
2524
// Package
2625
package: string;
2726

@@ -30,6 +29,10 @@ export interface DownloadNPMPackageOptions
3029

3130
// Log commands
3231
log?: boolean;
32+
33+
// Use fetch instead of 'npm' command to get version
34+
// Can be used in environments where 'npm' command is not available
35+
fetch?: boolean;
3336
}
3437

3538
/**

@iconify/tools/src/download/npm/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ export interface NPMPackageOptions {
77

88
// Tag, default is 'latest'
99
tag?: string;
10+
11+
// Use fetch instead of 'npm' command to get version
12+
// Can be used in environments where 'npm' command is not available
13+
fetch?: boolean;
1014
}

@iconify/tools/src/download/npm/version.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs/promises';
22
import type { NPMPackageOptions } from './types.js';
33
import { execAsync } from '../../misc/exec.js';
4+
import { getFetch } from '../api/fetch.js';
45

56
export interface GetNPMVersionResult {
67
// Version
@@ -17,12 +18,7 @@ export async function getNPMVersion(
1718
options: NPMPackageOptions
1819
): Promise<GetNPMVersionResult> {
1920
const tag = options.tag || 'latest';
20-
const result = await execAsync(
21-
`npm view ${options.package}@${tag} --json`,
22-
{
23-
maxBuffer: 1024 * 1024 * 8,
24-
}
25-
);
21+
const packageName = options.package;
2622

2723
interface NPMViewResponse {
2824
'name': string;
@@ -38,10 +34,30 @@ export async function getNPMVersion(
3834
unpackedSize: number;
3935
};
4036
}
41-
const data = JSON.parse(result.stdout) as NPMViewResponse;
37+
38+
let data: NPMViewResponse;
39+
if (options.fetch) {
40+
// Fetch from registry.npmjs.org
41+
const fetch = getFetch();
42+
data = (await fetch(
43+
`https://registry.npmjs.org/${packageName}/${tag}`
44+
).then((res) => res.json())) as NPMViewResponse;
45+
} else {
46+
// Execute 'npm' command
47+
const result = await execAsync(
48+
`npm view ${packageName}@${tag} --json`,
49+
{
50+
maxBuffer: 1024 * 1024 * 8,
51+
}
52+
);
53+
data = JSON.parse(result.stdout) as NPMViewResponse;
54+
}
55+
4256
return {
4357
version: data.version,
44-
file: data.dist?.tarball,
58+
file:
59+
data.dist?.tarball ??
60+
`https://registry.npmjs.org/${packageName}/-/${packageName.split('/').pop()!}-${data.version}.tgz`,
4561
};
4662
}
4763

@iconify/tools/tests/import/npm-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('Downloading NPM package', () => {
4747
ifModifiedSince: true,
4848
package: testPackage,
4949
target,
50+
fetch: true,
5051
});
5152
const expectedResult: typeof result = {
5253
downloadType: 'npm',
@@ -99,6 +100,7 @@ describe('Downloading NPM package', () => {
99100
package: testPackage,
100101
tag: branch.tag,
101102
target,
103+
exec: false,
102104
});
103105
expect(result).toBe('not_modified');
104106
}

0 commit comments

Comments
 (0)