-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.ts
341 lines (320 loc) · 10.6 KB
/
service.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { AbstractCommand, CommandOptions } from '../abstract-command'
import {
fileExists,
findCommonPath,
locateFile,
readFileContent,
writeToFile
} from '../../utils/file-operations'
import {
contextInterfacePattern,
contextInterfaceSignatureFn,
contextInterfaceStartSeparators,
contextPattern,
contextSignatureFn,
contextStartSeparators,
importServicePrefix,
injectorPattern,
injectorSignatureFn,
injectorStartSeparators,
serviceFileNameSuffix,
serviceFilePrefix,
serviceFileSuffix,
servicePattern
} from './service-files-constants'
import * as path from 'path'
import logger from '../../utils/logger'
import * as parser from 'typescript-parser'
import * as _ from 'lodash'
import { ActionCmd } from '../ActionCmd'
type DefinitionFileType = 'injector' | 'context' | 'context-interface'
interface DefinitionFileProps {
filePattern: object
declarationName: string
possibleStartSeparators: string[]
definitionSignatureFn: (serviceName: string) => string
ignoreContext: boolean
}
interface DeclarationInfo {
content: string
start: number
end: number
}
const fileDefinitionMap: Map<DefinitionFileType, DefinitionFileProps> = new Map<
DefinitionFileType,
DefinitionFileProps
>([
[
'injector',
{
filePattern: injectorPattern,
declarationName: 'injector',
possibleStartSeparators: injectorStartSeparators,
definitionSignatureFn: injectorSignatureFn,
ignoreContext: false
}
],
[
'context',
{
filePattern: contextPattern,
declarationName: 'getContext',
possibleStartSeparators: contextStartSeparators,
definitionSignatureFn: contextSignatureFn,
ignoreContext: true
}
],
[
'context-interface',
{
filePattern: contextInterfacePattern,
declarationName: 'IAppContext',
possibleStartSeparators: contextInterfaceStartSeparators,
definitionSignatureFn: contextInterfaceSignatureFn,
ignoreContext: true
}
]
])
export class Service extends AbstractCommand {
private parser: parser.TypescriptParser
constructor() {
super()
this.parser = new parser.TypescriptParser()
}
public getName(): string {
return 'service [service-path]'
}
public getDescription(): string {
return 'Add new service to services dir, and to server gql context'
}
public getAlias(): string {
return 's'
}
public getOptions(): CommandOptions {
return {
optionsPattern: ['--ignoreContext'],
optionsDescription: ['Do not add service to server gql context']
}
}
public getAction(): (...args: any[]) => void {
return async (servicePath: string, cmd: ActionCmd) => {
return this.createServiceFile(servicePath, cmd.parent.ignoreContext)
}
}
private async createServiceFile(servicePath: string, ignoreContext: boolean | undefined) {
await this.writeServiceFile(servicePath, ignoreContext)
logger.info('Service generation command was completed')
}
private async writeServiceFile(servicePath: string, ignoreContext: boolean | undefined) {
try {
const pathToServicesDir = await locateFile(servicePattern, './src', 'dir')
const commonRootPath = findCommonPath(pathToServicesDir)
if (!servicePattern.test(commonRootPath)) {
logger.warn(
'services directory should appear exactly once in the project, found paths- ' +
pathToServicesDir.toString()
)
logger.warn(
'please make sure you are at the project root directory and that src dir contains single services dir'
)
} else {
const joinedPathToService = path.join(pathToServicesDir[0], servicePath)
const serviceFileExists = await fileExists(joinedPathToService)
if (serviceFileExists) {
logger.warn('Service file already exists. Please select a different location')
} else {
logger.info('Generating service file...')
const serviceName = this.getServiceClassName(servicePath)
await writeToFile(joinedPathToService, this.getServiceFileContent(serviceName))
logger.info('Service file was added to- ' + joinedPathToService)
logger.info('Editing service definition files...')
const definitionFileTypes = Array.from(fileDefinitionMap.keys())
const serviceDefinitionPromises = definitionFileTypes
.filter((def: DefinitionFileType) => {
const definitionProps = fileDefinitionMap.get(def) as DefinitionFileProps
return ignoreContext ? !definitionProps.ignoreContext : true
})
.map(def => this.modifyServiceDefinitionFile(joinedPathToService, serviceName, def))
await Promise.all(serviceDefinitionPromises)
logger.info('Definition files were successfully updated!')
}
}
} catch (err) {
logger.error(`Got error in generating service- ${err}`)
}
}
private getServiceFileContent(serviceClassName: string) {
return serviceFilePrefix + serviceClassName + serviceFileSuffix
}
private getServiceClassName(servicePath: string) {
return (
_.startCase(path.parse(servicePath).base.split('.')[0]).replace(/\W/g, '') +
serviceFileNameSuffix
)
}
private async modifyServiceDefinitionFile(
pathToService: string,
serviceClassName: string,
definitionFileType: DefinitionFileType
): Promise<void> {
logger.info('Modifying definitions in ' + definitionFileType + ' file...')
const filePatternToLocate = (fileDefinitionMap.get(definitionFileType) as DefinitionFileProps)
.filePattern
const pathToDefFile = await locateFile(filePatternToLocate, './src', 'file')
if (!pathToDefFile || pathToDefFile.length !== 1) {
logger.warn('Definition file for ' + definitionFileType + ' not found')
} else {
const fileContent = await this.getModifiedFileContent(
pathToService,
serviceClassName,
pathToDefFile[0],
definitionFileType
)
return writeToFile(pathToDefFile[0], fileContent)
}
}
private async getModifiedFileContent(
pathToService: string,
serviceClassName: string,
defFilePath: string,
definitionFileType: DefinitionFileType
): Promise<string> {
const defFileContent = await readFileContent(defFilePath)
const parsedFile: parser.File = await this.parser.parseFile(defFilePath, './')
const { content: importsContent, end: importEnd } = this.getImportDefinitions(
pathToService,
serviceClassName,
parsedFile,
defFileContent
)
const {
content: defContent,
start: defStart,
end: defEnd
} = this.getModifiedServiceDefinitions(
serviceClassName,
parsedFile,
defFileContent,
definitionFileType
)
return (
importsContent +
defFileContent.substring(importEnd, defStart) +
defContent +
defFileContent.substring(defEnd)
)
}
private getImportDefinitions(
pathToService: string,
serviceClassName: string,
parsedFile: parser.File,
defFileContent: string
): DeclarationInfo {
const importsEndChar = Array.isArray(parsedFile.imports)
? parsedFile.imports[parsedFile.imports.length - 1].end || 0
: 0
const originalImports = defFileContent.substring(0, importsEndChar)
return {
content: originalImports + '\n' + this.getServiceImportLine(pathToService, serviceClassName),
start: 0,
end: importsEndChar
}
}
private getServiceImportLine(pathToService: string, serviceClassName: string): string {
return (
'import { ' +
serviceClassName +
" } from '" +
importServicePrefix +
this.locationInsideServicesDir(pathToService) +
"'"
)
}
private locationInsideServicesDir(pathToService: string) {
return pathToService
.split('.')[0]
.split('services')[1]
.replace(/\\/g, '/')
}
private getModifiedServiceDefinitions(
serviceClassName: string,
parsedFile: parser.File,
defFileContent: string,
definitionFileType: DefinitionFileType
): DeclarationInfo {
const declarationsArray = parsedFile.declarations
const { content: definitionToModify, start, end } = this.getDeclarationContentByDefType(
declarationsArray,
defFileContent,
definitionFileType
)
return {
content: this.addServiceToPreviousDefinition(
serviceClassName,
definitionToModify,
definitionFileType
),
start,
end
}
}
private getDeclarationContentByDefType(
declarationsArray: parser.Declaration[],
defFileContent: string,
definitionFileType: DefinitionFileType
): DeclarationInfo {
const emptyDeclarationInfo: DeclarationInfo = {
content: '',
start: 0,
end: 0
}
const nameToFilterBy = (fileDefinitionMap.get(definitionFileType) as DefinitionFileProps)
.declarationName
const filteredDeclaration: parser.Declaration[] = declarationsArray.filter(
declaration => declaration.name === nameToFilterBy && declaration.start
)
if (filteredDeclaration.length !== 1) {
logger.warn(
'Definition file ' +
definitionFileType +
' should contain exactly one definition for ' +
nameToFilterBy +
', file will not be changed...'
)
return emptyDeclarationInfo
} else {
const declarationToFind: parser.Declaration = filteredDeclaration[0]
if (!declarationToFind.start || !declarationToFind.end) {
logger.warn('Could not find start and end for declaration ' + declarationToFind.name)
return emptyDeclarationInfo
} else {
return {
content: defFileContent.substring(declarationToFind.start, declarationToFind.end),
start: declarationToFind.start,
end: declarationToFind.end
}
}
}
}
private addServiceToPreviousDefinition(
serviceClassName: string,
definitionToModify: string,
definitionFileType: DefinitionFileType
): string {
const definitionFileProps = fileDefinitionMap.get(definitionFileType) as DefinitionFileProps
const possibleStartSeparators: string[] = definitionFileProps.possibleStartSeparators
const serviceSignatureFn = definitionFileProps.definitionSignatureFn
for (let separator of possibleStartSeparators) {
const indexOfSeparator = definitionToModify.lastIndexOf(separator)
if (indexOfSeparator !== -1) {
return (
definitionToModify.substring(0, indexOfSeparator + separator.length) +
'\n' +
serviceSignatureFn(serviceClassName) +
definitionToModify.substring(indexOfSeparator + separator.length)
)
}
}
return definitionToModify
}
}