-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental nitro tasks (#1929)
- Loading branch information
Showing
18 changed files
with
359 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineCommand } from "citty"; | ||
|
||
export default defineCommand({ | ||
meta: { | ||
name: "tasks", | ||
description: "Operate in nitro tasks (experimental)", | ||
}, | ||
subCommands: { | ||
list: () => import("./list").then((r) => r.default), | ||
run: () => import("./run").then((r) => r.default), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { defineCommand } from "citty"; | ||
import { resolve } from "pathe"; | ||
import { consola } from "consola"; | ||
import { listNitroTasks } from "../../../task"; | ||
|
||
export default defineCommand({ | ||
meta: { | ||
name: "run", | ||
description: "List available tasks (experimental)", | ||
}, | ||
args: { | ||
dir: { | ||
type: "string", | ||
description: "project root directory", | ||
}, | ||
}, | ||
async run({ args }) { | ||
const cwd = resolve((args.dir || args.cwd || ".") as string); | ||
const tasks = await listNitroTasks({ cwd, buildDir: ".nitro" }); | ||
for (const [name, task] of Object.entries(tasks)) { | ||
consola.log( | ||
` - \`${name}\`${task.description ? ` - ${task.description}` : ""}` | ||
); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { defineCommand } from "citty"; | ||
import { resolve } from "pathe"; | ||
import destr from "destr"; | ||
import { consola } from "consola"; | ||
import { runNitroTask } from "../../../task"; | ||
|
||
export default defineCommand({ | ||
meta: { | ||
name: "run", | ||
description: | ||
"Run a runtime task in the currently running dev server (experimental)", | ||
}, | ||
args: { | ||
name: { | ||
type: "positional", | ||
description: "task name", | ||
required: true, | ||
}, | ||
dir: { | ||
type: "string", | ||
description: "project root directory", | ||
}, | ||
payload: { | ||
type: "string", | ||
description: "payload json to pass to the task", | ||
}, | ||
}, | ||
async run({ args }) { | ||
const cwd = resolve((args.dir || args.cwd || ".") as string); | ||
consola.info(`Running task \`${args.name}\`...`); | ||
try { | ||
const { result } = await runNitroTask( | ||
args.name, | ||
destr(args.payload || "{}"), | ||
{ | ||
cwd, | ||
buildDir: ".nitro", | ||
} | ||
); | ||
consola.success("Result:", result); | ||
} catch (err) { | ||
consola.error(`Failed to run task \`${args.name}\`: ${err.message}`); | ||
process.exit(1); // eslint-disable-line unicorn/no-process-exit | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createError } from "h3"; | ||
import { useNitroApp, type NitroApp } from "./app"; | ||
import { tasks } from "#internal/nitro/virtual/tasks"; | ||
|
||
/** @experimental */ | ||
export interface NitroTaskContext {} | ||
|
||
/** @experimental */ | ||
export interface NitroTaskPayload { | ||
[key: string]: unknown; | ||
} | ||
|
||
/** @experimental */ | ||
export interface NitroTaskMeta { | ||
name?: string; | ||
description?: string; | ||
} | ||
|
||
/** @experimental */ | ||
export interface NitroTask<RT = unknown> extends NitroTaskMeta { | ||
run( | ||
payload: NitroTaskPayload, | ||
context: NitroTaskContext | ||
): { result: RT | Promise<RT> }; | ||
} | ||
|
||
/** @experimental */ | ||
export function defineNitroTask<RT = unknown>( | ||
def: NitroTask<RT> | ||
): NitroTask<RT> { | ||
if (typeof def.run !== "function") { | ||
def.run = () => { | ||
throw new TypeError("Nitro task must implement a `run` method!"); | ||
}; | ||
} | ||
return def; | ||
} | ||
|
||
/** @experimental */ | ||
export async function runNitroTask<RT = unknown>( | ||
name: string, | ||
payload: NitroTaskPayload = {} | ||
): Promise<{ result: RT }> { | ||
if (!(name in tasks)) { | ||
throw createError({ | ||
message: `Nitro task \`${name}\` is not available!`, | ||
statusCode: 404, | ||
}); | ||
} | ||
if (!tasks[name].get) { | ||
throw createError({ | ||
message: `Nitro task \`${name}\` is not implemented!`, | ||
statusCode: 501, | ||
}); | ||
} | ||
const context: NitroTaskContext = {}; | ||
const handler = await tasks[name].get().then((mod) => mod.default); | ||
const { result } = handler.run(payload, context); | ||
return { | ||
result: result as RT, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { NitroTask } from "../task"; | ||
|
||
export const tasks: Record< | ||
string, | ||
{ get: () => Promise<{ default: NitroTask }>; description?: string } | ||
> = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.