forked from electron-vite/create-electron-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 1.7 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const prompts = require('prompts');
const cwd = process.cwd();
async function init() {
const template = await prompts([
{
type: 'select',
name: 'value',
message: 'Project template:',
choices: [
{
title: 'Vue',
value: { projectName: 'electron-vite-vue', repoName: 'electron-vite-vue' },
},
{
title: 'React',
value: { projectName: 'electron-vite-react', repoName: 'electron-vite-react' },
},
{
title: 'Vanilla',
value: { projectName: 'electron-vite-vanilla', repoName: 'vite-plugin-electron-quick-start' },
},
],
}
]);
if (!template.value) return;
const { projectName, repoName, branch } = template.value;
const repo = `https://github.com/electron-vite/${repoName}`;
try {
if (fs.existsSync(projectName) && fs.statSync(projectName).isDirectory()) {
console.error(`🚧 Directory "${projectName}" already exists.`);
process.exit(1);
}
await gitClone(repo, projectName, branch);
fs.rmSync(path.join(cwd, projectName, '.git'), { recursive: true, force: true });
} catch (error) {
process.exit(error);
}
}
function gitClone(repo, projectName, branch) {
return new Promise((resolve, reject) => {
const _branch = branch ? ['-b', branch] : [];
cp.spawn(
'git',
['clone', ..._branch, repo, projectName, '--depth', '1'],
{ stdio: 'inherit' },
)
.on('close', (code, signal) => {
if (code) {
reject(code);
return;
}
resolve(signal);
});
});
}
init();