-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-aupi-front.mjs
206 lines (185 loc) · 5.14 KB
/
create-aupi-front.mjs
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
#!/usr/bin/env node
'use strict'
import chalk from 'chalk'
import { exec } from 'child_process'
import figlet from 'figlet'
import { existsSync, mkdirSync } from 'fs'
import { join } from 'path'
import inquirer from 'inquirer'
import readlineSync from 'readline-sync'
const repositoryUrls = {
web: 'https://github.com/aupigit/aupi-web-frontend-boilerplate.git',
mobile: 'https://github.com/aupigit/aupi-mobile-frontend-boilerplate.git',
}
console.log(
chalk.blue.bold(
'Welcome to ao Aupi boilerplate. To run this you need to be on Linux!',
),
)
exec('pnpm --version', (pnpmCheckError) => {
if (pnpmCheckError) {
console.log(chalk.yellow.bold('pnpm not found. You want to install pnpm?'))
askForPnpmInstallation()
} else {
checkBun()
}
})
function askForPnpmInstallation() {
inquirer
.prompt([
{
type: 'confirm',
name: 'installPnpm',
message: 'You want to install pnpm?',
default: true,
},
])
.then(({ installPnpm }) => {
if (installPnpm) {
installPnpmGlobal()
} else {
console.log(
chalk.red.bold(
'pnpm is required to continue. Please install pnpm and run again.',
),
)
process.exit(1)
}
})
}
function installPnpmGlobal() {
console.log(chalk.yellow.bold('Installing pnpm globally...'))
exec('npm install -g pnpm', (installPnpmError) => {
if (installPnpmError) {
console.error(
chalk.red.bold('Error installing pnpm globally:'),
installPnpmError,
)
process.exit(1)
}
console.log(chalk.green.bold('pnpm installed globally successfully!'))
checkBun()
})
}
function checkBun() {
exec('bun --version', (bunCheckError) => {
if (bunCheckError) {
console.log(chalk.yellow.bold('bun not found. You want to install bun?'))
askForBunInstallation()
} else {
cloneRepository()
}
})
}
function askForBunInstallation() {
inquirer
.prompt([
{
type: 'confirm',
name: 'installBun',
message: 'You want to install bun?',
default: true,
},
])
.then(({ installBun }) => {
if (installBun) {
installBunGlobal()
} else {
console.log(
chalk.red.bold(
'bun is required to continue. Please install bun and run again.',
),
)
process.exit(1)
}
})
}
function installBunGlobal() {
console.log(chalk.yellow.bold('Installing bun globally...'))
exec('curl -fsSL https://bun.sh/install | bash', (installBunError) => {
if (installBunError) {
console.error(
chalk.red.bold('Error installing bun globally:'),
installBunError,
)
process.exit(1)
}
console.log(chalk.green.bold('bun installed globally successfully!'))
cloneRepository()
})
}
function cloneRepository() {
let projectName = readlineSync.question(
chalk.blue.bold('? ') +
chalk.white.bold('What is your project named? ') +
chalk.gray('› '),
)
if (!projectName) {
projectName = 'my-app'
}
const destinationPath = join(process.cwd(), projectName)
inquirer
.prompt([
{
type: 'list',
name: 'chosenOption',
message: 'Choose the type of project:',
choices: ['web', 'mobile'],
},
])
.then((answers) => {
const chosenOption = answers.chosenOption
const repositoryUrl = repositoryUrls[chosenOption]
if (existsSync(destinationPath)) {
console.error(
`The destination "${destinationPath}" already exists. Please choose another location.`,
)
process.exit(1)
}
mkdirSync(destinationPath, { recursive: true })
exec(`git clone ${repositoryUrl} ${destinationPath}`, (error) => {
if (error) {
console.error(`Error cloning the repository: ${error}`)
process.exit(1)
}
const installCommand =
chosenOption === 'web' ? 'pnpm install' : 'bun install'
const installSpinner = ['-', '\\', '|', '/']
let i = 0
const spinnerInterval = setInterval(() => {
process.stdout.write(
chalk.blue.bold('Installing dependencies... ') +
installSpinner[i % installSpinner.length] +
'\r',
)
i++
}, 100)
exec(installCommand, { cwd: destinationPath }, (installError) => {
clearInterval(spinnerInterval)
process.stdout.clearLine()
process.stdout.cursorTo(0)
if (installError) {
console.error(`Error installing dependencies: ${installError}`)
process.exit(1)
}
console.log(
chalk.green.bold('✅ Dependencies installed successfully!'),
)
})
exec('rm -rf .git', { cwd: destinationPath }, (rmError) => {
if (rmError) {
console.error(`Error removing .git: ${rmError}`)
process.exit(1)
}
})
console.log(
chalk.blue(
figlet.textSync('AUPI', {
horizontalLayout: 'full',
verticalLayout: 'full',
}),
),
)
})
})
}