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

Feat/cli error handling #1092

Merged
merged 2 commits into from
Nov 16, 2024
Merged
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
34 changes: 31 additions & 3 deletions script/inkjs-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Compiler } from '../src/compiler/Compiler';
import { CompilerOptions } from '../src/compiler/CompilerOptions';
import { Story } from '../src/engine/Story';
import { PosixFileHandler } from '../src/compiler/FileHandler/PosixFileHandler';
import { Stats } from '../src/compiler/Stats';
var readline = require('readline');
var path = require('path');

Expand All @@ -20,11 +21,16 @@ Usage: inkjs-compiler <options> <ink file>
-c: Count all visits to knots, stitches and weave points, not
just those referenced by TURNS_SINCE and read counts.
-p: Play mode (automatic if a json file is passed as argument)
-s: Print stats about story including word count in JSON format
-k: Keep inklecate running in play mode even after story is complete
`);
process.exit(0);
}

const countAllVisit = process.argv.includes("-c");
let printStats = process.argv.includes("-s");
let statsResults: Stats|null = null

let play = process.argv.includes("-p") || process.argv.includes("-k");
const write = !process.argv.includes("-k") && !process.argv.includes("-p");
const explicitOutput = process.argv.includes("-o");
Expand All @@ -33,7 +39,7 @@ if(explicitOutput){
const opos = process.argv.indexOf("-o") + 1;
outputfile = process.argv.splice(opos, 1)[0];
}
process.argv = process.argv.filter(p => !['-c', '-o', '-p', '-k'].includes(p));
process.argv = process.argv.filter(p => !['-c', '-o', '-p', '-k', '-s'].includes(p));

const inputFile = process.argv[2] || null;
if(!inputFile){
Expand All @@ -57,11 +63,21 @@ if(!inputFile.endsWith(".json")){
)

const c = new Compiler(mainInk, options);
const rstory = c.Compile();
let rstory:Story|null = null;
try {
rstory = c.Compile();
} catch (error: unknown) {
if((error as Error).message != "Compilation failed.") throw error; //re-throw if an illegitimate js error
}

if (!rstory) {
process.stderr.write("*** Compilation failed ***\n");
process.exit(1);
}

if(printStats){
statsResults = c.GenerateStats()
}

let jsonified: string | void;

if((jsonified = rstory.ToJson())){
Expand All @@ -72,10 +88,22 @@ if(!inputFile.endsWith(".json")){
fs.writeFileSync(outputfile, BOM+jsonStory);
}
}else{
if(printStats){
process.stderr.write("WARNING: Could not generate stats for an already compiled story. Try it on a .ink file instead." + "\n");
printStats = false;
}

jsonStory = fs.readFileSync(inputFile,"utf-8").replace(BOM, "")
play = true;
}

if(jsonStory && !play && printStats && statsResults){
Object.entries(statsResults).forEach(([w, n]) => {
const capitalizedW = w.charAt(0).toUpperCase() + w.slice(1)
process.stdout.write(`${capitalizedW}: ${n}\n`)
});
}

if(jsonStory && play){
const rl = readline.createInterface({
input: process.stdin, //or fileStream
Expand Down