-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathindex.js
93 lines (70 loc) · 2.16 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
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
import pkgJson from '../renderer/package.json' with {type: 'json'};
import * as fs from 'node:fs';
const step = createStepLogger();
await step(
'Changing renderer package name to "@vite-electron-builder/renderer"',
changeRendererPackageName,
);
await step(
'Add "--base=./" flag to vite build command',
addTheBaseFlagToBuildCommand,
);
await step(
'Change the "main" property to "./dist/index.html"',
addTheMainProperty,
);
function changeRendererPackageName() {
if (pkgJson?.name === '@vite-electron-builder/renderer') {
return;
}
pkgJson.name = '@vite-electron-builder/renderer';
savePkg();
}
function addTheBaseFlagToBuildCommand() {
if (!pkgJson?.scripts?.build) {
console.warn('No build script found. Skip.');
return false;
}
if (!pkgJson.scripts.build.includes('vite build')) {
console.warn('The build script is founded but it was not recognized as "vite build" command. Skip.');
return false;
}
if (pkgJson.scripts.build.includes('--base')) {
console.warn('The "--base" flag already exists. Skip.');
return false;
}
pkgJson.scripts.build = pkgJson.scripts.build.replaceAll('vite build', 'vite build --base ./');
savePkg();
}
function addTheMainProperty() {
if (pkgJson.main) {
console.warn('The "main" property already exists. Skip.');
return false;
}
pkgJson.main = './dist/index.html';
savePkg();
}
function createStepLogger() {
console.log('\n\n\n\n----------');
console.log('Default vite project has been successfully created.');
console.log('However, additional modifications to the default vite project are now being implemented');
console.log('to ensure compatibility with the template.');
console.log('All changes are detailed below.');
console.log('\n');
let stepNumber = 1;
return async function(message, callback) {
const stepMessage = `${stepNumber++}. ${message}`;
console.group(stepMessage);
try {
await callback();
} catch (error) {
console.error(error);
process.exit(1);
} finally {
console.groupEnd();
}
};
}
function savePkg() {
fs.writeFileSync('../renderer/package.json', JSON.stringify(pkgJson, null, 2), {encoding: 'utf8'});
}