Skip to content

Commit

Permalink
Fixes an issue where create Astro doesn't respect custom npm registri…
Browse files Browse the repository at this point in the history
…es (#7326)

* Fixes an issue where create Astro doesn't respect custom npm registries

* chore: fix pnpm-lock

* chore: update lockfile

---------

Co-authored-by: Nate Moore <nate@astro.build>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 9, 2023
1 parent 5c20476 commit 1430ffb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-books-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': patch
---

Ensure create-astro respects package manager registry configuration
5 changes: 5 additions & 0 deletions .changeset/yellow-plants-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes issue where Astro doesn't respect custom npm registry settings during project creation
13 changes: 12 additions & 1 deletion packages/astro/src/core/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
deno: '@astrojs/deno',
};

// Users might lack access to the global npm registry, this function
// checks the user's project type and will return the proper npm registry
//
// A copy of this function also exists in the create-astro package
async function getRegistry(): Promise<string> {
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
return stdout || 'https://registry.npmjs.org';
}

export default async function add(names: string[], { cwd, flags, logging, telemetry }: AddOptions) {
applyPolyfill();
if (flags.help || names.length === 0) {
Expand Down Expand Up @@ -673,7 +683,8 @@ async function fetchPackageJson(
tag: string
): Promise<object | Error> {
const packageName = `${scope ? `${scope}/` : ''}${name}`;
const res = await fetch(`https://registry.npmjs.org/${packageName}/${tag}`);
const registry = await getRegistry();
const res = await fetch(`${registry}/${packageName}/${tag}`);
if (res.status === 404) {
return new Error();
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/create-astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"chai": "^4.3.6",
"execa": "^6.1.0",
"giget": "1.0.0",
"mocha": "^9.2.2"
"mocha": "^9.2.2",
"preferred-pm": "^3.0.3"
},
"devDependencies": {
"@types/which-pm-runs": "^1.0.0",
Expand Down
17 changes: 15 additions & 2 deletions packages/create-astro/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ import { color, label, say as houston, spinner as load } from '@astrojs/cli-kit'
import { align, sleep } from '@astrojs/cli-kit/utils';
import { exec } from 'node:child_process';
import { get } from 'node:https';
import { execa } from 'execa';
import preferredPM from 'preferred-pm';
import stripAnsi from 'strip-ansi';

// Users might lack access to the global npm registry, this function
// checks the user's project type and will return the proper npm registry
//
// A copy of this function also exists in the astro package
async function getRegistry(): Promise<string> {
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
return stdout || 'https://registry.npmjs.org';
}

let stdout = process.stdout;
/** @internal Used to mock `process.stdout.write` for testing purposes */
export function setStdout(writable: typeof process.stdout) {
Expand Down Expand Up @@ -63,9 +75,10 @@ export const getName = () =>

let v: string;
export const getVersion = () =>
new Promise<string>((resolve) => {
new Promise<string>(async (resolve) => {
if (v) return resolve(v);
get('https://registry.npmjs.org/astro/latest', (res) => {
const registry = await getRegistry();
get(`${registry}/astro/latest`, (res) => {
let body = '';
res.on('data', (chunk) => (body += chunk));
res.on('end', () => {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1430ffb

Please sign in to comment.