UNPKG

6.67 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/ergo-compiler').Logger;
19const Commands = require('./lib/commands');
20
21require('yargs')
22 .command('parse', 'parse sample text using a template', (yargs) => {
23 yargs.option('template', {
24 describe: 'path to the directory with the template',
25 type: 'string'
26 });
27 yargs.option('sample', {
28 describe: 'path to the clause text',
29 type: 'string'
30 });
31 yargs.option('out', {
32 describe: 'path to the output file',
33 type: 'string'
34 });
35 yargs.option('currentTime', {
36 describe: 'execute with this current time',
37 type: 'string',
38 default: null
39 });
40 }, (argv) => {
41 if (argv.verbose) {
42 Logger.info(`parse sample ${argv.sample} using a template ${argv.template}`);
43 }
44
45 try {
46 argv = Commands.validateParseArgs(argv);
47 return Commands.parse(argv.template, argv.sample, argv.out, argv.currentTime)
48 .then((result) => {
49 Logger.info(JSON.stringify(result));
50 })
51 .catch((err) => {
52 Logger.error(err.message);
53 });
54 } catch (err){
55 Logger.error(err.message);
56 return;
57 }
58 })
59 .command('archive', 'archive a template directory', (yargs) => {
60 yargs.option('target', {
61 describe: 'the target language of the archive',
62 type: 'string',
63 default: 'ergo'
64 });
65 yargs.option('template', {
66 describe: 'path to the directory with the template',
67 type: 'string'
68 });
69 yargs.option('archiveFile', {
70 describe: 'file name for the archive',
71 type: 'string'
72 });
73 yargs.option('omitLogic', {
74 describe: 'omit the logic in the archive',
75 type: 'boolean',
76 default: false
77 });
78 }, (argv) => {
79 if (argv.verbose) {
80 Logger.info(`archive the template in the directory ${argv.template} into the file ${argv.archiveFile}`);
81 }
82
83 try {
84 argv = Commands.validateArchiveArgs(argv);
85 return Commands.archive(argv.target, argv.template, argv.archiveFile, argv.omitLogic)
86 .catch((err) => {
87 Logger.error(err.message);
88 });
89 } catch (err){
90 Logger.error(err.message);
91 return;
92 }
93 })
94 .command('execute', 'execute a clause with JSON request', (yargs) => {
95 yargs.option('template', {
96 describe: 'path to the directory with the template',
97 type: 'string'
98 });
99 yargs.option('sample', {
100 describe: 'path to the clause text',
101 type: 'string'
102 });
103 yargs.option('request', {
104 describe: 'path to the JSON request',
105 type: 'string'
106 }).array('request');
107 yargs.option('state', {
108 describe: 'path to the JSON state',
109 type: 'string'
110 });
111 yargs.option('currentTime', {
112 describe: 'execute with this current time',
113 type: 'string',
114 default: null
115 });
116 }, (argv) => {
117
118 try {
119 argv = Commands.validateExecuteArgs(argv);
120 return Commands.execute(argv.template, argv.sample, argv.request, argv.state, argv.currentTime)
121 .then((result) => {
122 Logger.info(JSON.stringify(result));
123 })
124 .catch((err) => {
125 Logger.error(err.message);
126 });
127 } catch (err){
128 Logger.error(err.message);
129 }
130 })
131 .command('init', 'initialize a clause', (yargs) => {
132 yargs.option('template', {
133 describe: 'path to the directory with the template',
134 type: 'string'
135 });
136 yargs.option('sample', {
137 describe: 'path to the clause text',
138 type: 'string'
139 });
140 yargs.option('currentTime', {
141 describe: 'initialize with this current time',
142 type: 'string',
143 default: null
144 });
145 }, (argv) => {
146
147 try {
148 argv = Commands.validateInitArgs(argv);
149 return Commands.init(argv.template, argv.sample, argv.currentTime)
150 .then((result) => {
151 Logger.info(JSON.stringify(result));
152 })
153 .catch((err) => {
154 Logger.error(err.message);
155 });
156 } catch (err){
157 Logger.error(err.message);
158 }
159 })
160 .command('generate', 'generate code from the template model', (yargs) => {
161 yargs.option('template', {
162 describe: 'path to the directory with the template',
163 type: 'string',
164 default: '.'
165 });
166 yargs.option('format', {
167 describe: 'format of the code to generate',
168 type: 'string',
169 default: 'JSONSchema'
170 });
171 yargs.option('outputDirectory', {
172 describe: 'output directory path',
173 type: 'string',
174 default: './output/'
175 });
176 }, (argv) => {
177 if (argv.verbose) {
178 Logger.info(`generate code in format ${argv.format} from the model for template ${argv.template} into directory ${argv.outputDirectory}`);
179 }
180
181 try {
182 argv = Commands.validateExecuteArgs(argv);
183 return Commands.generate(argv.format, argv.template, argv.outputDirectory)
184 .then((result) => {
185 Logger.info('Completed.');
186 })
187 .catch((err) => {
188 Logger.error(err.message + ' ' + JSON.stringify(err));
189 });
190 } catch (err){
191 Logger.error(err.message);
192 return;
193 }
194 })
195 .option('verbose', {
196 alias: 'v',
197 default: false
198 })
199 .argv;
\No newline at end of file