forked from ci010/electron-vue-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (187 loc) · 7.12 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
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
#!/usr/bin/env node
const inquirer = require('inquirer')
const commander = require('commander')
const chalk = require('chalk')
const { version, name } = require('./package.json')
const { join, relative, resolve } = require('path')
const { copy, readFile, writeFile, readJSON, writeJSON } = require('fs-extra')
const program = new commander.Command(name)
const options = {
outputDirectory: join(process.cwd(), 'electron-vue-next'),
projectName: 'electron-vue-next',
nodeIntegration: false,
threadWorker: true,
mainService: true,
vscode: true
}
const provided = {
nodeIntegration: false,
name: false,
service: false,
vscode: false
}
program
.storeOptionsAsProperties(false)
.version(version)
.option('-ni, --no-interactive', 'disable interactive interface')
.option('-n, --name <name>', 'name of the project')
.option('-en, --enable-node-integration', 'enable node integration')
.option('-ns, --no-service', 'do not generate Service infra')
.option('-ns, --no-thread-worker', 'do not generate thread worker example')
.option('-nc, --no-vscode', 'do not generate VSCode debug config')
.on('option:enable-node-integration', () => { provided.nodeIntegration = true })
.on('option:name', () => { provided.name = true })
.on('option:no-service', () => { provided.service = true })
.on('option:no-vscode', () => { provided.vscode = true })
.description('Setup vue-electron-next-app to a directory')
.action(() => {
const dir = program.args[0]
const opts = program.opts()
options.outputDirectory = dir
options.name = opts.name || dir
options.nodeIntegration = opts.enableNodeIntegration
options.mainService = opts.service
options.vscode = opts.vscode
if (!options.noInteractive) {
console.log(`
Answer questions in prompt to config the project generator:
${chalk.italic.magenta('If you have question, please refer the document https://ci010.github.io/electron-vue-next/')}
`)
interactive(dir).then(setupProject)
} else {
setupProject()
}
})
.parse(process.argv)
async function interactive(name) {
const { projectName, nodeIntegration, mainService, vscode, threadWorker } = await inquirer.prompt([
{ type: 'input', default: name || 'electron-vue-app', message: 'Name of the project:', name: 'projectName', when: !provided.name },
{ type: 'confirm', default: false, message: 'Enable node integration for renderer:', name: 'nodeIntegration', when: !provided.nodeIntegration },
{
type: 'confirm',
default: true,
message: 'Use Service infrastructure to handle main/renderer communication:',
name: 'mainService',
when: !provided.service
},
{
type: 'confirm',
default: false,
message: 'Include thread_worker example',
name: 'threadWorker'
},
{ type: 'confirm', default: true, message: 'Generate vscode debug config:', name: 'vscode', when: !provided.vscode }
])
options.threadWorker = threadWorker
options.projectName = projectName
options.nodeIntegration = nodeIntegration
options.mainService = mainService
options.vscode = vscode
options.outputDirectory = options.outputDirectory || projectName
}
async function setupProject() {
const srcDir = join(__dirname, 'electron-vue-next')
const distDir = resolve(options.outputDirectory || 'electron-vue-next')
if (srcDir === distDir) {
throw new Error('The generated directory cannot be the same as the source directory in node_modules!')
}
await copy(srcDir, distDir, {
overwrite: true,
filter: (src, dest) => {
const relativePath = relative(srcDir, src)
if (!options.mainService) {
if (relativePath.startsWith(join('src', 'main', 'services'))) {
return false
}
if (relativePath.startsWith(join('src', 'main', 'logger.ts'))) {
return false
}
if (relativePath.startsWith(join('src', 'renderer', 'components', 'About.vue'))) {
return false
}
if (relativePath.startsWith(join('src', 'renderer', 'composables', 'service.ts'))) {
return false
}
}
if (!options.threadWorker) {
if (relativePath.startsWith(join('src', 'main', 'workers'))) {
return false
}
}
if (!options.vscode) {
if (relativePath.startsWith('.vscode')) {
return false
}
}
return true
}
})
const packageJSON = await readJSON(join(distDir, 'package.json'))
packageJSON.name = options.projectName
if (!options.mainService) {
{
const indexPath = join(distDir, 'src/main/index.ts')
const lines = (await readFile(indexPath)).toString().split('\n')
const filteredLine = new Set([
'import { Logger } from \'./logger\'',
'import { initialize } from \'./services\'',
'const logger = new Logger()',
'logger.initialize(app.getPath(\'userData\'))',
'initialize(logger)'
])
const result = lines.filter((line) => !filteredLine.has(line.trim())).join('\n')
await writeFile(indexPath, result)
}
{
const routerPath = join(distDir, 'src/renderer/router.ts')
const routerLines = (await readFile(routerPath)).toString().split('\n')
routerLines.splice(11, 4)
routerLines.splice(2, 1)
await writeFile(routerPath, routerLines.join('\n'))
}
{
const path = join(distDir, 'src/renderer/composables/index.ts')
const lines = (await readFile(path)).toString().split('\n')
lines.splice(11, 4)
await writeFile(path, lines.join('\n'))
}
{
const path = join(distDir, 'src/renderer/components/HomeNavigator.vue')
const lines = (await readFile(path)).toString().split('\n')
lines.splice(3, 1)
await writeFile(path, lines.join('\n'))
}
}
if (options.nodeIntegration) {
const indexPath = join(distDir, 'src/main/index.ts')
const lines = (await readFile(indexPath)).toString().split('\n')
const nodeIntegrationLine = lines.indexOf(' nodeIntegration: false')
lines[nodeIntegrationLine] = ' nodeIntegration: true'
await writeFile(indexPath, lines.join('\n'))
}
if (!options.threadWorker) {
const indexPath = join(distDir, 'src/main/index.ts')
const lines = (await readFile(indexPath)).toString().split('\n')
const filteredLine = new Set([
'import createBaseWorker from \'./workers/index?worker\'',
'// thread_worker example',
'createBaseWorker({ workerData: \'worker world\' }).on(\'message\', (message) => {',
// eslint-disable-next-line no-template-curly-in-string
'logger.log(`Message from worker: ${message}`)',
'}).postMessage(\'\')'
])
const result = lines
.filter(l => !filteredLine.has(l.trim()))
.join('\n')
await writeFile(indexPath, result)
}
await writeJSON(join(distDir, 'package.json'), packageJSON, { spaces: 4 })
console.log()
console.log(`Project Generated at: ${resolve(options.outputDirectory)}\n`)
console.log(`Next, you should process following commands:
${chalk.cyan('cd')} ${options.outputDirectory}
${chalk.cyan('npm')} install
Install dependencies of the project
${chalk.cyan('npm')} run dev
Start the development environment`)
}