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

Fix repl open #84

Merged
merged 2 commits into from
Oct 1, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stated-js",
"version": "0.1.37",
"version": "0.1.39",
"license": "Apache-2.0",
"description": "JSONata embedded in JSON",
"main": "./dist/src/index.js",
Expand Down
62 changes: 62 additions & 0 deletions src/CliCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as repl from 'repl';
import {stringifyTemplateJSON} from "./utils/stringify.js";
import jsonata from "jsonata";
import {CliCoreBase} from "./CliCoreBase.js";
import fs from 'fs';


export default class CliCore extends CliCoreBase{
Expand All @@ -27,6 +28,67 @@ export default class CliCore extends CliCoreBase{
super(templateProcessor);
}

public async open(directory: string = this.currentDirectory) {
if(directory === ""){
directory = this.currentDirectory;
}

let files: string[]|undefined = undefined;
try {
// Read all files from the directory
files = await fs.promises.readdir(directory);
} catch (error) {
console.log(`Error reading directory ${directory}: ${error}`);
console.log('Changed directory with .cd or .open an/existing/directory');
this.replServer.displayPrompt();
return {error: `Error reading directory ${directory}: ${error}`};
}
// Filter out only .json and .yaml files
const templateFiles: string[] = files.filter(file => file.endsWith('.json') || file.endsWith('.yaml'));

// Display the list of files to the user
templateFiles.forEach((file, index) => {
console.log(`${index + 1}: ${file}`);
});

// Create an instance of AbortController
const ac = new AbortController();
const {signal} = ac; // Get the AbortSignal from the controller

// Ask the user to choose a file
this.replServer.question('Enter the number of the file to open (or type "abort" to cancel): ', {signal}, async (answer) => {
// Check if the operation was aborted
if (signal.aborted) {
console.log('File open operation was aborted.');
this.replServer.displayPrompt();
return;
}

const fileIndex = parseInt(answer, 10) - 1; // Convert to zero-based index
if (fileIndex >= 0 && fileIndex < templateFiles.length) {
// User has entered a valid file number; initialize with this file
const filepath = templateFiles[fileIndex];
try {
const result = await this.init(`-f "${filepath}"`); // Adjust this call as per your init method's expected format
console.log(stringifyTemplateJSON(result));
console.log("...try '.out' or 'template.output' to see evaluated template")
} catch (error) {
console.log('Error loading file:', error);
}
} else {
console.log('Invalid file number.');
}
this.replServer.displayPrompt();
});

// Allow the user to type "abort" to cancel the file open operation
this.replServer.once('SIGINT', () => {
ac.abort();
});

return "open... (type 'abort' to cancel)";
}


public async tail(args: string): Promise<any> {
console.log("Started tailing... Press Ctrl+C to stop.")
Expand Down
1 change: 1 addition & 0 deletions src/StatedREPL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export default class StatedREPL {
console.error(stringify(e.message));
}
this.replServer.displayPrompt();
return true;
}

static colorize(s:string):string{
Expand Down
1 change: 1 addition & 0 deletions src/test/CliCore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,4 @@ test("import and run __init function", async () => {




21 changes: 21 additions & 0 deletions src/test/StatedREPL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ if(typeof Bun === "undefined") { //run on node, not bun
}
});

test("open", async () => {
const originalCmdLineArgsStr = process.argv;
process.argv = ["node", "dist/stated.js"]; // this is an argv when running stated.js repl.
// extend CliCore with restore command
const repl = new StatedREPL();

try {
await repl.initialize();

// we call restore on the repl, which will expect it to be defined in CliCore.
const msg = await repl.cli('open');

expect(msg).toBe(true);
} finally {
process.argv = originalCmdLineArgsStr;
if (repl !== undefined) await repl.close();
}
});

}

// This test validates a bug when running an init command in StatedREPL overwrites context of provided TemplateProcessor
Expand All @@ -113,3 +132,5 @@ test("TemplateProcessor keeps context on init", async () => {
);
});



Loading