-
Notifications
You must be signed in to change notification settings - Fork 166
/
init.ts
115 lines (101 loc) · 3.81 KB
/
init.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
import { Untar } from 'https://deno.land/std@0.90.0/archive/tar.ts'
import { green, dim } from 'https://deno.land/std@0.90.0/fmt/colors.ts'
import { ensureDir } from 'https://deno.land/std@0.90.0/fs/ensure_dir.ts'
import { join } from 'https://deno.land/std@0.90.0/path/mod.ts'
import { gzipDecode } from 'https://deno.land/x/wasm_gzip@v1.0.0/mod.ts'
import { ensureTextFile } from '../shared/fs.ts'
import util from '../shared/util.ts'
import { VERSION } from '../version.ts'
export const helpMessage = `
Usage:
aleph init <name> [...options]
<name> represents the name of Aleph.js app.
Options:
-h, --help Prints help message
`
export default async function (nameArg?: string) {
const cwd = Deno.cwd()
const rev = 'master'
const name = nameArg || (await ask('Name:')).trim()
if (name === '') {
return
}
const template = 'hello-world' // todo: add template select ui
const vscode = await confirm('Add recommended workspace settings of VS Code?')
console.log('Downloading template...')
const resp = await fetch('https://codeload.github.com/alephjs/alephjs-templates/tar.gz/' + rev)
const gzData = await Deno.readAll(new Deno.Buffer(await resp.arrayBuffer()))
console.log('Saving template...')
const tarData = gzipDecode(gzData)
const entryList = new Untar(new Deno.Buffer(tarData))
for await (const entry of entryList) {
if (entry.fileName.startsWith(`alephjs-templates-${rev}/${template}/`)) {
const fp = join(cwd, name, util.trimPrefix(entry.fileName, `alephjs-templates-${rev}/${template}/`))
if (entry.type === 'directory') {
await ensureDir(fp)
continue
}
await ensureTextFile(fp, '')
const file = await Deno.open(fp, { write: true })
await Deno.copy(entry, file)
}
}
const gitignore = [
'.DS_Store',
'Thumbs.db',
'.aleph/',
'dist/',
]
const importMap = {
imports: {
'~/': './', '@/': './',
'aleph': `https://deno.land/x/aleph@v${VERSION}/mod.ts`,
'aleph/': `https://deno.land/x/aleph@v${VERSION}/`,
'react': 'https://esm.sh/react@17.0.1',
'react-dom': 'https://esm.sh/react-dom@17.0.1',
},
scopes: {}
}
await Promise.all([
Deno.writeTextFile(join(cwd, name, '.gitignore'), gitignore.join('\n')),
Deno.writeTextFile(join(cwd, name, 'import_map.json'), JSON.stringify(importMap, undefined, 4))
])
if (vscode) {
const extensions = {
'recommendations': [
'denoland.vscode-deno'
]
}
const settigns = {
'deno.enable': true,
'deno.unstable': true,
'deno.importMap': './import_map.json'
}
await ensureDir(join(name, '.vscode'))
await Promise.all([
Deno.writeTextFile(join(name, '.vscode', 'extensions.json'), JSON.stringify(extensions, undefined, 4)),
Deno.writeTextFile(join(name, '.vscode', 'settings.json'), JSON.stringify(settigns, undefined, 4))
])
}
console.log('Done')
console.log(dim('---'))
console.log(green('Aleph.js is ready to go!'))
console.log(`${dim('$')} cd ${name}`)
console.log(`${dim('$')} aleph dev ${dim('# start the app in `development` mode')}`)
console.log(`${dim('$')} aleph start ${dim('# start the app in `production` mode')}`)
console.log(`${dim('$')} aleph build ${dim('# build the app to a static site (SSG)')}`)
console.log(dim('---'))
Deno.exit(0)
}
async function ask(question: string = ':', stdin = Deno.stdin, stdout = Deno.stdout) {
await stdout.write(new TextEncoder().encode(question + ' '))
const buf = new Uint8Array(1024)
const n = <number>await stdin.read(buf)
const answer = new TextDecoder().decode(buf.subarray(0, n))
return answer.trim()
}
async function confirm(question: string = 'are you sure?') {
let a: string
while (!/^(y(es)?|no?)$/i.test(a = (await ask(question + ' [y/n]')).trim())) { }
return a.charAt(0).toLowerCase() === 'y'
}