Skip to content

Commit

Permalink
chore(cmds): improve deno compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Nov 4, 2024
1 parent 80cde96 commit f5327ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
49 changes: 24 additions & 25 deletions cmds/cmds.browser.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
export * from './cmds-command'
export { default as Command } from './cmds-command'
export * from './cmds-step'
export { default as Step } from './cmds-step'

import Command from './cmds-command'
import Step from './cmds-step'
import type { StepFn, Steps } from './cmds-step'
export * from './cmds-command.ts'
export { default as Command } from './cmds-command.ts'
export * from './cmds-step.ts'
export { default as Step } from './cmds-step.ts'

import Command from './cmds-command.ts'
import type { StepFn } from './cmds-step.ts'
import { hideProperties } from '@hackbg/hide'
import { timestamp } from '@hackbg/time'
import { Console, Logged, colors, bold } from '@hackbg/logs'
import { Logged, bold } from '@hackbg/logs'

export type CommandTree<T extends CommandContext> =
Record<string, Command<T>|CommandContext>

declare const globalThis: { process?: { env: object, cwd: () => string } }

export default class CommandContext extends Logged {
/** Name of this command tree. */
name: string
Expand All @@ -22,9 +23,9 @@ export default class CommandContext extends Logged {
/** Start of command execution. */
timestamp: string = timestamp()
/** Process environment at lauch of process. */
env: Record<string, string|undefined> = { ...process.env }
env: Record<string, string|undefined> = { ...globalThis.process?.env||{} }
/** Current working directory at launch of process. */
cwd: string = process.cwd()
cwd: string = globalThis!.process?.cwd() || '.'
/** All registered commands. */
commandTree: CommandTree<this> = {}
/** Currently executing command. */
Expand All @@ -45,19 +46,19 @@ export default class CommandContext extends Logged {

/** Define a command and return it. */
command <X extends StepFn<this, unknown>> (parameters: {
name: string,
args: string,
info: string,
name: string,
args?: string,
info?: string,
}, step: X): X {
this.addCommand(parameters, step)
return step
}

/** Define a command and return `this`. */
addCommand <X extends StepFn<this, unknown>> (parameters: {
name: string,
args: string,
info: string,
name: string,
args?: string,
info?: string,
}, step: X): this {
// store command
this.commandTree[parameters.name] = new Command({
Expand Down Expand Up @@ -92,30 +93,28 @@ export default class CommandContext extends Logged {
async run <T> (argv: string[], context: any = this): Promise<T> {
// If no arguments were passed, exit.
if (argv.length === 0) {
await this.printUsageNoCommand(this)
this.printUsageNoCommand(this)
return null as unknown as T
}
// Parse the command and arguments
const [command, ...args] = this.parse(argv)
// Run the command
if (command) {
return await command.run(args, context) as T
}
if (command) return await command.run(args, context) as T
// If no command was run, print usage and throw
await this.printUsageMissingCommand(this)
this.printUsageMissingCommand(this)
throw new Error(`Invalid invocation: "${argv.join(' ')}"`)
}

async printUsageNoCommand (arg0: this) {
printUsageNoCommand (arg0: this) {
this.log.br().error('No command invoked.')
return this.printUsage(arg0)
}

async printUsageMissingCommand (arg0: Parameters<typeof this["printUsage"]>[0]) {
printUsageMissingCommand (arg0: Parameters<typeof this["printUsage"]>[0]) {
return this.printUsage(arg0)
}

async printUsage ({ constructor: { name }, commandTree }: CommandContext) {
printUsage ({ constructor: { name }, commandTree }: CommandContext) {
// Align
const columns = { name: 0, args: 0, sub: 0 }
for (const name of Object.keys(commandTree)) {
Expand Down
19 changes: 10 additions & 9 deletions cmds/cmds.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from './cmds.browser'
export * from './cmds.browser.ts'

import CommandContext from './cmds.browser'
import CommandContext from './cmds.browser.ts'
import { fileURLToPath } from 'node:url'
import { Console, colors, bold } from '@hackbg/logs'
import { Console, colors } from '@hackbg/logs'
import process from 'node:process'

export default class LocalCommandContext extends CommandContext {

Expand Down Expand Up @@ -49,22 +50,22 @@ export default class LocalCommandContext extends CommandContext {

}

export function entrypoint (url: string, callback: Function) {
export function entrypoint (url: string, callback: (...args: unknown[])=>unknown) {
if (isEntrypoint(url)) callback()
}

export function isEntrypoint (url: string) {
return process.argv[1] === fileURLToPath(url)
}

export function startRepl (context: object, log = new Console('REPL')) {
return Promise.all([
//@ts-ignore
export async function startRepl (context: object, log = new Console('REPL')) {
return await Promise.all([
//@ts-ignore for build
import('node:repl'),
//@ts-ignore
//@ts-ignore for build
import('node:vm')
]).then(([repl, { createContext }])=>{
let prompt = `\n${colors.black.bgGreen.bold(' JS ')}${colors.green('▒')} `
const prompt = `\n${colors.black.bgGreen.bold(' JS ')}${colors.green('▒')} `
context = createContext(context)
return new Promise(resolve=>setTimeout(()=>{
Object.assign(repl.start({ prompt }), { context }).on('close', resolve)
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"resolveJsonModule": true,
"downlevelIteration": true,
"skipLibCheck": true,
"strict": true
"strict": true,
"allowImportingTsExtensions": true
}
}

0 comments on commit f5327ba

Please sign in to comment.