Skip to content

Commit

Permalink
feat: add support for manually updating js file
Browse files Browse the repository at this point in the history
  • Loading branch information
zyf722 committed Jun 4, 2024
1 parent d70cc78 commit b84a202
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/twinkled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ jobs:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
PARATRANZ_TOKEN: ${{ secrets.PARATRANZ_TOKEN }}
MANUAL_MODE: ""
25 changes: 18 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { existsSync } from "fs";
import { config } from "dotenv";

export default class Config {
public readonly personaId: string;
public readonly githubToken: string;
public readonly giteeToken: string;
public readonly paratranzToken: string;
public readonly manualMode: boolean;

constructor(debug: boolean = false) {
if (debug) {
constructor() {
if (existsSync(".env.debug")) {
config({ path: ".env.debug" });
}

this.personaId = process.env.PERSONA_ID || "";
this.githubToken = process.env.GH_TOKEN || "";
this.giteeToken = process.env.GITEE_TOKEN || "";
this.paratranzToken = process.env.PARATRANZ_TOKEN || "";
const rawConfig = {
personaId: process.env.PERSONA_ID,
githubToken: process.env.GH_TOKEN,
giteeToken: process.env.GITEE_TOKEN,
paratranzToken: process.env.PARATRANZ_TOKEN,
manualMode: process.env.MANUAL_MODE,
};

if (Object.values(this).some((value) => value === "")) {
if (Object.values(this).some((value) => value === undefined)) {
throw new Error("Missing environment variables.");
}

this.personaId = rawConfig.personaId as string;
this.githubToken = rawConfig.githubToken as string;
this.giteeToken = rawConfig.giteeToken as string;
this.paratranzToken = rawConfig.paratranzToken as string;
this.manualMode = Boolean(rawConfig.manualMode as string);
}
}
61 changes: 40 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,44 @@ class Twinkled {
const bundleManifest = await this.fetchBundleManifest();

log.info("Fetching latest release version from GitHub...");
const githubVersion = await this.githubApi.fetchLatestReleaseVer();
const rawGithubVersion = await this.githubApi.fetchLatestReleaseVer();

log.info("Fetching latest release version from Gitee...");
const giteeVersion = await this.giteeApi.fetchLatestReleaseVer();
const rawGiteeVersion = await this.giteeApi.fetchLatestReleaseVer();

if (githubVersion !== giteeVersion) {
if (rawGithubVersion !== rawGiteeVersion) {
log.error(
"GitHub and Gitee versions do not match. Please check the release versions."
);
log.error(`GitHub version: ${rawGithubVersion}`);
log.error(`Gitee version: ${rawGiteeVersion}`);
return;
}

// if (bundleManifest.bundleVersion === githubVersion) {
// log.info("Bundle is up to date.");
// return;
// }
log.info("Bundle is outdated.");
log.info(`Github/Gitee version: ${githubVersion}`);
log.info(`Github/Gitee version: ${rawGithubVersion}`);
log.info(`Remote version: ${bundleManifest.bundleVersion}`);

const [releaseVersion, releaseMinorVersion] =
rawGithubVersion.split(".");

if (
bundleManifest.bundleVersion === releaseVersion &&
!this.config.manualMode
) {
log.info("Bundle is up to date.");
log.info("Not in manual mode, exiting.");
return;
}

if (bundleManifest.bundleVersion !== releaseVersion) {
log.info("Bundle is outdated.");
} else {
log.info("Bundle is up to date, but in manual mode, continuing...");
}

log.info("Updating...");

// Hook the translator function into the codse
// Hook the translator function into the code
const bundle = await this.fetchBundleCode(bundleManifest);
const hooker = new JSHooker(bundle.jsCode);
hooker.hook();
Expand All @@ -93,25 +109,28 @@ class Twinkled {
log.info("Bundle saved.");

// Create a new release
// try {
// log.info("Creating release for GitHub...");
// await this.githubApi.createRelease(bundle.version, fileName);
const newVersion = `${bundle.version}.${
releaseMinorVersion ? Number.parseInt(releaseMinorVersion) + 1 : 0
}`;
try {
log.info("Creating release for GitHub...");
await this.githubApi.createRelease(newVersion, fileName);

// log.info("Creating release for Gitee...");
// await this.giteeApi.createRelease(bundle.version, fileName);
log.info("Creating release for Gitee...");
await this.giteeApi.createRelease(newVersion, fileName);

// log.info("Release created.");
// } catch (error) {
// log.error("Failed to create release:", error);
// }
log.info("Release created.");
} catch (error) {
log.error("Failed to create release: ", error);
}

// Reply to Paratranz thread
try {
log.info("Replying to Paratranz thread...");
await this.paraTranzApi.reply(bundle.version);
await this.paraTranzApi.reply(newVersion);
log.info("Replied to Paratranz thread.");
} catch (error) {
log.error("Failed to reply to Paratranz thread:", error);
log.error("Failed to reply to Paratranz thread: ", error);
}
}
}
Expand Down

0 comments on commit b84a202

Please sign in to comment.