-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.ts
240 lines (211 loc) · 6.98 KB
/
add.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import {flags} from '@oclif/command'
import * as Path from 'path'
import * as FileSystem from 'fs'
import Base from '../helper/base'
/**
* @class {Add}
*/
export default class Add extends Base {
/**
* @type {string}
*/
static description = 'initialize a directory to be recognized as a devitools project'
/**
* @type {string[]}
*/
static examples = [
'$ devi add general --group | devi add general -g // create new namespace without schemas',
'$ devi add main.category // add a new entity Category namespaced by Main',
'$ devi add financial.bank-account // use kebab case',
'$ devi add main.company.partners // use dot notation to nested items',
'$ devi add foo.bar -o | devi add foo.bar --override // override all without ask',
'$ devi add foo.bar -t=my-template // use a custom template (the default value is default)',
'$ devi add foo.bar --template=my-template // use a custom template (the default value is default)',
'$ devi add foo.bar -p=whatever | devi add foo.bar --parameters=whatever',
'* special parameters:',
' - devi add foo.bar -p=array',
' - devi add foo.bar -p=builtin',
'* JSON support:',
' - devi add foo.bar -p={"foo":"bar"}',
]
/**
* @type {Array}
*/
static args = [
{
// name of arg to show in help and reference with args[name]
name: 'domain',
// make the arg required with `required: true`
required: true,
// help description
description: 'the domain that will be created',
// hide this arg from help
// hidden: false,
// default value if no arg input
// default: 'world',
// only allow input to be from a discrete set
// options: ['a', 'b'],
},
]
/**
* @type {Record<string, any>}
*/
static flags = {
help: flags.help({char: 'h'}),
override: flags.boolean({char: 'o'}),
template: flags.string({char: 't'}),
parameters: flags.string({char: 'p'}),
group: flags.boolean({char: 'g'}),
}
/**
* @param {string} dir
* @param {string[]} filter
*/
getFiles(dir: string, filter: string[] = []) {
const isDirectory = (path: string) => FileSystem.statSync(path).isDirectory()
const isNotWanted = (path: string) => {
if (filter.length === 0) {
return true
}
for (const expression of filter) {
if (path.match(expression)) {
return true
}
}
return false
}
const getDirectories = (path: string) => FileSystem
.readdirSync(path)
.map((name: string) => Path.join(path, name))
.filter(isDirectory)
const isFile = (path: string) => FileSystem.statSync(path).isFile()
const getFiles = (path: string) => FileSystem
.readdirSync(path).map(name => Path.join(path, name))
.filter(isFile)
.filter(isNotWanted)
const getFilesRecursively = (path: string): string[] => {
const dirs = getDirectories(path)
const files = dirs
.map(dir => getFilesRecursively(dir)) // go through each directory
.reduce((a, b) => a.concat(b), []) // map returns a 2d array (array of file arrays) so flatten
return files.concat(getFiles(path))
}
return getFilesRecursively(dir)
}
/**
* @param {string} source
* @param {string} target
* @param {Record<string, any> | string[]} replaces
* @param {string[]} filter
*/
async generate(source: string, target: string, replaces: Record<string, unknown> | string[], filter: string[] = []) {
const {flags} = this.parse(Add)
const files = this.getFiles(source, filter)
const map = files.map(file => {
const origin = file.replace(source, '')
return {
source: origin,
target: this.replaceExtension(String(this.replacePath(origin, replaces))),
}
})
for (const entry of map) {
const content = String(FileSystem.readFileSync(Path.join(source, entry.source)))
const file = this.replaceTemplate(content, replaces)
const filename = Path.join(target, entry.target)
if (flags.override) {
this.writeFile(Path.join(target, entry.target), file)
continue
}
// eslint-disable-next-line no-await-in-loop
const exists = await this.exists(filename)
if (exists) {
// eslint-disable-next-line no-await-in-loop
const answer = await this.prompt(` File '${entry.target}' already exists. Override?`, 'y')
if (answer !== 'y') {
continue
}
}
this.writeFile(Path.join(target, entry.target), file)
}
}
/**
*/
async run() {
await this.welcome()
const devitoolsFile = Path.join(process.cwd(), '.devitools.json')
try {
await FileSystem.promises.access(devitoolsFile)
} catch (error) {
this.error('The project is not valid')
this.exit(2)
return
}
const content = FileSystem.readFileSync(devitoolsFile)
const target = JSON.parse(String(content))
const {args, flags} = this.parse(Add)
const entry = String(args.domain)
const separator = entry.includes('.') ? '.' : '/'
const fragments = entry.split(separator)
if (!flags.group && fragments.length <= 1) {
this.error('The domain is not valid')
this.exit(2)
return
}
const template = flags.template || target.template || 'default'
let entity = fragments[0]
if (!flags.group) {
entity = String(fragments.pop())
.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>{}[]\/]/gi, '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
}
const domain = fragments.map(fragment => {
return fragment
.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>{}[]\/]/gi, '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
})
const project = Path.join(process.cwd(), '.devitools', 'templates', template, 'index.js')
const bundled = Path.join(__dirname, '..', '..', 'templates', template, 'index.js')
let runner
try {
runner = require(project)
this.disabled(`~> Template '${template}' loaded from project`)
} catch (error) {
// silent is gold
}
if (!runner) {
try {
runner = require(bundled)
this.disabled(`~> Template '${template}' loaded from bundled`)
} catch (error) {
// silent is gold
}
}
if (!runner) {
this.log(JSON.stringify({bundled, project}, null, 2))
this.error(`~> Template '${template}' not found`)
return
}
let parameters: Record<string, any> = {}
if (flags.parameters) {
const input = flags.parameters
try {
parameters = JSON.parse(input)
} catch (error) {
const pieces = String(input).split('.')
for (const piece of pieces) {
const fragments = piece.split('=')
if (fragments.length > 1) {
const [key, value] = fragments
parameters[key] = value === 'false' ? false : value
continue
}
parameters[piece] = true
}
}
}
await runner(this, target, entity, domain, parameters, flags.group)
this.bye()
}
}