Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors commander loading into cli module #750

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading