1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
import { createRequire } from 'node:module';
import boxen from 'boxen';
import ci from 'ci-info';
import { execa } from 'execa';
import { bold, cyan, dim, magenta } from 'kleur/colors';
import ora from 'ora';
import preferredPM from 'preferred-pm';
import prompts from 'prompts';
import whichPm from 'which-pm';
import type { Logger } from '../core/logger/core.js';
const require = createRequire(import.meta.url);
type GetPackageOptions = {
skipAsk?: boolean;
optional?: boolean;
cwd?: string;
};
export async function getPackage<T>(
packageName: string,
logger: Logger,
options: GetPackageOptions,
otherDeps: string[] = []
): Promise<T | undefined> {
try {
// Try to resolve with `createRequire` first to prevent ESM caching of the package
// if it errors and fails here
require.resolve(packageName, { paths: [options.cwd ?? process.cwd()] });
const packageImport = await import(packageName);
return packageImport as T;
} catch {
if (options.optional) return undefined;
let message = `To continue, Astro requires the following dependency to be installed: ${bold(
packageName
)}.`;
if (ci.isCI) {
message += ` Packages cannot be installed automatically in CI environments.`;
}
logger.info('SKIP_FORMAT', message);
if (ci.isCI) {
return undefined;
}
const result = await installPackage([packageName, ...otherDeps], options, logger);
if (result) {
const packageImport = await import(packageName);
return packageImport;
} else {
return undefined;
}
}
}
function getInstallCommand(packages: string[], packageManager: string) {
switch (packageManager) {
case 'npm':
return { pm: 'npm', command: 'install', flags: [], dependencies: packages };
case 'yarn':
return { pm: 'yarn', command: 'add', flags: [], dependencies: packages };
case 'pnpm':
return { pm: 'pnpm', command: 'add', flags: [], dependencies: packages };
case 'bun':
return { pm: 'bun', command: 'add', flags: [], dependencies: packages };
default:
return null;
}
}
/**
* Get the command to execute and download a package (e.g. `npx`, `yarn dlx`, `pnpm dlx`, etc.)
* @param packageManager - Optional package manager to use. If not provided, Astro will attempt to detect the preferred package manager.
* @returns The command to execute and download a package
*/
export async function getExecCommand(packageManager?: string): Promise<string> {
if (!packageManager) {
packageManager = (await preferredPM(process.cwd()))?.name ?? 'npm';
}
switch (packageManager) {
case 'npm':
return 'npx';
case 'yarn':
return 'yarn dlx';
case 'pnpm':
return 'pnpm dlx';
case 'bun':
return 'bunx';
default:
return 'npx';
}
}
async function installPackage(
packageNames: string[],
options: GetPackageOptions,
logger: Logger
): Promise<boolean> {
const cwd = options.cwd ?? process.cwd();
const packageManager = (await whichPm(cwd))?.name ?? 'npm';
const installCommand = getInstallCommand(packageNames, packageManager);
if (!installCommand) {
return false;
}
const coloredOutput = `${bold(installCommand.pm)} ${installCommand.command}${[
'',
...installCommand.flags,
].join(' ')} ${cyan(installCommand.dependencies.join(' '))}`;
const message = `\n${boxen(coloredOutput, {
margin: 0.5,
padding: 0.5,
borderStyle: 'round',
})}\n`;
logger.info(
'SKIP_FORMAT',
`\n ${magenta('Astro will run the following command:')}\n ${dim(
'If you skip this step, you can always run it yourself later'
)}\n${message}`
);
let response;
if (options.skipAsk) {
response = true;
} else {
response = (
await prompts({
type: 'confirm',
name: 'askToContinue',
message: 'Continue?',
initial: true,
})
).askToContinue;
}
if (Boolean(response)) {
const spinner = ora('Installing dependencies...').start();
try {
await execa(
installCommand.pm,
[installCommand.command, ...installCommand.flags, ...installCommand.dependencies],
{ cwd: cwd }
);
spinner.succeed();
return true;
} catch (err) {
logger.debug('add', 'Error installing dependencies', err);
spinner.fail();
return false;
}
} else {
return false;
}
}
export async function fetchPackageJson(
scope: string | undefined,
name: string,
tag: string
): Promise<Record<string, any> | Error> {
const packageName = `${scope ? `${scope}/` : ''}${name}`;
const registry = await getRegistry();
const res = await fetch(`${registry}/${packageName}/${tag}`);
if (res.status >= 200 && res.status < 300) {
return await res.json();
} else if (res.status === 404) {
// 404 means the package doesn't exist, so we don't need an error message here
return new Error();
} else {
return new Error(`Failed to fetch ${registry}/${packageName}/${tag} - GET ${res.status}`);
}
}
export async function fetchPackageVersions(packageName: string): Promise<string[] | Error> {
const registry = await getRegistry();
const res = await fetch(`${registry}/${packageName}`, {
headers: { accept: 'application/vnd.npm.install-v1+json' },
});
if (res.status >= 200 && res.status < 300) {
return await res.json().then((data) => Object.keys(data.versions));
} else if (res.status === 404) {
// 404 means the package doesn't exist, so we don't need an error message here
return new Error();
} else {
return new Error(`Failed to fetch ${registry}/${packageName} - GET ${res.status}`);
}
}
// 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
let _registry: string;
async function getRegistry(): Promise<string> {
if (_registry) return _registry;
const fallback = 'https://registry.npmjs.org';
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
try {
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
_registry = stdout?.trim()?.replace(/\/$/, '') || fallback;
// Detect cases where the shell command returned a non-URL (e.g. a warning)
if (!new URL(_registry).host) _registry = fallback;
} catch {
_registry = fallback;
}
return _registry;
}
|