-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerator.js
109 lines (92 loc) · 3.3 KB
/
generator.js
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
#! /usr/bin/env node
const program = require('commander')
const path = require('path')
const { error } = require('./utils/message')
const { createDirectory, sanitizeName, sanitizeAndPascalCase, sanitizeAndCamelCase, createFile } = require('./utils/files')
const { createServiceFile, createUtilFile } = require('./react')
const packageJson = require("./package.json");
const { ADDRGETNETWORKPARAMS } = require('dns')
program
.name('@chef/services')
.description('CLI to generate your API specific code in lightening speed')
.version(packageJson.version)
.option('-f, --postmanFile <value>', 'postman collection exported file')
.option('-l, --location <value>', 'location separated by slashes')
.option('-u, --utilLocation <value>', 'location separated by slashes')
.parse()
try {
const { postmanFile, location = 'hooks/services', utilLocation = 'utils' } = program.opts()
const FILE_EXTENSION = 'js'
const DEFINITION_FILE = postmanFile
if (!DEFINITION_FILE) {
error('Please provide a postman exported collection file.')
}
const postmanCollectionJson = require(path.resolve(DEFINITION_FILE))
const { item: collections = [] } = postmanCollectionJson || {}
const endpointList = []
const getEndpoinName = (apiItem) => {
return sanitizeName(apiItem?.name, { replaceWith: '_' }).toUpperCase()
}
const createServiceEngine = (apiItem = {}, directoryName) => {
const pascalCasedServiceName = sanitizeAndPascalCase(apiItem?.name)
const customHookName = `use${pascalCasedServiceName}`
const customHookFile = `${customHookName}.${FILE_EXTENSION}`
const { method, url = {}, body = {} } = apiItem?.request || {}
if (!method || !url?.raw) {
return
}
createFile({
pathToFile: path.resolve(location, directoryName, customHookFile),
data: createServiceFile({
name: customHookName,
serviceName: {
default: apiItem?.name,
camelCased: sanitizeAndCamelCase(apiItem?.name),
pascalCased: pascalCasedServiceName,
},
method,
endpoint: getEndpoinName(apiItem),
body: body?.raw || ''
})
})
}
if (Array.isArray(collections)) {
createDirectory(
path.resolve(location)
)
let directoryName = ''
collections.forEach(collectionItem => {
if (Array.isArray(collectionItem?.item)) {
directoryName = sanitizeName(collectionItem?.name)
createDirectory(
path.resolve(location, directoryName)
)
collectionItem?.item.forEach(apiItem => {
const { url = {} } = apiItem?.request || {}
endpointList.push({
name: getEndpoinName(apiItem),
url
})
createServiceEngine(apiItem, directoryName)
})
} else {
const { url = {} } = collectionItem?.request || {}
endpointList.push({
name: getEndpoinName(collectionItem),
url
})
createServiceEngine(collectionItem, directoryName)
}
})
createDirectory(
path.resolve(location, utilLocation)
)
createFile({
pathToFile: path.resolve(location, utilLocation, `endpoints.${FILE_EXTENSION}`),
data: createUtilFile(endpointList)
})
}
console.log('Service Files created. Happy Coding!')
} catch (err) {
console.log('Error while reading file.', err);
}