Skip to content

Commit

Permalink
feat(CLI): Add initial CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Oct 14, 2019
1 parent 0122bba commit bf360c5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 60 deletions.
8 changes: 8 additions & 0 deletions executa
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

# A little script to make it a litle easier to test the CLI
# during development of this repo

# Change into the current directory of this script so it can be run from anywhere
cd "$(dirname "$0")"
npm run cli -- "$@"
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Execution delegator for various language backends.",
"main": "index.js",
"scripts": {
"serve": "ts-node src/serve.ts --tcp=7300",
"serve:dev": "ts-node-dev src/serve.ts --tcp=7300",
"cli": "ts-node src/cli.ts",
"cli:dev": "ts-node-dev src/cli.ts",
"console": "ts-node src/console.ts",
"format": "npx prettier --write './**/*.{js,json,md,ts,yaml}'",
"lint": "eslint 'src/**/*.{ts,js}' --fix",
Expand Down
109 changes: 109 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { defaultHandler, getLogger, LogLevel, replaceHandlers } from '@stencila/logga';
import fs from 'fs';
import minimist from 'minimist';
import { promisify } from 'util';
import { ClientType } from './base/Client';
import Executor from './base/Executor';
import Server from './base/Server';
import discoverStdio from './stdio/discover';
import StdioClient from './stdio/StdioClient';
import TcpServer from './tcp/TcpServer';

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)

const { _: args, ...options } = minimist(process.argv.slice(2))

const log = getLogger('executa:serve')
replaceHandlers(data =>
defaultHandler(data, {
level: options.debug !== undefined ? LogLevel.debug : LogLevel.info
})
)

const main = async () => {
// Initialize the executor
const executor = await init()

// Run command
const command = args[0]
if (command === 'serve' || command === undefined) return serve(executor)
else if (command === 'execute') return execute(executor)
else {
log.error(`Unrecognised command: ${command}`)
}
}

/**
* Initialize the executor
*/
const init = async () => {
// Discover other executors registered on this machine
// In the future this may attempt to discover remote executors as well
const manifests = await discoverStdio()
if (manifests.length === 0) {
log.warn(
'No peer executors discovered on this machine. Executor will have limited capabilities.'
)
}

// Create a list of client types that can be used by executor
const clientTypes: ClientType[] = [StdioClient as ClientType]

return new Executor(manifests, clientTypes)
}

/**
* Serve the executor
*/
const serve = (executor: Executor) => {
// Add server classes based on supplied options
const servers: Server[] = []
if (options.tcp !== undefined) {
servers.push(new TcpServer(executor, options.tcp))
}
if (servers.length === 0) {
log.warn(
'No servers specified in options (e.g. --tcp --stdio). Executor will not be accessible.'
)
}
executor.start(servers)
}

/**
* Convert a document
*/
const convert = async (executor: Executor): Promise<void> => {
const input = args[1]
const output = args[2] || '-'

const content = await readFile(input, 'utf8')

const decoded = await executor.decode(content)
const encoded = await executor.encode(decoded)

if (output === '-') console.log(encoded)
else await writeFile(output, encoded)
}

/**
* Execute a document
*/
const execute = async (executor: Executor): Promise<void> => {
const input = args[1]
const output = args[2] || input

const content = await readFile(input, 'utf8')

const decoded = await executor.decode(content)
const executed = await executor.execute(decoded)
const encoded = await executor.encode(executed)

if (output === '-') console.log(encoded)
else await writeFile(output, encoded)
}

// Run the main function and log any exceptions
main()
.then(() => {})
.catch(err => log.error(err))
58 changes: 0 additions & 58 deletions src/serve.ts

This file was deleted.

0 comments on commit bf360c5

Please sign in to comment.