Skip to content

Commit 4d038c5

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

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
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/commands/init/version.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export async function createTemplateUri(
5858
// lower cadence). We have to assume the user is running against the latest nightly by pointing to the tag.
5959
return `${TEMPLATE_PACKAGE_COMMUNITY}@nightly`;
6060
}
61-
const templateVersion = await getTemplateVersion(version);
61+
const templateVersion = await getTemplateVersion(
62+
version,
63+
options.authToken,
64+
);
6265
return `${TEMPLATE_PACKAGE_COMMUNITY}@${templateVersion}`;
6366
}
6467

packages/cli/src/tools/npm.ts

Lines changed: 16 additions & 6 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
@@ -126,10 +130,16 @@ const minorVersion = (version: string) => {
126130

127131
export async function getTemplateVersion(
128132
reactNativeVersion: string,
133+
authToken?: string,
129134
): Promise<TemplateVersion | undefined> {
130-
const json = await fetch(
131-
new URL('@react-native-community/template', getNpmRegistryUrl()),
132-
).then((resp) => resp.json() as Promise<NpmTemplateResponse>);
135+
const url = new URL(getNpmRegistryUrl());
136+
url.pathname = `${url.pathname}@react-native-community/template`;
137+
const headers = authToken
138+
? {Authorization: `Bearer ${authToken}`}
139+
: undefined;
140+
const json = await fetch(url, {headers}).then(
141+
(resp) => resp.json() as Promise<NpmTemplateResponse>,
142+
);
133143

134144
// We are abusing which npm metadata is publicly available through the registry. Scripts
135145
// is always captured, and we use this in the Github Action that manages our releases to

0 commit comments

Comments
 (0)