UNPKG

7.52 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 Commands = require('./lib/commands');
19const Moment = require('moment-mini');
20const Logger = require('@accordproject/ergo-compiler').Logger;
21
22require('yargs')
23 .scriptName('ergo')
24 .demandCommand(1, '# Please specify a command')
25 .recommendCommands()
26 .strict()
27 .command('trigger', 'send a request to the contract', (yargs) => {
28 yargs.demandOption(['data', 'request'], 'Please provide at least the contract data and a request');
29 yargs.usage('Usage: $0 trigger --data [file] --state [file] --request [file] [cto files] [ergo files]');
30 yargs.option('data', {
31 describe: 'path to the contract data'
32 });
33 yargs.option('state', {
34 describe: 'path to the state data',
35 type: 'string',
36 default: null
37 });
38 yargs.option('currentTime', {
39 describe: 'the current time',
40 type: 'string',
41 default: Moment().format() // Defaults to now
42 });
43 yargs.option('request', {
44 describe: 'path to the request data'
45 }).array('request');
46 yargs.option('template', {
47 describe: 'path to the template directory',
48 type: 'string',
49 default: null
50 });
51 yargs.option('warnings', {
52 describe: 'print warnings',
53 type: 'boolean',
54 default: false
55 });
56 }, (argv) => {
57 let files = argv._;
58
59 if (argv.verbose) {
60 Logger.info(`send request ${argv.request} for contract data ${argv.data}`);
61 }
62
63 // Run contract
64 Commands.trigger(argv.template, files, { file: argv.data }, argv.state ? { file: argv.state } : null,
65 argv.currentTime, argv.request.map(r => { return { file: r }; }), argv.warnings)
66 .then((result) => {
67 Logger.info(JSON.stringify(result));
68 })
69 .catch((err) => {
70 Logger.error(err.message);
71 });
72 })
73 .command('invoke', 'invoke a clause of the contract', (yargs) => {
74 yargs.demandOption(['clauseName', 'data', 'state', 'params'], 'Please provide at least the clauseName, with contract data, state, and parameters');
75 yargs.usage('Usage: $0 invoke --data [file] --state [file] --params [file] [cto files] [ergo files]');
76 yargs.option('clauseName', {
77 describe: 'the name of the clause to invoke'
78 });
79 yargs.option('data', {
80 describe: 'path to the contract data'
81 });
82 yargs.option('state', {
83 describe: 'path to the state data',
84 type: 'string'
85 });
86 yargs.option('currentTime', {
87 describe: 'the current time',
88 type: 'string',
89 default: Moment().format() // Defaults to now
90 });
91 yargs.option('params', {
92 describe: 'path to the parameters',
93 type: 'string',
94 default: {}
95 });
96 yargs.option('template', {
97 describe: 'path to the template directory',
98 type: 'string',
99 default: null
100 });
101 yargs.option('warnings', {
102 describe: 'print warnings',
103 type: 'boolean',
104 default: false
105 });
106 }, (argv) => {
107 let files = argv._;
108
109 if (argv.verbose) {
110 Logger.info(`invoke clause ${argv.clauseName} in contract`);
111 }
112
113 // Run contract
114 Commands.invoke(argv.template, files, argv.clauseName, { file: argv.data }, { file: argv.state }, argv.currentTime, { file: argv.params }, argv.warnings)
115 .then((result) => {
116 Logger.info(JSON.stringify(result));
117 })
118 .catch((err) => {
119 Logger.error(err.message);
120 });
121 })
122 .command('initialize', 'initialize the state for a contract', (yargs) => {
123 yargs.demandOption(['data'], 'Please provide at least contract data and parameters');
124 yargs.usage('Usage: $0 intialize --data [file] --params [file] [cto files] [ergo files]');
125 yargs.option('data', {
126 describe: 'path to the contract data'
127 });
128 yargs.option('currentTime', {
129 describe: 'the current time',
130 type: 'string',
131 default: Moment().format() // Defaults to now
132 });
133 yargs.option('params', {
134 describe: 'path to the parameters',
135 type: 'string',
136 default: null
137 });
138 yargs.option('template', {
139 describe: 'path to the template directory',
140 type: 'string',
141 default: null
142 });
143 yargs.option('warnings', {
144 describe: 'print warnings',
145 type: 'boolean',
146 default: false
147 });
148 }, (argv) => {
149 let files = argv._;
150
151 if (argv.verbose) {
152 Logger.info(`initialize contract state with data ${argv.data}`);
153 }
154
155 // Run contract
156 Commands.initialize(argv.template, files, { file: argv.data }, argv.currentTime, argv.params ? { file: argv.params } : { content: '{}' }, argv.warnings)
157 .then((result) => {
158 Logger.info(JSON.stringify(result));
159 })
160 .catch((err) => {
161 Logger.error(err.message);
162 });
163 })
164 .command('compile', 'compile a contract', (yargs) => {
165 yargs.usage('Usage: $0 compile --target [lang] --link --monitor --warnings [cto files] [ergo files]');
166 yargs.option('target', {
167 describe: 'Target platform (available: es5,es6,cicero,java)',
168 type: 'string',
169 default: 'es6'
170 });
171 yargs.option('link', {
172 describe: 'Link the Ergo runtime with the target code (es5,es6,cicero only)',
173 type: 'boolean',
174 default: false
175 });
176 yargs.option('monitor', {
177 describe: 'Produce compilation time information',
178 type: 'boolean',
179 default: false
180 });
181 yargs.option('warnings', {
182 describe: 'print warnings',
183 type: 'boolean',
184 default: false
185 });
186 }, (argv) => {
187 try {
188 // Removes the `compile`
189 const args = [process.argv[0],process.argv[1]].concat(process.argv.slice(3));
190 for (let i = 0; i < args.length; i++) {
191 if (args[i].split('.').pop() === 'cto') {
192 const ctoPath = args[i];
193 Commands.parseCTOtoFile(ctoPath);
194 args[i] = ctoPath.substr(0, ctoPath.lastIndexOf('.')) + '.ctoj';
195 }
196 }
197 process.argv = args;
198 require('./extracted/ergoccore.js');
199 } catch (err) {
200 Logger.error(err.message);
201 }
202 })
203 .option('verbose', {
204 alias: 'v',
205 default: false
206 })
207 .argv;