This repository was archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·144 lines (124 loc) · 3.92 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const arg = require('arg');
const chalk = require('chalk');
const cwd = process.cwd();
const args = arg({
'--help': Boolean,
'--version': Boolean,
'--example': String,
'-h': '--help',
'-v': '--version',
});
if (args['--version']) {
const pkg = require(path.join(__dirname, 'package.json'));
console.log(`create-viteron-app v${pkg.version}`);
process.exit(0);
}
if (args['--help'] || (!args._[0])) {
console.log(chalk`
{bold.cyan create-viteron-app} - Create viteron (vite + electron) apps in one command ⚡
{bold USAGE}
{bold $} {cyan create-viteron-app} --help
{bold $} {cyan create-viteron-app} {underline my-app}
{bold $} {cyan create-viteron-app} {underline my-app} [--example {underline example_folder_name}]
{bold OPTIONS}
--help, -h shows this help message
--version, -v displays the current version of create-viteron-app
--example, -e {underline example_folder_name} sets the example as a template
`);
process.exit(0);
}
createViteronApp();
async function createViteronApp() {
const spinner = require('./spinner');
const example = args['--example'] || 'with-javascript';
try {
spinner.create('Validating existence...');
await validateExistence(example);
} catch (error) {
spinner.fail(`Not found: ${example}`);
}
try {
spinner.create('Downloading and extracting...');
const name = path.join(cwd, args._[0]);
await require('make-dir')(name);
await downloadAndExtract(name, example, spinner);
} catch (error) {
spinner.fail(error);
}
}
async function validateExistence(example) {
const { Octokit } = require('@octokit/rest');
await new Octokit().repos.getContent({
owner: 'saltyshiomix',
repo: 'viteron',
path: `examples/${example}/package.json`,
});
}
async function downloadAndExtract(name, example, spinner) {
const mainUrl = 'https://codeload.github.com/saltyshiomix/viteron/tar.gz/main';
const got = require('got');
const { t, x } = require('tar');
let ext = 'js';
await got
.stream(mainUrl)
.pipe(t({ cwd: name, strip: 3, filter: (path) => {
if (path.endsWith(`${example}/tsconfig.json`)) {
ext = 'ts';
}
return false;
}}))
.on('finish', async () => {
Promise.all([
new Promise(resolve => {
got
.stream(mainUrl)
.pipe(x({ cwd: name, strip: 3 }, ['viteron-main/examples/_template/gitignore.txt']))
.on('finish', () => {
fs.renameSync(path.join(name, 'gitignore.txt'), path.join(name, '.gitignore'));
resolve();
});
}),
new Promise(resolve => {
got
.stream(mainUrl)
.pipe(x({ cwd: name, strip: 4 }, [`viteron-main/examples/_template/${ext}`]))
.on('finish', () => resolve());
}),
new Promise(resolve => {
got
.stream(mainUrl)
.pipe(x({ cwd: name, strip: 3 }, [`viteron-main/examples/${example}`]))
.on('finish', () => resolve());
}),
]).then(async () => {
const cmd = (await pm() === 'yarn') ? 'yarn && yarn dev' : 'npm install && npm run dev';
spinner.clear(`Run \`${cmd}\` inside of "${name}" to start the app`);
}).catch(() => {
spinner.fail('Unknown error occurred.');
});
});
}
async function pm() {
const { promisify } = require('util');
const { exec: defaultExec } = require('child_process');
let pm = 'yarn';
const exec = promisify(defaultExec);
try {
await exec(`${pm} -v`, { cwd });
} catch (_) {
pm = 'npm';
try {
await exec(`${pm} -v`, { cwd });
} catch (_) {
pm = undefined;
}
}
if (pm === undefined) {
console.log(chalk.red('No available package manager! (`npm` or `yarn` is required)'));
process.exit(1);
}
return pm;
}