/
createServerlessConfig.ts
91 lines (79 loc) · 2.85 KB
/
createServerlessConfig.ts
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
import fs from "fs-extra";
import path from "path";
function getCustomData(importName: string, target: string): string {
return `
module.exports = function(...args) {
let original = require('./${importName}');
const finalConfig = {};
const target = { target: '${target}' };
if (typeof original === 'function' && original.constructor.name === 'AsyncFunction') {
// AsyncFunctions will become promises
original = original(...args);
}
if (original instanceof Promise) {
// Special case for promises, as it's currently not supported
// and will just error later on
return original
.then((originalConfig) => Object.assign(finalConfig, originalConfig))
.then((config) => Object.assign(config, target));
} else if (typeof original === 'function') {
Object.assign(finalConfig, original(...args));
} else if (typeof original === 'object') {
Object.assign(finalConfig, original);
}
Object.assign(finalConfig, target);
return finalConfig;
}
`.trim();
}
function getDefaultData(target: string): string {
return `module.exports = { target: '${target}' };`;
}
type CreateServerlessConfigResult = {
restoreUserConfig: () => Promise<void>;
};
export default async function createServerlessConfig(
workPath: string,
entryPath: string,
useServerlessTraceTarget: boolean
): Promise<CreateServerlessConfigResult> {
const target = useServerlessTraceTarget
? "experimental-serverless-trace"
: "serverless";
const primaryConfigPath = path.join(entryPath, "next.config.js");
const secondaryConfigPath = path.join(workPath, "next.config.js");
const backupConfigName = `next.config.original.${Date.now()}.js`;
const hasPrimaryConfig = fs.existsSync(primaryConfigPath);
const hasSecondaryConfig = fs.existsSync(secondaryConfigPath);
let configPath: string;
let backupConfigPath: string;
if (hasPrimaryConfig) {
// Prefer primary path
configPath = primaryConfigPath;
backupConfigPath = path.join(entryPath, backupConfigName);
} else if (hasSecondaryConfig) {
// Work with secondary path (some monorepo setups)
configPath = secondaryConfigPath;
backupConfigPath = path.join(workPath, backupConfigName);
} else {
// Default to primary path for creation
configPath = primaryConfigPath;
backupConfigPath = path.join(entryPath, backupConfigName);
}
const configPathExists = fs.existsSync(configPath);
if (configPathExists) {
await fs.rename(configPath, backupConfigPath);
await fs.writeFile(configPath, getCustomData(backupConfigName, target));
} else {
await fs.writeFile(configPath, getDefaultData(target));
}
return {
restoreUserConfig: async (): Promise<void> => {
const needToRestoreUserConfig = configPathExists;
await fs.remove(configPath);
if (needToRestoreUserConfig) {
await fs.rename(backupConfigPath, configPath);
}
}
};
}