|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { pathToFileURL } from 'url'; |
| 3 | +import { resolve } from 'import-meta-resolve'; |
1 | 4 | import { adapters } from './adapters.js';
|
| 5 | +import { dirname, join } from 'path'; |
| 6 | +import { existsSync } from 'fs'; |
2 | 7 |
|
3 | 8 | /** @type {import('./index').default} */
|
4 | 9 | let fn;
|
5 | 10 |
|
| 11 | +/** @type {Record<string, (name: string) => string>} */ |
| 12 | +const commands = { |
| 13 | + npm: (name) => `npm install -D ${name}`, |
| 14 | + pnpm: (name) => `pnpm add -D ${name}`, |
| 15 | + yarn: (name) => `yarn add -D ${name}` |
| 16 | +}; |
| 17 | + |
| 18 | +function detect_lockfile() { |
| 19 | + let dir = process.cwd(); |
| 20 | + |
| 21 | + do { |
| 22 | + if (existsSync(join(dir, 'pnpm-lock.yaml'))) return 'pnpm'; |
| 23 | + if (existsSync(join(dir, 'yarn.lock'))) return 'yarn'; |
| 24 | + if (existsSync(join(dir, 'package-lock.json'))) return 'npm'; |
| 25 | + } while (dir !== (dir = dirname(dir))); |
| 26 | + |
| 27 | + return 'npm'; |
| 28 | +} |
| 29 | + |
| 30 | +function detect_package_manager() { |
| 31 | + const manager = detect_lockfile(); |
| 32 | + |
| 33 | + try { |
| 34 | + execSync(`${manager} --version`); |
| 35 | + return manager; |
| 36 | + } catch { |
| 37 | + return 'npm'; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** @param {string} name */ |
| 42 | +async function import_from_cwd(name) { |
| 43 | + const cwd = pathToFileURL(process.cwd()).href; |
| 44 | + const url = await resolve(name, cwd + '/x.js'); |
| 45 | + |
| 46 | + return import(url); |
| 47 | +} |
| 48 | + |
6 | 49 | for (const candidate of adapters) {
|
7 | 50 | if (candidate.test()) {
|
8 | 51 | /** @type {{ default: () => import('@sveltejs/kit').Adapter }} */
|
9 | 52 | let module;
|
10 | 53 |
|
11 | 54 | try {
|
12 |
| - module = await import(candidate.module); |
13 |
| - |
14 |
| - fn = () => { |
15 |
| - const adapter = module.default(); |
16 |
| - return { |
17 |
| - ...adapter, |
18 |
| - adapt: (builder) => { |
19 |
| - builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`); |
20 |
| - return adapter.adapt(builder); |
21 |
| - } |
22 |
| - }; |
23 |
| - }; |
24 |
| - |
25 |
| - break; |
| 55 | + module = await import_from_cwd(candidate.module); |
26 | 56 | } catch (error) {
|
27 | 57 | if (
|
28 | 58 | error.code === 'ERR_MODULE_NOT_FOUND' &&
|
29 | 59 | error.message.startsWith(`Cannot find package '${candidate.module}'`)
|
30 | 60 | ) {
|
31 |
| - throw new Error( |
32 |
| - `It looks like ${candidate.module} is not installed. Please install it and try building your project again.` |
33 |
| - ); |
34 |
| - } |
| 61 | + const package_manager = detect_package_manager(); |
| 62 | + const command = commands[package_manager](candidate.module); |
| 63 | + |
| 64 | + try { |
| 65 | + console.log(`Installing ${candidate.module}...`); |
| 66 | + |
| 67 | + execSync(command, { |
| 68 | + stdio: 'inherit', |
| 69 | + env: { |
| 70 | + ...process.env, |
| 71 | + NODE_ENV: undefined |
| 72 | + } |
| 73 | + }); |
35 | 74 |
|
36 |
| - throw error; |
| 75 | + module = await import_from_cwd(candidate.module); |
| 76 | + |
| 77 | + console.log(`Successfully installed ${candidate.module}.`); |
| 78 | + console.warn( |
| 79 | + `\nIf you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with ${candidate.module}. This will give you faster and more robust installs, and more control over deployment configuration.\n` |
| 80 | + ); |
| 81 | + } catch (e) { |
| 82 | + throw new Error( |
| 83 | + `Could not install ${candidate.module}. Please install it yourself by adding it to your package.json's devDependencies and try building your project again.` |
| 84 | + ); |
| 85 | + } |
| 86 | + } else { |
| 87 | + throw error; |
| 88 | + } |
37 | 89 | }
|
| 90 | + |
| 91 | + fn = () => { |
| 92 | + const adapter = module.default(); |
| 93 | + return { |
| 94 | + ...adapter, |
| 95 | + adapt: (builder) => { |
| 96 | + builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`); |
| 97 | + return adapter.adapt(builder); |
| 98 | + } |
| 99 | + }; |
| 100 | + }; |
| 101 | + |
| 102 | + break; |
38 | 103 | }
|
39 | 104 | }
|
40 | 105 |
|
|
0 commit comments