Skip to content

Commit

Permalink
commands have state and context
Browse files Browse the repository at this point in the history
  • Loading branch information
daretodave committed Apr 25, 2024
1 parent 7f1b453 commit 21701e5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
35 changes: 35 additions & 0 deletions src/main/framework/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import short from 'short-uuid'
import { runInNewContext } from 'node:vm'
import * as tryRequire from 'try-require'
import { compile } from '../vendor/webpack'
import { ExecuteContext } from './runtime'

export class Commands {
public lib: object = {}
public state: Map<string, object> = new Map<string, object>()

constructor(
private workingDirectory: string,
Expand All @@ -18,6 +20,38 @@ export class Commands {
return tryRequire.default(path)
}

setTimeout = global.setTimeout
console = global.console

has(key: string): boolean {
return !!this.lib[key]
}

async run(context: ExecuteContext, key: string, ...args: string[]): Promise<unknown> {
let state = this.state[key]
const cmd = this.lib[key]

if (!state) {
state = {}

this.state[key] = state
}

const scoped = {
...state,
context
}

const exec = cmd.call(scoped, ...args)

this.state[key] = {
...scoped,
context: undefined
}

return exec
}

async load(): Promise<void> {
const filesToCreate = [
{ templateFile: 'package.json', outputFile: 'package.json' },
Expand Down Expand Up @@ -57,6 +91,7 @@ export class Commands {
await compile(scriptFile, temp, join(this.workingDirectory, 'node_modules'))

const jsFile: Buffer = await readFile(join(temp, 'commands.js'))

runInNewContext(`${jsFile}`, this)
}
}
9 changes: 6 additions & 3 deletions src/main/framework/runtime-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,19 @@ export function attach({ app, workspace }: BootstrapContext): void {
}
const finish = (code: number): void => {
result.code = code

command.complete = true
command.error = result.code !== 0
}

const finalizeConfirm = await execute(
const finalizeConfirm = await execute({
platform,
workspace,
runtimeTarget,
runtime: runtimeTarget,
command,
out,
finish
)
})
if (finalizeConfirm !== undefined && finalizeConfirm === false) {
finalize = false
}
Expand Down
20 changes: 6 additions & 14 deletions src/main/framework/runtime-executor.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { Command, Runtime } from './runtime'
import { resolveFolderPathForMTERM, Workspace } from './workspace'
import { ExecuteContext } from './runtime'
import { resolveFolderPathForMTERM } from './workspace'
import { RunnerWindow } from '../window/windows/runner'
import { app } from 'electron'
import { spawn } from 'node:child_process'
import { resolve } from 'path'
import { pathExists } from 'fs-extra'

export async function execute(
platform: string,
workspace: Workspace,
runtime: Runtime,
command: Command,
out: (text: string, error?: boolean) => void,
finish: (code: number) => void
): Promise<void | boolean> {
export async function execute(context: ExecuteContext): Promise<void | boolean> {
const { platform, workspace, runtime, command, out, finish } = context
const [cmd, ...args] = command.prompt.split(' ')

// check for system commands
Expand Down Expand Up @@ -81,10 +75,8 @@ export async function execute(
}
}

if (workspace.commands.lib[cmd]) {
const exec = workspace.commands.lib[cmd]

const result = await Promise.resolve(exec(...args))
if (workspace.commands.has(cmd)) {
const result = await Promise.resolve(workspace.commands.run(context, cmd, ...args))

if (!result) {
// nothing was replied with, assume this is a run that will happen in time
Expand Down
11 changes: 10 additions & 1 deletion src/main/framework/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolveFolderPathForMTERM } from './workspace'
import { resolveFolderPathForMTERM, Workspace } from './workspace'
import short from 'short-uuid'
import { ChildProcessWithoutNullStreams } from 'node:child_process'

Expand Down Expand Up @@ -65,3 +65,12 @@ export class Runtime {
title: 'mterm [$idx]'
}
}

export interface ExecuteContext {
platform: string
workspace: Workspace
runtime: Runtime
command: Command
out: (text: string, error?: boolean) => void
finish: (code: number) => void
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": [],
"compilerOptions": {
"esModuleInterop": true
"esModuleInterop": true,
},
"references": [{ "path": "./tsconfig.node.json" }, { "path": "./tsconfig.web.json" }]
}

0 comments on commit 21701e5

Please sign in to comment.