forked from antony-k1208/vite-add-vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·120 lines (99 loc) · 2.74 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
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
#!/usr/bin/env node
import prompts from 'prompts';
import exec from 'await-exec';
import { mkdir, readFile, writeFile } from 'fs/promises';
function generateRouterConfig() {
return `
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView,
},
],
});
export default router;
`;
}
function generateAppVue() {
return `
<template>
<router-view />
</template>
`;
}
async function generateRouterFiles(fileEnding) {
await mkdir('src/router', { recursive: true });
await writeFile(`src/router/index.${fileEnding}`, generateRouterConfig());
}
async function generateVueFiles() {
await mkdir('src/views', { recursive: true });
const oldAppVue = (await readFile('src/App.vue')).toString();
await writeFile('src/views/HomeView.vue', oldAppVue.replaceAll('./', '../'));
await writeFile('src/App.vue', generateAppVue());
}
async function generateMainFile(fileEnding) {
const vueConfig = (await readFile(`src/main.${fileEnding}`)).toString();
let vueConfigLines = vueConfig.slice().split('\n');
const lastImportLine = vueConfigLines
.map((line) => line.indexOf('import ') !== -1)
.lastIndexOf(true);
vueConfigLines.splice(
lastImportLine + 1,
0,
'import router from "./router";'
);
vueConfigLines[vueConfigLines.length - 2] = vueConfigLines[
vueConfigLines.length - 2
].replace('.mount(', '.use(router).mount(');
await writeFile(`src/main.${fileEnding}`, vueConfigLines.join('\n'));
}
async function init() {
let result = {};
try {
result = await prompts(
[
{
name: 'usesTypeScropt',
type: () => 'toggle',
message: 'Is it a TypeScript-based project?',
initial: false,
active: 'Yes',
inactive: 'No',
},
{
name: 'npmExecutable',
type: () => 'text',
message: 'The npm executable to use',
initial: 'npm',
},
],
{
onCancel: () => {
throw new Error(red('✖') + ' Operation cancelled');
},
}
);
} catch (cancelled) {
console.log(cancelled.message);
process.exit(1);
}
const {
usesTypeScript = result.usesTypeScropt,
npmExecutable = result.npmExecutable,
} = result;
const fileEnding = usesTypeScript ? 'ts' : 'js';
console.log('Installing vue-router...');
await exec(`${npmExecutable} install vue-router`);
await generateRouterFiles(fileEnding);
await generateVueFiles();
await generateMainFile(fileEnding);
console.log('Installed vue-router!');
}
init().catch((e) => {
console.error(e);
});