Skip to content

Commit 0932a3c

Browse files
committed
chore: complete build script
1 parent 48ae279 commit 0932a3c

File tree

2 files changed

+99
-30
lines changed

2 files changed

+99
-30
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.godot/
33
exports/
44
bin/
5+
dist/
56

6-
.env
7+
.env

build.ts

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,110 @@
1-
#!/usr/bin/env -S deno run --allow-read --allow-write
1+
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run
22
// vi: ft=typescript
33

4-
import { zip } from "jsr:@deno-library/compress";
4+
//** * This script zips the 2D and 3D starter files by copying their contents to a temporary directory
5+
// * and then creating a zip file using the external zip command.
56
import { join } from "jsr:@std/path";
67
import { ensureDir } from "jsr:@std/fs";
78
import { walk } from "jsr:@std/fs/walk";
89

910
const dirs = {
10-
project2d: new URL("./starter-files/2d-project-start", import.meta.url).pathname,
11-
project3d: new URL("./starter-files/3d-project-assets", import.meta.url).pathname,
12-
temp2D: await Deno.makeTempDir({ prefix: '2d' }),
13-
temp3D: await Deno.makeTempDir({ prefix: '3d' }),
14-
out: new URL("./dist", import.meta.url).pathname,
15-
}
11+
project2dStart:
12+
new URL("./starter-files/2d-project-start", import.meta.url).pathname,
13+
project3dStart:
14+
new URL("./starter-files/3d-project-assets", import.meta.url).pathname,
15+
project2DCompleted:
16+
new URL("./2d-project-completed", import.meta.url).pathname,
17+
project3DCompleted:
18+
new URL("./3d-project-completed", import.meta.url).pathname,
19+
out: new URL("./dist", import.meta.url).pathname,
20+
};
1621

22+
//** * This function copies a directory's contents to a temporary location and creates a zip file, using the unix `zip` command.
23+
// * @param dir - The directory to zip.
24+
// * @param outputZipPath - The path where the zip file will be saved.
25+
// * @param skip - An array of regexes to skip files that match.
26+
// * @returns The path to the zip file.
27+
const createProjectZip = async (
28+
dir: string,
29+
outputZipPath: string,
30+
skip: RegExp[] = [],
31+
) => {
32+
console.log(`Processing directory: ${dir}`);
33+
const tempDir = await Deno.makeTempDir({ prefix: "project-zip-" });
1734

18-
await Deno.remove(dirs.out, { recursive: true });
19-
await ensureDir(dirs.out);
35+
// Copy and count files to zip to temporary directory
36+
let fileCount = 0;
37+
for await (
38+
const dirEntry of walk(dir, {
39+
skip: skip,
40+
})
41+
) {
42+
if (dirEntry.isFile) {
43+
const relativePath = dirEntry.path.replace(dir, "");
44+
const destPath = join(tempDir, relativePath);
45+
await ensureDir(join(destPath, ".."));
46+
await Deno.copyFile(dirEntry.path, destPath);
47+
fileCount += 1;
48+
}
49+
}
2050

51+
console.log(`Copied ${fileCount} files to temporary directory`);
2152

22-
const zipDirectory = async (dir: string, outDir: string, skip?: RegExp) => {
23-
for await (const dirEntry of walk(dir)) {
24-
if (dirEntry.isFile) {
25-
const relativePath = dirEntry.path.replace(dir, "");
26-
if (skip && skip.test(relativePath)) {
27-
continue;
28-
}
29-
const destPath = join(outDir, relativePath);
30-
await ensureDir(new URL("..", destPath));
31-
await Deno.copyFile(dirEntry.path, destPath);
32-
}
33-
}
34-
const tmpFileName = await Deno.makeTempFile({ prefix: 'zip' });
35-
await zip.compress(outDir, tmpFileName, { excludeSrc: true })
36-
return tmpFileName;
53+
const zipCommand = new Deno.Command("zip", {
54+
args: ["-r", outputZipPath, "."],
55+
cwd: tempDir,
56+
});
57+
const output = await zipCommand.output();
58+
if (!output.success) {
59+
throw new Error(
60+
`Failed to create zip file ${outputZipPath}: ${output.stderr}`,
61+
);
62+
}
63+
64+
await Deno.remove(tempDir, { recursive: true });
65+
console.log(`Created zip file at: ${outputZipPath}`);
66+
return outputZipPath;
67+
};
68+
69+
const outDirExists = await Deno.stat(dirs.out).then(() => true).catch(() =>
70+
false
71+
);
72+
if (outDirExists) {
73+
await Deno.remove(dirs.out, { recursive: true });
3774
}
75+
await ensureDir(dirs.out);
76+
77+
// Zip the starter files, excluding .import and project.godot files
78+
const output2DPath = join(dirs.out, "2d-project-assets.zip");
79+
const output3DPath = join(dirs.out, "3d-project-assets.zip");
80+
await createProjectZip(
81+
dirs.project2dStart,
82+
output2DPath,
83+
[/\.(import)/, /project\.godot/],
84+
);
85+
console.log(`2D project zip created at: ${output2DPath}`);
86+
await createProjectZip(
87+
dirs.project3dStart,
88+
output3DPath,
89+
[/\.(import)/, /project\.godot/],
90+
);
91+
console.log(`3D project zip created at: ${output3DPath}`);
92+
93+
// Now create completed project zips, only exclude .godot/ cache folders
94+
const output2DCompletedPath = join(dirs.out, "2d-project-completed.zip");
95+
const output3DCompletedPath = join(dirs.out, "3d-project-completed.zip");
96+
await createProjectZip(
97+
dirs.project2DCompleted,
98+
output2DCompletedPath,
99+
[/\.godot\//],
100+
);
101+
console.log(`2D completed project zip created at: ${output2DCompletedPath}`);
102+
103+
await createProjectZip(
104+
dirs.project3DCompleted,
105+
output3DCompletedPath,
106+
[/\.godot\//],
107+
);
108+
console.log(`3D completed project zip created at: ${output3DCompletedPath}`);
38109

39-
const zip2D = await zipDirectory(dirs.project2d, dirs.temp2D);
40-
await Deno.copyFile(zip2D, join(dirs.out, "2d-project.zip"));
41-
const zip3D = await zipDirectory(dirs.project3d, dirs.temp3D, /(\.import)|project\.godot/);
42-
await Deno.copyFile(zip3D, join(dirs.out, "3d-project.zip"));
110+
console.log("Build completed successfully!");

0 commit comments

Comments
 (0)