-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·151 lines (129 loc) · 4.09 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
#!/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',
'-e': '--example',
});
if (args['--version']) {
const pkg = require(path.join(__dirname, 'package.json'));
console.log(`create-nextron-app v${pkg.version}`);
process.exit(0);
}
if (args['--help'] || (!args._[0])) {
console.log(chalk`
{bold.cyan create-nextron-app} - Create Nextron (Electron + Next.js) apps in one command ⚡
{bold USAGE}
{bold $} {cyan create-nextron-app} --help
{bold $} {cyan create-nextron-app} {underline my-app}
{bold $} {cyan create-nextron-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-nextron-app
--example, -e {underline example_folder_name} sets the example as a template
`);
process.exit(0);
}
createNextronApp();
async function createNextronApp() {
const spinner = require('./spinner');
const example = args['--example'] || 'basic-lang-javascript';
try {
spinner.create('Validating existence...');
await validateExistence(example);
} catch (error) {
console.error(error);
spinner.fail(`Not found: ${example}`);
}
try {
spinner.create('Downloading and extracting...');
const dirname = path.join(cwd, args._[0]);
await require('make-dir')(dirname);
await downloadAndExtract(example, dirname, spinner);
} catch (error) {
console.error(error);
spinner.fail(error);
}
}
async function validateExistence(example) {
const { Octokit } = require('@octokit/rest');
await new Octokit().repos.getContent({
owner: 'saltyshiomix',
repo: 'nextron',
ref: 'main',
path: `examples/${example}/package.json`,
});
}
async function downloadAndExtract(example, dirname, spinner) {
const mainUrl = 'https://codeload.github.com/saltyshiomix/nextron/tar.gz/main';
const got = require('got');
const { t, x } = require('tar');
let ext = 'js';
await got
.stream(mainUrl)
.pipe(t({ cwd: dirname, strip: 3, filter: (path) => {
if (path.endsWith(`${example}/tsconfig.json`)) {
ext = 'ts';
}
return false;
}}))
.on('finish', async () => {
try {
await Promise.all([
new Promise(resolve => {
got
.stream(mainUrl)
.pipe(x({ cwd: dirname, strip: 3 }, [`nextron-main/examples/_template/gitignore.txt`]))
.on('finish', () => {
fs.renameSync(path.join(dirname, 'gitignore.txt'), path.join(dirname, '.gitignore'));
resolve();
});
}),
new Promise(resolve => {
got
.stream(mainUrl)
.pipe(x({ cwd: dirname, strip: 4 }, [`nextron-main/examples/_template/${ext}`]))
.on('finish', () => resolve());
}),
]);
await new Promise(resolve => {
got
.stream(mainUrl)
.pipe(x({ cwd: dirname, strip: 3 }, [`nextron-main/examples/${example}`]))
.on('finish', () => resolve());
});
const cmd = (await pm() === 'yarn') ? 'yarn && yarn dev' : 'npm install && npm run dev';
spinner.clear(`Run \`${cmd}\` inside of "${dirname}" to start the app`);
} catch (error) {
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;
}