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

Commit

Permalink
chore: preserve generated files in sdk output
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Oct 2, 2024
1 parent d8547b0 commit 4250c20
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions packages/backend/toolchain/sdk/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exists, move } from "@std/fs";
import { resolve } from "@std/path";
import { CommandError, UnreachableError } from "../error/mod.ts";
import { UnreachableError } from "../error/mod.ts";
import { Project } from "../project/mod.ts";
import { projectDataPath, SDK_PATH } from "../project/project.ts";
import { progress, success } from "../term/status.ts";
Expand All @@ -9,23 +9,20 @@ import { generateTypescript } from "./typescript/mod.ts";
import { generateUnity } from "./unity/mod.ts";
import { generateGodot } from "./godot/mod.ts";
import { SdkConfig } from "../config/project.ts";
import { unimplemented } from "@std/assert";

export enum SdkTarget {
TypeScript = "typescript",
Unity = "unity",
Godot = "godot",
}

interface Generator {
}

export async function generateSdk(
project: Project,
sdk: SdkConfig,
) {
const targetString = targetToString(sdk.target);
const sdkGenPath = resolve(projectDataPath(project, SDK_PATH), targetString);
const sdkOutput = resolve(Deno.cwd(), sdk.output);

// Clear artifacts
try {
Expand All @@ -38,21 +35,52 @@ export async function generateSdk(

progress("Building SDK", targetString);

let sdkCopyPath = sdkGenPath;
// Generate files
//
// preservePaths is used for preserving generated artifacts from things like
// `npm install`.
let preservePaths: string[];
if (sdk.target == SdkTarget.TypeScript) {
preservePaths = ["dist"];
await generateTypescript(project, sdkGenPath);
} else if (sdk.target == SdkTarget.Unity) {
preservePaths = [];
await generateUnity(project, sdkGenPath);
} else if (sdk.target == SdkTarget.Godot) {
preservePaths = [];
await generateGodot(project, sdkGenPath);
} else {
throw new UnreachableError(sdk.target);
}

if (await exists(sdk.output, { isDirectory: true })) {
await Deno.remove(sdk.output, { recursive: true });
// Delete target dir
const preserveTempDir = await Deno.makeTempDir();
if (await exists(sdkOutput, { isDirectory: true })) {
// Preserve files before deleting the output path
for (const path of preservePaths) {
const srcPreservePath = resolve(sdkOutput, path);
const dstPreservePath = resolve(preserveTempDir, path);
if (await exists(srcPreservePath)) {
await move(srcPreservePath, dstPreservePath);
} else {
}
}

// Remove output
await Deno.remove(sdkOutput, { recursive: true });
}
await move(sdkCopyPath, sdk.output, { overwrite: true });

// Move generated SDK
await move(sdkGenPath, sdkOutput, { overwrite: true });

// Move back preserved files
for (const path of preservePaths) {
const srcPreservePath = resolve(preserveTempDir, path);
const dstPreservePath = resolve(sdkOutput, path);
if (await exists(srcPreservePath, { isDirectory: true })) {
await move(srcPreservePath, dstPreservePath, { overwrite: false });
}
}

success("Success");
}
Expand Down

0 comments on commit 4250c20

Please sign in to comment.