Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export default {
description:
'Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `npm`',
},
{
name: '--auth-token <string>',
description:
'Use a specific authentication token when connecting to the registry, typically only needed for third-party registries',
},
{
name: '--directory <string>',
description: 'Uses a custom directory instead of `<projectName>`.',
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ export default (async function initialize(
const updatedVersion = await npmResolveConcreteVersion(
options.platformName ?? 'react-native',
version,
options.authToken,
);
logger.debug(`Mapped: ${version} -> ${updatedVersion}`);
version = updatedVersion;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/init/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type Options = {
template?: string;
npm?: boolean;
pm?: PackageManager;
authToken?: string;
directory?: string;
displayName?: string;
title?: string;
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/commands/init/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export async function createTemplateUri(
// lower cadence). We have to assume the user is running against the latest nightly by pointing to the tag.
return `${TEMPLATE_PACKAGE_COMMUNITY}@nightly`;
}
const templateVersion = await getTemplateVersion(version);
const templateVersion = await getTemplateVersion(
version,
options.authToken,
);
return `${TEMPLATE_PACKAGE_COMMUNITY}@${templateVersion}`;
}

Expand Down
22 changes: 16 additions & 6 deletions packages/cli/src/tools/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ export const getNpmRegistryUrl = (() => {
export async function npmResolveConcreteVersion(
packageName: string,
tagOrVersion: string,
authToken?: string,
): Promise<string> {
const url = new URL(getNpmRegistryUrl());
url.pathname = `${packageName}/${tagOrVersion}`;
const resp = await fetch(url);
url.pathname = `${url.pathname}${packageName}/${tagOrVersion}`;
const headers = authToken
? {Authorization: `Bearer ${authToken}`}
: undefined;
const resp = await fetch(url, {headers});
if (
[
200, // OK
301, // Moved Permanemently
301, // Moved Permanently
302, // Found
304, // Not Modified
307, // Temporary Redirect
Expand Down Expand Up @@ -126,10 +130,16 @@ const minorVersion = (version: string) => {

export async function getTemplateVersion(
reactNativeVersion: string,
authToken?: string,
): Promise<TemplateVersion | undefined> {
const json = await fetch(
new URL('@react-native-community/template', getNpmRegistryUrl()),
).then((resp) => resp.json() as Promise<NpmTemplateResponse>);
const url = new URL(getNpmRegistryUrl());
url.pathname = `${url.pathname}@react-native-community/template`;
const headers = authToken
? {Authorization: `Bearer ${authToken}`}
: undefined;
const json = await fetch(url, {headers}).then(
(resp) => resp.json() as Promise<NpmTemplateResponse>,
);

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