Skip to content

Commit

Permalink
Start WebFromGit and Orizuru Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryCraft committed May 29, 2024
1 parent a0b3b72 commit ef31f30
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/example.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module.exports = {
i18n: {
baseLanguage: "en",
},
orizuru: {
serverName: "Orizuru",
},
web_from_git: {
gitRepo: "",
gitSecret: "",
gitUser: "",
},
//$StripEnd
},
};
16 changes: 16 additions & 0 deletions src/config/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Parseable, ValidateProperty } from "parzival";
import DiscordConfig from "./discord";
//$StripStart
import I18nConfig from "./i18n";
import OrizuruConfig from "./orizuru";
import WebFromGitConfig from "./web_from_git";
//$StripEnd

@Parseable()
Expand All @@ -20,5 +22,19 @@ export default class ModuleConfigs {
className: "I18nConfig",
})
i18n!: I18nConfig;

@ValidateProperty({
type: "object",
recurse: true,
className: "OrizuruConfig",
})
orizuru!: OrizuruConfig;

@ValidateProperty({
type: "object",
recurse: true,
className: "WebFromGitConfig",
})
web_from_git!: WebFromGitConfig;
//$StripEnd
}
10 changes: 10 additions & 0 deletions src/config/modules/orizuru.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Parseable, ValidateProperty } from "parzival";

@Parseable()
export default class OrizuruConfig {
@ValidateProperty({
type: "string",
optional: true
})
serverName?: string;
}
19 changes: 19 additions & 0 deletions src/config/modules/web_from_git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Parseable, ValidateProperty } from "parzival";

@Parseable()
export default class WebFromGitConfig {
@ValidateProperty({
type: "string",
})
gitUser!: string;

@ValidateProperty({
type: "string",
})
gitSecret!: string;

@ValidateProperty({
type: "string",
})
gitRepo!: string;
}
16 changes: 16 additions & 0 deletions src/modules/orizuru/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Module from "@src/engine/modules";
import { debug } from "@src/engine/utils/Logger";
import { Orizuru } from "@garycraft/orizuru";
import { getAppContext } from "@src/engine/utils/Composable";


export default {
name: "orizuru",
loadFunction: async (config) => {
return new Orizuru(getAppContext())
},
initFunction: async (ctx, config) => {
getAppContext().http.server.post("/orizuru", ctx.getExpressHandler())
debug("Orizuru module initialized")
}
} satisfies Module<Orizuru, "orizuru">;
16 changes: 16 additions & 0 deletions src/modules/web_from_git/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Module from "@src/engine/modules";
import EventEmitter from "events";


export default {
name: "web_from_git",
paths: {
tasks: "tasks",
},
loadFunction: async (config) => {
return new EventEmitter();
},
initFunction: async (ctx, config) => {

}
} satisfies Module<EventEmitter, "web_from_git">;
104 changes: 104 additions & 0 deletions src/modules/web_from_git/tasks/pullSite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { simpleGit, SimpleGitOptions, CheckRepoActions } from 'simple-git';
import { ScheduledTask } from "@src/engine/types/Executors";
import { debug, error, info } from "@src/engine/utils/Logger";
import path from "path";
import { getTempPath, getWebPublicDir } from "@src/engine/utils/Runtime";
import fs from "fs";
import { spawnChild } from "@src/engine/utils/Process";
import ExtendedClient from "@src/modules/discord/extendedclient";
import { getConfigProperty } from '@src/engine/utils/Configuration';
import WebFromGitConfig from '@src/config/modules/web_from_git';

const WebStorageBasePath = path.join(getTempPath(), "webpage");

const GITOPTIONS: Partial<SimpleGitOptions> = {
baseDir: WebStorageBasePath,
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: false,
};

export default {
name: "BuildWebsite",
cronInterval: "* * * * *",
async task(app) {
const cfg = getConfigProperty<WebFromGitConfig>("modules.web_from_git");
if (!cfg) {
error("No config found for web_from_git");
return;
}
/* if (app.config.node_env !== "production") {
debug("Not building site in development mode");
return;
} */
debug("Checking repo for changes");
// does directory exist?
if (!fs.existsSync(WebStorageBasePath)) {
debug("Creating web storage directory", WebStorageBasePath);
fs.mkdirSync(WebStorageBasePath, { recursive: true });
}
const repoUrl = `https://${cfg.gitUser}:${cfg.gitSecret}@${cfg.gitRepo}`
const git = simpleGit(GITOPTIONS);
try {
debug(WebStorageBasePath)
// is there a repo?
const exists = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);
if (!exists) {
debug("Cloning repo", cfg.gitRepo, "to", WebStorageBasePath);
await git.clone(repoUrl, WebStorageBasePath);
}
// are there remote changes?
await git.fetch();
const status = await git.status();
const hasIndexHtml = fs.existsSync(path.join(getWebPublicDir(), "index.html"));
if (status.behind > 0) {
// pull the changes
await git.pull();
}
else if (!hasIndexHtml) {
debug("No index.html found, not skipping build");
}
else {
debug("No changes in the repo, skipping build");
return;
}
}
catch (e) {
error("Error checking repo for changes!", e);
}

app.events.emit("modules:discord:siteBuildStarted")
info("Repo checked for changes, building site!");
// build the site, expecting to use npm run build
let buildError = false;
try {
const npmVersion = await spawnChild("npm -v");
const nodeVersion = await spawnChild("node -v");
debug("Node version", nodeVersion.stdout);
debug("NPM version", npmVersion.stdout);

// dependencies
const installed = await spawnChild("npm install", {
cwd: WebStorageBasePath,
});
debug("Dependencies installed!", installed.stdout, installed.stderr);
// build
const built = await spawnChild("npm run build", {
cwd: WebStorageBasePath,
});
debug("Build Result", built.stdout, built.stderr);

} catch (e) {
error("Error building site!", e);
buildError = true;
}
if (buildError) {
error("Site build failed!");
return;
}
debug("Site built!");
app.events.emit("modules:discord:siteBuildCompleted")

info("Done!")
},
} satisfies ScheduledTask;

0 comments on commit ef31f30

Please sign in to comment.