-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathgenerator.ts
More file actions
128 lines (112 loc) · 3.66 KB
/
generator.ts
File metadata and controls
128 lines (112 loc) · 3.66 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
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
import path from 'node:path';
import type { ProdServerOptions } from '@modern-js/prod-server';
import {
ROUTE_SPEC_FILE,
SERVER_DIR,
fs as fse,
getMeta,
} from '@modern-js/utils';
import { merge } from '@modern-js/utils/lodash';
import { normalizePath } from '.';
import type { AppToolsNormalizedConfig } from '../../../types';
import type { AppToolsContext } from '../../../types/plugin';
export const serverAppContextTemplate = (appContext: AppToolsContext) => {
const {
appDirectory,
sharedDirectory,
apiDirectory,
lambdaDirectory,
metaName,
bffRuntimeFramework,
} = appContext;
const getRelativePathTemplate = (targetDirectory: string) =>
`path.join(__dirname, ${JSON.stringify(
normalizePath(path.relative(appDirectory, targetDirectory)),
)})`;
return {
sharedDirectory: getRelativePathTemplate(sharedDirectory),
apiDirectory: getRelativePathTemplate(apiDirectory),
lambdaDirectory: getRelativePathTemplate(lambdaDirectory),
metaName,
bffRuntimeFramework: bffRuntimeFramework || 'hono',
};
};
export type PluginItem = [string, Record<string, any> | undefined];
export const genPluginImportsCode = (plugins: PluginItem[], isESM = false) => {
return plugins
.map(([name, options], index) => {
const im = isESM
? `import * as plugin_${index}_ns from '${name}'`
: `const plugin_${index}_ns = require('${name}')`;
return `${im};const plugin_${index} = plugin_${index}_ns.default || plugin_${index}_ns`;
})
.join(';\n');
};
export const getPluginsCode = (plugins: PluginItem[]) => {
return `[${plugins
.map(
([, options], index) =>
`plugin_${index}(${options ? JSON.stringify(options) : ''})`,
)
.join(',')}]`;
};
export const getServerConfigPath = (meta: string) =>
`path.join(__dirname, "${SERVER_DIR}", "${meta}.server")`;
export interface GenerateHandlerOptions {
template: string;
appContext: AppToolsContext;
config: AppToolsNormalizedConfig;
serverConfig?: Partial<ProdServerOptions>;
genAppContextTemplate?: typeof serverAppContextTemplate;
genPluginImports?: typeof genPluginImportsCode;
isESM?: boolean;
}
export const generateHandler = async ({
template,
appContext,
config,
serverConfig: modifyServerConfig,
genAppContextTemplate = serverAppContextTemplate,
genPluginImports = genPluginImportsCode,
isESM,
}: GenerateHandlerOptions) => {
const { serverPlugins, metaName } = appContext;
const plugins: PluginItem[] = serverPlugins.map(plugin => [
plugin.name,
plugin.options,
]);
const serverConfig = merge(
{
bff: {
prefix: config?.bff?.prefix,
},
output: {
distPath: {
root: '.',
},
},
},
modifyServerConfig || {},
);
const meta = getMeta(metaName);
const pluginImportCode = genPluginImports(plugins || [], Boolean(isESM));
const dynamicProdOptions = {
config: serverConfig,
};
const serverConfigPath = getServerConfigPath(meta);
const pluginsCode = getPluginsCode(plugins);
const serverAppContext = genAppContextTemplate(appContext);
return template
.replace('p_genPluginImportsCode', pluginImportCode)
.replace('p_ROUTE_SPEC_FILE', `"${ROUTE_SPEC_FILE}"`)
.replace('p_dynamicProdOptions', JSON.stringify(dynamicProdOptions))
.replace('p_plugins', pluginsCode)
.replace(
'p_bffRuntimeFramework',
`"${serverAppContext.bffRuntimeFramework}"`,
)
.replace('p_serverDirectory', serverConfigPath)
.replace('p_sharedDirectory', serverAppContext.sharedDirectory)
.replace('p_apiDirectory', serverAppContext.apiDirectory)
.replace('p_lambdaDirectory', serverAppContext.lambdaDirectory);
};