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 nightly
25////////////////////////////////////////////
26
27const oldArtifact = versions[version];
28let removeOlderArtifact = version === Deno.env.get("VERSION");
29
30if (
31 oldArtifact && !removeOlderArtifact
32) {
33 console.log(
34 "✅️ Version already present as artifact:",
35 versions[version].cid,
36 );
37 Deno.exit();
38}
39
40if (oldArtifact && removeOlderArtifact) {
41 const oldCid = oldArtifact.cid;
42 delete versions[version];
43
44 const cidStillReferenced = Object.values(versions).some((a) =>
45 a.cid === oldCid
46 );
47
48 if (!cidStillReferenced) {
49 delete artifacts[oldCid];
50 Deno.removeSync(Path.join("artifacts", oldCid), { recursive: true });
51 console.log("💀 Removed artifact:", oldCid);
52 }
53}
54
55////////////////////////////////////////////
56// BUILD DIFFUSE
57////////////////////////////////////////////
58
59Deno.chdir("diffuse");
60
61execSync("rm", "-rf", ".env");
62execSync("deno", "run", "build");
63
64Deno.chdir("..");
65
66const BUILD = Path.join("diffuse", "dist");
67
68////////////////////////////////////////////
69// TEMP DIR
70////////////////////////////////////////////
71
72const TMP_DIR = Deno.makeTempDirSync();
73const TMP_CAR = Path.join(TMP_DIR, "diffuse.car");
74
75////////////////////////////////////////////
76// MAKE CAR
77////////////////////////////////////////////
78
79const carCmd = execSync(
80 "deno",
81 "run",
82 "-A",
83 "ipfs-car",
84 "pack",
85 BUILD,
86 "--no-wrap",
87 "--output",
88 TMP_CAR,
89);
90
91const cid = utf8String(carCmd.stdout).trim();
92
93Deno.removeSync(TMP_DIR, { recursive: true });
94
95if (!cid) throw new Error("CID result missing");
96console.log("💎 Artifact CID:", cid);
97
98////////////////////////////////////////////
99// CHECK IF ARTIFACT ALREADY EXISTS
100////////////////////////////////////////////
101
102if (artifacts[cid]) {
103 console.log(
104 "✅️ Artifact already present as version:",
105 artifacts[cid].version,
106 );
107 Deno.exit();
108}
109
110////////////////////////////////////////////
111// ADD ARTIFACT
112////////////////////////////////////////////
113
114Fs.copySync(BUILD, Path.join("artifacts", cid), { overwrite: true });
115
116////////////////////////////////////////////
117// ADD TO INDEXES
118////////////////////////////////////////////
119
120const artifact: Artifact = {
121 cid,
122 createdAt: new Date().toISOString(),
123 version,
124};
125
126artifacts[cid] = artifact;
127versions[version] = artifact;
128
129Deno.writeTextFileSync(
130 "artifacts/artifacts.json",
131 JSON.stringify(artifacts, null, 2),
132);
133
134Deno.writeTextFileSync(
135 "artifacts/versions.json",
136 JSON.stringify(versions, null, 2),
137);