-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathrun.js
More file actions
executable file
·127 lines (107 loc) · 3.58 KB
/
Copy pathrun.js
File metadata and controls
executable file
·127 lines (107 loc) · 3.58 KB
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
#!/usr/bin/env node
import { cp, readdir } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { parseArgs } from 'node:util'
import { select, question, spinner, bold, gray } from './cli.js'
import { execute } from './shell.js'
const FRAMEWORKS = new Set(['nextjs', 'remix'])
try {
const { values, positionals } = parseArgs({
options: { framework: { type: 'string', short: 'f' }, help: { type: 'boolean', short: 'h' } },
allowPositionals: true,
})
if (values.help) help()
else init({ framework: values.framework, name: positionals[0] })
} catch (e) {
console.log(e)
help()
}
function help() {
console.log(bold('Usage: create-y-sweet-app [name] [options]'))
console.log('\nOptions:')
console.log(' -f, --framework <framework>\t\tUse a specific framework')
console.log('\n Available frameworks:', [...FRAMEWORKS].join(', '), '\n')
console.log(' -h, --help \t\tShow help')
process.exit(0)
}
/**
* @param {object} [options]
* @param {string} [options.framework]
* @param {string} [options.name]
*/
async function init(options = {}) {
let { framework, name } = options
if (!name) {
name = await question(
bold('What do you want to call your app? ') + gray('(my-y-sweet-app) '),
'my-y-sweet-app',
)
}
if (!framework) framework = await select('What framework do you want to use?', [...FRAMEWORKS])
else if (!FRAMEWORKS.has(framework)) {
console.log(`No matching framework "${framework}".`)
console.log('Available frameworks:', [...FRAMEWORKS].join(', '))
process.exit(1)
}
const __dirname = dirname(fileURLToPath(import.meta.url))
const src = resolve(__dirname, './frameworks', framework)
const dest = resolve(process.cwd(), name)
// ensure the destination directory is empty
try {
const contents = await readdir(dest)
if (contents.length > 0) {
console.error(`Directory ${name} already exists and is not empty.`)
process.exit(1)
}
} catch (err) {
// directory doesn't exist, which is fine
if (err.code !== 'ENOENT') throw err
}
let install = false
const installResponse = await question(
bold('Do you want to install dependencies with npm? ') + gray('(Y/n) '),
'y',
)
if (installResponse.toLowerCase() === 'y') install = true
let git = false
const gitResponse = await question(
bold('Do you want to initialize a Git repository? ') + gray('(Y/n) '),
'y',
)
if (gitResponse.toLowerCase() === 'y') git = true
// copy the template files
const { stop } = spinner('Copying files...')
try {
await cp(src, dest, { recursive: true })
stop()
console.log('✅ Copied files!')
} catch (err) {
stop()
console.error('\nError copying template files:', err)
process.exit(1)
}
// install dependencies
if (install) {
const { stop } = spinner('Installing dependencies...')
for await (const { stdout, stderr } of execute('npm', ['install'], { cwd: dest })) {
stop()
if (stdout) console.log(stdout)
if (stderr) console.error(stderr)
}
console.log('✅ Installed dependencies!')
}
// initialize a git repository
if (git) {
const { stop } = spinner('Initializing Git repository...')
for await (const { stdout, stderr } of execute('git', ['init', '-q'], { cwd: dest })) {
if (stdout) console.log(stdout)
if (stderr) console.error(stderr)
}
stop()
console.log('✅ Initialized git repository!')
}
console.log('🚀 Created y-sweet app!')
console.log(`Run ${bold(`cd ${name}`)} to get started.`)
process.exit(0)
}