Skip to content

Commit

Permalink
chore: remove uses of __dirname and __filename (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 4, 2024
2 parents ef563cc + e08483b commit 5d6f66f
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 107 deletions.
8 changes: 5 additions & 3 deletions src/build/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export async function generateEntrypoint(project: Project) {
if (mod.db) {
const prismaImportName = `prisma$$${mod.name}`;
const prismaImportPath = `${mod.path}/_gen/prisma/esm.js`;
modImports +=
`import ${prismaImportName} from "${prismaImportPath}";\n`;
modImports += `import ${prismaImportName} from "${prismaImportPath}";\n`;

modConfig += `db: {`;
modConfig += `name: ${JSON.stringify(mod.db.name)},`;
Expand Down Expand Up @@ -104,7 +103,10 @@ main();
await Deno.mkdir(distDir, { recursive: true });
await Deno.writeTextFile(configPath, configSource);
await Deno.writeTextFile(entrypointPath, entrypointSource);
await Deno.writeTextFile(join(distDir, ".gitignore"), ".");
await Deno.writeTextFile(
join(distDir, ".gitignore"),
".",
);

// Format files
const { success } = await new Deno.Command("deno", {
Expand Down
59 changes: 33 additions & 26 deletions src/build/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ async function compileModuleHelper(
// Generate source
const dbImports = [
'import prisma from "./prisma/esm.js";',
'export { prisma };',
'export const Prisma = prisma.Prisma;',
"export { prisma };",
"export const Prisma = prisma.Prisma;",
];
const source = [
`import { ModuleContext as ModuleContextInner } from "${runtimePath}";`,
`import { Registry as RegistryTypeInner } from "${project.path}/_gen/registry.d.ts";`,
'',
"",
...(module.db ? dbImports : []),
'',
"",
`export { RuntimeError } from "${runtimePath}";`,
`export type ModuleContext = ModuleContextInner<RegistryTypeInner, ${module.db ? "prisma.PrismaClient" : "undefined"}>;`,
'',
`export type ModuleContext = ModuleContextInner<RegistryTypeInner, ${
module.db ? "prisma.PrismaClient" : "undefined"
}>;`,
"",
].join("\n");

// Write source
Expand All @@ -65,20 +67,21 @@ async function compileTestHelper(
`import { Runtime, TestContext as TestContextInner } from "${runtimePath}";`,
`import { Registry as RegistryTypeInner } from "${project.path}/_gen/registry.d.ts";`,
`import config from "${project.path}/_gen/runtime_config.ts";`,
'',
"",
'export * from "./mod.ts";',
'',
`export type TestContext = TestContextInner<RegistryTypeInner, ${module.db ? "module.prisma.PrismaClient" : "undefined"}>;`,
'',
'export type TestFn = (ctx: TestContext) => Promise<void>;',
'',
'export function test(name: string, fn: TestFn) {',
"",
`export type TestContext = TestContextInner<RegistryTypeInner, ${
module.db ? "module.prisma.PrismaClient" : "undefined"
}>;`,
"",
"export type TestFn = (ctx: TestContext) => Promise<void>;",
"",
"export function test(name: string, fn: TestFn) {",
` Runtime.test(config, "${module.name}", name, fn);`,
'}',
'',
"}",
"",
].join("\n");


// Write source
const helperPath = testGenPath(project, module);
await Deno.mkdir(dirname(helperPath), { recursive: true });
Expand All @@ -96,14 +99,18 @@ async function compileScriptHelper(
const source = [
'import * as module from "../mod.ts";',
`import { ScriptContext as ScriptContextInner } from "${runtimePath}";`,
`import { Registry as RegistryTypeInner } from "${join(project.path, "_gen", "registry.d.ts")}";`,
'',
module.db ? 'import { PrismaClient } from "../prisma/index.d.ts";' : '', // NOTE: This is not used anywhere
'',
`import { Registry as RegistryTypeInner } from "${
join(project.path, "_gen", "registry.d.ts")
}";`,
"",
module.db ? 'import { PrismaClient } from "../prisma/index.d.ts";' : "", // NOTE: This is not used anywhere
"",
'export * from "../mod.ts";',
'',
`export type ScriptContext = ScriptContextInner<RegistryTypeInner, ${module.db ? "module.prisma.PrismaClient" : "undefined"}>;`,
'',
"",
`export type ScriptContext = ScriptContextInner<RegistryTypeInner, ${
module.db ? "module.prisma.PrismaClient" : "undefined"
}>;`,
"",
].join("\n");

// Write source
Expand Down Expand Up @@ -156,10 +163,10 @@ async function compileTypeHelpers(project: Project) {
}

const moduleComment = [
'//',
"//",
`// Types for ${module.name}`,
'//',
'',
"//",
"",
`interface ${moduleInterfaceName} {}`,
].join("\n");

Expand Down
8 changes: 4 additions & 4 deletions src/build/ogs_source.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { dirname, fromFileUrl, join } from "../deps.ts";
import { join } from "../deps.ts";
import { Project } from "../project/project.ts";

const __dirname = dirname(fromFileUrl(import.meta.url));

export async function getRuntimePath(_project: Project): Promise<string> {
const dirname = import.meta.dirname;
if (!dirname) throw new Error("Missing dirname");
// TODO: https://github.com/rivet-gg/open-game-services-engine/issues/81
return join(__dirname, "..", "runtime", "mod.ts");
return join(dirname, "..", "runtime", "mod.ts");
}
9 changes: 5 additions & 4 deletions src/config/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Ajv, fileURLToPath, join, parse, tjs } from "../deps.ts";

const __filename = fileURLToPath(import.meta.url);
import { Ajv, join, parse, tjs } from "../deps.ts";

export interface ModuleConfig extends Record<string, unknown> {
status?: "preview" | "beta" | "stable" | "deprecated";
Expand Down Expand Up @@ -61,6 +59,9 @@ export async function readConfig(modulePath: string): Promise<ModuleConfig> {
function generateModuleConfigJsonSchema(): tjs.Definition {
console.log("Generating registry.ts schema");

const filename = import.meta.filename;
if (!filename) throw new Error("Missing filename");

// https://docs.deno.com/runtime/manual/advanced/typescript/configuration#what-an-implied-tsconfigjson-looks-like
const DEFAULT_COMPILER_OPTIONS = {
"allowJs": true,
Expand All @@ -79,7 +80,7 @@ function generateModuleConfigJsonSchema(): tjs.Definition {
"allowImportingTsExtensions": true,
};

const schemaFiles = [__filename];
const schemaFiles = [filename];

const program = tjs.getProgramFromFiles(
schemaFiles,
Expand Down
9 changes: 5 additions & 4 deletions src/config/project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { parse, join, Ajv, tjs, fileURLToPath } from "../deps.ts";

const __filename = fileURLToPath(import.meta.url);
import { parse, join, Ajv, tjs } from "../deps.ts";

export interface ProjectConfig extends Record<string, unknown> {
registries: { [name: string]: RegistryConfig };
Expand Down Expand Up @@ -71,6 +69,9 @@ export async function readConfig(projectPath: string): Promise<ProjectConfig> {
function generateProjectConfigJsonSchema(): tjs.Definition {
console.log("Generating registry.ts schema");

const filename = import.meta.filename;
if (!filename) throw new Error("Missing filename");

// https://docs.deno.com/runtime/manual/advanced/typescript/configuration#what-an-implied-tsconfigjson-looks-like
const DEFAULT_COMPILER_OPTIONS = {
"allowJs": true,
Expand All @@ -89,7 +90,7 @@ function generateProjectConfigJsonSchema(): tjs.Definition {
"allowImportingTsExtensions": true,
};

const schemaFiles = [__filename];
const schemaFiles = [filename];

const program = tjs.getProgramFromFiles(
schemaFiles,
Expand Down
12 changes: 7 additions & 5 deletions src/migrate/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { buildPrismaPackage } from "./build_prisma_esm.ts";
import { Project } from "../project/mod.ts";
import { forEachPrismaSchema } from "./mod.ts";

export async function migrateDev(project: Project) {
export interface MigrateDevOpts {
createOnly: boolean;
}

export async function migrateDev(project: Project, opts: MigrateDevOpts) {
await forEachPrismaSchema(
project,
async ({ databaseUrl, module, tempDir, generatedClientDir }) => {
const createOnly = Deno.args.includes("--create-only");

// Generate migrations & client
console.log("Generating migrations");
const status = await new Deno.Command("deno", {
Expand All @@ -22,7 +24,7 @@ export async function migrateDev(project: Project) {
"npm:prisma@5.9.1",
"migrate",
"dev",
...(createOnly ? ["--create-only"] : []),
...(opts.createOnly ? ["--create-only"] : []),
],
cwd: tempDir,
stdin: "inherit",
Expand All @@ -37,7 +39,7 @@ export async function migrateDev(project: Project) {
throw new Error("Failed to generate migrations");
}

if (!createOnly) {
if (!opts.createOnly) {
// Specify the path to the library & binary types
await (async () => {
for (
Expand Down
2 changes: 0 additions & 2 deletions src/migrate/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export async function forEachDatabase(
project: Project,
callback: ForEachDatabaseCallback,
) {
const dbFilter = Deno.args[0];

// Setup database
const defaultDatabaseUrl = Deno.env.get("DATABASE_URL") ??
"postgres://postgres:password@localhost:5432/postgres";
Expand Down
3 changes: 0 additions & 3 deletions src/sdk/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { initProject } from "../cli/common.ts";
import { join, dirname, fromFileUrl } from "../deps.ts";

const __dirname = dirname(fromFileUrl(import.meta.url));

export async function generate(path?: string) {
interface Generator {
Expand Down
54 changes: 0 additions & 54 deletions tests/test_project/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_project/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"imports": {
"@ogs/runtime": "/Users/nathan/rivet/ogs-engine/src/runtime/mod.ts"
"@ogs/runtime": "/Users/nathan/rivet/opengb/src/runtime/mod.ts"
},
"lint": {
"include": [
Expand Down
2 changes: 1 addition & 1 deletion tests/test_project/ogs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ registries:
# git: https://github.com/rivet-gg/open-game-services.git
# branch: main
default:
directory: ../../../ogs/modules
directory: ../../../opengb-registry/modules
local:
directory: ./modules
modules:
Expand Down

0 comments on commit 5d6f66f

Please sign in to comment.