Skip to content

Commit

Permalink
feat: script runner (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultleouay authored Nov 18, 2024
1 parent b26b808 commit 74ca3ab
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 14 deletions.
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
},
"imports": {
"@cliffy/command": "jsr:@cliffy/[email protected]",
"@cliffy/testing": "jsr:@cliffy/[email protected]",
"@rebeccastevens/deepmerge": "jsr:@rebeccastevens/deepmerge@^7.1.3",
"@std/assert": "jsr:@std/assert@^1.0.6",
"@std/dotenv": "jsr:@std/dotenv@^0.225.2",
"@std/fmt": "jsr:@std/fmt@^1.0.3",
"@std/jsonc": "jsr:@std/jsonc@^1.0.1",
"@std/path": "jsr:@std/path@^1.0.7",
"@std/streams": "jsr:@std/streams@^1.0.7",
"@std/testing": "jsr:@std/testing@^1.0.3",
"@std/testing": "jsr:@std/testing@^1.0.3"
}
}
526 changes: 523 additions & 3 deletions deno.lock

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/commands/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { $, Command, path } from '../deps.ts'
import type { Script, ScriptContext } from '../lib/types.ts'
import { logger } from '../lib/logger.ts'
import { config } from '../lib/config.ts'

import * as esbuild from 'https://deno.land/x/[email protected]/mod.js'

import { denoPlugins } from 'jsr:@luca/[email protected]'

export const script = new Command()
.description('script')
.arguments('<input:string>')
.action(async (options, ...args: string[]) => {
if (!args[0]) {
logger.error('No script name provided')
Deno.exit(1)
}

try {
const current = Deno.cwd()
const file = `${current}/${args[0]}`
const cfg = config.get()
const context: ScriptContext = {
env: 'dev',
config: cfg,
lib: {
$: $,
path: path,
},
}

const builtOutput = `${current}/.runreal/dist/script.esm.js`

await esbuild.build({
plugins: [...denoPlugins({ loader: 'portable' })],
entryPoints: [file],
outfile: builtOutput,
bundle: true,
format: 'esm',
})
esbuild.stop()

const script = (await import(builtOutput)) as Script
await script.main(context)
} catch (e) {
logger.error(e)
Deno.exit(1)
}
})
2 changes: 2 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * as path from 'jsr:@std/path'
export { mergeReadableStreams } from 'jsr:@std/streams'

export { $ } from 'jsr:@david/[email protected]'

export * as dotenv from 'jsr:@std/dotenv'
export * as fmt from 'jsr:@std/fmt/colors'

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { buildgraph } from './commands/buildgraph/index.ts'
import { workflow } from './commands/workflow/index.ts'
import { clean } from './commands/clean.ts'
import { cmd } from './cmd.ts'
import { script } from './commands/script.ts'

await cmd
.name('runreal')
Expand All @@ -26,4 +27,5 @@ await cmd
.command('pkg', pkg)
.command('buildgraph', buildgraph)
.command('workflow', workflow)
.command('script', script)
.parse(Deno.args)
2 changes: 1 addition & 1 deletion src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const ConfigSchema = z.object({
}),
workflows: z.array(
z.object({
id: z.string().regex(new RegExp('^[a-zA-Z0-9][a-zA-Z0-9\\-]*$')).optional().describe('Workflow id'),
id: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9\-]*$/).optional().describe('Workflow id'),
name: z.string().describe('Workflow name'),
steps: z.array(
z.object({
Expand Down
27 changes: 20 additions & 7 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Command, z } from '../deps.ts'
import { cmd } from '../cmd.ts'
import type { $, Command, path, z } from '../deps.ts'
import type { cmd } from '../cmd.ts'

import { DebugConfigOptions } from '../commands/debug/debug-config.ts'
import { SetupOptions } from '../commands/engine/setup.ts'
import { InstallOptions } from '../commands/engine/install.ts'
import { UpdateOptions } from '../commands/engine/update.ts'
import { ConfigSchema, InternalSchema } from './schema.ts'
import type { DebugConfigOptions } from '../commands/debug/debug-config.ts'
import type { SetupOptions } from '../commands/engine/setup.ts'
import type { InstallOptions } from '../commands/engine/install.ts'
import type { UpdateOptions } from '../commands/engine/update.ts'
import type { ConfigSchema, InternalSchema } from './schema.ts'

export type GlobalOptions = typeof cmd extends
Command<void, void, void, [], infer Options extends Record<string, unknown>> ? Options
Expand Down Expand Up @@ -43,3 +43,16 @@ export interface GitIgnoreFiles {
files: string[]
dirs: string[]
}

export interface ScriptContext {
env: string
config: RunrealConfig
lib: {
$: typeof $
path: typeof path
}
}

export interface Script {
main: (ctx: ScriptContext) => Promise<void>
}
10 changes: 10 additions & 0 deletions tests/__snapshots__/script.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const snapshot = {};

snapshot[`should execute the command 1`] = `
stdout:
"hello from script
hello from dax
"
stderr:
""
`;
2 changes: 1 addition & 1 deletion tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, assertEquals } from '@std/assert'
import { Config } from '../src/lib/config.ts'
import { path, ulid } from '../src/deps.ts'
import { CliOptions } from '../src/lib/types.ts'
import type { CliOptions } from '../src/lib/types.ts'
import { FakeTime } from '@std/testing/time'

Deno.test('Config.create should initialize with default values', async () => {
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { ScriptContext } from '../../src/lib/types.ts'

export async function main(ctx: ScriptContext) {
console.log('hello from script')
await ctx.lib.$`echo hello from dax`
}
10 changes: 10 additions & 0 deletions tests/fixtures/test.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@
]
}
]
},
{
"id": "script",
"name": "script",
"steps": [
{
"command": "runreal script",
"args": ["./tests/fixtures/hello-world.ts"]
}
]
}
]
}
13 changes: 13 additions & 0 deletions tests/script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { snapshotTest } from '@cliffy/testing'
import { script } from '../src/commands/script.ts'

await snapshotTest({
name: 'should execute the command ',
meta: import.meta,
args: ['./tests/fixtures/hello-world.ts'],
// maybe -A
denoArgs: ['--allow-read', '--allow-env', '--allow-write', '--allow-run', '--allow-net'],
async fn() {
await script.parse()
},
})
2 changes: 1 addition & 1 deletion tests/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from '@std/assert'
import { getSubstitutions, normalizePaths, render, renderConfig } from '../src/lib/template.ts'
import { RunrealConfig } from '../src/lib/types.ts'
import type { RunrealConfig } from '../src/lib/types.ts'

Deno.test('template tests', () => {
const tmpl =
Expand Down

0 comments on commit 74ca3ab

Please sign in to comment.