-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
169 lines (138 loc) · 3.67 KB
/
cli.ts
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
import path from 'path'
import { parseArgs, promisify } from 'util'
import semver from 'semver'
import fs from 'fs'
import readline from 'readline'
import {
type Tuple,
error,
success,
isError,
unwrap,
$,
LogLevel,
log,
} from './util.js'
import { TEMPLATES } from './templates.js'
import { printBanner } from './banner.js'
const {
positionals,
values: opts,
} = parseArgs({
allowPositionals: true,
options: {
bare: {
type: 'boolean',
short: 'b',
},
stable: {
type: 'boolean',
short: 's',
},
force: {
type: 'boolean',
short: 'f',
},
template: {
type: 'string',
short: 't',
},
},
})
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const question = promisify(rl.question).bind(rl)
const allChecksPass = async (checks: Promise<Tuple>[]) => {
for( const check of checks ) {
const result = await check
log(isError(result)
? LogLevel.Error
: LogLevel.Info, unwrap(result))
if( isError(result) ) {
return
}
}
return true
}
const checkPackageVersion = async () => {
if( opts.force ) {
return success('skipping version check as -f option was passed')
}
const {
name: packageName,
version: packageVersion,
} = require('../package.json')
const remoteVersion = await $(`npm view ${packageName} version`)
if( packageVersion !== remoteVersion ) {
return error([
'local and remote versions of this package differ',
`rerun using the '@latest' specifier (npm create -y aeria-app@latest ${positionals[0] || 'my-app'})`,
'alternativelly, rerun this command with the -f option (npm create aeria-app package-name -- -f)',
])
}
return success('package is up-to-date')
}
const checkCompatibility = async () => {
const localNodeVersion = await $('node -v')
const remoteNodeVersion = await $('npm view aeria engine.node')
if( !semver.satisfies(localNodeVersion, remoteNodeVersion) ) {
return error('local node version is outdated')
}
return success('node version is compatible')
}
const main = async () => {
printBanner()
const { template = 'app-ts' } = opts
const checksOk = await allChecksPass([
checkPackageVersion(),
checkCompatibility(),
])
if( !checksOk ) {
return
}
if( !(template in TEMPLATES) ) {
log(LogLevel.Error, `invalid template, available ones are: ${Object.keys(TEMPLATES)}`)
return
}
const projectName = positionals[0]
? positionals[0]
: await question('what\'s the project name? ')
if( !projectName ) {
log(LogLevel.Error, 'project name can not be empty')
return
}
const cwd = process.cwd()
const projectPath = path.join(cwd, projectName)
if( fs.existsSync(projectPath) ) {
log(LogLevel.Error, `path '${projectPath}' already exists`)
return
}
await $(`git clone --depth=1 --branch=master ${TEMPLATES[template]} ${projectPath}`)
await fs.promises.rm(path.join(projectPath, '.git'), {
recursive: true,
})
await fs.promises.copyFile(
path.join(projectPath, 'api', 'sample.env'),
path.join(projectPath, 'api', '.env'),
)
if( !opts.stable ) {
log(LogLevel.Info, 'removing create-aeria-app.lock')
await fs.promises.unlink(path.join(projectPath, 'create-aeria-app.lock'))
}
if( !opts.bare ) {
log(LogLevel.Info, 'installing dependencies')
await $([
`cd ${projectPath.replace(/\\/g, '/')}`,
'npm i',
])
}
log(LogLevel.Info, `your project '${projectName}' was created, make good use of it`)
log(LogLevel.Info, 'to serve your project, cd into it and run: npm run dev')
}
const wrapper = async () => {
await main()
rl.close()
}
wrapper()