Skip to content

Commit

Permalink
chore: test templating e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 7, 2024
1 parent d1d10d5 commit 6c0d49d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/build/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { shutdownAllPools } from "../utils/worker_pool.ts";
import { migrateDev } from "../migrate/dev.ts";
import { compileModuleTypeHelper } from "./gen.ts";
import { migrateDeploy } from "../migrate/deploy.ts";
import { ensurePostgresRunning } from "../utils/postgres_daemon.ts";

/**
* Which format to use for building.
Expand Down Expand Up @@ -187,6 +188,9 @@ async function waitForBuildPromises(buildState: BuildState): Promise<void> {
export async function build(project: Project, opts: BuildOpts) {
const buildCachePath = resolve(project.path, "_gen", "cache.json");

// Required for `migrateDev` and `migrateDeploy`
await ensurePostgresRunning(project);

// Read hashes from file
let oldCache: BuildCachePersist;
if (await exists(buildCachePath)) {
Expand Down
2 changes: 2 additions & 0 deletions src/template/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import dedent from "npm:dedent@^1.5.1";
export { dedent };
40 changes: 31 additions & 9 deletions src/template/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { resolve } from "../deps.ts";
import { ModuleConfig } from "../config/module.ts";
import { resolve, stringify } from "../deps.ts";
import { Project } from "../project/mod.ts";
import { dedent } from "./deps.ts";

export async function templateModule(project: Project, moduleName: string) {
if (project.modules.has(moduleName)) {
Expand All @@ -12,16 +14,36 @@ export async function templateModule(project: Project, moduleName: string) {
await Deno.mkdir(resolve(modulePath, "scripts"));
await Deno.mkdir(resolve(modulePath, "tests"));
await Deno.mkdir(resolve(modulePath, "db"));
await Deno.mkdir(resolve(modulePath, "db", "migrations"));
await Deno.mkdir(resolve(modulePath, "schema"));

// Write default config
const moduleYaml = `scripts: {}
errors: {}
`;
await Deno.writeTextFile(resolve(modulePath, "module.yaml"), moduleYaml);
const defaultModule: ModuleConfig = {
scripts: {},
errors: {},
};
await Deno.writeTextFile(
resolve(modulePath, "module.yaml"),
stringify(defaultModule),
);

// TODO: Write default schema
// Write default migration
const defaultSchema = dedent`
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
`;
await Deno.writeTextFile(
resolve(modulePath, "db", "schema.prisma"),
defaultSchema,
);

// TODO: Add to backend.yaml
// Add to backend.yaml
const newConfig = structuredClone(project.config);
newConfig.modules[moduleName] = {
registry: "local",
};
await Deno.writeTextFile(
resolve(project.path, "backend.yaml"),
stringify(newConfig),
);
}
22 changes: 22 additions & 0 deletions src/template/template_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DbDriver } from "../build/mod.ts";
import { build, Format, Runtime } from "../build/mod.ts";
import { loadProject } from "../project/mod.ts";
import { templateModule } from "./module.ts";
import { templateProject } from "./project.ts";
import { templateScript } from "./script.ts";

Deno.test("e2e", async () => {
const path = await Deno.makeTempDir();

await templateProject(path);

await templateModule(await loadProject({ path }), "module_a");

await templateScript(await loadProject({ path }), "module_a", "script_a");

await build(await loadProject({ path }), {
format: Format.Native,
runtime: Runtime.Deno,
dbDriver: DbDriver.NodePostgres,
});
});

0 comments on commit 6c0d49d

Please sign in to comment.