Improve Package Detection (#8306)

This commit is contained in:
Jacob Lamb 2023-09-06 02:15:10 -07:00 committed by GitHub
parent eb8c4cc2fc
commit d2f2a11cdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 36 deletions

View file

@ -0,0 +1,6 @@
---
'create-astro': patch
'astro': patch
---
Support detecting Bun when logging messages with package manager information.

View file

@ -156,7 +156,7 @@
"ora": "^7.0.1", "ora": "^7.0.1",
"p-limit": "^4.0.0", "p-limit": "^4.0.0",
"path-to-regexp": "^6.2.1", "path-to-regexp": "^6.2.1",
"preferred-pm": "^3.0.3", "preferred-pm": "^3.1.2",
"prompts": "^2.4.2", "prompts": "^2.4.2",
"rehype": "^12.0.1", "rehype": "^12.0.1",
"resolve": "^1.22.4", "resolve": "^1.22.4",
@ -172,7 +172,7 @@
"vfile": "^5.3.7", "vfile": "^5.3.7",
"vite": "^4.4.9", "vite": "^4.4.9",
"vitefu": "^0.2.4", "vitefu": "^0.2.4",
"which-pm": "^2.0.0", "which-pm": "^2.1.1",
"yargs-parser": "^21.1.1", "yargs-parser": "^21.1.1",
"zod": "3.21.1" "zod": "3.21.1"
}, },

View file

@ -620,6 +620,8 @@ async function getInstallIntegrationsCommand({
return { pm: 'yarn', command: 'add', flags: [], dependencies }; return { pm: 'yarn', command: 'add', flags: [], dependencies };
case 'pnpm': case 'pnpm':
return { pm: 'pnpm', command: 'add', flags: [], dependencies }; return { pm: 'pnpm', command: 'add', flags: [], dependencies };
case 'bun':
return { pm: 'bun', command: 'add', flags: [], dependencies };
default: default:
return null; return null;
} }

View file

@ -67,6 +67,8 @@ function getInstallCommand(packages: string[], packageManager: string) {
return { pm: 'yarn', command: 'add', flags: [], dependencies: packages }; return { pm: 'yarn', command: 'add', flags: [], dependencies: packages };
case 'pnpm': case 'pnpm':
return { pm: 'pnpm', command: 'add', flags: [], dependencies: packages }; return { pm: 'pnpm', command: 'add', flags: [], dependencies: packages };
case 'bun':
return { pm: 'bun', command: 'add', flags: [], dependencies: packages };
default: default:
return null; return null;
} }

View file

@ -2,14 +2,17 @@
import type yargs from 'yargs-parser'; import type yargs from 'yargs-parser';
import * as msg from '../../core/messages.js'; import * as msg from '../../core/messages.js';
import { telemetry } from '../../events/index.js'; import { telemetry } from '../../events/index.js';
import whichPm from 'which-pm';
interface TelemetryOptions { interface TelemetryOptions {
flags: yargs.Arguments; flags: yargs.Arguments;
} }
export async function notify() { export async function notify() {
const packageManager = (await whichPm(process.cwd())).name ?? 'npm';
await telemetry.notify(() => { await telemetry.notify(() => {
console.log(msg.telemetryNotice() + '\n'); console.log(msg.telemetryNotice(packageManager) + '\n');
return true; return true;
}); });
} }

View file

@ -105,10 +105,10 @@ export function serverStart({
.join('\n'); .join('\n');
} }
export function telemetryNotice() { export function telemetryNotice(packageManager = 'npm') {
const headline = `${cyan('◆')} Astro collects completely anonymous usage data.`; const headline = `${cyan('◆')} Astro collects completely anonymous usage data.`;
const why = dim(' This optional program helps shape our roadmap.'); const why = dim(' This optional program helps shape our roadmap.');
const disable = dim(' Run `npm run astro telemetry disable` to opt-out.'); const disable = dim(` Run \`${packageManager} run astro telemetry disable\` to opt-out.`);
const details = ` Details: ${underline('https://astro.build/telemetry')}`; const details = ` Details: ${underline('https://astro.build/telemetry')}`;
return [headline, why, disable, details].map((v) => ' ' + v).join('\n'); return [headline, why, disable, details].map((v) => ' ' + v).join('\n');
} }

View file

@ -9,7 +9,7 @@ export interface Context {
help: boolean; help: boolean;
prompt: typeof prompt; prompt: typeof prompt;
cwd: string; cwd: string;
pkgManager: string; packageManager: string;
username: string; username: string;
version: string; version: string;
skipHouston: boolean; skipHouston: boolean;
@ -51,7 +51,7 @@ export async function getContext(argv: string[]): Promise<Context> {
{ argv, permissive: true } { argv, permissive: true }
); );
const pkgManager = detectPackageManager()?.name ?? 'npm'; const packageManager = detectPackageManager()?.name ?? 'npm';
const [username, version] = await Promise.all([getName(), getVersion()]); const [username, version] = await Promise.all([getName(), getVersion()]);
let cwd = flags['_'][0]; let cwd = flags['_'][0];
let { let {
@ -85,7 +85,7 @@ export async function getContext(argv: string[]): Promise<Context> {
const context: Context = { const context: Context = {
help, help,
prompt, prompt,
pkgManager, packageManager,
username, username,
version, version,
skipHouston, skipHouston,

View file

@ -6,7 +6,7 @@ import { shell } from '../shell.js';
import type { Context } from './context'; import type { Context } from './context';
export async function dependencies( export async function dependencies(
ctx: Pick<Context, 'install' | 'yes' | 'prompt' | 'pkgManager' | 'cwd' | 'dryRun'> ctx: Pick<Context, 'install' | 'yes' | 'prompt' | 'packageManager' | 'cwd' | 'dryRun'>
) { ) {
let deps = ctx.install ?? ctx.yes; let deps = ctx.install ?? ctx.yes;
if (deps === undefined) { if (deps === undefined) {
@ -25,15 +25,15 @@ export async function dependencies(
await info('--dry-run', `Skipping dependency installation`); await info('--dry-run', `Skipping dependency installation`);
} else if (deps) { } else if (deps) {
await spinner({ await spinner({
start: `Installing dependencies with ${ctx.pkgManager}...`, start: `Installing dependencies with ${ctx.packageManager}...`,
end: 'Dependencies installed', end: 'Dependencies installed',
while: () => { while: () => {
return install({ pkgManager: ctx.pkgManager, cwd: ctx.cwd }).catch((e) => { return install({ packageManager: ctx.packageManager, cwd: ctx.cwd }).catch((e) => {
error('error', e); error('error', e);
error( error(
'error', 'error',
`Dependencies failed to install, please run ${color.bold( `Dependencies failed to install, please run ${color.bold(
ctx.pkgManager + ' install' ctx.packageManager + ' install'
)} to install them manually after setup.` )} to install them manually after setup.`
); );
}); });
@ -47,9 +47,9 @@ export async function dependencies(
} }
} }
async function install({ pkgManager, cwd }: { pkgManager: string; cwd: string }) { async function install({ packageManager, cwd }: { packageManager: string; cwd: string }) {
if (pkgManager === 'yarn') await ensureYarnLock({ cwd }); if (packageManager === 'yarn') await ensureYarnLock({ cwd });
return shell(pkgManager, ['install'], { cwd, timeout: 90_000, stdio: 'ignore' }); return shell(packageManager, ['install'], { cwd, timeout: 90_000, stdio: 'ignore' });
} }
async function ensureYarnLock({ cwd }: { cwd: string }) { async function ensureYarnLock({ cwd }: { cwd: string }) {

View file

@ -3,14 +3,17 @@ import type { Context } from './context';
import { nextSteps, say } from '../messages.js'; import { nextSteps, say } from '../messages.js';
export async function next(ctx: Pick<Context, 'cwd' | 'pkgManager' | 'skipHouston'>) { export async function next(ctx: Pick<Context, 'cwd' | 'packageManager' | 'skipHouston'>) {
let projectDir = path.relative(process.cwd(), ctx.cwd); let projectDir = path.relative(process.cwd(), ctx.cwd);
const devCmd =
ctx.pkgManager === 'npm' const commandMap: { [key: string]: string } = {
? 'npm run dev' npm: 'npm run dev',
: ctx.pkgManager === 'bun' bun: 'bun run dev',
? 'bun run dev' yarn: 'yarn dev',
: `${ctx.pkgManager} dev`; pnpm: 'pnpm dev',
};
const devCmd = commandMap[ctx.packageManager as keyof typeof commandMap] || 'npm run dev';
await nextSteps({ projectDir, devCmd }); await nextSteps({ projectDir, devCmd });
if (!ctx.skipHouston) { if (!ctx.skipHouston) {

View file

@ -10,7 +10,7 @@ describe('dependencies', () => {
const context = { const context = {
cwd: '', cwd: '',
yes: true, yes: true,
pkgManager: 'npm', packageManager: 'npm',
dryRun: true, dryRun: true,
prompt: () => ({ deps: true }), prompt: () => ({ deps: true }),
}; };
@ -21,7 +21,7 @@ describe('dependencies', () => {
it('prompt yes', async () => { it('prompt yes', async () => {
const context = { const context = {
cwd: '', cwd: '',
pkgManager: 'npm', packageManager: 'npm',
dryRun: true, dryRun: true,
prompt: () => ({ deps: true }), prompt: () => ({ deps: true }),
install: undefined, install: undefined,
@ -34,7 +34,7 @@ describe('dependencies', () => {
it('prompt no', async () => { it('prompt no', async () => {
const context = { const context = {
cwd: '', cwd: '',
pkgManager: 'npm', packageManager: 'npm',
dryRun: true, dryRun: true,
prompt: () => ({ deps: false }), prompt: () => ({ deps: false }),
install: undefined, install: undefined,
@ -48,7 +48,7 @@ describe('dependencies', () => {
const context = { const context = {
cwd: '', cwd: '',
install: true, install: true,
pkgManager: 'npm', packageManager: 'npm',
dryRun: true, dryRun: true,
prompt: () => ({ deps: false }), prompt: () => ({ deps: false }),
}; };
@ -61,7 +61,7 @@ describe('dependencies', () => {
const context = { const context = {
cwd: '', cwd: '',
install: false, install: false,
pkgManager: 'npm', packageManager: 'npm',
dryRun: true, dryRun: true,
prompt: () => ({ deps: false }), prompt: () => ({ deps: false }),
}; };

View file

@ -7,14 +7,14 @@ describe('next steps', () => {
const fixture = setup(); const fixture = setup();
it('no arguments', async () => { it('no arguments', async () => {
await next({ skipHouston: false, cwd: './it/fixtures/not-empty', pkgManager: 'npm' }); await next({ skipHouston: false, cwd: './it/fixtures/not-empty', packageManager: 'npm' });
expect(fixture.hasMessage('Liftoff confirmed.')).to.be.true; expect(fixture.hasMessage('Liftoff confirmed.')).to.be.true;
expect(fixture.hasMessage('npm run dev')).to.be.true; expect(fixture.hasMessage('npm run dev')).to.be.true;
expect(fixture.hasMessage('Good luck out there, astronaut!')).to.be.true; expect(fixture.hasMessage('Good luck out there, astronaut!')).to.be.true;
}); });
it('--skip-houston', async () => { it('--skip-houston', async () => {
await next({ skipHouston: true, cwd: './it/fixtures/not-empty', pkgManager: 'npm' }); await next({ skipHouston: true, cwd: './it/fixtures/not-empty', packageManager: 'npm' });
expect(fixture.hasMessage('Good luck out there, astronaut!')).to.be.false; expect(fixture.hasMessage('Good luck out there, astronaut!')).to.be.false;
}); });
}); });

View file

@ -594,8 +594,8 @@ importers:
specifier: ^6.2.1 specifier: ^6.2.1
version: 6.2.1 version: 6.2.1
preferred-pm: preferred-pm:
specifier: ^3.0.3 specifier: ^3.1.2
version: 3.0.3 version: 3.1.2
prompts: prompts:
specifier: ^2.4.2 specifier: ^2.4.2
version: 2.4.2 version: 2.4.2
@ -642,8 +642,8 @@ importers:
specifier: ^0.2.4 specifier: ^0.2.4
version: 0.2.4(vite@4.4.9) version: 0.2.4(vite@4.4.9)
which-pm: which-pm:
specifier: ^2.0.0 specifier: ^2.1.1
version: 2.0.0 version: 2.1.1
yargs-parser: yargs-parser:
specifier: ^21.1.1 specifier: ^21.1.1
version: 21.1.1 version: 21.1.1
@ -6816,7 +6816,7 @@ packages:
meow: 6.1.1 meow: 6.1.1
outdent: 0.5.0 outdent: 0.5.0
p-limit: 2.3.0 p-limit: 2.3.0
preferred-pm: 3.0.3 preferred-pm: 3.1.2
resolve-from: 5.0.0 resolve-from: 5.0.0
semver: 7.5.4 semver: 7.5.4
spawndamnit: 2.0.0 spawndamnit: 2.0.0
@ -15166,8 +15166,8 @@ packages:
tar-fs: 2.1.1 tar-fs: 2.1.1
tunnel-agent: 0.6.0 tunnel-agent: 0.6.0
/preferred-pm@3.0.3: /preferred-pm@3.1.2:
resolution: {integrity: sha512-+wZgbxNES/KlJs9q40F/1sfOd/j7f1O9JaHcW5Dsn3aUUOZg3L2bjpVUcKV2jvtElYfoTuQiNeMfQJ4kwUAhCQ==} resolution: {integrity: sha512-nk7dKrcW8hfCZ4H6klWcdRknBOXWzNQByJ0oJyX97BOupsYD+FzLS4hflgEu/uPUEHZCuRfMxzCBsuWd7OzT8Q==}
engines: {node: '>=10'} engines: {node: '>=10'}
dependencies: dependencies:
find-up: 5.0.0 find-up: 5.0.0
@ -17860,6 +17860,14 @@ packages:
load-yaml-file: 0.2.0 load-yaml-file: 0.2.0
path-exists: 4.0.0 path-exists: 4.0.0
/which-pm@2.1.1:
resolution: {integrity: sha512-xzzxNw2wMaoCWXiGE8IJ9wuPMU+EYhFksjHxrRT8kMT5SnocBPRg69YAMtyV4D12fP582RA+k3P8H9J5EMdIxQ==}
engines: {node: '>=8.15'}
dependencies:
load-yaml-file: 0.2.0
path-exists: 4.0.0
dev: false
/which-typed-array@1.1.11: /which-typed-array@1.1.11:
resolution: {integrity: sha512-qe9UWWpkeG5yzZ0tNYxDmd7vo58HDBc39mZ0xWWpolAGADdFOzkfamWLDxkOWcvHQKVmdTyQdLD4NOfjLWTKew==} resolution: {integrity: sha512-qe9UWWpkeG5yzZ0tNYxDmd7vo58HDBc39mZ0xWWpolAGADdFOzkfamWLDxkOWcvHQKVmdTyQdLD4NOfjLWTKew==}
engines: {node: '>= 0.4'} engines: {node: '>= 0.4'}