Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lagon): write Lagon config on build #996

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/presets/lagon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import type { PackageJson } from "pkg-types";
import { resolve } from "pathe";
import { resolve, relative } from "pathe";
import { defineNitroPreset } from "../preset";
import { writeFile } from "../utils";

/**
* Both function_id and organization_id fields are required but only used when deploying the function
* Ref: https://github.com/lagonapp/lagon/blob/06093d051898d7603f356b9cae5e3f14078d480a/crates/cli/src/utils/deployments.rs#L34
*/
export interface LagonFunctionConfig {
function_id: string;
organization_id: string;
index: string;
client?: string;
assets?: string;
}

export const lagon = defineNitroPreset({
extends: "base-worker",
entry: "#internal/nitro/entries/lagon",
Expand All @@ -19,7 +31,24 @@ export const lagon = defineNitroPreset({

hooks: {
async compiled(nitro) {
// TODO: write lagon config when it's supported
// Write Lagon config
const root = nitro.options.output.dir;
const indexPath = relative(
root,
resolve(nitro.options.output.serverDir, "index.mjs")
);
const assetsDir = relative(root, nitro.options.output.publicDir);

await writeFile(
resolve(root, ".lagon", "config.json"),
JSON.stringify(<LagonFunctionConfig>{
function_id: "",
organization_id: "",
index: indexPath,
client: null,
assets: assetsDir,
})
);

// Write package.json for deployment
await writeFile(
Expand All @@ -28,9 +57,8 @@ export const lagon = defineNitroPreset({
<PackageJson>{
private: true,
scripts: {
dev: "npx -p esbuild -p @lagon/cli lagon dev ./server/index.mjs -p ./public",
deploy:
"npx -p esbuild -p @lagon/cli lagon deploy ./server/index.mjs -p ./public",
dev: "npx -p esbuild -p @lagon/cli lagon dev",
deploy: "npx -p esbuild -p @lagon/cli lagon deploy",
},
},
null,
Expand Down