UNPKG

13.9 kBJavaScriptView Raw
1#!/usr/bin/env node
2/*
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16'use strict';
17
18const Logger = require('@accordproject/concerto-core').Logger;
19const Commands = require('./lib/commands');
20
21require('yargs')
22 .scriptName('cicero')
23 .usage('$0 <cmd> [args]')
24 .demandCommand(1, '# Please specify a command')
25 .recommendCommands()
26 .strict()
27 .command('parse', 'parse a contract text', (yargs) => {
28 yargs.option('template', {
29 describe: 'path to the template',
30 type: 'string'
31 });
32 yargs.option('sample', {
33 describe: 'path to the contract text',
34 type: 'string'
35 });
36 yargs.option('output', {
37 describe: 'path to the output file',
38 type: 'string'
39 });
40 yargs.option('currentTime', {
41 describe: 'set current time',
42 type: 'string',
43 default: null
44 });
45 yargs.option('warnings', {
46 describe: 'print warnings',
47 type: 'boolean',
48 default: false
49 });
50 }, (argv) => {
51 if (argv.verbose) {
52 Logger.info(`parse sample ${argv.sample} for template ${argv.template}`);
53 }
54
55 try {
56 argv = Commands.validateParseArgs(argv);
57 const options = {
58 warnings: argv.warnings,
59 };
60 return Commands.parse(argv.template, argv.sample, argv.output, argv.currentTime, options)
61 .then((result) => {
62 if(result) {Logger.info(JSON.stringify(result));}
63 })
64 .catch((err) => {
65 Logger.error(err.message);
66 });
67 } catch (err){
68 Logger.error(err.message);
69 return;
70 }
71 })
72 .command('draft', 'create contract text from data', (yargs) => {
73 yargs.option('template', {
74 describe: 'path to the template',
75 type: 'string'
76 });
77 yargs.option('data', {
78 describe: 'path to the contract data',
79 type: 'string'
80 });
81 yargs.option('output', {
82 describe: 'path to the output file',
83 type: 'string'
84 });
85 yargs.option('currentTime', {
86 describe: 'set current time',
87 type: 'string',
88 default: null
89 });
90 yargs.option('format', {
91 describe: 'target format',
92 type: 'string'
93 });
94 yargs.option('unquoteVariables', {
95 describe: 'remove variables quoting',
96 type: 'boolean',
97 default: false
98 });
99 yargs.option('warnings', {
100 describe: 'print warnings',
101 type: 'boolean',
102 default: false
103 });
104 }, (argv) => {
105 if (argv.verbose) {
106 Logger.info(`create contract from data ${argv.data} for template ${argv.template}`);
107 }
108
109 try {
110 argv = Commands.validateDraftArgs(argv);
111 const options = {
112 unquoteVariables: argv.unquoteVariables,
113 warnings: argv.warnings,
114 format: argv.format,
115 };
116 return Commands.draft(argv.template, argv.data, argv.output, argv.currentTime, options)
117 .then((result) => {
118 if(result) {Logger.info(result);}
119 })
120 .catch((err) => {
121 Logger.error(err.message);
122 });
123 } catch (err){
124 Logger.error(err.message);
125 return;
126 }
127 })
128 .command('normalize', 'normalize markdown (parse & redraft)', (yargs) => {
129 yargs.option('template', {
130 describe: 'path to the template',
131 type: 'string'
132 });
133 yargs.option('sample', {
134 describe: 'path to the contract text',
135 type: 'string'
136 });
137 yargs.option('overwrite', {
138 describe: 'overwrite the contract text',
139 type: 'boolean',
140 default: false
141 });
142 yargs.option('output', {
143 describe: 'path to the output file',
144 type: 'string'
145 });
146 yargs.option('currentTime', {
147 describe: 'set current time',
148 type: 'string',
149 default: null
150 });
151 yargs.option('warnings', {
152 describe: 'print warnings',
153 type: 'boolean',
154 default: false
155 });
156 yargs.option('format', {
157 describe: 'target format',
158 type: 'string'
159 });
160 yargs.option('unquoteVariables', {
161 describe: 'remove variables quoting',
162 type: 'boolean',
163 default: false
164 });
165 }, (argv) => {
166 if (argv.verbose) {
167 Logger.info(`parse sample and re-create sample ${argv.sample} for template ${argv.template}`);
168 }
169
170 try {
171 argv = Commands.validateNormalizeArgs(argv);
172 const options = {
173 unquoteVariables: argv.unquoteVariables,
174 warnings: argv.warnings,
175 format: argv.format,
176 };
177 return Commands.normalize(argv.template, argv.sample, argv.overwrite, argv.output, argv.currentTime, options)
178 .then((result) => {
179 if(result) {Logger.info(result);}
180 })
181 .catch((err) => {
182 Logger.error(err.message);
183 });
184 } catch (err){
185 Logger.error(err.message);
186 return;
187 }
188 })
189 .command('trigger', 'send a request to the contract', (yargs) => {
190 yargs.option('template', {
191 describe: 'path to the template',
192 type: 'string'
193 });
194 yargs.option('sample', {
195 describe: 'path to the contract text',
196 type: 'string'
197 });
198 yargs.option('request', {
199 describe: 'path to the JSON request',
200 type: 'string'
201 }).array('request');
202 yargs.option('state', {
203 describe: 'path to the JSON state',
204 type: 'string'
205 });
206 yargs.option('currentTime', {
207 describe: 'set current time',
208 type: 'string',
209 default: null
210 });
211 yargs.option('warnings', {
212 describe: 'print warnings',
213 type: 'boolean',
214 default: false
215 });
216 }, (argv) => {
217
218 try {
219 argv = Commands.validateTriggerArgs(argv);
220 const options = {
221 warnings: argv.warnings,
222 };
223 return Commands.trigger(argv.template, argv.sample, argv.request, argv.state, argv.currentTime, options)
224 .then((result) => {
225 if(result) {Logger.info(JSON.stringify(result));}
226 })
227 .catch((err) => {
228 Logger.error(err.message);
229 });
230 } catch (err){
231 Logger.error(err.message);
232 }
233 })
234 .command('invoke', 'invoke a clause of the contract', (yargs) => {
235 yargs.option('template', {
236 describe: 'path to the template',
237 type: 'string'
238 });
239 yargs.option('sample', {
240 describe: 'path to the contract text',
241 type: 'string'
242 });
243 yargs.option('clauseName', {
244 describe: 'the name of the clause to invoke',
245 type: 'string'
246 });
247 yargs.option('params', {
248 describe: 'path to the parameters',
249 type: 'string'
250 });
251 yargs.option('state', {
252 describe: 'path to the JSON state',
253 type: 'string'
254 });
255 yargs.option('currentTime', {
256 describe: 'set current time',
257 type: 'string',
258 default: null
259 });
260 yargs.option('warnings', {
261 describe: 'print warnings',
262 type: 'boolean',
263 default: false
264 });
265 }, (argv) => {
266 try {
267 argv = Commands.validateInvokeArgs(argv);
268 const options = {
269 warnings: argv.warnings,
270 };
271 return Commands.invoke(argv.template, argv.sample, argv.clauseName, argv.params, argv.state, argv.currentTime, options)
272 .then((result) => {
273 if(result) {Logger.info(JSON.stringify(result));}
274 })
275 .catch((err) => {
276 Logger.error(err.message);
277 });
278 } catch (err){
279 Logger.error(err.message);
280 }
281 })
282 .command('initialize', 'initialize a clause', (yargs) => {
283 yargs.option('template', {
284 describe: 'path to the template',
285 type: 'string'
286 });
287 yargs.option('sample', {
288 describe: 'path to the contract text',
289 type: 'string'
290 });
291 yargs.option('currentTime', {
292 describe: 'initialize with this current time',
293 type: 'string',
294 default: null
295 });
296 yargs.option('warnings', {
297 describe: 'print warnings',
298 type: 'boolean',
299 default: false
300 });
301 }, (argv) => {
302
303 try {
304 argv = Commands.validateInitializeArgs(argv);
305 const options = {
306 warnings: argv.warnings,
307 };
308 return Commands.initialize(argv.template, argv.sample, argv.currentTime, options)
309 .then((result) => {
310 if(result) {Logger.info(JSON.stringify(result));}
311 })
312 .catch((err) => {
313 Logger.error(err.message);
314 });
315 } catch (err){
316 Logger.error(err.message);
317 }
318 })
319 .command('archive', 'create a template archive', (yargs) => {
320 yargs.option('template', {
321 describe: 'path to the template',
322 type: 'string'
323 });
324 yargs.option('target', {
325 describe: 'the target language of the archive',
326 type: 'string',
327 default: 'ergo'
328 });
329 yargs.option('output', {
330 describe: 'file name for new archive',
331 type: 'string',
332 default: null
333 });
334 yargs.option('warnings', {
335 describe: 'print warnings',
336 type: 'boolean',
337 default: false
338 });
339 }, (argv) => {
340 if (argv.verbose) {
341 Logger.info(`create an archive for ${argv.template}`);
342 }
343
344 try {
345 argv = Commands.validateArchiveArgs(argv);
346 const options = {
347 warnings: argv.warnings,
348 };
349 return Commands.archive(argv.template, argv.target, argv.output, options)
350 .catch((err) => {
351 Logger.error(err.message);
352 });
353 } catch (err){
354 Logger.error(err.message);
355 return;
356 }
357 })
358 .command('compile', 'generate code for a target platform', (yargs) => {
359 yargs.option('template', {
360 describe: 'path to the template',
361 type: 'string'
362 });
363 yargs.option('target', {
364 describe: 'target of the code generation',
365 type: 'string',
366 default: 'JSONSchema'
367 });
368 yargs.option('output', {
369 describe: 'path to the output directory',
370 type: 'string',
371 default: './output/'
372 });
373 yargs.option('warnings', {
374 describe: 'print warnings',
375 type: 'boolean',
376 default: false
377 });
378 }, (argv) => {
379 if (argv.verbose) {
380 Logger.info(`compile template ${argv.template} for target ${argv.target}`);
381 }
382
383 try {
384 argv = Commands.validateCompileArgs(argv);
385 const options = {
386 warnings: argv.warnings,
387 };
388 return Commands.compile(argv.template, argv.target, argv.output, options)
389 .then((result) => {
390 Logger.info('Completed.');
391 })
392 .catch((err) => {
393 Logger.error(err.message + ' ' + JSON.stringify(err));
394 });
395 } catch (err){
396 Logger.error(err.message);
397 return;
398 }
399 })
400 .command('get', 'save local copies of external dependencies', (yargs) => {
401 yargs.option('template', {
402 describe: 'path to the template',
403 type: 'string'
404 });
405 yargs.option('output', {
406 describe: 'output directory path',
407 type: 'string'
408 });
409 }, (argv) => {
410 if (argv.verbose) {
411 Logger.info(`saving external models into directory: ${argv.output}`);
412 }
413
414 try {
415 argv = Commands.validateGetArgs(argv);
416 return Commands.get(argv.template, argv.output)
417 .then((result) => {
418 Logger.info(result);
419 })
420 .catch((err) => {
421 Logger.error(err.message);
422 });
423 } catch (err){
424 Logger.error(err.message);
425 return;
426 }
427 })
428 .option('verbose', {
429 alias: 'v',
430 default: false
431 })
432 .help()
433 .argv;
\No newline at end of file