-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·218 lines (164 loc) · 5.95 KB
/
cli.js
File metadata and controls
executable file
·218 lines (164 loc) · 5.95 KB
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
#!/usr/bin/env node
'use strict';
const fs = require('fs'),
path = require('path'),
jsyaml = require('js-yaml'),
rewire = require('rewire'),
specVariables = require('./libs/specVariables'),
templating = require('./libs/templating'),
swaggerTools = require('swagger-tools'),
strw = rewire( './node_modules/swagger-tools/middleware/swagger-metadata.js' );
/**
*
* Extracts the path option from the CLI params,
* checks if it is local or absolute and then
* checks that it exists.
*
* @param options
* @param {string} val
* @param {boolean} shouldExist
* @returns {*}
*/
const normaliseFileParam = function ( options, val, shouldExist ) {
if ( !options || !options[val] ){
throw new Error( 'Target file/dir not specified' );
}
const file = ( path.isAbsolute( options[val] ) ? options[val] : path.join( __dirname, options[val] ) );
if ( shouldExist && !fs.existsSync( file ) ){
throw new Error( 'File/Dir not found: ' + file );
}
return file;
};
/**
*
* @param swaggerFile
* @returns {Promise<any>}
*/
const processSwaggerFile = function ( swaggerFile ) {
return new Promise( ( resolve, reject ) => {
const spec = fs.readFileSync( path.join( swaggerFile ), 'utf8' );
const swaggerDoc = jsyaml.safeLoad( spec );
const services = {};
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware( swaggerDoc, function () {
const pSpec = strw.__get__( 'processSwaggerDocuments' )( swaggerDoc );
for ( const i in pSpec ){
for ( const j in pSpec[i].operations ){
const operation = pSpec[i].operations[j].operation;
const pin = specVariables.identifyPin( operation );
if ( !services[pin] ){
services[pin] = specVariables.extractServiceDetails( swaggerDoc, operation, i, j );
services[pin].operations = [];
}
//merge operations from when the pin is identical.
services[pin].operations.push( specVariables.extractOperationDetails( operation, i, j ) )
}
}
resolve( services );
});
} )
};
/**
*
* Action function that handles the dump templates command.
*
* @param args
* @param options
*/
const dumpTemplates = function ( args, options ) {
options.template = 'templates';
const templateDir = normaliseFileParam( options, 'template', false );
const target = normaliseFileParam( options, 'target', false );
templating.dumpTemplates( templateDir, target );
};
/**
*
* Reads a Swagger spec file and extracts all the data that is
* used to create the services in the moustache files.
*
* @param args
* @param options
*/
const printVariables = function ( args, options ) {
const swaggerFile = normaliseFileParam( options, 'spec', true );
processSwaggerFile( swaggerFile ).then(value => {
for ( const i in value ){
console.log( "\n\n" );
console.log( 'Pin: ' + i + '\n');
console.log( JSON.stringify( value[i], null, 4 ) );
}
}).catch(reason => {
throw new Error( reason );
});
};
/**
*
*
* @param args
* @param options
*/
const generate = function ( args, options ) {
if ( !options.template ){
options.template = 'templates';
}
const swaggerFile = normaliseFileParam( options, 'spec', true );
const targetDir = normaliseFileParam( options, 'target', false );
const templateDir = normaliseFileParam( options, 'template', false );
const override = options.override;
const standalone = options.standalone;
processSwaggerFile( swaggerFile ).then(value => {
for ( const i in value ){
templating.createService( value[i], templateDir, targetDir, override, standalone );
}
console.log( 'Modules/projects generated in: ' + targetDir );
}).catch(reason => {
throw new Error( reason );
});
};
const prog = require('caporal');
prog
.version('1.0.0')
.command('generate')
.option( '--spec <swagger_file>',
'the location of the swagger file to debug',
prog.STRING,
null,
true )
.option( '--target <directory>',
'target directory to create the micro-services in',
prog.STRING,
'micro-services',
false )
.option( '--template <directory>',
'Directory containing custom template files',
prog.STRING,
null,
false )
.option( '--override <directory>',
'Directory containing custom template files',
prog.BOOLEAN,
true)
.option( '--standalone <boolean>',
'Output each pin as a standalone service.',
prog.BOOLEAN,
true,
true)
.help('The generate command consumes a swagger spec and creates one or more Seneca modules/projects based on the Swagger operations.') // here our custom help for the `order` command
.action(generate)
.command('dump_templates')
.option( '--target <directory>',
'target directory to create the templates',
prog.STRING,
'swagger-seneca-codegen-templates',
false )
.help('The dump_template command writes the default moustache files to a directory. The default files can be customised and fed back into the generate command by using the --template option.')
.action(dumpTemplates)
.command('print_variables')
.option( '--spec <swagger_file>',
'the location of the swagger file to debug',
prog.STRING,
null,
true )
.help('This command consumes a Swagger spec and processes it as if module/project generation was to occur, however instead it prints the processed data to the console. This command is included to help you create your own template files.')
.action(printVariables);
prog.parse(process.argv);