Skip to content

Commit 74ca3ab

Browse files
feat: script runner (#42)
1 parent b26b808 commit 74ca3ab

File tree

13 files changed

+640
-14
lines changed

13 files changed

+640
-14
lines changed

deno.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
},
2929
"imports": {
3030
"@cliffy/command": "jsr:@cliffy/command@1.0.0-rc.7",
31+
"@cliffy/testing": "jsr:@cliffy/testing@1.0.0-rc.7",
3132
"@rebeccastevens/deepmerge": "jsr:@rebeccastevens/deepmerge@^7.1.3",
3233
"@std/assert": "jsr:@std/assert@^1.0.6",
3334
"@std/dotenv": "jsr:@std/dotenv@^0.225.2",
3435
"@std/fmt": "jsr:@std/fmt@^1.0.3",
3536
"@std/jsonc": "jsr:@std/jsonc@^1.0.1",
3637
"@std/path": "jsr:@std/path@^1.0.7",
3738
"@std/streams": "jsr:@std/streams@^1.0.7",
38-
"@std/testing": "jsr:@std/testing@^1.0.3",
39+
"@std/testing": "jsr:@std/testing@^1.0.3"
3940
}
4041
}

deno.lock

Lines changed: 523 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/script.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { $, Command, path } from '../deps.ts'
2+
import type { Script, ScriptContext } from '../lib/types.ts'
3+
import { logger } from '../lib/logger.ts'
4+
import { config } from '../lib/config.ts'
5+
6+
import * as esbuild from 'https://deno.land/x/esbuild@v0.24.0/mod.js'
7+
8+
import { denoPlugins } from 'jsr:@luca/esbuild-deno-loader@0.11.0'
9+
10+
export const script = new Command()
11+
.description('script')
12+
.arguments('<input:string>')
13+
.action(async (options, ...args: string[]) => {
14+
if (!args[0]) {
15+
logger.error('No script name provided')
16+
Deno.exit(1)
17+
}
18+
19+
try {
20+
const current = Deno.cwd()
21+
const file = `${current}/${args[0]}`
22+
const cfg = config.get()
23+
const context: ScriptContext = {
24+
env: 'dev',
25+
config: cfg,
26+
lib: {
27+
$: $,
28+
path: path,
29+
},
30+
}
31+
32+
const builtOutput = `${current}/.runreal/dist/script.esm.js`
33+
34+
await esbuild.build({
35+
plugins: [...denoPlugins({ loader: 'portable' })],
36+
entryPoints: [file],
37+
outfile: builtOutput,
38+
bundle: true,
39+
format: 'esm',
40+
})
41+
esbuild.stop()
42+
43+
const script = (await import(builtOutput)) as Script
44+
await script.main(context)
45+
} catch (e) {
46+
logger.error(e)
47+
Deno.exit(1)
48+
}
49+
})

src/deps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export * as path from 'jsr:@std/path'
22
export { mergeReadableStreams } from 'jsr:@std/streams'
33

4+
export { $ } from 'jsr:@david/dax@0.42.0'
5+
46
export * as dotenv from 'jsr:@std/dotenv'
57
export * as fmt from 'jsr:@std/fmt/colors'
68

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { buildgraph } from './commands/buildgraph/index.ts'
1111
import { workflow } from './commands/workflow/index.ts'
1212
import { clean } from './commands/clean.ts'
1313
import { cmd } from './cmd.ts'
14+
import { script } from './commands/script.ts'
1415

1516
await cmd
1617
.name('runreal')
@@ -26,4 +27,5 @@ await cmd
2627
.command('pkg', pkg)
2728
.command('buildgraph', buildgraph)
2829
.command('workflow', workflow)
30+
.command('script', script)
2931
.parse(Deno.args)

src/lib/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const ConfigSchema = z.object({
4646
}),
4747
workflows: z.array(
4848
z.object({
49-
id: z.string().regex(new RegExp('^[a-zA-Z0-9][a-zA-Z0-9\\-]*$')).optional().describe('Workflow id'),
49+
id: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9\-]*$/).optional().describe('Workflow id'),
5050
name: z.string().describe('Workflow name'),
5151
steps: z.array(
5252
z.object({

src/lib/types.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Command, z } from '../deps.ts'
2-
import { cmd } from '../cmd.ts'
1+
import type { $, Command, path, z } from '../deps.ts'
2+
import type { cmd } from '../cmd.ts'
33

4-
import { DebugConfigOptions } from '../commands/debug/debug-config.ts'
5-
import { SetupOptions } from '../commands/engine/setup.ts'
6-
import { InstallOptions } from '../commands/engine/install.ts'
7-
import { UpdateOptions } from '../commands/engine/update.ts'
8-
import { ConfigSchema, InternalSchema } from './schema.ts'
4+
import type { DebugConfigOptions } from '../commands/debug/debug-config.ts'
5+
import type { SetupOptions } from '../commands/engine/setup.ts'
6+
import type { InstallOptions } from '../commands/engine/install.ts'
7+
import type { UpdateOptions } from '../commands/engine/update.ts'
8+
import type { ConfigSchema, InternalSchema } from './schema.ts'
99

1010
export type GlobalOptions = typeof cmd extends
1111
Command<void, void, void, [], infer Options extends Record<string, unknown>> ? Options
@@ -43,3 +43,16 @@ export interface GitIgnoreFiles {
4343
files: string[]
4444
dirs: string[]
4545
}
46+
47+
export interface ScriptContext {
48+
env: string
49+
config: RunrealConfig
50+
lib: {
51+
$: typeof $
52+
path: typeof path
53+
}
54+
}
55+
56+
export interface Script {
57+
main: (ctx: ScriptContext) => Promise<void>
58+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const snapshot = {};
2+
3+
snapshot[`should execute the command 1`] = `
4+
stdout:
5+
"hello from script
6+
hello from dax
7+
"
8+
stderr:
9+
""
10+
`;

tests/config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert, assertEquals } from '@std/assert'
22
import { Config } from '../src/lib/config.ts'
33
import { path, ulid } from '../src/deps.ts'
4-
import { CliOptions } from '../src/lib/types.ts'
4+
import type { CliOptions } from '../src/lib/types.ts'
55
import { FakeTime } from '@std/testing/time'
66

77
Deno.test('Config.create should initialize with default values', async () => {

tests/fixtures/hello-world.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { ScriptContext } from '../../src/lib/types.ts'
2+
3+
export async function main(ctx: ScriptContext) {
4+
console.log('hello from script')
5+
await ctx.lib.$`echo hello from dax`
6+
}

0 commit comments

Comments
 (0)