Skip to content

Commit

Permalink
fix: add :cmd and commands to real time edit command file
Browse files Browse the repository at this point in the history
resolves #100
  • Loading branch information
daretodave committed May 9, 2024
1 parent d8fbce5 commit 24c5e84
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/main/framework/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import * as tryRequire from 'try-require'
import { compile } from '../vendor/webpack'
import { Settings } from './settings'
import { ExecuteContext } from './execute-context'

export type FrontendHook = () => Promise<void> | void
export class Commands {
public lib: object = {}
public commandFileLocation: string = ''
public state: Map<string, object> = new Map<string, object>()
public handlers: Map<string, FrontendHook> = new Map<string, FrontendHook>()
constructor(
private workingDirectory: string,
private templateDirectory: string
Expand Down Expand Up @@ -99,13 +97,14 @@ export class Commands {
}

const packageJson = await readJson(join(this.workingDirectory, 'package.json'))
const commandFileLocation = join(this.workingDirectory, packageJson.main)

this.commandFileLocation = join(this.workingDirectory, packageJson.main)

const id = short.generate()
const temp = join(tmpdir(), `mterm-${id}`)
await mkdirs(temp)

await compile(commandFileLocation, temp, join(this.workingDirectory, 'node_modules'))
await compile(this.commandFileLocation, temp, join(this.workingDirectory, 'node_modules'))

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

Expand Down
21 changes: 19 additions & 2 deletions src/main/framework/runtime-executor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { spawn } from 'node:child_process'

import { ExecuteContext } from './execute-context'

import Reload from './system-commands/reload'
import Exit from './system-commands/exit'
import History from './system-commands/history'
Expand All @@ -13,13 +15,28 @@ import Workspace from './system-commands/workspace'
import Settings from './system-commands/settings'
import Edit from './system-commands/edit'
import Reset from './system-commands/reset'
import { ExecuteContext } from './execute-context'
import Commands from './system-commands/commands'

const systemCommands: Array<{
command: string
alias?: string[]
task: (context: ExecuteContext, ...args: string[]) => Promise<void> | void
}> = [Reload, Exit, History, Cd, Tab, Test, Clear, Version, Vault, Workspace, Settings, Edit, Reset]
}> = [
Reload,
Exit,
History,
Cd,
Tab,
Test,
Clear,
Version,
Vault,
Workspace,
Settings,
Edit,
Reset,
Commands
]
export async function execute(context: ExecuteContext): Promise<void | boolean> {
const [cmd, ...args] = context.command.prompt.split(' ')

Expand Down
25 changes: 25 additions & 0 deletions src/main/framework/system-commands/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ExecuteContext } from '../execute-context'
import { RunnerWindow } from '../../window/windows/runner'

export default {
command: ':commands',
alias: [':commands', ':cmd'],
async task(context: ExecuteContext, task?: string): Promise<void> {
context.out('')
if (!task) {
if (!context.workspace.commands.commandFileLocation) {
context.out(
'No command file to edit, make sure ~/mterm/package.json has a proper `main` field'
)
return
}
await context.edit(context.workspace.commands.commandFileLocation, async () => {
context.out('Saved command file!\n')

await context.workspace.commands.load(context.workspace.settings)

context.out('Commands reloaded\n')
})
}
}
}

0 comments on commit 24c5e84

Please sign in to comment.