/* Copyright 2024 Resultium LLC This file is part of RCZ. RCZ is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version. RCZ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with RCZ. If not, see . */ import { existsSync } from "fs"; import { readFile, writeFile } from "fs/promises"; import { join } from "path"; import { cwd } from "process"; import { Config } from "../types"; import { request } from "http"; import { tmpdir } from "os"; import { execSync } from "child_process"; import { gt } from "semver"; import { note } from "@clack/prompts"; export const getConfig = async () => { if (existsSync(join(cwd(), ".rczrc"))) { return JSON.parse( (await readFile(join(cwd(), ".rczrc"))).toString(), ) as Config; } else if (existsSync(join(cwd(), ".rczrc.json"))) { return JSON.parse( (await readFile(join(cwd(), ".rczrc.json"))).toString(), ) as Config; } else if (existsSync(join(cwd(), "rcz.config.json"))) { return JSON.parse( (await readFile(join(cwd(), "rcz.config.json"))).toString(), ) as Config; } else if (existsSync(join(cwd(), ".rcz", "config.json"))) { return JSON.parse( (await readFile(join(cwd(), ".rcz", "config.json"))).toString(), ) as Config; } else { return null; } }; export const getOnReleaseScript = async () => { if (existsSync(join(cwd(), ".rcz.onrelease.js"))) { return (await readFile(join(cwd(), ".rcz.onrelease.js"))).toString(); } else if (existsSync(join(cwd(), ".rcz", "onrelease.js"))) { return (await readFile(join(cwd(), ".rcz", "onrelease.js"))).toString(); } else { return null; } }; export const getPostReleaseScript = async () => { if (existsSync(join(cwd(), ".rcz.postrelease.js"))) { return (await readFile(join(cwd(), ".rcz.postrelease.js"))).toString(); } else if (existsSync(join(cwd(), ".rcz", "postrelease.js"))) { return (await readFile(join(cwd(), ".rcz", "postrelease.js"))).toString(); } else { return null; } }; export const isOnline = () => { return new Promise((resolve) => { request({ method: "GET", hostname: "icanhazip.com" }, (res) => { res.on("data", () => {}); res.on("end", () => { resolve(res.statusCode === 200); }); }) .on("error", () => { resolve(false); }) .end(); }); }; export const checkForUpdates = async () => { const updateText = "You are running on an outdated version of Resultium Commitizen, in order to update run\n\nnpm install -g @resultium/rcz@latest"; const cachedVersion = existsSync(join(tmpdir(), "rcz-server-version")) ? (await readFile(join(tmpdir(), "rcz-server-version"))).toString().trim() : null; const localVersion = execSync("rcz --version").toString().trim(); // even if cached once in a while it should get newest data if ((cachedVersion && Math.random() < 0.1) || cachedVersion === null) { if (!(await isOnline())) return; const serverVersion = execSync("npm show @resultium/rcz version") .toString() .trim(); writeFile(join(tmpdir(), "rcz-server-version"), serverVersion); if (gt(serverVersion, localVersion)) note(updateText); } else if (cachedVersion) { if (gt(cachedVersion, localVersion)) note(updateText); } };