|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 3 | + |
| 4 | +import { exec as _exec } from "child_process"; |
| 5 | +import { mkdir, readFile, rm, writeFile } from "fs/promises"; |
| 6 | +import { basename, join, relative } from "path"; |
| 7 | +import { promisify } from "util"; |
| 8 | + |
| 9 | +const exec = promisify(_exec); |
| 10 | +const header = `// Code generated by ${basename(import.meta.url)}. DO NOT EDIT.`; |
| 11 | +const testsDir = "tests"; |
| 12 | + |
| 13 | +/** |
| 14 | + * @param {{ target: "nexus" | "pothos", proto: { package: String, lib: String } } opts |
| 15 | + */ |
| 16 | +function getTestPath(opts) { |
| 17 | + return `${testsDir}/${[opts.target, opts.proto.package.replace("/", "-"), opts.proto.lib].join("--")}`; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {{ target: "nexus" | "pothos", proto: { package: String, lib: String } } opts |
| 22 | + */ |
| 23 | +async function setupTest(opts) { |
| 24 | + const path = getTestPath(opts); |
| 25 | + const genDir = join(path, "__generated__"); |
| 26 | + const tsconfigPath = join(path, "tsconfig.json"); |
| 27 | + const schemaPath = join(path, "schema.ts"); |
| 28 | + |
| 29 | + await rm(genDir, { recursive: true, force: true }); |
| 30 | + await mkdir(genDir, { recursive: true }); |
| 31 | + |
| 32 | + const tsconfig = { |
| 33 | + extends: "../../tsconfig.base.json", |
| 34 | + compilerOptions: { |
| 35 | + baseUrl: ".", |
| 36 | + paths: { |
| 37 | + "@/*": ["./*"], |
| 38 | + }, |
| 39 | + }, |
| 40 | + include: [ |
| 41 | + ".", |
| 42 | + relative(path, "./src"), |
| 43 | + relative( |
| 44 | + path, |
| 45 | + [testsDir, "__generated__", opts.target, opts.proto.lib, "testapis", ...opts.proto.package.split("/")].join("/") |
| 46 | + ), |
| 47 | + ].map((p) => `${p}/**/*`), |
| 48 | + }; |
| 49 | + |
| 50 | + await writeFile(tsconfigPath, `${header}\n${JSON.stringify(tsconfig, undefined, 2)}`, "utf-8"); |
| 51 | + |
| 52 | + await exec(`yarn ts-node --transpile-only --require tsconfig-paths/register --project ${tsconfigPath} ${schemaPath}`); |
| 53 | +} |
| 54 | + |
| 55 | +async function setupTests() { |
| 56 | + /** @type {{ tests: { target: "nexus" | "pothos", proto: { package: String, lib: String} }[] } */ |
| 57 | + const config = JSON.parse(await readFile("tests.json", "utf-8")); |
| 58 | + |
| 59 | + await Promise.all(config.tests.map(setupTest)); |
| 60 | + await writeFile( |
| 61 | + "tsconfig.json", |
| 62 | + `${header}\n${JSON.stringify( |
| 63 | + { |
| 64 | + files: [], |
| 65 | + references: config.tests.map((tt) => ({ path: getTestPath(tt) })), |
| 66 | + }, |
| 67 | + undefined, |
| 68 | + 2 |
| 69 | + )}`, |
| 70 | + "utf-8" |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +async function setupProtoNexus() { |
| 75 | + await rm(join(testsDir, "__generated__"), { recursive: true, force: true }); |
| 76 | + await exec(`buf generate ${join("..", "packages", "@testapis", "proto", "src")}`); |
| 77 | +} |
| 78 | + |
| 79 | +async function main() { |
| 80 | + await setupProtoNexus(); |
| 81 | + await setupTests(); |
| 82 | +} |
| 83 | + |
| 84 | +await main(); |
0 commit comments