diff --git a/browser/src/parser/node.ts b/browser/src/parser/node.ts index fcee854..f248a12 100644 --- a/browser/src/parser/node.ts +++ b/browser/src/parser/node.ts @@ -1,5 +1,5 @@ -import readline from 'readline'; import Parser from './parser.ts'; +import { prompting } from './utils/prompting.ts'; // mutate fetch to be local @@ -14,11 +14,4 @@ fetch = async (path: string) => { const parser = await Parser.instance(); -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, -}); -rl.question(`Terminal ID? `, async (id) => { - console.log(JSON.stringify(await parser.parse(Number.parseInt(id)))); - rl.close(); -}); +console.log(JSON.stringify(await parser.parse(Number.parseInt((await prompting('Terminal ID')) ?? '')))); diff --git a/browser/src/parser/parser.ts b/browser/src/parser/parser.ts index 60b84d6..8012838 100644 --- a/browser/src/parser/parser.ts +++ b/browser/src/parser/parser.ts @@ -20,6 +20,7 @@ import { TerminatorsVI } from './terminators/VI.ts'; import { TerminatorsVM } from './terminators/VM.ts'; import { TerminatorsVR } from './terminators/VR.ts'; import './utils/extensions.ts'; +import { prompting } from './utils/prompting.ts'; /* Runway IDs for LIED @@ -73,11 +74,7 @@ class Parser { // Get runway this procedure is for let runway = this.runways.find(({ ID }) => ID === terminal.RwyID); if (!runway) { - let id = 26156; - if (typeof prompt !== 'undefined') - // throw new Error("Prompt not defined, cannot continue"); - - id = Number.parseInt(prompt('Runway ID') ?? ''); + const id = Number.parseInt((await prompting('Runway ID')) ?? ''); runway = this.runways.find(({ ID }) => ID === id); if (!runway) throw new Error('Procedure links to non existent Runway'); } diff --git a/browser/src/parser/utils/prompting.ts b/browser/src/parser/utils/prompting.ts new file mode 100644 index 0000000..5b8a175 --- /dev/null +++ b/browser/src/parser/utils/prompting.ts @@ -0,0 +1,19 @@ +import readline from 'readline'; + +export const prompting = (message: string) => { + return new Promise((resolve) => { + if (typeof prompt !== 'undefined') { + return resolve(prompt(message)); + } + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }); + rl.question(`${message}: `, (id) => { + rl.close(); + + resolve(id); + }); + }); +}; diff --git a/browser/src/types/types.d.ts b/browser/src/types/types.d.ts index 3599a00..9dedfb5 100644 --- a/browser/src/types/types.d.ts +++ b/browser/src/types/types.d.ts @@ -72,7 +72,7 @@ export declare global { ID: number; FullName: string; ICAO: string; - RwyID: number; + RwyID?: number; }; type NavFix = {