Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

Commit 6ec777e

Browse files
committed
fix(cli) Remove moment library in CLI and add utcOffset option
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 3753f8e commit 6ec777e

File tree

5 files changed

+71
-56
lines changed

5 files changed

+71
-56
lines changed

packages/ergo-cli/index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
'use strict';
1717

1818
const Commands = require('./lib/commands');
19-
const Moment = require('moment-mini');
2019
const Logger = require('@accordproject/ergo-compiler').Logger;
2120

2221
require('yargs')
@@ -36,9 +35,14 @@ require('yargs')
3635
default: null
3736
});
3837
yargs.option('currentTime', {
39-
describe: 'the current time',
38+
describe: 'set current time',
4039
type: 'string',
41-
default: Moment().format() // Defaults to now
40+
default: null
41+
});
42+
yargs.option('utcOffset', {
43+
describe: 'set UTC offset',
44+
type: 'number',
45+
default: null
4246
});
4347
yargs.option('request', {
4448
describe: 'path to the request data'
@@ -62,7 +66,7 @@ require('yargs')
6266

6367
// Run contract
6468
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)
69+
argv.currentTime, argv.utcOffset, argv.request.map(r => { return { file: r }; }), argv.warnings)
6670
.then((result) => {
6771
Logger.info(JSON.stringify(result));
6872
})
@@ -84,9 +88,14 @@ require('yargs')
8488
type: 'string'
8589
});
8690
yargs.option('currentTime', {
87-
describe: 'the current time',
91+
describe: 'set current time',
8892
type: 'string',
89-
default: Moment().format() // Defaults to now
93+
default: null
94+
});
95+
yargs.option('utcOffset', {
96+
describe: 'set UTC offset',
97+
type: 'number',
98+
default: null
9099
});
91100
yargs.option('params', {
92101
describe: 'path to the parameters',
@@ -111,7 +120,7 @@ require('yargs')
111120
}
112121

113122
// Run contract
114-
Commands.invoke(argv.template, files, argv.clauseName, { file: argv.data }, { file: argv.state }, argv.currentTime, { file: argv.params }, argv.warnings)
123+
Commands.invoke(argv.template, files, argv.clauseName, { file: argv.data }, { file: argv.state }, argv.currentTime, argv.utcOffset, { file: argv.params }, argv.warnings)
115124
.then((result) => {
116125
Logger.info(JSON.stringify(result));
117126
})
@@ -126,9 +135,14 @@ require('yargs')
126135
describe: 'path to the contract data'
127136
});
128137
yargs.option('currentTime', {
129-
describe: 'the current time',
138+
describe: 'set current time',
130139
type: 'string',
131-
default: Moment().format() // Defaults to now
140+
default: null
141+
});
142+
yargs.option('utcOffset', {
143+
describe: 'set UTC offset',
144+
type: 'number',
145+
default: null
132146
});
133147
yargs.option('params', {
134148
describe: 'path to the parameters',
@@ -153,7 +167,7 @@ require('yargs')
153167
}
154168

155169
// Run contract
156-
Commands.initialize(argv.template, files, { file: argv.data }, argv.currentTime, argv.params ? { file: argv.params } : { content: '{}' }, argv.warnings)
170+
Commands.initialize(argv.template, files, { file: argv.data }, argv.currentTime, argv.utcOffset, argv.params ? { file: argv.params } : { content: '{}' }, argv.warnings)
157171
.then((result) => {
158172
Logger.info(JSON.stringify(result));
159173
})

packages/ergo-cli/lib/commands.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ class Commands {
6565
*
6666
* @param {string} template - template directory
6767
* @param {string[]} files - input files
68-
* @param {string} contractInput the contract data
69-
* @param {string} stateInput the contract state
70-
* @param {string} currentTime the definition of 'now'
71-
* @param {string[]} requestsInput the requests
72-
* @param {boolean} warnings whether to print warnings
68+
* @param {string} contractInput - the contract data
69+
* @param {string} stateInput - the contract state
70+
* @param {string} [currentTime] - the definition of 'now', defaults to current time
71+
* @param {number} [utcOffset] - UTC Offset for this execution, defaults to local offset
72+
* @param {string[]} requestsInput - the requests
73+
* @param {boolean} warnings - whether to print warnings
7374
* @returns {object} Promise to the result of execution
7475
*/
75-
static async trigger(template,files,contractInput,stateInput,currentTime,requestsInput,warnings) {
76+
static async trigger(template,files,contractInput,stateInput,currentTime,utcOffset,requestsInput,warnings) {
7677
try {
7778
const logicManager = await loadTemplate(template,files);
7879
const contractJson = getJson(contractInput);
@@ -83,15 +84,15 @@ class Commands {
8384
const engine = new Engine();
8485
let initResponse;
8586
if (stateInput === null) {
86-
initResponse = engine.compileAndInit(logicManager, contractJson, {}, currentTime, null);
87+
initResponse = engine.compileAndInit(logicManager, contractJson, {}, currentTime, utcOffset);
8788
} else {
8889
const stateJson = getJson(stateInput);
8990
initResponse = Promise.resolve({ state: stateJson });
9091
}
9192
// Get all the other requests and chain execution through Promise.reduce()
9293
return requestsJson.reduce((promise,requestJson) => {
9394
return promise.then((result) => {
94-
return engine.compileAndTrigger(logicManager, contractJson, requestJson, result.state, currentTime, null);
95+
return engine.compileAndTrigger(logicManager, contractJson, requestJson, result.state, currentTime, utcOffset);
9596
});
9697
}, initResponse);
9798
} catch (err) {
@@ -104,22 +105,23 @@ class Commands {
104105
*
105106
* @param {string} template - template directory
106107
* @param {string[]} files - input files
107-
* @param {string} clauseName the name of the clause to invoke
108-
* @param {string} contractInput the contract data
109-
* @param {string} stateInput the contract state
110-
* @param {string} currentTime the definition of 'now'
111-
* @param {object} paramsInput the parameters for the clause
112-
* @param {boolean} warnings whether to print warnings
108+
* @param {string} clauseName - the name of the clause to invoke
109+
* @param {string} contractInput - the contract data
110+
* @param {string} stateInput - the contract state
111+
* @param {string} [currentTime] - the definition of 'now', defaults to current time
112+
* @param {number} [utcOffset] - UTC Offset for this execution, defaults to local offset
113+
* @param {object} paramsInput - the parameters for the clause
114+
* @param {boolean} warnings - whether to print warnings
113115
* @returns {object} Promise to the result of invocation
114116
*/
115-
static async invoke(template,files,clauseName,contractInput,stateInput,currentTime,paramsInput,warnings) {
117+
static async invoke(template,files,clauseName,contractInput,stateInput,currentTime,utcOffset,paramsInput,warnings) {
116118
try {
117119
const logicManager = await loadTemplate(template,files);
118120
const contractJson = getJson(contractInput);
119121
const clauseParams = getJson(paramsInput);
120122
const stateJson = getJson(stateInput);
121123
const engine = new Engine();
122-
return engine.compileAndInvoke(logicManager, clauseName, contractJson, clauseParams, stateJson, currentTime, null);
124+
return engine.compileAndInvoke(logicManager, clauseName, contractJson, clauseParams, stateJson, currentTime, utcOffset);
123125
} catch (err) {
124126
return Promise.reject(err);
125127
}
@@ -130,19 +132,20 @@ class Commands {
130132
*
131133
* @param {string} template - template directory
132134
* @param {string[]} files - input files
133-
* @param {string} contractInput the contract data
134-
* @param {string} currentTime the definition of 'now'
135-
* @param {object} paramsInput the parameters for the clause
136-
* @param {boolean} warnings whether to print warnings
135+
* @param {string} contractInput - the contract data
136+
* @param {string} [currentTime] - the definition of 'now', defaults to current time
137+
* @param {number} [utcOffset] - UTC Offset for this execution, defaults to local offset
138+
* @param {object} paramsInput - the parameters for the clause
139+
* @param {boolean} warnings - whether to print warnings
137140
* @returns {object} Promise to the result of execution
138141
*/
139-
static async initialize(template,files,contractInput,currentTime,paramsInput,warnings) {
142+
static async initialize(template,files,contractInput,currentTime,utcOffset,paramsInput,warnings) {
140143
try {
141144
const logicManager = await loadTemplate(template,files);
142145
const contractJson = getJson(contractInput);
143146
const clauseParams = getJson(paramsInput);
144147
const engine = new Engine();
145-
return engine.compileAndInit(logicManager, contractJson, clauseParams, currentTime, null);
148+
return engine.compileAndInit(logicManager, contractJson, clauseParams, currentTime, utcOffset);
146149
} catch (err) {
147150
return Promise.reject(err);
148151
}

0 commit comments

Comments
 (0)