Implement commit watcher without GitHub API dependency

This commit is contained in:
Andrei Andreev 2021-11-14 22:20:39 +03:00
parent 2d918dbb19
commit 2b86d687f0
6 changed files with 100 additions and 78 deletions

17
build/post-build.js Normal file
View File

@ -0,0 +1,17 @@
const fs = require("fs");
const path = require("path");
const proc = require("child_process");
function executeCommand(command) {
return proc.execSync(command).toString().trim();
}
const commit = {
sha: executeCommand("git rev-parse HEAD"),
message: executeCommand("git log -1 --pretty=%B"),
author: executeCommand("git log -1 --pretty=format:%an")
};
const json = JSON.stringify(commit);
fs.writeFileSync(path.resolve(__dirname, "../dist/commit.json"), json);

View File

@ -1,4 +1,3 @@
import { isLocalEnvironment } from "./core/devtools.js";
import { playFabLogin } from "./core/playfab.js"; import { playFabLogin } from "./core/playfab.js";
import { DC } from "./core/constants.js"; import { DC } from "./core/constants.js";
@ -245,36 +244,6 @@ export function gainedInfinities() {
return infGain; return infGain;
} }
// TODO: remove before release
if (!isLocalEnvironment()) {
let commit;
setInterval(() => {
const url = "https://api.github.com/repos/IvarK/IToughtAboutCurseWordsButThatWouldBeMeanToOmsi/commits/master";
const headers = new Headers();
// Yes, this is my GitHub API key for reading private repo details
headers.append("Authorization", `Basic ${btoa("WaitingIdly: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")}`);
fetch(url, { method: "GET", headers })
.then(response => response.json())
.then(json => {
if (commit === undefined) {
commit = json.sha;
return;
}
if (commit === json.sha) return;
// GH Pages need some time to get rebuilt, so show message after 60 seconds
setTimeout(() => {
Modal.message.show(
"Refresh the page (game will be saved), we've got new stuff: " +
`"${json.commit.message}" by ${json.author.login}`,
updateRefresh,
true
);
}, 60000);
});
}, 60000);
}
export function updateRefresh() { export function updateRefresh() {
GameStorage.save(true); GameStorage.save(true);
location.reload(true); location.reload(true);

View File

@ -2,7 +2,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build": "vue-cli-service build", "build": "vue-cli-service build && node build/post-build.js",
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },
"dependencies": { "dependencies": {

32
src/commit-watcher.js Normal file
View File

@ -0,0 +1,32 @@
// TODO: remove before release
export function watchLatestCommit() {
if (isLocalEnvironment()) {
return;
}
const url = "commit.json";
let current;
function watch() {
fetch(url, { method: "GET" })
.then(response => response.json())
.then(json => {
if (json === undefined) {
return;
}
current = current ?? json.sha;
if (current === json.sha) {
return;
}
Modal.message.show(
"Refresh the page (game will be saved), we've got new stuff: " +
`"${json.message}" by ${json.author}`,
updateRefresh,
true
);
});
}
setInterval(watch, 60000);
}

View File

@ -1,47 +1,6 @@
function mergeIntoGlobal(object) { import "./merge-globals";
for (const key in object) { import { init } from "../javascripts/game";
const value = object[key]; import { watchLatestCommit } from "@/commit-watcher";
const existingValue = window[key];
if (existingValue !== undefined) {
throw `Property ${key} already exists in global context`;
}
window[key] = value; init();
} watchLatestCommit();
}
import * as Utils from "../javascripts/core/utils.js";
mergeIntoGlobal(Utils);
import "../javascripts/components";
import * as GameDB from "../javascripts/core/secret-formula";
mergeIntoGlobal(GameDB);
// Start of bullshit
// Hevi, why
import * as AutomatorBlockEditor from "../javascripts/components/reality/automator/automator-block-editor.js";
mergeIntoGlobal(AutomatorBlockEditor);
// Hevi, whyy
import * as AutomatorBlocks from "../javascripts/components/reality/automator/automator-blocks.js";
mergeIntoGlobal(AutomatorBlocks);
// Garnet, nooo
import * as AutomatorTextEditor from "../javascripts/components/reality/automator/automator-text-editor.js";
mergeIntoGlobal(AutomatorTextEditor);
// Spec, reeee
import * as PerksTab from "../javascripts/components/reality/perks-tab.js";
mergeIntoGlobal(PerksTab);
// End of bullshit
import * as core from "../javascripts/core/globals.js";
mergeIntoGlobal(core);
import * as game from "../javascripts/game.js";
mergeIntoGlobal(game);
game.init();

45
src/merge-globals.js Normal file
View File

@ -0,0 +1,45 @@
function mergeIntoGlobal(object) {
for (const key in object) {
const value = object[key];
const existingValue = window[key];
if (existingValue !== undefined) {
throw `Property ${key} already exists in global context`;
}
window[key] = value;
}
}
import * as Utils from "../javascripts/core/utils";
mergeIntoGlobal(Utils);
import "../javascripts/components";
import * as GameDB from "../javascripts/core/secret-formula";
mergeIntoGlobal(GameDB);
// Start of bullshit
// Hevi, why
import * as AutomatorBlockEditor from "../javascripts/components/reality/automator/automator-block-editor";
mergeIntoGlobal(AutomatorBlockEditor);
// Hevi, whyy
import * as AutomatorBlocks from "../javascripts/components/reality/automator/automator-blocks";
mergeIntoGlobal(AutomatorBlocks);
// Garnet, nooo
import * as AutomatorTextEditor from "../javascripts/components/reality/automator/automator-text-editor";
mergeIntoGlobal(AutomatorTextEditor);
// Spec, reeee
import * as PerksTab from "../javascripts/components/reality/perks-tab";
mergeIntoGlobal(PerksTab);
// End of bullshit
import * as core from "../javascripts/core/globals";
mergeIntoGlobal(core);
import * as game from "../javascripts/game";
mergeIntoGlobal(game);