|
1 | | -import { defaultHandler, getLogger, LogLevel, replaceHandlers } from '@stencila/logga'; |
2 | | -import fs from 'fs'; |
3 | | -import minimist from 'minimist'; |
4 | | -import { promisify } from 'util'; |
5 | | -import { ClientType } from './base/Client'; |
6 | | -import Executor from './base/Executor'; |
7 | | -import Server from './base/Server'; |
8 | | -import discoverStdio from './stdio/discover'; |
9 | | -import StdioClient from './stdio/StdioClient'; |
10 | | -import TcpServer from './tcp/TcpServer'; |
11 | | - |
12 | | -const readFile = promisify(fs.readFile) |
13 | | -const writeFile = promisify(fs.writeFile) |
| 1 | +import { |
| 2 | + defaultHandler, |
| 3 | + getLogger, |
| 4 | + LogLevel, |
| 5 | + replaceHandlers |
| 6 | +} from '@stencila/logga' |
| 7 | +import minimist from 'minimist' |
| 8 | +import { ClientType } from './base/Client' |
| 9 | +import Executor from './base/Executor' |
| 10 | +import Server from './base/Server' |
| 11 | +import discoverStdio from './stdio/discover' |
| 12 | +import StdioClient from './stdio/StdioClient' |
| 13 | +import HttpServer from './http/HttpServer' |
| 14 | +import TcpServer from './tcp/TcpServer' |
| 15 | +import WebSocketServer from './ws/WebSocketServer' |
14 | 16 |
|
15 | 17 | const { _: args, ...options } = minimist(process.argv.slice(2)) |
16 | 18 |
|
@@ -56,51 +58,47 @@ const init = async () => { |
56 | 58 | /** |
57 | 59 | * Serve the executor |
58 | 60 | */ |
59 | | -const serve = (executor: Executor) => { |
| 61 | +const serve = async (executor: Executor) => { |
60 | 62 | // Add server classes based on supplied options |
61 | 63 | const servers: Server[] = [] |
62 | 64 | if (options.tcp !== undefined) { |
63 | 65 | servers.push(new TcpServer(executor, options.tcp)) |
64 | 66 | } |
| 67 | + if (options.http !== undefined) { |
| 68 | + servers.push(new HttpServer(executor, options.http)) |
| 69 | + } |
| 70 | + if (options.ws !== undefined) { |
| 71 | + servers.push(new WebSocketServer(executor, options.ws)) |
| 72 | + } |
65 | 73 | if (servers.length === 0) { |
66 | 74 | log.warn( |
67 | 75 | 'No servers specified in options (e.g. --tcp --stdio). Executor will not be accessible.' |
68 | 76 | ) |
69 | 77 | } |
70 | | - executor.start(servers) |
| 78 | + await executor.start(servers) |
71 | 79 | } |
72 | 80 |
|
73 | 81 | /** |
74 | 82 | * Convert a document |
75 | 83 | */ |
76 | 84 | const convert = async (executor: Executor): Promise<void> => { |
77 | 85 | const input = args[1] |
78 | | - const output = args[2] || '-' |
79 | | - |
80 | | - const content = await readFile(input, 'utf8') |
| 86 | + const output = args[2] !== undefined ? args[2] : '-' |
81 | 87 |
|
82 | | - const decoded = await executor.decode(content) |
83 | | - const encoded = await executor.encode(decoded) |
84 | | - |
85 | | - if (output === '-') console.log(encoded) |
86 | | - else await writeFile(output, encoded) |
| 88 | + const decoded = await executor.decode(input) |
| 89 | + await executor.encode(decoded, output) |
87 | 90 | } |
88 | 91 |
|
89 | 92 | /** |
90 | 93 | * Execute a document |
91 | 94 | */ |
92 | 95 | const execute = async (executor: Executor): Promise<void> => { |
93 | 96 | const input = args[1] |
94 | | - const output = args[2] || input |
95 | | - |
96 | | - const content = await readFile(input, 'utf8') |
| 97 | + const output = args[2] !== undefined ? args[2] : input |
97 | 98 |
|
98 | | - const decoded = await executor.decode(content) |
| 99 | + const decoded = await executor.decode(input) |
99 | 100 | const executed = await executor.execute(decoded) |
100 | | - const encoded = await executor.encode(executed) |
101 | | - |
102 | | - if (output === '-') console.log(encoded) |
103 | | - else await writeFile(output, encoded) |
| 101 | + await executor.encode(executed, output) |
104 | 102 | } |
105 | 103 |
|
106 | 104 | // Run the main function and log any exceptions |
|
0 commit comments