Skip to content

Commit 0305382

Browse files
committed
fix: allow specifying an auth token for third-party registries
1 parent ddbba6c commit 0305382

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

packages/cli/src/commands/init/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export default {
2121
description:
2222
'Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `npm`',
2323
},
24+
{
25+
name: '--auth-token <string>',
26+
description:
27+
'Use a specific authentication token when connecting to the registry, typically only needed for third-party registries',
28+
},
2429
{
2530
name: '--directory <string>',
2631
description: 'Uses a custom directory instead of `<projectName>`.',

packages/cli/src/commands/init/init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ export default (async function initialize(
463463
const updatedVersion = await npmResolveConcreteVersion(
464464
options.platformName ?? 'react-native',
465465
version,
466+
options.authToken,
466467
);
467468
logger.debug(`Mapped: ${version} -> ${updatedVersion}`);
468469
version = updatedVersion;

packages/cli/src/commands/init/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type Options = {
44
template?: string;
55
npm?: boolean;
66
pm?: PackageManager;
7+
authToken?: string;
78
directory?: string;
89
displayName?: string;
910
title?: string;

packages/cli/src/tools/npm.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ export const getNpmRegistryUrl = (() => {
5858
export async function npmResolveConcreteVersion(
5959
packageName: string,
6060
tagOrVersion: string,
61+
authToken?: string,
6162
): Promise<string> {
6263
const url = new URL(getNpmRegistryUrl());
63-
url.pathname = `${packageName}/${tagOrVersion}`;
64-
const resp = await fetch(url);
64+
url.pathname = `${url.pathname}${packageName}/${tagOrVersion}`;
65+
const headers = authToken
66+
? {Authorization: `Bearer ${authToken}`}
67+
: undefined;
68+
const resp = await fetch(url, {headers});
6569
if (
6670
[
6771
200, // OK
68-
301, // Moved Permanemently
72+
301, // Moved Permanently
6973
302, // Found
7074
304, // Not Modified
7175
307, // Temporary Redirect

0 commit comments

Comments
 (0)