Skip to content

Commit

Permalink
Fix relative paths in includes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajyoon committed May 12, 2024
1 parent ceca5ad commit dec25b8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
import process from 'process';
import path from 'path';
import fs from 'fs';
import { RenderSettings } from './settings';
import { analyze } from './analysis';
import { launchInteractive } from './interactive';
Expand Down Expand Up @@ -31,7 +32,7 @@ export type Action = { function: Function, args: any[] };

function readFile(path: string): string {
try {
return fileUtils.readFile(path, null);
return '' + fs.readFileSync(path);
} catch {
handleNonexistingPath(path);
process.exit(1);
Expand Down
13 changes: 0 additions & 13 deletions src/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import fs from 'fs';
import path from 'path';


export function readFile(filePath: string, workingDir: string | null): string {
if (workingDir) {
// Sanity check that the given working dir is a directory.
// the stat call will also error if the directory is invalid
if (!fs.lstatSync(workingDir).isDirectory()) {
throw new Error()
}
filePath = path.resolve(workingDir, filePath);
}
return '' + fs.readFileSync(filePath);
}

export function readStdin(): string {
return fs.readFileSync(0, 'utf8'); // STDIN_FILENO = 0
}
Expand Down
11 changes: 6 additions & 5 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import process from 'process';
import fs from 'fs';
import * as rand from './rand';
import * as postprocessing from './postprocessing';
import { defaultBMLSettings, defaultRenderSettings, mergeSettings, RenderSettings } from './settings';
Expand All @@ -12,7 +13,6 @@ import { Choice } from './weightedChoice';
import { isStr } from './stringUtils';
import { EvalDisabledError, IncludeError } from './errors';
import { EvalContext } from './evalBlock';
import * as fileUtils from './fileUtils';


// If the referred fork is a silent set fork that has not yet been executed,
Expand Down Expand Up @@ -157,9 +157,10 @@ export class Renderer {
renderInclude(includePath: string): string {
let rngState = rand.saveRngState();
let bmlDocumentString;
let resolvedWorkingDir = this.settings.workingDir || process.cwd();
let resolvedIncludePath = path.resolve(resolvedWorkingDir, includePath);
try {
let resolvedWorkingDir = this.settings.workingDir || process.cwd();
bmlDocumentString = fileUtils.readFile(includePath, resolvedWorkingDir);
bmlDocumentString = '' + fs.readFileSync(resolvedIncludePath);
} catch (e) {
if (typeof window !== 'undefined') {
throw new IncludeError(includePath, `Includes can't be used in browsers`);
Expand All @@ -172,10 +173,10 @@ export class Renderer {

let lexer = new Lexer(bmlDocumentString);
let ast = parseDocument(lexer, true);
let subWorkingDir = path.dirname(path.join(this.settings.workingDir || '', includePath))
let subWorkingDir = path.dirname(resolvedIncludePath)
let subSettings = {
...this.settings,
workingDir: subWorkingDir,
...this.settings
}
let subRenderer = new Renderer(subSettings);
let result = subRenderer.renderWithoutPostProcess(ast);
Expand Down
2 changes: 1 addition & 1 deletion test/testRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ insert('foo got ' + forkResult);

it('Allows disabling eval execution', function() {
let testString = `{[insert('foo')]}`;
expect(() => render(testString, { allowEval: false }, null))
expect(() => render(testString, { allowEval: false }))
.toThrow(EvalDisabledError);
})

Expand Down

0 comments on commit dec25b8

Please sign in to comment.