-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
executable file
·507 lines (498 loc) · 17.4 KB
/
index.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/env node
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const Logger = require('@accordproject/concerto-util').Logger;
const fs = require('fs');
const { glob } = require('glob');
const Commands = require('./lib/commands');
require('yargs')
.scriptName('concerto')
.demandCommand(1, '# Please specify a command')
.recommendCommands()
.strict()
.usage('$0 <cmd> [args]')
.command('validate', 'validate JSON against model files', (yargs) => {
yargs.option('input', {
describe: 'JSON to validate',
type: 'string'
});
yargs.option('model', {
describe: 'array of concerto model files',
type: 'string',
array: true
});
yargs.option('utcOffset', {
describe: 'set UTC offset',
type: 'number'
});
yargs.option('offline', {
describe: 'do not resolve external models',
type: 'boolean',
default: false
});
yargs.option('functional', {
describe: 'new validation API',
type: 'boolean',
default: false
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`validate ${argv.input} against the models ${argv.model}`);
}
try {
argv = Commands.validateValidateArgs(argv);
const options = {};
options.offline = argv.offline;
if (argv.utcOffset !== undefined) {
options.utcOffset = argv.utcOffset;
}
options.functional = argv.functional;
return Commands.validate(argv.input, argv.model, options)
.then((result) => {
Logger.info('Input is valid');
if (!options.functional) {
console.log(result);
}
})
.catch((err) => {
Logger.info('Input is invalid');
Logger.error(err);
});
} catch (err){
Logger.error(err);
return;
}
})
.command('compile', 'generate code for a target platform', (yargs) => {
yargs.option('model', {
describe: 'array of concerto model files',
type: 'string',
array: true,
default: [],
});
yargs.option('offline', {
describe: 'do not resolve external models',
type: 'boolean',
default: false
});
yargs.option('target', {
describe: 'target of the code generation',
type: 'string',
default: 'JSONSchema'
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
default: './output/'
});
yargs.option('metamodel', {
describe: 'Include the Concerto Metamodel in the output',
type: 'boolean',
default: false,
});
yargs.option('strict', {
describe: 'Require versioned namespaces and imports',
type: 'boolean',
default: false,
});
yargs.option('useSystemTextJson', {
describe: 'Compile for System.Text.Json library (`csharp` target only)',
type: 'boolean',
default: false
});
yargs.option('useNewtonsoftJson', {
describe: 'Compile for Newtonsoft.Json library (`csharp` target only)',
type: 'boolean',
default: false
});
yargs.option('namespacePrefix', {
describe: 'A prefix to add to all namespaces (`csharp` target only)',
type: 'string',
});
yargs.option('enableReferenceType', {
describe: 'Enable resolving referential type id (`csharp` target only)',
type: 'boolean',
default: false
});
yargs.option('pascalCase', {
describe: 'Use PascalCase for generated identifier names',
type: 'boolean',
default: true
});
yargs.option('showCompositionRelationships', {
describe: 'Show UML Composition Relationships in diagrams (`plantuml` and `mermaid` targets only)',
type: 'boolean',
default: false
});
yargs.option('hideBaseModel', {
describe: 'Hide the base concerto namespace in diagrams (`plantuml` and `mermaid` targets only)',
type: 'boolean',
default: false
});
yargs.check(({ model, metamodel }) => {
if (model.length > 0 || metamodel) {
return true;
} else {
throw new Error('Please provide models, or specify metamodel');
}
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`generate code for target ${argv.target} from models ${argv.model} into directory: ${argv.output}`);
}
const options = {};
options.offline = argv.offline;
options.strict = argv.strict;
options.metamodel = argv.metamodel;
options.useSystemTextJson = argv.useSystemTextJson;
options.useNewtonsoftJson = argv.useNewtonsoftJson;
options.namespacePrefix = argv.namespacePrefix;
options.enableReferenceType = argv.enableReferenceType;
options.pascalCase = argv.pascalCase;
options.hideBaseModel = argv.hideBaseModel;
options.showCompositionRelationships = argv.showCompositionRelationships;
return Commands.compile(argv.target, argv.model, argv.output, options)
.then((result) => {
Logger.info(result);
})
.catch((err) => {
Logger.error(err.message);
});
})
.command('get', 'save local copies of external model dependencies', (yargs) => {
yargs.demandOption(['model'], 'Please provide models');
yargs.option('model', {
describe: 'array of concerto model files',
type: 'string',
array: true
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
default: './'
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`saving external models from ${argv.model} into directory: ${argv.output}`);
}
return Commands.get(argv.model, argv.output)
.then((result) => {
Logger.info(result);
})
.catch((err) => {
Logger.error(err.message);
});
})
.command('parse', 'parse a cto string to a JSON syntax tree', (yargs) => {
yargs.demandOption(['model'], 'Please provide Concerto model(s)');
yargs.option('model', {
describe: 'array of concerto model files',
type: 'string',
array: true
});
yargs.option('resolve', {
describe: 'resolve names to fully qualified names',
type: 'boolean',
default: false
});
yargs.option('all', {
describe: 'import all models',
type: 'boolean',
default: false
});
yargs.option('output', {
describe: 'path to the output file',
type: 'string'
});
yargs.option('excludeLineLocations', {
describe: 'Exclude file line location metadata from metamodel instance',
type: 'boolean',
default: false
});
}, (argv) => {
const options = {};
options.excludeLineLocations = argv.excludeLineLocations;
return Commands.parse(argv.model, argv.resolve, argv.all, argv.output, options)
.then((result) => {
if (result) {
console.log(result);
}
})
.catch((err) => {
Logger.error(err.message);
});
})
.command('print', 'print a JSON syntax tree to a cto string', (yargs) => {
yargs.demandOption(['input'], 'Please provide an input Concerto syntax tree');
yargs.option('input', {
describe: 'the metamodel to export',
type: 'string'
});
yargs.option('output', {
describe: 'path to the output file',
type: 'string'
});
}, (argv) => {
return Commands.print(argv.input, argv.output)
.then((result) => {
if (result) {
Logger.info(result);
}
})
.catch((err) => {
Logger.error(err.message);
});
})
.command('version <release>', 'modify the version of one or more model files', yargs => {
yargs.demandOption(['model'], 'Please provide Concerto model(s)');
yargs.positional('release', {
describe: 'the new version, or a release to use when incrementing the existing version',
type: 'string',
choices: [
'keep',
'major',
'minor',
'patch',
'premajor',
'preminor',
'prepatch',
'prerelease'
]
});
yargs.option('model', {
alias: 'models',
describe: 'array of concerto model files',
type: 'string',
array: true
});
yargs.option('prerelease', {
describe: 'set the specified pre-release version',
type: 'string'
});
}, argv => {
const modelFiles = argv.model.flatMap(model => {
if (glob.hasMagic(model)) {
return glob.sync(model);
}
return model;
});
return Commands.version(argv.release, modelFiles, argv.prerelease)
.then((result) => {
if (result) {
Logger.info(result);
}
});
})
.command('compare', 'compare two Concerto model files', yargs => {
yargs.demandOption(['old'], 'Please provide the old model');
yargs.demandOption(['new'], 'Please provide the new model');
yargs.option('old', {
describe: 'the old Concerto model file',
type: 'string',
});
yargs.option('new', {
describe: 'the new Concerto model file',
type: 'string',
});
}, argv => {
return Commands.compare(argv.old, argv.new)
.catch((err) => {
Logger.error(err.message);
});
})
.command('infer', 'generate a concerto model from a source schema', (yargs) => {
yargs.demandOption(['input', 'namespace']);
yargs.option('input', {
describe: 'path to the input file',
type: 'string',
});
yargs.option('output', {
describe: 'path to the output file',
type: 'string',
});
yargs.option('format', {
describe: 'either `openapi` or `jsonSchema`',
default: 'jsonSchema',
type: 'string'
});
yargs.option('namespace', {
describe: 'The namespace for the output model',
type: 'string',
});
yargs.option('typeName', {
describe: 'The name of the root type',
type: 'string',
default: 'Root'
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`Infer Concerto model from ${argv.input} in the ${argv.format} format`);
}
try {
const cto = Commands.inferConcertoSchema(argv.input, argv.namespace, argv.typeName, argv.format, argv.output);
if (argv.output){
fs.writeFileSync(argv.output, cto);
} else {
console.log(cto);
}
} catch (err){
Logger.error(err);
return;
}
})
.command('generate <mode>', 'generate a sample JSON object for a concept', yargs => {
yargs.demandOption(['model'], 'Please provide a model');
yargs.demandOption(['concept'], 'Please provide the concept name');
yargs.option('model', {
describe: 'The file location of the source models',
type: 'string',
array: true,
});
yargs.option('concept', {
describe: 'The fully qualified name of the Concept type to generate',
type: 'string',
});
yargs.positional('mode', {
describe: 'Generation mode. `empty` will generate a minimal example, `sample` will generate random values',
type: 'string',
choices: [
'sample',
'empty',
]
});
yargs.option('includeOptionalFields', {
describe: 'Include optional fields will be included in the output',
type: 'boolean',
default: false
});
yargs.option('metamodel', {
describe: 'Include the Concerto Metamodel in the output',
type: 'boolean',
default: false,
});
yargs.option('strict', {
describe: 'Require versioned namespaces and imports',
type: 'boolean',
default: false,
});
yargs.option('disableValidation', {
describe: 'Do not validate generated output',
type: 'boolean',
default: false
});
}, argv => {
return Commands.generate(argv.model, argv.concept, argv.mode, {
optionalFields: argv.includeOptionalFields,
metamodel: argv.metamodel,
strict: argv.strict,
disableValidation: argv.disableValidation
})
.then(obj => {
console.log(JSON.stringify(obj, null, 2));
})
.catch((err) => {
Logger.error(err.message);
});
})
.command('decorate', 'apply the decorators and vocabs to the target models from given list of dcs files and vocab files', yargs => {
yargs.demandOption('models', 'Please provide a model');
yargs.option('models', {
describe: 'The file location of the source models',
alias: 'model',
type: 'string',
array:true,
});
yargs.option('decorator', {
describe: 'List of dcs files to be applied to model',
type: 'string',
array:true
});
yargs.option('vocabulary', {
describe: 'List of vocab files to be applied to model',
type: 'string',
array:true
});
yargs.option('format', {
describe: 'Output format (json or cto)',
type: 'string',
default:'cto',
choices: ['json', 'cto']
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
});
yargs.check((args) => {
// Custom validation to ensure at least one of the two options is provided
if (!args.decorator && !args.vocabulary) {
throw new Error('You must provide at least one of dcs files or voc files');
}
return true;
});
}, argv => {
let options={};
options.format=argv.format;
options.output=argv.output;
return Commands.decorate(argv.models, argv.decorator,argv.vocabulary,options)
.then(obj => {
console.log(obj);
})
.catch((err) => {
Logger.error(err.message);
});
})
.command('extract-decorators','extracts the decorator command sets and vocabularies from a list of model files', yargs =>{
yargs.demandOption('models','Please provide a model');
yargs.option('models', {
describe: 'The file location of the source models',
alias: 'model',
type: 'string',
array:true,
});
yargs.option('locale', {
describe: 'The locale for extracted vocabularies',
type: 'string',
default:'en'
});
yargs.option('removeDecoratorsFromSource', {
describe: 'Flag to determine whether to remove decorators from source model',
type: 'boolean',
default: false
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
default: 'output'
});
}, argv => {
const options = {
locale: argv.locale,
removeDecoratorsFromModel: argv.removeDecoratorsFromSource,
output: argv.output
};
return Commands.extractDecorators(argv.models,options)
.then(result => {
Logger.info(result);
})
.catch((err) => {
Logger.error(err.message);
});
})
.option('verbose', {
alias: 'v',
default: false
})
.help()
.argv;