-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade-vite.cjs
193 lines (172 loc) · 5.38 KB
/
upgrade-vite.cjs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Steps:
1. determine if it is Vanilla vite, VueJs, React vite, or Svelt
2. For each one different upgrade method
*/
const fs = require("fs").promises;
const util = require("util");
const child_process = require("child_process");
const exec = util.promisify(child_process.exec);
main().catch(console.error);
async function main() {
const packageJson = JSON.parse((await fs.readFile("./package.json")).toString());
const upgradeType = getUpgradeType(packageJson);
if (upgradeType === 'svelte') {
await svelteUpgrade(packageJson);
} else if (upgradeType === 'vue') {
await vueUpgrade(packageJson);
} else if (upgradeType === 'react') {
await reactUpgrade(packageJson);
} else if (upgradeType === 'vanilla') {
await vanillaUpgrade(packageJson);
}
}
function getDep(dep, packageJson) {
if (packageJson.devDependencies && dep in packageJson.devDependencies) {
return packageJson.devDependencies[dep];
}
if (packageJson.dependencies && dep in packageJson.dependencies) {
return packageJson.dependencies[dep];
}
return null;
}
function updateDep(dep, version, packageJson) {
if (packageJson.devDependencies && dep in packageJson.devDependencies) {
packageJson.devDependencies[dep] = version;
}
if (packageJson.dependencies && dep in packageJson.dependencies) {
packageJson.dependencies[dep] = version;
}
}
function getUpgradeType(packageJson) {
if (getDep('svelte', packageJson)) {
return 'svelte';
} else if (getDep("@vitejs/plugin-vue", packageJson)) {
return 'vue';
} else if (getDep('react', packageJson)) {
return 'react';
} else {
return 'vanilla';
}
}
async function svelteUpgrade(packageJson) {
console.log("Performing Svelte Vite.js upgrade...")
packageJson.devDependencies.vite = '^3.0.4';
packageJson.devDependencies.svelte = '^3.49.0';
packageJson.devDependencies['@sveltejs/vite-plugin-svelte'] = '^1.0.1';
console.log(packageJson);
await fs.writeFile("./package.json", JSON.stringify(packageJson, null, " "));
console.log("Wrote package.json");
console.log("Execute npm install...");
const { stdout, stderr } = await exec("npm install");
console.log(stdout);
if (stderr) {
console.log(stderr);
}
const viteConfig = `import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
server: {
host: true
}
})
`;
await fs.writeFile('./vite.config.js', viteConfig);
console.log("Wrote vite.config.js");
}
async function vueUpgrade(packageJson) {
console.log("Performing VueJS Vite.js upgrade...")
packageJson.devDependencies.vite = '^3.0.4';
delete packageJson.devDependencies["@vue/compiler-sfc"];
packageJson.devDependencies["@vitejs/plugin-vue"] = "^3.0.1";
if (packageJson.dependencies && packageJson.dependencies.vue) {
console.log("setting vue version")
packageJson.dependencies.vue = "^3.2.37";
}
console.log(packageJson);
await fs.writeFile("./package.json", JSON.stringify(packageJson, null, " "));
console.log("Wrote package.json");
console.log("Execute npm install...");
const { stdout, stderr } = await exec("npm install");
console.log(stdout);
if (stderr) {
console.log(stderr);
}
if (await exists("./yarn.lock")) {
await fs.unlink("./yarn.lock");
}
await exec(`sed -i 's/"yarn dev"/"npm run dev"/' .replit`);
const viteConfig = `
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
server: {
host: true
},
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})
`;
await fs.writeFile("./vite.config.js", viteConfig);
console.log("Wrote vite.config.js");
}
async function reactUpgrade(packageJson) {
console.log("Performing React Vite.js upgrade...");
updateDep('vite', "^3.0.4", packageJson);
if (packageJson.devDependencies) {
packageJson.devDependencies["@vitejs/plugin-react"] = "^2.0.0";
} else {
packageJson.dependencies["@vitejs/plugin-react"] = "^2.0.0";
}
delete packageJson.dependencies['@vitejs/plugin-react-refresh'];
console.log(packageJson);
await fs.writeFile("./package.json", JSON.stringify(packageJson, null, " "));
console.log("Wrote package.json");
console.log("Execute npm install...");
const { stdout, stderr } = await exec("npm install");
console.log(stdout);
if (stderr) {
console.log(stderr);
}
const viteConfig = `import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
host: '0.0.0.0',
}
})
`;
await fs.writeFile("./vite.config.js", viteConfig);
console.log("Wrote vite.config.js");
}
async function vanillaUpgrade(packageJson) {
console.log("Performing vanilla Vite.js upgrade...");
updateDep('vite', "^3.0.4", packageJson);
console.log(packageJson);
await fs.writeFile("./package.json", JSON.stringify(packageJson, null, " "));
console.log("Wrote package.json");
console.log("Execute npm install...");
const { stdout, stderr } = await exec("npm install");
console.log(stdout);
if (stderr) {
console.log(stderr);
}
}
async function exists(filepath) {
try {
await fs.access(filepath, fs.F_OK);
return true;
} catch (e) {
return false;
}
}