Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit cc1063a

Browse files
committed
refator(cli): improve init command
1 parent e0b2cdc commit cc1063a

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

cli.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,21 @@ async function main() {
172172
}
173173

174174
const { default: cmd } = await import(`./cli/${command}.ts`)
175-
if (command === 'upgrade') {
176-
await cmd(flags.v || flags.version || args[0] || 'latest')
177-
return
178-
}
179-
const appDir = path.resolve(args[0] || '.')
180-
if (command !== 'init' && !existsDirSync(appDir)) {
181-
log.fatal('No such directory:', appDir)
175+
switch (command) {
176+
case 'init':
177+
await cmd(args[0])
178+
break
179+
case 'upgrade':
180+
await cmd(flags.v || flags.version || args[0] || 'latest')
181+
break
182+
default:
183+
const appDir = path.resolve(args[0] || '.')
184+
if (!existsDirSync(appDir)) {
185+
log.fatal('No such directory:', appDir)
186+
}
187+
await cmd(appDir, flags)
188+
break
182189
}
183-
await cmd(appDir, flags)
184190
}
185191

186192
if (import.meta.main) {

cli/init.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ Options:
1414
-h, --help Prints help message
1515
`
1616

17-
export default async function (appDir: string, options: Record<string, string | boolean>) {
17+
export default async function (n?: string) {
18+
const cwd = Deno.cwd()
1819
const rev = 'master'
19-
const template = 'hello-world'
20-
const vscode = await confirm(`Add recommended VS Code workspace settings?`)
20+
21+
const name = n || (await ask('Name:')).trim()
22+
if (name === '') {
23+
return
24+
}
25+
26+
const template = 'hello-world' // todo: add template select ui
27+
const vscode = await confirm('Add recommended workspace settings of VS Code?')
2128

2229
console.log('Downloading template...')
2330
const resp = await fetch('https://codeload.github.com/alephjs/alephjs-templates/tar.gz/' + rev)
@@ -27,10 +34,9 @@ export default async function (appDir: string, options: Record<string, string |
2734
const tarData = gzipDecode(gzData)
2835
const entryList = new Untar(new Deno.Buffer(tarData))
2936

30-
// todo: add template select ui
3137
for await (const entry of entryList) {
3238
if (entry.fileName.startsWith(`alephjs-templates-${rev}/${template}/`)) {
33-
const fp = path.join(appDir, util.trimPrefix(entry.fileName, `alephjs-templates-${rev}/${template}/`))
39+
const fp = path.join(cwd, name, util.trimPrefix(entry.fileName, `alephjs-templates-${rev}/${template}/`))
3440
if (entry.type === 'directory') {
3541
await ensureDir(fp)
3642
continue
@@ -49,6 +55,7 @@ export default async function (appDir: string, options: Record<string, string |
4955
]
5056
const importMap = {
5157
imports: {
58+
'~/': './', '@/': './',
5259
'aleph': `https://deno.land/x/aleph@v${VERSION}/mod.ts`,
5360
'aleph/': `https://deno.land/x/aleph@v${VERSION}/`,
5461
'react': 'https://esm.sh/[email protected]',
@@ -57,8 +64,8 @@ export default async function (appDir: string, options: Record<string, string |
5764
scopes: {}
5865
}
5966
await Promise.all([
60-
Deno.writeTextFile(path.join(appDir, '.gitignore'), gitignore.join('\n')),
61-
Deno.writeTextFile(path.join(appDir, 'import_map.json'), JSON.stringify(importMap, undefined, 4))
67+
Deno.writeTextFile(path.join(cwd, name, '.gitignore'), gitignore.join('\n')),
68+
Deno.writeTextFile(path.join(cwd, name, 'import_map.json'), JSON.stringify(importMap, undefined, 4))
6269
])
6370

6471
if (vscode) {
@@ -72,21 +79,21 @@ export default async function (appDir: string, options: Record<string, string |
7279
'deno.unstable': true,
7380
'deno.import_map': './import_map.json'
7481
}
75-
await ensureDir(path.join(appDir, '.vscode'))
82+
await ensureDir(path.join(name, '.vscode'))
7683
await Promise.all([
77-
Deno.writeTextFile(path.join(appDir, '.vscode', 'extensions.json'), JSON.stringify(extensions, undefined, 4)),
78-
Deno.writeTextFile(path.join(appDir, '.vscode', 'settings.json'), JSON.stringify(settigns, undefined, 4))
84+
Deno.writeTextFile(path.join(name, '.vscode', 'extensions.json'), JSON.stringify(extensions, undefined, 4)),
85+
Deno.writeTextFile(path.join(name, '.vscode', 'settings.json'), JSON.stringify(settigns, undefined, 4))
7986
])
8087
}
8188

8289
console.log('Done')
83-
console.log('---')
84-
console.log(colors.green('Aleph.js is ready to Go.'))
85-
console.log(`${colors.dim('$')} cd ` + path.basename(appDir))
86-
console.log(`${colors.dim('$')} aleph ${colors.bold('dev')} ${colors.dim('# start the app in `development` mode')}`)
87-
console.log(`${colors.dim('$')} aleph ${colors.bold('start')} ${colors.dim('# start the app in `production` mode')}`)
88-
console.log(`${colors.dim('$')} aleph ${colors.bold('build')} ${colors.dim('# build the app to a static site (SSG)')}`)
89-
console.log('---')
90+
console.log(colors.dim('---'))
91+
console.log(colors.green('Aleph.js is ready to go!'))
92+
console.log(`${colors.dim('$')} cd ${name}`)
93+
console.log(`${colors.dim('$')} aleph dev ${colors.dim('# start the app in `development` mode')}`)
94+
console.log(`${colors.dim('$')} aleph start ${colors.dim('# start the app in `production` mode')}`)
95+
console.log(`${colors.dim('$')} aleph build ${colors.dim('# build the app to a static site (SSG)')}`)
96+
console.log(colors.dim('---'))
9097
Deno.exit(0)
9198
}
9299

0 commit comments

Comments
 (0)