diff --git a/packages/cli/src/commands/init/index.ts b/packages/cli/src/commands/init/index.ts index f89397840..aec670777 100644 --- a/packages/cli/src/commands/init/index.ts +++ b/packages/cli/src/commands/init/index.ts @@ -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 ', + description: + 'Use a specific authentication token when connecting to the registry, typically only needed for third-party registries', + }, { name: '--directory ', description: 'Uses a custom directory instead of ``.', diff --git a/packages/cli/src/commands/init/init.ts b/packages/cli/src/commands/init/init.ts index 9f9227491..cd89665cc 100644 --- a/packages/cli/src/commands/init/init.ts +++ b/packages/cli/src/commands/init/init.ts @@ -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; diff --git a/packages/cli/src/commands/init/types.ts b/packages/cli/src/commands/init/types.ts index 57ff1e54a..e02f8ad9a 100644 --- a/packages/cli/src/commands/init/types.ts +++ b/packages/cli/src/commands/init/types.ts @@ -4,6 +4,7 @@ export type Options = { template?: string; npm?: boolean; pm?: PackageManager; + authToken?: string; directory?: string; displayName?: string; title?: string; diff --git a/packages/cli/src/commands/init/version.ts b/packages/cli/src/commands/init/version.ts index 2fb8dfcaf..c16c3f6c1 100644 --- a/packages/cli/src/commands/init/version.ts +++ b/packages/cli/src/commands/init/version.ts @@ -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}`; } diff --git a/packages/cli/src/tools/npm.ts b/packages/cli/src/tools/npm.ts index 3ad234a80..f6d869002 100644 --- a/packages/cli/src/tools/npm.ts +++ b/packages/cli/src/tools/npm.ts @@ -58,14 +58,18 @@ export const getNpmRegistryUrl = (() => { export async function npmResolveConcreteVersion( packageName: string, tagOrVersion: string, + authToken?: string, ): Promise { 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 @@ -126,10 +130,16 @@ const minorVersion = (version: string) => { export async function getTemplateVersion( reactNativeVersion: string, + authToken?: string, ): Promise { - const json = await fetch( - new URL('@react-native-community/template', getNpmRegistryUrl()), - ).then((resp) => resp.json() as Promise); + 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, + ); // 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