Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
fix(backend): fix conflicting dev & deploy by running them in differe…
Browse files Browse the repository at this point in the history
…nt contexts (#477)
  • Loading branch information
NathanFlurry committed Sep 24, 2024
1 parent c1b27d5 commit 4ebdc90
Show file tree
Hide file tree
Showing 59 changed files with 656 additions and 598 deletions.
72 changes: 36 additions & 36 deletions packages/backend/cli/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@ import { verbose } from "../toolchain/term/status.ts";
import { z } from "zod";

export interface Task<T extends z.ZodType> {
inputSchema: T;
run(input: z.infer<T>): Promise<void>;
inputSchema: T;
run(input: z.infer<T>): Promise<void>;
}

export async function runTask<T extends z.ZodType>(task: Task<T>) {
Deno.addSignalListener(Deno.build.os == "windows" ? "SIGBREAK" : "SIGINT", async () => {
verbose("Received shutdown signal");
await runShutdown();
Deno.exit(0);
});
Deno.addSignalListener(Deno.build.os == "windows" ? "SIGBREAK" : "SIGINT", async () => {
verbose("Received shutdown signal");
await runShutdown();
Deno.exit(0);
});

let exitCode = 0;
try {
// Parse flags
const args = parseArgs(Deno.args);
const inputJson = args["input"];
if (!inputJson) {
throw new UserError("Missing --input argument");
}
let exitCode = 0;
try {
// Parse flags
const args = parseArgs(Deno.args);
const inputJson = args["input"];
if (!inputJson) {
throw new UserError("Missing --input argument");
}

// Parse input
let input;
try {
input = JSON.parse(inputJson);
} catch (cause) {
throw new UserError("Invalid input JSON", { cause });
}
// Parse input
let input;
try {
input = JSON.parse(inputJson);
} catch (cause) {
throw new UserError("Invalid input JSON", { cause });
}

// Validate input using the task's inputSchema
const validatedInput = task.inputSchema.safeParse(input);
if (!validatedInput.success) {
throw new UserError("Input violates schema", { details: JSON.stringify(validatedInput.error, null, 2) });
}
// Validate input using the task's inputSchema
const validatedInput = task.inputSchema.safeParse(input);
if (!validatedInput.success) {
throw new UserError("Input violates schema", { details: JSON.stringify(validatedInput.error, null, 2) });
}

// Execute task
await task.run(validatedInput.data);
} catch (err) {
printError(err);
exitCode = 1;
} finally {
await runShutdown();
}
// Execute task
await task.run(validatedInput.data);
} catch (err) {
printError(err);
exitCode = 1;
} finally {
await runShutdown();
}

Deno.exit(exitCode);
Deno.exit(exitCode);
}
101 changes: 50 additions & 51 deletions packages/backend/cli/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,57 @@ export const inputSchema = z.object({
}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
// Defaults based on runtime
if (input.runtime == Runtime.Deno) {
if (input.outputFormat == undefined) input.outputFormat = Format.Native;
if (input.dbDriver == undefined) input.dbDriver = DbDriver.NodePostgres;
} else if (input.runtime == Runtime.CloudflareWorkersPlatforms) {
if (input.outputFormat == undefined) input.outputFormat = Format.Bundled;
if (input.dbDriver == undefined) input.dbDriver = DbDriver.NeonServerless;
}

// Validate
if (input.runtime == Runtime.CloudflareWorkersPlatforms) {
if (input.outputFormat != Format.Bundled) {
throw new Error(
`\`format\` must be "${Format.Bundled}" if \`runtime\` is "${Runtime.CloudflareWorkersPlatforms}".`,
);
inputSchema,
async run(input) {
// Defaults based on runtime
if (input.runtime == Runtime.Deno) {
if (input.outputFormat == undefined) input.outputFormat = Format.Native;
if (input.dbDriver == undefined) input.dbDriver = DbDriver.NodePostgres;
} else if (input.runtime == Runtime.CloudflareWorkersPlatforms) {
if (input.outputFormat == undefined) input.outputFormat = Format.Bundled;
if (input.dbDriver == undefined) input.dbDriver = DbDriver.NeonServerless;
}
if (input.dbDriver != DbDriver.NeonServerless && input.dbDriver != DbDriver.CloudflareHyperdrive) {
throw new Error(
`\`db-driver\` must be "${DbDriver.NeonServerless}" or "${DbDriver.CloudflareHyperdrive}" if \`runtime\` is "${Runtime.CloudflareWorkersPlatforms}".`,
);

// Validate
if (input.runtime == Runtime.CloudflareWorkersPlatforms) {
if (input.outputFormat != Format.Bundled) {
throw new Error(
`\`format\` must be "${Format.Bundled}" if \`runtime\` is "${Runtime.CloudflareWorkersPlatforms}".`,
);
}
if (input.dbDriver != DbDriver.NeonServerless && input.dbDriver != DbDriver.CloudflareHyperdrive) {
throw new Error(
`\`db-driver\` must be "${DbDriver.NeonServerless}" or "${DbDriver.CloudflareHyperdrive}" if \`runtime\` is "${Runtime.CloudflareWorkersPlatforms}".`,
);
}
}
}
if (input.runtime == Runtime.Deno) {
if (input.outputFormat != Format.Native) {
throw new Error(
`\`format\` must be "${Format.Native}" if \`runtime\` is "${Runtime.Deno}".`,
);
if (input.runtime == Runtime.Deno) {
if (input.outputFormat != Format.Native) {
throw new Error(
`\`format\` must be "${Format.Native}" if \`runtime\` is "${Runtime.Deno}".`,
);
}
}
}

await watch({
loadProjectOpts: input,
disableWatch: !input.watch,
async fn(project: Project, signal: AbortSignal) {
await build(project, {
format: input.outputFormat!,
runtime: input.runtime,
dbDriver: input.dbDriver!,
strictSchemas: input.strictSchemas,
skipDenoCheck: false,
sdk: input.sdk ? {} : undefined,
migrate: input.migrate
? {
mode: input.migrateMode,
}
: undefined,
signal,
});
},
});

}
})
await watch({
loadProjectOpts: input,
disableWatch: !input.watch,
async fn(project: Project, signal: AbortSignal) {
await build(project, {
format: input.outputFormat!,
runtime: input.runtime,
dbDriver: input.dbDriver!,
strictSchemas: input.strictSchemas,
skipDenoCheck: false,
sdk: input.sdk ? {} : undefined,
migrate: input.migrate
? {
mode: input.migrateMode,
}
: undefined,
signal,
});
},
});
},
});
13 changes: 6 additions & 7 deletions packages/backend/cli/tasks/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { cleanProject } from "../../toolchain/project/project.ts";
import { runTask } from "../task.ts";

runTask({
inputSchema: globalOptsSchema,
async run(input) {
const project = await initProject(input);
await cleanProject(project);

}
})
inputSchema: globalOptsSchema,
async run(input) {
const project = await initProject(input);
await cleanProject(project);
},
});
25 changes: 11 additions & 14 deletions packages/backend/cli/tasks/config/manifest_path.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { z } from "zod";
import { globalOptsSchema } from "../../common.ts";
import {
computeProjectCachePath,
loadProjectConfigPath,
PROJECT_MANIFEST_PATH,
} from "../../../toolchain/project/mod.ts";
import { loadProjectConfigPath, PROJECT_MANIFEST_PATH } from "../../../toolchain/project/mod.ts";
import { dirname, resolve } from "@std/path";
import { runTask } from "../../task.ts";
import { backendDataDir } from "../../../toolchain/project/project.ts";

export const inputSchema = z.object({}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
// Don't load project since that requires acquiring a lock on the project
inputSchema,
async run(input) {
// Don't load project since that requires acquiring a lock on the project

const projectRoot = dirname(loadProjectConfigPath(input));
const cachePath = await computeProjectCachePath(projectRoot);
const manifestPath = resolve(cachePath, PROJECT_MANIFEST_PATH);
console.log(manifestPath);
}
})
const projectRoot = dirname(loadProjectConfigPath(input));
const dataDir = backendDataDir();
const manifestPath = resolve(dataDir, PROJECT_MANIFEST_PATH);
console.log(manifestPath);
},
});
24 changes: 10 additions & 14 deletions packages/backend/cli/tasks/config/output_manifest_path.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { z } from "zod";
import { globalOptsSchema, initProject } from "../../common.ts";
import { OUTPUT_MANIFEST_PATH } from "../../../toolchain/project/mod.ts";
import {
computeProjectCachePath,
loadProjectConfigPath,
} from "../../../toolchain/project/project.ts";
import { backendDataDir, loadProjectConfigPath } from "../../../toolchain/project/project.ts";
import { dirname, resolve } from "@std/path";
import { runTask } from "../../task.ts";

export const inputSchema = z.object({}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
// Don't load project since that requires acquiring a lock on the project
inputSchema,
async run(input) {
// Don't load project since that requires acquiring a lock on the project

const projectRoot = dirname(loadProjectConfigPath(input));
const cachePath = await computeProjectCachePath(projectRoot);
const manifestPath = resolve(cachePath, OUTPUT_MANIFEST_PATH);
console.log(manifestPath);

}
})
const projectRoot = dirname(loadProjectConfigPath(input));
const dataDir = backendDataDir();
const manifestPath = resolve(dataDir, OUTPUT_MANIFEST_PATH);
console.log(manifestPath);
},
});
15 changes: 7 additions & 8 deletions packages/backend/cli/tasks/config/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import { runTask } from "../../task.ts";
export const inputSchema = z.object({}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
// Don't load project since that requires acquiring a lock on the project
inputSchema,
async run(input) {
// Don't load project since that requires acquiring a lock on the project

const config = await readConfig(loadProjectConfigPath(input));
console.log(JSON.stringify(config, null, "\t"));

}
})
const config = await readConfig(loadProjectConfigPath(input));
console.log(JSON.stringify(config, null, "\t"));
},
});
15 changes: 7 additions & 8 deletions packages/backend/cli/tasks/create/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ export const inputSchema = z.object({
}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
validateIdentifier(input.actor, Casing.Snake);
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
validateIdentifier(input.actor, Casing.Snake);

await templateActor(await initProject(input), input.module, input.actor);

}
})
await templateActor(await initProject(input), input.module, input.actor);
},
});
15 changes: 7 additions & 8 deletions packages/backend/cli/tasks/create/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ export const inputSchema = z.object({
}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {

validateIdentifier(input.module, Casing.Snake);
const project = await initProject(input);
await templateModule(project, opts.module);
}
})
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
const project = await initProject(input);
await templateModule(project, input.module);
},
});
15 changes: 7 additions & 8 deletions packages/backend/cli/tasks/create/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ export const inputSchema = z.object({
}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
validateIdentifier(input.script, Casing.Snake);
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
validateIdentifier(input.script, Casing.Snake);

await templateScript(await initProject(input), input.module, input.script);

}
})
await templateScript(await initProject(input), input.module, input.script);
},
});
16 changes: 7 additions & 9 deletions packages/backend/cli/tasks/create/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ export const inputSchema = z.object({
}).merge(globalOptsSchema);

runTask({
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
validateIdentifier(input.test, Casing.Snake);

await templateTest(await initProject(input), input.module, input.test);

}
})
inputSchema,
async run(input) {
validateIdentifier(input.module, Casing.Snake);
validateIdentifier(input.test, Casing.Snake);

await templateTest(await initProject(input), input.module, input.test);
},
});
Loading

0 comments on commit 4ebdc90

Please sign in to comment.