Skip to content

Commit

Permalink
Add async play support
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeno committed Aug 11, 2023
1 parent e7b23e3 commit 08f3fe6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/commands/play.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function playCommand(): Command {
return new Command('play')
.description('Runs a test')
.argument('<script>', 'Script to run')
.action((scriptName: string) => {
playFile(scriptName).replay();
.action(async (scriptName: string) => {
await playFile(scriptName).replay();
});
}
24 changes: 13 additions & 11 deletions src/modules/play/utils/play.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ export class PlayFile {
import './${workspace.contextFile.dir.relativePathFrom(file.dir)}/${
workspace.contextFile.basenameWithoutExt
}';
// ToDo: Add your logic
export default null;
export async function main() {
// ToDo: Add your logic
return null;
}
`.trimStart();
}
}
Expand Down Expand Up @@ -75,7 +76,7 @@ export default null;
setTimeout(() => {
let prevText: string | null = null;

this.unwatch = file.watch((event) => {
this.unwatch = file.watch(async (event) => {
if (event === 'change') {
let result = undefined;
try {
Expand All @@ -93,7 +94,7 @@ export default null;
log(`Running play ${playId}...`);

running = true;
result = this.replay();
result = await this.replay();
} finally {
running = false;
}
Expand Down Expand Up @@ -133,15 +134,16 @@ export default null;
/**
* Runs the playground file.
*/
replay<ResultType = any>(): ResultType {
async replay<ResultType = any>(): Promise<ResultType> {
const file = this.file;
if (!file.exists) {
throw new Error(`No play found for '${file.path}'`);
}

let result = file.rerequire();

if (typeof result.default !== 'undefined') {
if (typeof result.main !== 'undefined') {
result = await result.main();
} else if (typeof result.default !== 'undefined') {
result = result.default;
}

Expand Down Expand Up @@ -231,9 +233,9 @@ export function unplay(pathOrFile: string | File | null = null): void {
/**
* Runs the given playground file.
*/
export function replay<ResultType = any>(
export async function replay<ResultType = any>(
pathOrFile: string | File,
): ResultType {
): Promise<ResultType> {
const f = playFile(pathOrFile);

return f.replay();
Expand Down

0 comments on commit 08f3fe6

Please sign in to comment.