forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold.ts
86 lines (68 loc) · 2.51 KB
/
scaffold.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
import path from 'path'
import fs from 'fs'
import { flags } from '@oclif/command'
import _ from 'lodash'
import chalk from 'chalk'
import Listr from 'listr'
import { Kommand } from '../../common'
import { execute } from '../../support/execute'
const templatesDir = path.join(__dirname, '..', '..', '..', 'templates')
const TEMPLATED_ENTRIES = [
'package.json',
'app.ts',
'README.md'
]
export default class AppScaffold extends Kommand {
static initSdk = false
static description = 'Scaffolds a new Kuzzle application'
static flags = {
help: flags.help(),
}
static args = [
{ name: 'name', description: 'Application name', required: true },
]
async runSafe() {
const templatePath = path.join(templatesDir, 'app-scaffold', 'ts')
const tasks = new Listr([
{
title: `Creating directory: ${chalk.gray(this.args.name + path.sep)}`,
task: () => execute('mkdir', this.args.name)
},
{
title: 'Creating and rendering application files',
task: () => this.renderTemplates(templatePath, templatePath)
},
{
title: 'Installing dependencies (this can take some time)',
task: () =>
execute('npm', 'run', 'install:docker', { cwd: this.args.name })
},
])
await tasks.run()
this.logOk(`Scaffolding complete! Use "${chalk.grey('cd ' + this.args.name + ' && npm run dev:docker')}" to run your application`)
}
async renderTemplates(directory: string, templatePath: string) {
const entries = fs.readdirSync(directory)
for (const entry of entries) {
const entryPath = path.join(directory, entry)
const entryDir = path.dirname(path.join(this.args.name, entryPath))
const entryInfo = fs.statSync(entryPath)
if (entryInfo.isDirectory()) {
fs.mkdirSync(entryDir.replace(templatePath, ''), { recursive: true })
await this.renderTemplates(entryPath, templatePath)
}
else if (entryInfo.isFile() || entryInfo.isSymbolicLink()) {
const content = fs.readFileSync(entryPath, 'utf8')
// Only render a whitelist a files who need it
const rendered = TEMPLATED_ENTRIES.includes(entry)
? _.template(content)({ appName: this.args.name })
: content
fs.mkdirSync(entryDir.replace(templatePath, ''), { recursive: true })
fs.writeFileSync(path.join(this.args.name, entryPath.replace(templatePath, '')), rendered)
}
else {
this.logInfo(`Skipped ${entryPath} because it's not a regular file`)
}
}
}
}