Skip to content

Commit

Permalink
chore: update generated type paths to work with multiple registries
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Feb 26, 2024
1 parent 40d2252 commit a614355
Show file tree
Hide file tree
Showing 31 changed files with 1,056 additions and 142 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# OGS
dist/
_gen/

# Created by https://www.toptal.com/developers/gitignore/api/deno,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=deno,macos
Expand Down
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,26 @@ deno install --allow-net --allow-read --allow-env --allow-run --allow-write --na

**Setup Dev Environment**

_[TODO:](https://github.com/rivet-gg/open-game-services-engine/issues/84)_

Write this `docker-compose.yaml` to the root of your project:

```yaml
version: '3.1'
services:
postgres:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
```

Then start the project with:

```
ogs dev setup
docker-compose up -d
```

**Start OGS server**
Expand Down Expand Up @@ -56,11 +74,11 @@ ogs create script foo bar
ogs sdk generate
```

SDKs are generated to `dist/sdks/`.
SDKs are generated to `_gen/sdks/`.

**OpenAPI & Postman/Insomnia/Paw**

Explore the APIs by opening `dist/openapi.json` in Postman/Insomnia/Paw.
Explore the APIs by opening `_gen/openapi.json` in Postman/Insomnia/Paw.

## Goals

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"cli:run": "deno run -A --check src/cli/main.ts --path tests/test_project",
"cli:compile": "deno compile --check --allow-net --allow-read --allow-env --allow-run --allow-write --output dist/cli src/cli/main.ts"
"cli:compile": "deno compile --check --allow-net --allow-read --allow-env --allow-run --allow-write --output _gen/cli src/cli/main.ts"
},
"lint": {
"include": ["src/"],
Expand Down
55 changes: 55 additions & 0 deletions deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"npm:@neondatabase/serverless@0.7.2": "npm:@neondatabase/serverless@0.7.2",
"npm:@prisma/adapter-neon": "npm:@prisma/adapter-neon@5.9.1_@neondatabase+serverless@0.7.2",
"npm:@prisma/adapter-neon@5.9.1": "npm:@prisma/adapter-neon@5.9.1_@neondatabase+serverless@0.7.2",
"npm:@prisma/adapter-pg@^5.9.1": "npm:@prisma/adapter-pg@5.9.1_pg@8.11.3",
"npm:@rivet-gg/esbuild-plugin-polyfill-node@^0.4.0": "npm:@rivet-gg/esbuild-plugin-polyfill-node@0.4.0_esbuild@0.20.0",
"npm:@types/node": "npm:@types/node@18.16.19",
"npm:ajv-formats@^2.1.1": "npm:ajv-formats@2.1.1_ajv@8.12.0",
"npm:ajv@^8.12.0": "npm:ajv@8.12.0",
"npm:esbuild@^0.20.0": "npm:esbuild@0.20.0",
"npm:glob@^10.3.10": "npm:glob@10.3.10",
"npm:pg@^8.11.3": "npm:pg@8.11.3",
"npm:typescript-json-schema@^0.62.0": "npm:typescript-json-schema@0.62.0_@types+node@18.16.19_typescript@5.1.6",
"npm:ws": "npm:ws@8.16.0"
},
Expand Down Expand Up @@ -168,6 +170,14 @@
"postgres-array": "postgres-array@3.0.2"
}
},
"@prisma/adapter-pg@5.9.1_pg@8.11.3": {
"integrity": "sha512-0RhnB1sqLmSzilbIQS75YE5qTNotlGvLWhTG7cp+F59VJdQ5sWWXZ1j2W4Tn+AMb116Yp4AuFe0vggW2f+ujiQ==",
"dependencies": {
"@prisma/driver-adapter-utils": "@prisma/driver-adapter-utils@5.9.1",
"pg": "pg@8.11.3",
"postgres-array": "postgres-array@3.0.2"
}
},
"@prisma/driver-adapter-utils@5.9.1": {
"integrity": "sha512-1h1+6kU3PfsE2CvQoU49Jt7yrXybU0C/H/+W8Bh0pJefMAbTY8KoWssYXgwQp1PUKBtdh1GDQsMf9ojFeQ0RWg==",
"dependencies": {
Expand Down Expand Up @@ -280,6 +290,10 @@
"balanced-match": "balanced-match@1.0.2"
}
},
"buffer-writer@2.0.0": {
"integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==",
"dependencies": {}
},
"cliui@8.0.1": {
"integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==",
"dependencies": {
Expand Down Expand Up @@ -476,6 +490,10 @@
"wrappy": "wrappy@1.0.2"
}
},
"packet-reader@1.0.0": {
"integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==",
"dependencies": {}
},
"path-equal@1.2.5": {
"integrity": "sha512-i73IctDr3F2W+bsOWDyyVm/lqsXO47aY9nsFZUjTT/aljSbkxHxxCoyZ9UUrM8jK0JVod+An+rl48RCsvWM+9g==",
"dependencies": {}
Expand All @@ -495,10 +513,24 @@
"minipass": "minipass@7.0.4"
}
},
"pg-cloudflare@1.1.1": {
"integrity": "sha512-xWPagP/4B6BgFO+EKz3JONXv3YDgvkbVrGw2mTo3D6tVDQRh1e7cqVGvyR3BE+eQgAvx1XhW/iEASj4/jCWl3Q==",
"dependencies": {}
},
"pg-connection-string@2.6.2": {
"integrity": "sha512-ch6OwaeaPYcova4kKZ15sbJ2hKb/VP48ZD2gE7i1J+L4MspCtBMAx8nMgz7bksc7IojCIIWuEhHibSMFH8m8oA==",
"dependencies": {}
},
"pg-int8@1.0.1": {
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
"dependencies": {}
},
"pg-pool@3.6.1_pg@8.11.3": {
"integrity": "sha512-jizsIzhkIitxCGfPRzJn1ZdcosIt3pz9Sh3V01fm1vZnbnCMgmGl5wvGGdNN2EL9Rmb0EcFoCkixH4Pu+sP9Og==",
"dependencies": {
"pg": "pg@8.11.3"
}
},
"pg-protocol@1.6.0": {
"integrity": "sha512-M+PDm637OY5WM307051+bsDia5Xej6d9IR4GwJse1qA1DIhiKlksvrneZOYQq42OM+spubpcNYEo2FcKQrDk+Q==",
"dependencies": {}
Expand All @@ -513,6 +545,25 @@
"postgres-interval": "postgres-interval@1.2.0"
}
},
"pg@8.11.3": {
"integrity": "sha512-+9iuvG8QfaaUrrph+kpF24cXkH1YOOUeArRNYIxq1viYHZagBxrTno7cecY1Fa44tJeZvaoG+Djpkc3JwehN5g==",
"dependencies": {
"buffer-writer": "buffer-writer@2.0.0",
"packet-reader": "packet-reader@1.0.0",
"pg-cloudflare": "pg-cloudflare@1.1.1",
"pg-connection-string": "pg-connection-string@2.6.2",
"pg-pool": "pg-pool@3.6.1_pg@8.11.3",
"pg-protocol": "pg-protocol@1.6.0",
"pg-types": "pg-types@2.2.0",
"pgpass": "pgpass@1.0.5"
}
},
"pgpass@1.0.5": {
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
"dependencies": {
"split2": "split2@4.2.0"
}
},
"postgres-array@2.0.0": {
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
"dependencies": {}
Expand Down Expand Up @@ -565,6 +616,10 @@
"integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==",
"dependencies": {}
},
"split2@4.2.0": {
"integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==",
"dependencies": {}
},
"string-width@4.2.3": {
"integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
"dependencies": {
Expand Down
26 changes: 26 additions & 0 deletions src/build/deno_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { join } from "../deps.ts";
import { Project } from "../project/mod.ts";
import { getRuntimePath } from "./ogs_source.ts";

export async function generateDenoConfig(project: Project) {
// Build config
const config = {
"imports": {
"@ogs/runtime": await getRuntimePath(project),
},
"lint": {
"include": ["src/"],
"exclude": ["tests/"],
"rules": {
"exclude": ["no-empty-interface", "no-explicit-any", "require-await"]
}
},
"fmt": {
"useTabs": true
}
};

// Write config
const configPath = join(project.path, "deno.json");
await Deno.writeTextFile(configPath, JSON.stringify(config, null, 4));
}
17 changes: 10 additions & 7 deletions src/build/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { join } from "../deps.ts";
import { Project } from "../project/mod.ts";
import { getRuntimePath } from "./ogs_source.ts";

export async function generateEntrypoint(project: Project) {
const runtimePath = await getRuntimePath(project);

// Generate module configs
let modImports = "";
let modConfig = "{";
Expand All @@ -14,7 +17,7 @@ export async function generateEntrypoint(project: Project) {
const runIdent = `modules$$${mod.name}$$${script.name}$$run`;

modImports +=
`import { run as ${runIdent} } from '../modules/${mod.name}/scripts/${script.name}.ts';\n`;
`import { run as ${runIdent} } from '${mod.path}/scripts/${script.name}.ts';\n`;

modConfig += `${JSON.stringify(script.name)}: {`;
modConfig += `run: ${runIdent},`;
Expand All @@ -30,10 +33,10 @@ export async function generateEntrypoint(project: Project) {

// Generate db config
if (mod.db) {
// HACK: https://github.com/prisma/prisma/issues/2452#issuecomment-1666513809
const prismaImportName = `prisma$$${mod.name}`;
const prismaImportPath = `${mod.path}/_gen/prisma/esm.js`;
modImports +=
`import ${prismaImportName} from "./prisma/${mod.name}/esm.js";\n`;
`import ${prismaImportName} from "${prismaImportPath}";\n`;

modConfig += `db: {`;
modConfig += `name: ${JSON.stringify(mod.db.name)},`;
Expand All @@ -59,14 +62,14 @@ export async function generateEntrypoint(project: Project) {
const configSource = `
// This file is generated by OGS engine
import { Config } from "@ogs/runtime";
import { Config } from "${runtimePath}";
// Create module for Prisma compatability
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
// Import Prisma adapter for Postgres
import pg from "https://deno.land/x/postgres@v0.17.2/mod.ts";
import * as pg from "npm:pg@^8.11.3";
import { PrismaPg } from "npm:@prisma/adapter-pg@^5.9.1";
${modImports}
Expand All @@ -81,7 +84,7 @@ export default {
const entrypointSource = `
// This file is generated by OGS engine
import { Runtime } from "@ogs/runtime";
import { Runtime } from "${runtimePath}";
import config from "./runtime_config.ts";
async function main() {
Expand All @@ -94,7 +97,7 @@ main();
`;

// Write file
const distDir = join(project.path, "dist");
const distDir = join(project.path, "_gen");
const configPath = join(distDir, "runtime_config.ts");
const entrypointPath = join(distDir, "entrypoint.ts");
console.log("Writing entrypoint");
Expand Down
34 changes: 19 additions & 15 deletions src/build/helper.ts → src/build/gen.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { dirname, join, relative } from "../deps.ts";
import {
Module,
moduleDistHelperPath,
moduleGenPath,
Project,
Script,
scriptDistHelperPath,
testDistHelperPath,
scriptGenPath,
testGenPath,
} from "../project/mod.ts";
import { getRuntimePath } from "./ogs_source.ts";

export async function compileScriptHelpers(project: Project) {
for (const module of project.modules.values()) {
Expand All @@ -27,28 +28,30 @@ async function compileModuleHelper(
) {
console.log("Generating module", module.path);

const runtimePath = await getRuntimePath(project);

// Generate source
const source = `
import { ModuleContext as ModuleContextInner } from "@ogs/runtime";
import { ModuleContext as ModuleContextInner } from "${runtimePath}";
${
module.db
? `
import prisma from "../../prisma/${module.name}/esm.js";
import prisma from "./prisma/esm.js";
export { prisma };
export const Prisma = prisma.Prisma;
`
: ""
}
export { RuntimeError } from "@ogs/runtime";
export { RuntimeError } from "${runtimePath}";
export type ModuleContext = ModuleContextInner<${
module.db ? "prisma.PrismaClient" : "undefined"
}>;
`;

// Write source
const helperPath = moduleDistHelperPath(project, module);
const helperPath = moduleGenPath(project, module);
await Deno.mkdir(dirname(helperPath), { recursive: true });
await Deno.writeTextFile(helperPath, source);
}
Expand All @@ -58,12 +61,13 @@ async function compileTestHelper(
module: Module,
) {
console.log("Generating test", module.path);
const runtimePath = await getRuntimePath(project);

// Generate source
const source = `
import * as module from "./mod.ts";
import { Runtime, TestContext as TestContextInner } from "@ogs/runtime";
import config from "../../../dist/runtime_config.ts";
import { Runtime, TestContext as TestContextInner } from "${runtimePath}";
import config from "../../../_gen/runtime_config.ts";
export * from "./mod.ts";
Expand All @@ -79,7 +83,7 @@ export function test(name: string, fn: TestFn) {
`;

// Write source
const helperPath = testDistHelperPath(project, module);
const helperPath = testGenPath(project, module);
await Deno.mkdir(dirname(helperPath), { recursive: true });
await Deno.writeTextFile(helperPath, source);
}
Expand All @@ -90,14 +94,15 @@ async function compileScriptHelper(
script: Script,
) {
console.log("Generating script", script.path);
const runtimePath = await getRuntimePath(project);

// Generate source
const source = `
import * as module from "../mod.ts";
import { ScriptContext as ScriptContextInner } from "@ogs/runtime";
import { ScriptContext as ScriptContextInner } from "${runtimePath}";
${
module.db
? `import { PrismaClient } from "../../../prisma/${module.name}/index.d.ts";`
? `import { PrismaClient } from "../prisma/index.d.ts";`
: ""
}
Expand All @@ -109,16 +114,15 @@ export type ScriptContext = ScriptContextInner<${
`;

// Write source
const helperPath = scriptDistHelperPath(project, module, script);
const helperPath = scriptGenPath(project, module, script);
await Deno.mkdir(dirname(helperPath), { recursive: true });
await Deno.writeTextFile(helperPath, source);
}

async function compileTypeHelpers(project: Project) {
const typedefPath = join(
project.path,
"dist",
"helpers",
"_gen",
"registry.d.ts",
);

Expand Down
Loading

0 comments on commit a614355

Please sign in to comment.