Skip to content

Commit

Permalink
refactor action script
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluemangoo committed Feb 16, 2024
1 parent 60d5df4 commit d98721b
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 242 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# @format

name: "update"
on:
push:
Expand All @@ -17,7 +15,9 @@ jobs:
uses: actions/checkout@v4

- name: "npm"
run: npm i -g typescript @types/node ts-node
working-directory: ./script
run: yarn install

- name: "update"
working-directory: ./script
run: npx ts-node ./src/index.ts
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.vscode
.vscode
.idea
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 100,
"trailingComma": "none",
"tabWidth": 4
}
2 changes: 1 addition & 1 deletion bucket/hello.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"hello","repo":"https://github.com/WhirlScript/hello","versions":[{"version":"1.0.0","dependencies":{},"sha1":"0e4e47152263a098a022c466d1a37c23bc5bd331"},{"version":"1.0.1","dependencies":{},"sha1":"ecb96fc983dd6ae016388ce4737ee9bd56dc74f6"},{"version":"1.0.2","dependencies":{},"sha1":"d238d177e9d212eac3a09a03c9c123e5f7ef12d7"}]}
{"name":"hello","repo":"https://github.com/WhirlScript/hello","versions":[{"version":"1.0.0","dependencies":{},"sha1":"0e4e47152263a098a022c466d1a37c23bc5bd331"},{"version":"1.0.1","dependencies":{},"sha1":"ecb96fc983dd6ae016388ce4737ee9bd56dc74f6"}]}
11 changes: 11 additions & 0 deletions script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"scripts": {
"run": "ts-node ./src/index.ts"
},
"dependencies": {
"@types/node": "^20.11.19",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
10 changes: 10 additions & 0 deletions script/src/githubApi.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export declare type GitHubRepoTags = {
"ref": string,
"node_id": string,
"url": string,
"object": {
"sha": string,
"type": string,
"url": string
}
}[];
166 changes: 166 additions & 0 deletions script/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import * as fs from "node:fs";
import * as path from "node:path";
import * as cp from "child_process";
import { GitHubRepoTags } from "./githubApi";
import { WhirlBucket, WhirlPkgJson } from "./whirlPackage";

// check ./bucket dir
const githubUrl = "https://github.com/";
const githubApiUrl = "https://api.github.com/repos/";

if (!fs.existsSync(path.resolve("../bucket/")))
fs.mkdirSync(path.resolve("../bucket/"));

const whirlPackageList: any = require(path.resolve("../packages.json"));

const fetchOption: RequestInit = {
method: "GET",
headers: {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "Whirlpkg-Index"
}
};

let commitDetails = "";

(async () => {
for (let file in whirlPackageList) {
let isNew = true;
let packageInf: WhirlBucket = {
name: file,
repo: whirlPackageList[file],
versions: []
};
const currentVersions: { [key: string]: boolean } = {};
if (fs.existsSync(`../bucket/${file}.json`)) {
isNew = false;
packageInf = JSON.parse(fs.readFileSync(`../bucket/${file}.json`, { encoding: "utf-8" }));
for (const version of packageInf.versions) {
currentVersions[version.version] = version.status == "deprecated";
}
}
const whirlPackName: string = file;
const whirlPackageRepoURL: string = whirlPackageList[file];
const whirlPackageGithubPath: string = whirlPackageRepoURL.split(githubUrl)[1];

const oldVersionList = packageInf.versions;
packageInf.versions = [];

const messageHead = `${whirlPackName}${isNew ? " (new!)" : ""}:`;
try {
const tagsReq = await (await fetch(`${githubApiUrl}${whirlPackageGithubPath}/git/refs/tags/`, fetchOption)).json() as GitHubRepoTags;

const tags: { version: string, sha: string }[] = [];
for (const tagsReqElement of tagsReq) {
tags.push({ version: tagsReqElement.ref.split("refs/tags/")[1], sha: tagsReqElement.object.sha });
}

const tasks: {
newVersion: { version: string, sha: string }[],
deprecateVersion: string[]
} = {
newVersion: [],
deprecateVersion: []
};

for (const tag of tags) {
if (currentVersions[tag.version] != undefined) {
currentVersions[tag.version] = true;
continue;
}

tasks.newVersion.push(tag);
}

for (const version in currentVersions) {
if (currentVersions[version]) {
continue;
}
tasks.deprecateVersion.push(version);
}

if (tasks.newVersion.length == 0 && tasks.deprecateVersion.length == 0) {
continue;
}

for (const tag of tasks.newVersion) {
let whirlpkg: WhirlPkgJson;
const req = await fetch(`${githubUrl}${whirlPackageGithubPath}/raw/${tag.version}/whirlpkg.json`, fetchOption);
try {
whirlpkg = await req.json() as WhirlPkgJson;
} catch {
console.warn(`${messageHead} invalid whirlpkg.json at version ${tag.version}`);
commitDetails += `:rotating_light: ${messageHead} invalid whirlpkg.json at version ${tag.version}\n`;
continue;
}

if (!whirlpkg.name || !whirlpkg.version || !whirlpkg.author || !whirlpkg.index) {
console.warn(`${messageHead} invalid whirlpkg.json at version ${tag.version}`);
commitDetails += `:rotating_light: ${messageHead} invalid whirlpkg.json at version ${tag.version}\n`;
continue;
}

packageInf.versions.push({
dependencies: whirlpkg.dependencies ?? {},
sha: tag.sha,
version: tag.version
});

console.log(`${messageHead} add ${tag.version}`);
commitDetails += `:sparkles: ${messageHead} add ${tag.version}`;
}

for (const oldVersionListElement of oldVersionList) {
if (tasks.deprecateVersion.indexOf(oldVersionListElement.version)) {
oldVersionListElement.status = "deprecated";
console.log(`${messageHead} deprecated ${oldVersionListElement.version}`);
commitDetails += `:wastebasket: ${messageHead} deprecated ${oldVersionListElement.version}\n`;
}
packageInf.versions.push(oldVersionListElement);
}

fs.writeFileSync(
`../bucket/${whirlPackName}.json`,
JSON.stringify(packageInf)
);
} catch (e) {
console.error(e);
console.warn(`${messageHead} cannot fetch tags.`);
}
}

if (commitDetails == "") {
console.log("Nothing changed.");
return;
}

// commit
const commitInf = "Update packages.\n\n" + commitDetails;
cp.execSync("git config user.name \"github-actions[bot]\"", {
cwd: path.resolve("../")
});
cp.execSync(
"git config user.email \"41898282+github-actions[bot]@users.noreply.github.com\"", {
cwd: path.resolve("../")
}
);
cp.execSync(`git remote set-url origin [email protected]:WhirlScript/WhirlPKG-index.git"`, {
cwd: path.resolve("../")
});
cp.execSync("git add .", {
cwd: path.resolve("../")
});
cp.execSync(`git commit -m "${commitInf}"`, {
cwd: path.resolve("../")
});

cp.execSync(`mkdir -p ~/.ssh/`);
cp.execSync(`touch ~/.ssh/id_rsa.pub`);
console.log("Gen ssh pub key");
cp.execSync(`echo '${process.env["ssh_key"]}' > ~/.ssh/id_rsa`);
console.log("Pushing to repo...");
cp.execSync(`git push`, {
cwd: path.resolve("../")
});
})();
21 changes: 21 additions & 0 deletions script/src/whirlPackage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export declare type WhirlPkgJson = {
name: string,
version: string,
author: string,
repo?: string,
license?: string,
index: string,
export?: { [key: string]: string },
dependencies?: { [key: string]: string },
}

export declare type WhirlBucket = {
name: string,
repo: string,
versions: {
version: string,
dependencies: { [key: string]: string },
sha: string,
status?: "deprecated"
}[],
}
124 changes: 124 additions & 0 deletions script/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
resolved "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1"
integrity sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@jridgewell/resolve-uri@^3.0.3":
version "3.1.2"
resolved "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz#7a0ee601f60f99a20c7c7c5ff0c80388c1189bd6"
integrity sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==

"@jridgewell/sourcemap-codec@^1.4.10":
version "1.4.15"
resolved "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz#d7c6e6755c78567a951e04ab52ef0fd26de59f32"
integrity sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==

"@jridgewell/[email protected]":
version "0.3.9"
resolved "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9"
integrity sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==
dependencies:
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@tsconfig/node10@^1.0.7":
version "1.0.9"
resolved "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz#df4907fc07a886922637b15e02d4cebc4c0021b2"
integrity sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==

"@tsconfig/node12@^1.0.7":
version "1.0.11"
resolved "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz#ee3def1f27d9ed66dac6e46a295cffb0152e058d"
integrity sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==

"@tsconfig/node14@^1.0.0":
version "1.0.3"
resolved "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz#e4386316284f00b98435bf40f72f75a09dabf6c1"
integrity sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==

"@tsconfig/node16@^1.0.2":
version "1.0.4"
resolved "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz#0b92dcc0cc1c81f6f306a381f28e31b1a56536e9"
integrity sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==

"@types/node@^20.11.19":
version "20.11.19"
resolved "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz#b466de054e9cb5b3831bee38938de64ac7f81195"
integrity sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==
dependencies:
undici-types "~5.26.4"

acorn-walk@^8.1.1:
version "8.3.2"
resolved "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.2.tgz#7703af9415f1b6db9315d6895503862e231d34aa"
integrity sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==

acorn@^8.4.1:
version "8.11.3"
resolved "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz#71e0b14e13a4ec160724b38fb7b0f233b1b81d7a"
integrity sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

ts-node@^10.9.2:
version "10.9.2"
resolved "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz#70f021c9e185bccdca820e26dc413805c101c71f"
integrity sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==
dependencies:
"@cspotcode/source-map-support" "^0.8.0"
"@tsconfig/node10" "^1.0.7"
"@tsconfig/node12" "^1.0.7"
"@tsconfig/node14" "^1.0.0"
"@tsconfig/node16" "^1.0.2"
acorn "^8.4.1"
acorn-walk "^8.1.1"
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

typescript@^5.3.3:
version "5.3.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==

undici-types@~5.26.4:
version "5.26.5"
resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==

[email protected]:
version "3.1.1"
resolved "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==
Loading

0 comments on commit d98721b

Please sign in to comment.