-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (72 loc) · 2.23 KB
/
index.js
File metadata and controls
79 lines (72 loc) · 2.23 KB
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
import { execSync } from 'child_process';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { adapters } from './adapters.js';
/** @type {import('./index').default} */
let fn;
for (const candidate of adapters) {
if (candidate.test()) {
/** @type {{ default: () => import('@sveltejs/kit').Adapter }} */
let module;
try {
module = await import(candidate.module);
} catch (error) {
if (
error.code === 'ERR_MODULE_NOT_FOUND' &&
error.message.startsWith(`Cannot find package '${candidate.module}'`)
) {
try {
console.log(`Installing ${candidate.module} on the fly...`);
execSync(
`${process.platform === 'win32' ? 'set' : ''} NODE_ENV=ignore_me && npm install ${
candidate.module
} --no-save --omit=dev --no-package-lock`,
{
stdio: 'inherit',
cwd: dirname(fileURLToPath(import.meta.url))
}
);
module = await import(candidate.module);
console.log(
`Successfully installed ${candidate.module} on the fly. If you plan on staying on this deployment platform, consider switching out @sveltejs/adapter-auto for ${candidate.module} for faster and more robust installs.`
);
} catch (e) {
// if (
// error.code === 'ERR_MODULE_NOT_FOUND' &&
// error.message.startsWith(`Cannot find package '${candidate.module}'`)
// ) {
throw new Error(
`Could not install ${candidate.module} on the fly. Please install it yourself by adding it to your package.json's devDependencies and try building your project again.`
);
// }
// ignore other errors, but print them
console.warn(e);
}
} else {
throw error;
}
}
fn = () => {
const adapter = module.default();
return {
...adapter,
adapt: (builder) => {
builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`);
return adapter.adapt(builder);
}
};
};
break;
}
}
if (!fn) {
fn = () => ({
name: '@sveltejs/adapter-auto',
adapt: (builder) => {
builder.log.warn(
'Could not detect a supported production environment. See https://kit.svelte.dev/docs/adapters to learn how to configure your app to run on the platform of your choosing'
);
}
});
}
export default fn;