Content-addressed version deploy system for Diffuse
elements.diffuse.sh
1import * as Fs from "@std/fs";
2import * as Path from "@std/path";
3
4import type { Artifact } from "../common/types.d.ts";
5import { diffuseVersion } from "../common/diffuse-version.ts";
6import { execSync, utf8String } from "../common/index.ts";
7
8import artifactsJson from "../artifacts/artifacts.json" with { type: "json" };
9import versionsJson from "../artifacts/versions.json" with { type: "json" };
10
11const artifacts: Record<string, Artifact> = artifactsJson as any;
12const versions: Record<string, Artifact> = versionsJson as any;
13
14////////////////////////////////////////////
15// ESTABLISH VERSION
16////////////////////////////////////////////
17
18let version = diffuseVersion();
19
20console.log("🎵 Diffuse version:", version);
21
22////////////////////////////////////////////
23// CHECK IF VERSION ALREADY EXISTS
24// Unless version ends with `-alpha` or `-beta`
25////////////////////////////////////////////
26
27if (
28 versions[version] &&
29 !(version.endsWith("-alpha") || version.endsWith("-beta"))
30) {
31 console.log(
32 "✅️ Version already present as artifact:",
33 versions[version].cid,
34 );
35 Deno.exit();
36}
37
38////////////////////////////////////////////
39// BUILD DIFFUSE
40////////////////////////////////////////////
41
42Deno.chdir("diffuse");
43
44execSync("rm", "-rf", ".env");
45execSync("deno", "run", "build");
46
47Deno.chdir("..");
48
49const BUILD = Path.join("diffuse", "dist");
50
51////////////////////////////////////////////
52// TEMP DIR
53////////////////////////////////////////////
54
55const TMP_DIR = Deno.makeTempDirSync();
56const TMP_CAR = Path.join(TMP_DIR, "diffuse.car");
57
58////////////////////////////////////////////
59// MAKE CAR
60////////////////////////////////////////////
61
62const carCmd = execSync(
63 "deno",
64 "run",
65 "-A",
66 "ipfs-car",
67 "pack",
68 BUILD,
69 "--no-wrap",
70 "--output",
71 TMP_CAR,
72);
73
74const cid = utf8String(carCmd.stdout).trim();
75
76Deno.removeSync(TMP_DIR, { recursive: true });
77
78if (!cid) throw new Error("CID result missing");
79console.log("💎 Artifact CID:", cid);
80
81////////////////////////////////////////////
82// CHECK IF ARTIFACT ALREADY EXISTS
83////////////////////////////////////////////
84
85if (artifacts[cid]) {
86 console.log(
87 "✅️ Artifact already present as version:",
88 artifacts[cid].version,
89 );
90 Deno.exit();
91}
92
93////////////////////////////////////////////
94// ADD ARTIFACT
95////////////////////////////////////////////
96
97Fs.copySync(BUILD, Path.join("artifacts", cid), { overwrite: true });
98
99////////////////////////////////////////////
100// ADD TO INDEXES
101////////////////////////////////////////////
102
103const artifact: Artifact = {
104 cid,
105 createdAt: new Date().toISOString(),
106 version,
107};
108
109artifacts[cid] = artifact;
110versions[version] = artifact;
111
112Deno.writeTextFileSync(
113 "artifacts/artifacts.json",
114 JSON.stringify(artifacts, null, 2),
115);
116
117Deno.writeTextFileSync(
118 "artifacts/versions.json",
119 JSON.stringify(versions, null, 2),
120);