-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdev.mts
206 lines (183 loc) · 6.88 KB
/
dev.mts
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
194
195
196
197
198
199
200
201
202
203
204
205
206
// pnpm i only-allow esno prompts cross-spawn kolorist magicast del -D -w
import type { ASTNode } from 'magicast'
import fs from 'node:fs'
import spawn from 'cross-spawn'
import { cyan } from 'kolorist'
import { loadFile, writeFile } from 'magicast'
import { addVitePlugin } from 'magicast/helpers'
import prompts from 'prompts'
type VueVersion = '3' | '2.7' | '2.6'
const vueVersion: VueVersion[] = ['3', '2.7', '2.6']
const toVitePlugin: Record<VueVersion, string> = {
3: '@vitejs/plugin-vue',
2.7: '@vitejs/plugin-vue2',
2.6: 'vite-plugin-vue2',
}
const toPackageOptions: Record<VueVersion, Record<string, Record<string, any>>> = {
3: {
devDependencies: {
'@vitejs/plugin-vue': 'latest',
'@vue/compiler-sfc': 'latest',
'@vue/test-utils': 'latest',
'vue': 'latest',
},
},
2.7: {
devDependencies: {
'@vitejs/plugin-vue2': 'latest',
'@vue/test-utils': 'legacy',
'vue': '~2.7',
'vue-template-compiler': '~2.7',
},
},
2.6: {
devDependencies: {
'@vue/composition-api': 'latest',
'@vue/test-utils': 'legacy',
'vite-plugin-vue2': 'latest',
'unplugin-vue2-script-setup': 'latest',
'vue': '~2.6',
'vue-template-compiler': '~2.6',
},
},
}
async function dev() {
const { targetVersion }: { targetVersion: VueVersion } = await prompts({
type: 'select',
name: 'targetVersion',
message: 'Select Vue version',
choices: Array.from(vueVersion, value => ({ title: value, value })),
})
if (!targetVersion) {
return
}
console.info(cyan('Fetching origin...'))
spawn('git', ['pull'], { stdio: 'inherit' })
console.info(cyan(`Switching to Vue ${targetVersion}...`))
const mod = await loadFile('./vite.config.ts')
// imported 表示命名导入的值,默认导入是 default
// k 和 mod.imports[k].local 和 constructor 三者一致,表示导入取的别名
// 删掉 vue 相关引入
const existedVuePlugins: Record<string, boolean> = {}
for (const k in mod.imports) {
for (const vueVersion in toVitePlugin) {
if (mod.imports[k] && [toVitePlugin[vueVersion as VueVersion], 'unplugin-vue2-script-setup/vite'].includes(mod.imports[k].from)) {
delete mod.imports[k]
existedVuePlugins[k] = true
}
}
}
// 删掉 vue 相关插件
const options = mod.exports.default.$type === 'function-call'
? mod.exports.default.$args[0]
: mod.exports.default
if (Object.keys(existedVuePlugins).length && options.plugins?.length) {
for (let i = options.plugins.length - 1; i > 0; i--) {
const p = options.plugins[i]
if (p?.$type === 'function-call' && existedVuePlugins[p.$callee]) {
options.plugins.splice(i, 1)
}
}
}
// 添加 vue 相关插件
addVitePlugin(mod, {
from: toVitePlugin[targetVersion],
imported: targetVersion === '2.6' ? 'createVuePlugin' : 'default',
constructor: 'vue',
})
if (targetVersion === '2.6') {
addVitePlugin(mod, {
from: 'unplugin-vue2-script-setup/vite',
imported: 'default',
constructor: 'ScriptSetup',
})
}
await writeFile(mod as unknown as ASTNode, './vite.config.ts')
spawn('npx', ['eslint', './vite.config.ts', '--fix'], { stdio: 'inherit' })
let isDepsChanged = false
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
// 删除非目标版本的依赖
for (const ver of vueVersion) {
if (ver !== targetVersion) {
for (const option in toPackageOptions[ver]) {
for (const dep in toPackageOptions[ver][option]) {
if (pkg[option][dep] && !toPackageOptions[targetVersion][option][dep]) {
delete pkg[option][dep]
isDepsChanged = true
}
}
}
}
}
// 添加目标版本的依赖
for (const option in toPackageOptions[targetVersion]) {
for (const dep in toPackageOptions[targetVersion][option]) {
const depVer = toPackageOptions[targetVersion][option][dep]
if (pkg[option][dep] !== depVer) {
pkg[option][dep] = depVer
isDepsChanged = true
}
}
}
if (isDepsChanged) {
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))
console.info(cyan('Linting package.json...'))
spawn('npx', ['eslint', './package.json', '--fix'], { stdio: 'inherit' })
await installDependencies()
}
spawn.sync('npx', ['vite', '--open', '--force'], { stdio: 'inherit' })
async function installDependencies() {
/* if (['darwin', 'linux'].includes(process.platform)) {
console.info(cyan('Checking pnpm version...'))
const latestPNPMVersion = spawn.sync('npm', ['view', 'pnpm', 'version']).stdout.toString().trim()
const currentPNPMVersion = spawn.sync('pnpm', ['-v']).stdout.toString().trim()
// Mac 自带 curl,Linux 不一定,Windows 不支持指定 pnpm 版本
if (latestPNPMVersion !== currentPNPMVersion) {
console.info(cyan('Upgrading pnpm...'))
try {
console.info(execSync(`curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=${latestPNPMVersion} sh -`).toString())
// const curlProcess = spawn.sync('curl', ['-fsSL', 'https://get.pnpm.io/install.sh'], {
// env: { PNPM_VERSION: latestPNPMVersion },
// stdio: ['pipe', 'pipe', 'pipe'], // Redirect stdin, stdout, and stderr
// })
// if (curlProcess.status === 0) {
// // If curl was successful, execute the shell command
// const shCommand = 'sh'
// const shArgs = ['-']
//
// const shProcess = spawn.sync(shCommand, shArgs, {
// input: curlProcess.stdout, // Pass the stdout of curl as input to sh
// stdio: ['pipe', 'inherit', 'inherit'], // Redirect stdin, inherit stdout and stderr
// })
//
// if (shProcess.status === 0) {
// console.info('pnpm installation successful.')
// } else {
// console.error('pnpm installation failed.')
// }
// } else {
// console.error('curl command failed.')
// }
console.info(cyan('Setting registry...'))
spawn.sync('pnpm', ['config', 'set', 'registry', 'https://registry.npmmirror.com'], { stdio: 'inherit' })
// console.info(cyan('Installing node lts...'))
// spawn.sync('pnpm', ['env', 'use', '-g', 'lts'], { stdio: 'inherit' })
console.info(cyan('Installing global packages...'))
spawn('pnpm', ['add', 'cnpm', '@antfu/ni', '-g'], { stdio: 'inherit' })
console.info(cyan('Deleting ./node_modules...'))
await deleteAsync(['./node_modules'])
} catch (e) {
}
}
} */
console.info(cyan('Installing dependencies...'))
spawn.sync('pnpm', ['i'], { stdio: 'inherit' })
spawn.sync('npx', ['vue-demi-switch', targetVersion === '2.6' ? '2' : targetVersion], { stdio: 'inherit' })
}
}
try {
dev()
}
catch (e) {
console.error(e)
}