This repository has been archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
75 lines (64 loc) · 2.03 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
import {flags} from '@oclif/command'
import axios from 'axios'
import {renameSync} from 'fs'
import {prompt} from 'inquirer'
import {join} from 'path'
import Command from '../../root-command'
import deployer from '../../utils/deployer'
const templatesURL = 'https://raw.githubusercontent.com/mesg-foundation/awesome/master/templates.json'
interface Template {
name: string
url: string
}
export default class ServiceInit extends Command {
static description = 'Initialize a service from a template'
static flags = {
...Command.flags,
template: flags.string({char: 't', description: 'Specify the template URL to use'}),
}
static args = [{
name: 'DIR',
required: true,
description: 'Directory to initialize a service into'
}]
async run(): Promise<string> {
const {args, flags} = this.parse(ServiceInit)
const templateUrl = await this.getTemplateUrl(flags.template)
this.spinner.start('Initialize your project')
await this.downloadTemplate(args.DIR, templateUrl)
this.spinner.stop(args.DIR)
return args.DIR
}
async getTemplateUrl(template: string | undefined) {
if (template) {
return template
}
this.spinner.start('Fetching templates')
const templates = await this.fetchTemplates()
this.spinner.stop()
const {value} = (await prompt({
type: 'list',
name: 'value',
message: 'Select the template to use',
default: 'Basic',
choices: templates.map(x => ({
name: `${x.name} ➜ ${x.url}`,
value: x.name,
short: x.name
}))
})) as { value: string }
const selectedTemplate = templates.find(x => x.name === value)
if (!selectedTemplate) {
throw new Error(`The template name "${value}" is not valid`)
}
return selectedTemplate.url
}
async fetchTemplates() {
const response = await axios.get(templatesURL)
return response.data as Template[]
}
async downloadTemplate(path: string, url: string) {
const tmpPath = await deployer(url)
renameSync(join(tmpPath, 'template'), path)
}
}