-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (52 loc) · 1.65 KB
/
index.js
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
import { spawn } from 'child_process';
/**
* Tauri plugin for Vite development server
* @returns {import('vite').Plugin} - An object with plugin configuration
*/
export default function TauriPlugin() {
function yellowText(text) {
/** yellow text start ${text} color text end */
return `\x1b[38;2;255;193;49m${text}\u001b[0m`;
}
/**
* Starts the Tauri process with the given Vite URL
* @param {import('http').Server} httpServer - The HTTP server instance with an address method
*/
function startTauriProcess(httpServer) {
const viteUrl = `http://localhost:${httpServer.address().port}/`;
const tauriProcess = spawn(
`npm run tauri dev -- --config {\\"build\\":{\\"devPath\\":\\"${viteUrl}\\"}}`,
{ shell: true }
);
/**
* Logs the Tauri process output with the '[tauri]' prefix in yellow color
* @param {Buffer} data - The output data buffer
*/
function logOutput(data) {
const lineSplitted = data.toString().split('\n');
``;
lineSplitted.forEach((line, i) => {
if (i !== lineSplitted.length - 1 || line !== '') {
if (line.includes(`tauri dev --config {"build":{"devPath":"${viteUrl}"}}`))
line = line.replace(/tauri dev.*/, 'tauri dev');
console.log(yellowText('[tauri]'), line);
}
});
}
tauriProcess.stdout.on('data', logOutput);
tauriProcess.stderr.on('data', logOutput);
}
/**
* @param {{ httpServer: import('http').Server | null }} server
*/
function configureServer(server) {
server.httpServer?.addListener('listening', () => {
startTauriProcess(server.httpServer);
});
}
return {
name: 'tauri-plugin',
configureServer,
configurePreviewServer: configureServer
};
}