Skip to content

Commit e150325

Browse files
committed
feat: optimize getting npm info
1 parent 7c85215 commit e150325

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/npm.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import chalk from 'chalk';
43
import inquirer from 'inquirer';
54
import { NPM_REGISTRY, NPM_YARN_REGISTRY } from './constants.js';
65
import { logger } from './logger.js';
@@ -24,8 +23,7 @@ export const getNpmInfo = async (pkg: PackageInfo) => {
2423
const cmd = `npm info ${pkg.name} --json --registry=${pkg.registry}`;
2524
let json: NpmInfo | undefined;
2625
try {
27-
console.info(`Get ${chalk.green(pkg.name)} npm info from ${chalk.green(pkg.registry)}`);
28-
const result = await run(cmd, { spinner: `Fetching ${chalk.green(pkg.name)}` });
26+
const result = await run(cmd);
2927
json = JSON.parse(result);
3028
} catch {}
3129

src/options.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { logger } from './logger.js';
2222
import { getPackageManagerConfig } from './manager.js';
2323
import { getNpmInfo, getNpmRegistry } from './npm.js';
2424
import type { PackageInfo, ReleaseCLIOptions, ReleaseOptions } from './types.js';
25-
import { isScopedPackage, joinArray, setOptions } from './utils.js';
25+
import { createSpin, isScopedPackage, joinArray, setOptions } from './utils.js';
2626
import {
2727
diffColor,
2828
getPreReleaseId,
@@ -52,8 +52,13 @@ export async function getReleaseOptions(options: ReleaseCLIOptions) {
5252
await checkPackagePublishConfig(opts);
5353

5454
// npm info
55-
for (const pkg of opts.pkgs) {
56-
pkg.npmInfo = await getNpmInfo(pkg);
55+
{
56+
const spin = createSpin(`Get npm info from npm registry`);
57+
const list = await Promise.all(opts.pkgs.map(pkg => getNpmInfo(pkg)));
58+
spin.stop();
59+
list.forEach((s, i) => {
60+
opts.pkgs[i].npmInfo = s;
61+
});
5762
}
5863

5964
await selectTypeVersion(opts);
@@ -81,7 +86,7 @@ async function checkCLIOptions(opts: ReleaseCLIOptions) {
8186
}
8287
}
8388

84-
if (!fs.existsSync(cwd)) {
89+
if (!fs.existsSync(cwd!)) {
8590
throw new Error(`[${chalk.yellow('--cwd')}] Directory "${chalk.red(cwd)}" does not exist.`);
8691
}
8792

@@ -96,7 +101,7 @@ async function checkCLIOptions(opts: ReleaseCLIOptions) {
96101
}
97102

98103
async function findPackages(opts: ReleaseOptions) {
99-
const { rootPackage, packages } = await getPackages(opts.cwd);
104+
const { rootPackage, packages } = await getPackages(opts.cwd!);
100105
if (!rootPackage || !packages || packages.length == 0) {
101106
throw new Error('No root package found.');
102107
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface PackageInfo {
5656
/**
5757
* The npm info from npm registry.
5858
*/
59-
npmInfo?: NpmInfo;
59+
npmInfo: NpmInfo;
6060
}
6161

6262
export { ReleaseCLIOptions };

src/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ interface RunExecaOptions extends ExecaOptions {
5959
dryRunOption?: boolean;
6060
}
6161

62+
/**
63+
* create terminal spinner.
64+
* @param msg display message
65+
* @returns
66+
*/
67+
export function createSpin(msg: string) {
68+
return ora(fixOraDisplay(msg)).start();
69+
}
70+
6271
/**
6372
* run a command
6473
* @param cmd command
@@ -104,7 +113,7 @@ export async function run(cmd: string | string[], options?: RunExecaOptions): Pr
104113
let spin: Ora | undefined;
105114
if (spinner) {
106115
const msg = typeof spinner === 'string' ? spinner : 'Running...';
107-
spin = ora(fixOraDisplay(msg)).start();
116+
spin = createSpin(msg);
108117
}
109118

110119
if (dryRun) {
@@ -130,7 +139,7 @@ export async function run(cmd: string | string[], options?: RunExecaOptions): Pr
130139

131140
log(msg);
132141

133-
return Promise.reject(msg);
142+
throw new ReleaseError(msg);
134143
}
135144
}
136145

0 commit comments

Comments
 (0)