UNPKG

2.34 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 Logger = require('@accordproject/ergo-compiler').Logger;
20
21try {
22 const args = process.argv;
23 for (let i = 0; i < args.length; i++) {
24 if (args[i].split('.').pop() === 'cto') {
25 const ctoPath = args[i];
26 Commands.parseCTOtoFile(ctoPath);
27 args[i] = ctoPath.substr(0, ctoPath.lastIndexOf('.')) + '.ctoj';
28 }
29 }
30
31 const ergotop = require('./extracted/ergotopcore.js').ergotop;
32 const readline = require('readline');
33 const chalk = require('chalk');
34
35 const PS1 = chalk.gray('ergo$ ');
36 const PS2 = chalk.gray(' ... ');
37 const rl = readline.createInterface({
38 input: process.stdin,
39 output: process.stdout,
40 prompt: PS1
41 });
42
43 let ctx = ergotop.initRCtxt;
44 let inp = '';
45
46 rl.on('line', (line) => {
47 if (line.charAt(line.length - 1) === '\\') {
48 inp += line.slice(0, line.length - 1) + '\n';
49 rl.setPrompt(PS2);
50 } else {
51 inp += line + '\n';
52 rl.setPrompt(PS1);
53 const x = ergotop.runLine(ctx, inp);
54 inp = '';
55 if (x) {
56 ctx = x.ctx;
57 process.stdout.write(
58 x.out
59 .replace(/^Response. /gm, chalk.green('Response. '))
60 .replace(/^Emit. /gm, chalk.magenta('Emit. '))
61 .replace(/^State. /gm, chalk.blue('State. '))
62 .replace(/^Failure. /gm, chalk.red('Failure. '))
63 );
64 }
65 }
66 rl.prompt();
67 }).on('close', () => {
68 process.exit(0);
69 });
70
71 rl.prompt();
72} catch (err) {
73 Logger.error(err.message);
74}