Skip to content

Simple Init UI #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions cli/init.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Confirm, Input } from 'https://deno.land/x/cliffy/prompt/mod.ts'
import { gzipDecode } from 'https://deno.land/x/wasm_gzip@v1.0.0/mod.ts'
import { ensureTextFile } from '../fs.ts'
import log from '../log.ts'
import { colors, ensureDir, fromStreamReader, path, Untar } from '../std.ts'
import util from '../util.ts'


const gitignore = [
'.DS_Store',
'Thumbs.db',
Expand Down Expand Up @@ -44,18 +46,18 @@ Options:

export default async function (appDir: string, options: Record<string, string | boolean>) {
const rev = 'master'
const templateRepo = await Input.prompt({ message: 'What Aleph.js Github Template Repository Do You Want To Use (Press Enter To Use Default)?', default: 'aleph.js/alephjs-templates' })
log.info('Downloading template...')
const resp = await fetch('https://codeload.github.com/alephjs/alephjs-templates/tar.gz/' + rev)
const resp = await fetch(`https://codeload.github.com/${templateRepo}/tar.gz/${rev}`)
const gzData = await Deno.readAll(fromStreamReader(resp.body!.getReader()))
log.info('Saving template...')
const tarData = gzipDecode(gzData)
const entryList = new Untar(new Deno.Buffer(tarData))

// todo: add template select ui
let template = 'hello-world'
const template = await Input.prompt({ message: 'What Aleph.js Template Do You Want To Use (Press Enter To Use Default)?', default: 'hello-world' })
for await (const entry of entryList) {
if (entry.fileName.startsWith(`alephjs-templates-${rev}/${template}/`)) {
const fp = path.join(appDir, util.trimPrefix(entry.fileName, `alephjs-templates-${rev}/${template}/`))
if (entry.fileName.startsWith(`${templateRepo.match(/([^/]+$)/i)}-${rev}/${template}/`)) {
const fp = path.join(appDir, util.trimPrefix(entry.fileName, `${templateRepo.match(/([^/]+$)/i)}-${rev}/${template}/`))
if (entry.type === 'directory') {
await ensureDir(fp)
continue
Expand All @@ -66,9 +68,11 @@ export default async function (appDir: string, options: Record<string, string |
}
}

await ensureDir(path.join(appDir, '.vscode'))
await Deno.writeTextFile(path.join(appDir, '.vscode', 'extensions.json'), JSON.stringify(vscExtensions, undefined, 4))
await Deno.writeTextFile(path.join(appDir, '.vscode', 'settings.json'), JSON.stringify(vscSettings, undefined, 4))
if (await Confirm.prompt('Are you using Visual Studio Code?')) {
await ensureDir(path.join(appDir, '.vscode'))
await Deno.writeTextFile(path.join(appDir, '.vscode', 'extensions.json'), JSON.stringify(vscExtensions, undefined, 4))
await Deno.writeTextFile(path.join(appDir, '.vscode', 'settings.json'), JSON.stringify(vscSettings, undefined, 4))
}
await Deno.writeTextFile(path.join(appDir, '.gitignore'), gitignore.join('\n'))
await Deno.writeTextFile(path.join(appDir, 'import_map.json'), JSON.stringify({ imports: {} }, undefined, 4))

Expand Down