GitRoot
craft your forge, build your project, grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: MIT
4
5import { JSON } from "json-as";
6import { FS } from "./fs";
7import {
8 _commitAll,
9 _commits,
10 _diffWithParent,
11 _forgeConf,
12 _log,
13 _logError,
14 _merge,
15 _modifyCacheContent,
16 _modifyContent,
17 _modifyWebContent,
18 _replaceWebContent,
19} from "./imports";
20import { Commit, CommitStr } from "./model/commit";
21import { ForgeConf } from "./model/forgeconf";
22import { Plugin } from "./model/plugin";
23import { PluginRun } from "./model/pluginrun";
24import { Server } from "./model/server";
25
26export * from "./model/index";
27
28export let __server: server | null = null;
29
30export const Register = function (
31 run: Array<PluginRun>,
32 p: (server: Server) => Plugin
33): void {
34 const s = new server();
35 s.plugin = p(s);
36 s.run = run;
37 __server = s;
38};
39
40class server implements Server {
41 private atLeastOneChange: bool = true;
42 private worktreeFS: FS = new FS("worktree");
43 private webcontentFS: FS = new FS("webcontent");
44 private cacheFS: FS = new FS("cache");
45
46 plugin: Plugin | null = null;
47 run: Array<PluginRun> = [];
48
49 forgeConf(): ForgeConf {
50 const ptrSize = _forgeConf();
51 const ptr = u32((ptrSize >> 32) & 0xffffffff);
52 const size = u32(ptrSize & 0xffffffff);
53 return JSON.parse<ForgeConf>(String.UTF8.decodeUnsafe(ptr, size));
54 }
55
56 worktree(): FS {
57 return this.worktreeFS;
58 }
59
60 webcontent(): FS {
61 return this.webcontentFS;
62 }
63
64 cache(): FS {
65 return this.cacheFS;
66 }
67
68 modifyContent(filepath: string, content: string): void {
69 this.atLeastOneChange = true;
70 _modifyContent(filepath, content);
71 }
72
73 modifyWebContent(filepath: string, content: string): void {
74 _modifyWebContent(filepath, content);
75 }
76
77 replaceWebContent(
78 filepath: string,
79 oldContent: string,
80 content: string
81 ): void {
82 _replaceWebContent(filepath, oldContent, content);
83 }
84
85 modifyCacheContent(filepath: string, content: string): void {
86 _modifyCacheContent(filepath, content);
87 }
88
89 commitAllIfNeeded(message: string): void {
90 if (this.atLeastOneChange) {
91 _commitAll(message);
92 }
93 }
94
95 diffWithParent(
96 hash: string,
97 oldFilepath: string,
98 newFilepath: string
99 ): string {
100 const ptrSize = _diffWithParent(hash, oldFilepath, newFilepath);
101 const ptr = u32((ptrSize >> 32) & 0xffffffff);
102 const size = u32(ptrSize & 0xffffffff);
103 return String.UTF8.decodeUnsafe(ptr, size);
104 }
105
106 merge(from: string, to: string): void {
107 _merge(from, to);
108 }
109
110 log(message: string): void {
111 _log(message);
112 }
113
114 logError(message: string, error: string): void {
115 _logError(message, error);
116 }
117
118 commits(from: string, to: string): Array<Commit> {
119 const ptrSize = _commits(from, to);
120 const ptr = u32((ptrSize >> 32) & 0xffffffff);
121 const size = u32(ptrSize & 0xffffffff);
122 const commits = JSON.parse<Array<CommitStr>>(
123 String.UTF8.decodeUnsafe(ptr, size)
124 );
125 return commits.map((c) => c.toCommit());
126 }
127}