-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuelectro.js
317 lines (272 loc) · 10.1 KB
/
vuelectro.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env node
const projectDir = process.cwd();
const {spawn} = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const glob = require('fast-glob');
const builder = require('electron-builder');
const vueService = require('@vue/cli-service');
const {info, done, error, warn} = require('@vue/cli-shared-utils');
const inquirer = require('inquirer');
const webpack = require('webpack');
const JavaScriptObfuscator = require('javascript-obfuscator');
const request = require('request');
const {install} = require('lmify');
let buildConfig = fs.pathExistsSync(path.join(projectDir, 'vuelectro.config.js')) ? require(path.join(projectDir, 'vuelectro.config')) : {};
const outDir = path.join(projectDir, 'app');
const srcDir = path.join(projectDir, 'src');
const service = new vueService(projectDir);
let args = process.argv.slice(2);
switch (args[0]) {
case 'init':
initVuelectro();
break;
case 'serve':
cleanOutDir(true);
serveDev();
break;
case 'build':
cleanOutDir(true);
buildProd();
break;
case 'compilemain':
cleanOutDir();
compileMain().catch(err => error(err));
break;
case 'clean':
cleanOutDir(true);
break;
default:
error('Invalid argument');
}
function initVuelectro() {
info('Initializing Vuelectro project..\n');
fs.pathExists(path.join(projectDir, 'vuelectro.config.js')).then((exists) => {
if (exists) {
let no = {name: 'No', value: false};
inquirer.prompt({
type: 'list',
name: 'reinit',
message: 'Vuelectro configuration already exists. Re-initializing will replace your source files and configurations. Are you sure want to continue?',
choices: [no, no, no, no, no, {name: 'Ok, fine.', value: true}],
default: false
}).then(answers => {
answers.reinit ? copyTemplate() : warn('Operation cancelled by user')
}).catch(error => console.error(error));
} else {
copyTemplate();
}
})
}
function copyTemplate() {
fs.copy(path.join(__dirname, 'template', 'projectfiles'), path.join(projectDir)).then(() => {
info('Template files copied\n');
editPkgJson();
}).catch(err => console.error(err));
}
function editPkgJson() {
let tmpltDeps;
fs.readJson(path.join(__dirname, 'template', 'template-package.json')).then((tmpltJson) => {
tmpltDeps = tmpltJson;
fs.readJson(path.join(projectDir, 'package.json')).then(async (orgPkgJson) => {
let newPkgJson = {
...orgPkgJson,
main: tmpltDeps.main,
"scripts": {
...orgPkgJson.scripts,
...tmpltDeps.scripts
},
"devDependencies": {
...orgPkgJson.devDependencies,
...tmpltDeps.devDependencies
},
browserslist: tmpltDeps.browserslist
};
newPkgJson.devDependencies.electron = await askElectronVersion(newPkgJson.devDependencies.electron);
fs.writeJsonSync(path.join(projectDir, 'package.json'), newPkgJson, {spaces: ' '});
fs.ensureDirSync(path.join(projectDir, 'resources'));
fs.removeSync(path.join(projectDir, '.browserslistrc'));
let gitignoreData = `\n# Vuelectro\n/dist_electron\n/app`
let gitignorePath = path.join(projectDir, '.gitignore');
fs.pathExistsSync(gitignorePath) ? fs.appendFileSync(gitignorePath, gitignoreData) : fs.outputFileSync(gitignorePath, gitignoreData);
try {
await install(['']);
console.log();
} catch {
error('There was an error trying to install dependencies. Try running "npm install" manually.\n');
}
done('Vuelectro initialization completed successfully. Try "npm run electron:serve"');
})
}).catch(err => error(err));
}
function askElectronVersion(current) {
return new Promise(resolve => {
request('https://registry.npmjs.org/-/package/electron/dist-tags', { json: true }, (err, res, body) => {
let versions = [
{name: `${current} (preset)`, value: current},
'15.3.1',
'14.2.0',
'13.6.1'
];
if (!err && body.latest) {
versions.unshift({name: `${body.latest} (latest)`, value: body.latest});
} else {
warn('Failed to fetch the latest version of Electron.\n');
}
inquirer.prompt({
type: 'list',
name: 'version',
message: 'What version of Electron do you want to use?',
choices: versions,
default: current
}).then(answers => {
console.log();
resolve(answers.version);
}).catch(() => {
error('Unable to show version selection. Choosing preset version.')
resolve(current);
});
});
});
}
function cleanOutDir(cleanAll = false) {
if (buildConfig.cleanOutputDir) {
try {
if (cleanAll) {
fs.emptyDirSync(outDir);
} else {
if (fs.existsSync(outDir)) {
fs.readdirSync(outDir).forEach(file => {
if (file !== 'renderer') fs.removeSync(path.join(outDir, file));
});
}
}
} catch (err) {
error(err);
}
}
}
function serveDev() {
service.init("development");
service.run('serve').then(({server, url}) => {
compileMain().then(() => {
info('Launching Electron...');
let electron = spawn(path.join(projectDir, 'node_modules', '.bin', process.platform === 'win32' ? 'electron.cmd' : 'electron'), [...args.slice(1), 'app/electron-main.js'], {stdio: 'inherit'});
electron.on('exit', function (code) {
process.exit(0);
});
}).catch(err => error(err));
}).catch((err) => {
error(err.stack);
});
}
function buildProd() {
service.init("production");
service.run('build').then(() => {
compileMain('production').then(() => {
info('Packaging Electron app...');
if (buildConfig.cleanOutputDir) fs.emptyDirSync(path.join(projectDir, buildConfig.electron_builder.directories.output));
builder.build({config: buildConfig.electron_builder}).then(() => {
done('Build successful');
}).catch((err) => {
error(err.stack);
});
}).catch(err => error(err));
}).catch((err) => {
error(err.stack);
});
}
function compileMain(mode = 'development') {
info('Building main process..\n');
return new Promise((resolve, reject) => {
if (buildConfig.vMain.bundle) {
webpackMain(mode).then(() => {
console.log();
done('Main process build completed\n')
resolve();
}).catch((err) => {
reject(err);
});
} else {
if (buildConfig.vMain.obfuscate) {
obfuscateMain(mode).then(() => {
console.log();
done('Main process build completed\n')
resolve();
}).catch((err) => {
reject(err);
});
} else {
copyMain().then(() => {
console.log();
done('Main process build completed\n')
resolve();
}).catch((err) => {
reject(err);
});
}
}
});
}
async function copyMain() {
const files = await glob(buildConfig.vMain.srcFiles, {cwd: srcDir});
files.forEach((file, idx) => {
fs.copySync(path.join(srcDir, file), path.join(outDir, file));
console.log(`[${idx + 1}].. ${path.parse(file).base} √`);
});
}
function webpackMain(_mode) {
return new Promise((resolve, reject) => {
let sourceMap = false;
if (_mode === 'production') {
if (buildConfig.vMain.productionSourceMap) {
sourceMap = buildConfig.vMain.webpackConfig.devtool || 'source-map';
}
} else {
sourceMap = buildConfig.vMain.webpackConfig.devtool;
}
webpack({
mode: _mode,
...buildConfig.vMain.webpackConfig,
devtool: sourceMap
}, (err, stats) => {
if (err) {
reject(err);
return;
}
if (stats.hasErrors()) {
reject(stats.toJson().errors);
return;
}
console.log(stats.toString({
chunks: false,
colors: true
}));
resolve();
});
});
}
async function obfuscateMain(_mode = 'development') {
info('Obfuscating your code..\n');
let sourceMap = false;
if (_mode === 'production') {
if (buildConfig.vMain.productionSourceMap) {
sourceMap = buildConfig.vMain.obfuscatorConfig.sourceMap || true;
}
} else {
sourceMap = buildConfig.vMain.obfuscatorConfig.sourceMap;
}
const files = await glob(buildConfig.vMain.srcFiles, {cwd: srcDir});
files.forEach((file, idx) => {
let srcData = fs.readFileSync(path.join(srcDir, file), 'utf8');
let filename = path.parse(file).base;
let srcObfuscated = JavaScriptObfuscator.obfuscate(srcData, {
...buildConfig.vMain.obfuscatorConfig,
inputFileName: filename,
sourceMapFileName: `${filename}.map`
});
fs.outputFileSync(path.join(outDir, file), srcObfuscated.getObfuscatedCode());
if (sourceMap) fs.outputFileSync(path.join(outDir, `${file}.map`), srcObfuscated.getSourceMap());
console.log(`[${idx + 1}].. ${path.parse(file).base} √`);
});
}