Skip to content

Commit

Permalink
Move working dir into render settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ajyoon committed May 12, 2024
1 parent 2bf73ce commit ceca5ad
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
right of quotes (single, double, or underscores). For example `"test" .`
will now correct to `"test".`. Note that this does not shift punctuation
to the inside of quotes, as this is context and style dependent.
* Move working directory from top-level call argument to render settings.

### 0.1.9
* Update experimental in-eval ref lookup. Now exposed by two
Expand Down
4 changes: 2 additions & 2 deletions src/bml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { analyze } from './analysis';

// Wrap the main entrypoint function so we can attach further API parts to it
export function entryFunc(bmlDocumentString: string,
renderSettings: RenderSettings | null, documentDir: string | null): string {
return render(bmlDocumentString, renderSettings, documentDir);
renderSettings: RenderSettings | null): string {
return render(bmlDocumentString, renderSettings);
}

entryFunc.analyze = analyze;
Expand Down
15 changes: 6 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const ALL_SWITCHES = ([] as string[]).concat(
export type BMLArgs = {
bmlSource: string,
settings: RenderSettings,
documentDir: string | null
};
export type Action = { function: Function, args: any[] };

Expand All @@ -43,15 +42,13 @@ export function executeFromStdin(settings: RenderSettings): BMLArgs {
return {
bmlSource: fileUtils.readStdin(),
settings: settings,
documentDir: null
};
}

export function executeFromPath(scriptPath: string, settings: RenderSettings): BMLArgs {
return {
bmlSource: readFile(scriptPath),
settings: settings,
documentDir: path.dirname(scriptPath)
settings: settings
}
}

Expand Down Expand Up @@ -187,6 +184,7 @@ export function determineAction(args: string[]): Action {
let settings = {
randomSeed: seed,
allowEval: !noEval,
workingDir: file ? path.dirname(file) : null,
};

if (interactive) {
Expand Down Expand Up @@ -234,10 +232,9 @@ export function stripArgs(argv: string[]): string[] {
}


export function runBmlWithErrorCheck(bmlSource: string, settings: RenderSettings,
documentDir: string | null): string {
export function runBmlWithErrorCheck(bmlSource: string, settings: RenderSettings): string {
try {
return bml(bmlSource, settings, documentDir);
return bml(bmlSource, settings);
} catch (e: any) {
console.error('BML rendering failed with error:\n'
+ e.stack + '\n\n'
Expand Down Expand Up @@ -270,8 +267,8 @@ function main() {
process.stdout.write(`Approx possible branches: ${formattedCount}\n`);
process.exit(0);
} else {
let { bmlSource, settings, documentDir } = action.function(...action.args);
let renderedContent = runBmlWithErrorCheck(bmlSource, settings, documentDir);
let { bmlSource, settings } = action.function(...action.args);
let renderedContent = runBmlWithErrorCheck(bmlSource, settings);
process.stdout.write(renderedContent);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function readFile(filePath: string, workingDir: string | null): string {
if (!fs.lstatSync(workingDir).isDirectory()) {
throw new Error()
}
filePath = path.join(workingDir, filePath);
filePath = path.resolve(workingDir, filePath);
}
return '' + fs.readFileSync(filePath);
}
Expand Down
4 changes: 2 additions & 2 deletions src/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function launchInteractive(scriptPath: string, settings: RenderSettings)
}

export function runInternal(scriptPath: string, settings: RenderSettings, state: InteractiveState) {
const documentDir = path.dirname(scriptPath);
settings.workingDir = path.dirname(scriptPath);

const screen = blessed.screen({
smartCSR: true,
Expand Down Expand Up @@ -138,7 +138,7 @@ export function runInternal(scriptPath: string, settings: RenderSettings, state:
let bmlSource = '' + fs.readFileSync(scriptPath);
let result = '';
try {
result = render(bmlSource, settings, documentDir);
result = render(bmlSource, settings);
} catch (e: any) {
// Also need to capture warnings somehow and print them out.
// Currently when a missing ref is found it gets printed weirdly over the TUI
Expand Down
20 changes: 12 additions & 8 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import process from 'process';
import * as rand from './rand';
import * as postprocessing from './postprocessing';
import { defaultBMLSettings, defaultRenderSettings, mergeSettings, RenderSettings } from './settings';
Expand Down Expand Up @@ -29,13 +30,11 @@ export class Renderer {
settings: RenderSettings;
executedForkMap: ExecutedForkMap;
evalContext: EvalContext;
documentDir: string | null;

constructor(settings: RenderSettings, documentDir: string | null) {
constructor(settings: RenderSettings) {
this.settings = settings;
this.executedForkMap = new Map();
this.evalContext = { bindings: {}, output: '', renderer: this };
this.documentDir = documentDir;
}

resolveReference(reference: Reference): string {
Expand Down Expand Up @@ -159,7 +158,8 @@ export class Renderer {
let rngState = rand.saveRngState();
let bmlDocumentString;
try {
bmlDocumentString = fileUtils.readFile(includePath, this.documentDir);
let resolvedWorkingDir = this.settings.workingDir || process.cwd();
bmlDocumentString = fileUtils.readFile(includePath, resolvedWorkingDir);
} catch (e) {
if (typeof window !== 'undefined') {
throw new IncludeError(includePath, `Includes can't be used in browsers`);
Expand All @@ -172,8 +172,12 @@ export class Renderer {

let lexer = new Lexer(bmlDocumentString);
let ast = parseDocument(lexer, true);
let workingDir = path.dirname(path.join(this.documentDir || '', includePath))
let subRenderer = new Renderer(this.settings, workingDir);
let subWorkingDir = path.dirname(path.join(this.settings.workingDir || '', includePath))
let subSettings = {
workingDir: subWorkingDir,
...this.settings
}
let subRenderer = new Renderer(subSettings);
let result = subRenderer.renderWithoutPostProcess(ast);
// Merge state from subrenderer into this renderer
// Any redefined references are silently overwritten; this is needed to support
Expand All @@ -190,10 +194,10 @@ export class Renderer {
}

export function render(bmlDocumentString: string,
renderSettings: RenderSettings | null, documentDir: string | null): string {
renderSettings: RenderSettings | null): string {
renderSettings = mergeSettings(defaultRenderSettings, renderSettings);
let lexer = new Lexer(bmlDocumentString);
let ast = parseDocument(lexer, true);
return new Renderer(renderSettings, documentDir).renderAndPostProcess(ast);
return new Renderer(renderSettings).renderAndPostProcess(ast);
}

10 changes: 10 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ export interface RenderSettings {
* the actual seed.
*/
randomSeed?: number | null;

/**
* Whether to disable `eval` blocks in the document.
*
* This can be useful for security purposes.
*/
allowEval?: boolean | null;

/**
* The working directory used for imports in this render.
*
* If the document uses imports, this should generally be set to the
* absolute path of the document file's directory.
*/
workingDir?: string | null;
}

/**
Expand All @@ -52,6 +61,7 @@ export const defaultBMLSettings: DocumentSettings = {
export const defaultRenderSettings: RenderSettings = {
randomSeed: null,
allowEval: true,
workingDir: null,
};

/**
Expand Down
15 changes: 12 additions & 3 deletions test/testCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ describe('cli', function() {
let path = 'some path';
let action = cli.determineAction([path]);
expect(action.function).toBe(cli.executeFromPath);
expect(action.args).toEqual([path, defaultRenderSettings]);
let expectedSettings = {
...defaultRenderSettings,
workingDir: ".",
}
expect(action.args).toEqual([path, expectedSettings]);
});

it('reads from stdin when not given any arguments', function() {
Expand Down Expand Up @@ -103,10 +107,14 @@ describe('cli', function() {
it('supports interactive mode from path', function() {
let action = cli.determineAction(['--interactive', '1.bml']);
expect(action.function).toBe(cli.executeInteractively);
expect(action.args).toEqual(['1.bml', defaultRenderSettings]);
let expectedSettings = {
...defaultRenderSettings,
workingDir: ".",
}
expect(action.args).toEqual(['1.bml', expectedSettings]);
action = cli.determineAction(['-i', '1.bml']);
expect(action.function).toBe(cli.executeInteractively);
expect(action.args).toEqual(['1.bml', defaultRenderSettings]);
expect(action.args).toEqual(['1.bml', expectedSettings]);
});

it('supports all settings', function() {
Expand All @@ -116,6 +124,7 @@ describe('cli', function() {
let expectedSettings = {
randomSeed: 123,
allowEval: false,
workingDir: ".",
};
expect(action.function).toBe(cli.executeFromPath);
expect(action.args).toEqual([path, expectedSettings]);
Expand Down
Loading

0 comments on commit ceca5ad

Please sign in to comment.