-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
121 lines (99 loc) · 4.6 KB
/
index.ts
File metadata and controls
121 lines (99 loc) · 4.6 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
import fs from 'fs/promises';
import path from 'path';
import { UserConfig, createServer } from 'vite';
import { PluginOption } from 'vite';
import { CLI_DIRECTORY, COLOR_ERROR, COLOR_GREEN, PROJECT_DIRECTORY, VITE_CONFIGURATION_FILENAME } from "../../../constants";
import showMessage from "../../../utils/showMessage";
import getViteWatchOutputDirectory from "../../../utils/getViteWatchOutputDirectory";
import pathIsExists from "../../../utils/pathIsExists";
import { getViteDefaultConfig } from '../../../configurations/vite';
import getWidgetName from '../../../utils/getWidgetName';
import getViteUserConfiguration from '../../../utils/getViteUserConfiguration';
import { generateTypesFromFile } from '../../../type-generator';
import { mendixHotreloadReactPlugin } from '../../../configurations/vite/plugins/mendix-hotreload-react-plugin';
import { mendixPatchViteClientPlugin } from '../../../configurations/vite/plugins/mendix-patch-vite-client-plugin';
const generateTyping = async () => {
const widgetName = await getWidgetName();
const originWidgetXmlPath = path.join(PROJECT_DIRECTORY, `src/${widgetName}.xml`);
const typingsPath = path.join(PROJECT_DIRECTORY, 'typings');
const typingsDirExists = await pathIsExists(typingsPath);
if (typingsDirExists) {
await fs.rm(typingsPath, { recursive: true, force: true });
}
await fs.mkdir(typingsPath);
const newTypingsFilePath = path.join(typingsPath, `${widgetName}Props.d.ts`);
const typingContents = await generateTypesFromFile(originWidgetXmlPath, 'web');
await fs.writeFile(newTypingsFilePath, typingContents);
};
const startWebCommand = async () => {
try {
showMessage('Start widget server');
await generateTyping();
const customViteConfigPath = path.join(PROJECT_DIRECTORY, VITE_CONFIGURATION_FILENAME);
const viteConfigIsExists = await pathIsExists(customViteConfigPath);
let resultViteConfig: UserConfig;
const widgetName = await getWidgetName();
if (viteConfigIsExists) {
const userConfig = await getViteUserConfiguration(customViteConfigPath);
resultViteConfig = await getViteDefaultConfig(false, userConfig);
} else {
resultViteConfig = await getViteDefaultConfig(false);
}
const viteCachePath = path.join(PROJECT_DIRECTORY, 'node_modules/.vite');
const viteCachePathExists = await pathIsExists(viteCachePath);
if (viteCachePathExists) {
await fs.rm(viteCachePath, { recursive: true, force: true });
}
const viteServer = await createServer({
...resultViteConfig,
root: PROJECT_DIRECTORY,
server: {
fs: {
strict: false
},
watch: {
usePolling: true,
interval: 100
},
},
plugins: [
...resultViteConfig.plugins as PluginOption[],
mendixHotreloadReactPlugin(),
mendixPatchViteClientPlugin(),
{
name: 'mendix-xml-watch-plugin',
configureServer(server) {
server.watcher.on('change', (file) => {
if (file.endsWith('xml')) {
generateTyping();
}
});
}
},
]
});
await viteServer.listen();
showMessage('Generate hot reload widget');
const hotReloadTemplate = path.join(CLI_DIRECTORY, 'src/configurations/hotReload/widget.proxy.js.template');
const hotReloadContents = await fs.readFile(hotReloadTemplate, 'utf-8');
const devServerUrl = viteServer.resolvedUrls?.local[0] || '';
const newHotReloadContents = hotReloadContents
.replaceAll('{{ WIDGET_NAME }}', widgetName)
.replaceAll('{{ DEV_SERVER_URL }}', devServerUrl)
const distDir = await getViteWatchOutputDirectory();
const distIsExists = await pathIsExists(distDir);
const hotReloadWidgetPath = path.join(distDir, `${widgetName}.mjs`);
const dummyCssPath = path.join(distDir, `${widgetName}.css`);
if (distIsExists) {
await fs.rm(distDir, { recursive: true, force: true });
}
await fs.mkdir(distDir, { recursive: true });
await fs.writeFile(hotReloadWidgetPath, newHotReloadContents);
await fs.writeFile(dummyCssPath, '');
showMessage(`${COLOR_GREEN('Widget hot reload is ready!')}`);
showMessage(`${COLOR_GREEN('Mendix webpage will refresh shortly. Hot reload will work after refreshing.')}`);
} catch (error) {
showMessage(`${COLOR_ERROR('Build failed.')}\nError occurred: ${COLOR_ERROR((error as Error).message)}`);
}
};
export default startWebCommand;