Skip to content

Commit b14e160

Browse files
committed
simplify
1 parent 8583df5 commit b14e160

3 files changed

Lines changed: 26 additions & 22 deletions

File tree

packages/cli/src/commands/publish/npm-utils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ interface PublishOptions {
2121
const NPM_REQUEST_CONCURRENCY_LIMIT = 40;
2222
const NPM_PUBLISH_CONCURRENCY_LIMIT = 10;
2323

24-
const npmRequestQueue = createPromiseQueue(NPM_REQUEST_CONCURRENCY_LIMIT);
25-
const npmPublishQueue = createPromiseQueue(NPM_PUBLISH_CONCURRENCY_LIMIT);
24+
export const npmRequestQueue = createPromiseQueue(
25+
NPM_REQUEST_CONCURRENCY_LIMIT
26+
);
27+
export const npmPublishQueue = createPromiseQueue(
28+
NPM_PUBLISH_CONCURRENCY_LIMIT
29+
);
2630

2731
function jsonParse(input: string) {
2832
try {
@@ -204,7 +208,7 @@ async function internalPublish(
204208
[scope ? `npm_config_${scope}:registry` : "npm_config_registry"]: registry,
205209
};
206210

207-
if (await requiresDelegatedAuth(twoFactorState)) {
211+
if (requiresDelegatedAuth(twoFactorState)) {
208212
const result =
209213
publishTool.name === "pnpm"
210214
? spawnSync("pnpm", ["publish", ...publishFlags], {
@@ -271,9 +275,9 @@ async function internalPublish(
271275
process.stdin.isTTY
272276
) {
273277
// the current otp code must be invalid since it errored
274-
twoFactorState.token = null;
278+
twoFactorState.token = undefined;
275279
// just in case this isn't already true
276-
twoFactorState.isRequired = Promise.resolve(true);
280+
twoFactorState.isRequired = true;
277281
twoFactorState.allowConcurrency = false;
278282
npmPublishQueue.setConcurrency(1);
279283
return {
@@ -303,10 +307,6 @@ export function publish(
303307
twoFactorState: TwoFactorState
304308
): Promise<{ published: boolean }> {
305309
return npmRequestQueue.add(async () => {
306-
if (await requiresDelegatedAuth(twoFactorState)) {
307-
npmPublishQueue.setConcurrency(1);
308-
}
309-
310310
let result: { published: boolean; allowRetry?: boolean };
311311
do {
312312
result = await npmPublishQueue.add(() =>

packages/cli/src/commands/publish/publishPackages.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getTokenIsRequired,
1212
publish,
1313
infoAllow404,
14+
npmPublishQueue,
1415
} from "./npm-utils";
1516
import { TwoFactorState } from "../../utils/types";
1617

@@ -39,17 +40,17 @@ function getReleaseTag(pkgInfo: PkgInfo, preState?: PreState, tag?: string) {
3940
return "latest";
4041
}
4142

42-
const getTwoFactorState = ({
43+
const getTwoFactorState = async ({
4344
otp,
4445
publicPackages,
4546
}: {
4647
otp?: string;
4748
publicPackages: Package[];
48-
}): TwoFactorState => {
49+
}): Promise<TwoFactorState> => {
4950
if (otp) {
5051
return {
5152
token: otp,
52-
isRequired: Promise.resolve(true),
53+
isRequired: true,
5354
};
5455
}
5556

@@ -61,24 +62,23 @@ const getTwoFactorState = ({
6162
isCustomRegistry(process.env.npm_config_registry)
6263
) {
6364
return {
64-
token: null,
65-
isRequired: Promise.resolve(false),
65+
token: undefined,
66+
isRequired: false,
6667
};
6768
}
6869

6970
return {
70-
token: null,
71-
// note: we're not awaiting this here, we want this request to happen in parallel with getUnpublishedPackages
72-
isRequired: getTokenIsRequired(),
71+
token: undefined,
72+
isRequired: await getTokenIsRequired(),
7373
};
7474
};
7575

76-
export const requiresDelegatedAuth = async (twoFactorState: TwoFactorState) => {
76+
export const requiresDelegatedAuth = (twoFactorState: TwoFactorState) => {
7777
return (
7878
process.stdin.isTTY &&
7979
!twoFactorState.token &&
8080
!twoFactorState.allowConcurrency &&
81-
(await twoFactorState.isRequired)
81+
twoFactorState.isRequired
8282
);
8383
};
8484

@@ -106,11 +106,15 @@ export default async function publishPackages({
106106
return [];
107107
}
108108

109-
const twoFactorState = getTwoFactorState({
109+
const twoFactorState = await getTwoFactorState({
110110
otp,
111111
publicPackages,
112112
});
113113

114+
if (requiresDelegatedAuth(twoFactorState)) {
115+
npmPublishQueue.setConcurrency(1);
116+
}
117+
114118
return Promise.all(
115119
unpublishedPackagesInfo.map((pkgInfo) => {
116120
let pkg = packagesByName.get(pkgInfo.name)!;

packages/cli/src/utils/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type TwoFactorState = {
2-
token: string | null;
3-
isRequired: Promise<boolean>;
2+
token: string | undefined;
3+
isRequired: boolean;
44
allowConcurrency?: boolean;
55
};

0 commit comments

Comments
 (0)