Skip to content

Commit

Permalink
Merge pull request #750 from Speech-Rule-Engine/refactor/commander
Browse files Browse the repository at this point in the history
Refactors commander loading into cli module
  • Loading branch information
zorkow authored Apr 28, 2024
2 parents 468aa15 + 1e9a234 commit f21fef9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
30 changes: 19 additions & 11 deletions ts/common/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ import { SystemExternal } from './system_external.js';
import { Variables } from './variables.js';

export class Cli {
public process = SystemExternal.extRequire('process');

public static process = SystemExternal.extRequire('process');

/**
* Commander library.
*/
public static commander = SystemExternal.documentSupported
? null
: SystemExternal.extRequire('commander');

public setup: { [key: string]: string | boolean } = {
mode: EngineConst.Mode.SYNC
Expand All @@ -41,7 +49,7 @@ export class Cli {

public dp: DOMParser;

private output: any = this.process.stdout;
private output: any = Cli.process.stdout;

constructor() {
this.dp = new SystemExternal.xmldom.DOMParser({
Expand Down Expand Up @@ -148,7 +156,7 @@ export class Cli {
}
let i = 0;
const header = order.map((x: string) => compStr(x, length[i++]));
const markdown = SystemExternal.commander.opts().pprint;
const markdown = Cli.commander.opts().pprint;
const separator = length.map((x: number) =>
new Array(x + 1).join(markdown ? '-' : '=')
);
Expand Down Expand Up @@ -188,9 +196,9 @@ export class Cli {
* to the given output file.
*/
public readline() {
this.process.stdin.setEncoding('utf8');
Cli.process.stdin.setEncoding('utf8');
const inter = SystemExternal.extRequire('readline').createInterface({
input: this.process.stdin,
input: Cli.process.stdin,
output: this.output
});
let input = '';
Expand Down Expand Up @@ -220,7 +228,7 @@ export class Cli {
* Method for the command line interface of the Speech Rule Engine
*/
public async commandLine() {
const commander = SystemExternal.commander;
const commander = Cli.commander;
const system = System;
const set = ((key: string) => {
return (val: string, def: string) => this.set(key, val, def);
Expand Down Expand Up @@ -367,9 +375,9 @@ export class Cli {
.on('option:opt-all', () => {
this.enumerate(true).then(() => System.exit(0));
})
.parse(this.process.argv);
.parse(Cli.process.argv);
await System.engineReady().then(() => System.setupEngine(this.setup));
const options = commander.opts();
const options = Cli.commander.opts();
if (options.output) {
this.output = SystemExternal.fs.createWriteStream(options.output);
}
Expand All @@ -379,8 +387,8 @@ export class Cli {
if (options.input) {
this.execute(options.input);
}
if (commander.args.length) {
commander.args.forEach(this.execute.bind(this));
if (Cli.commander.args.length) {
Cli.commander.args.forEach(this.execute.bind(this));
System.engineReady().then(() =>
Debugger.getInstance().exit(() => System.exit(0))
);
Expand Down Expand Up @@ -408,7 +416,7 @@ export class Cli {
}
} catch (err) {
console.error(err.name + ': ' + err.message);
Debugger.getInstance().exit(() => this.process.exit(1));
Debugger.getInstance().exit(() => Cli.process.exit(1));
}
}

Expand Down
7 changes: 0 additions & 7 deletions ts/common/system_external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ export class SystemExternal {
Variables.VERSION +
'mathmaps_ie.js';

/**
* Commander library.
*/
public static commander = SystemExternal.documentSupported
? null
: SystemExternal.extRequire('commander');

/**
* Filesystem library.
*/
Expand Down

0 comments on commit f21fef9

Please sign in to comment.