style: fmt scripts

main
imspace 2023-03-13 11:19:53 +08:00
parent 63635cda07
commit 772af8da0d
3 changed files with 64 additions and 48 deletions

View File

@ -1,4 +1,4 @@
import { Application, Router } from 'https://deno.land/x/oak@v12.1.0/mod.ts'; import { Application, Router } from "https://deno.land/x/oak@v12.1.0/mod.ts";
import * as path from "https://deno.land/std@0.178.0/path/mod.ts"; import * as path from "https://deno.land/std@0.178.0/path/mod.ts";
const PORT = 1421; const PORT = 1421;
@ -10,23 +10,29 @@ const keys: Set<string> = new Set();
async function updateFile() { async function updateFile() {
delayId = null; delayId = null;
for (const lng of ['en', 'zh-CN']) { for (const lng of ["en", "zh-CN"]) {
const translationPath = path.join(__dirname, `../src/i18n/translation/${lng}.json`); const translationPath = path.join(
__dirname,
`../src/i18n/translation/${lng}.json`,
);
let translations: Record<string, string> = {}; let translations: Record<string, string> = {};
try { try {
translations = JSON.parse(await Deno.readTextFile(translationPath)); translations = JSON.parse(await Deno.readTextFile(translationPath));
} catch (error) {} } catch (error) {}
const toAdd = [...keys].filter(k => !Object.keys(translations).includes(k)); const toAdd = [...keys].filter((k) =>
!Object.keys(translations).includes(k)
);
translations = Object.fromEntries( translations = Object.fromEntries(
[ [
...Object.entries(translations), ...Object.entries(translations),
...toAdd ...toAdd
.map(i => [i, i] as const)] .map((i) => [i, i] as const),
]
.sort(([a], [b]) => a.localeCompare(b)), .sort(([a], [b]) => a.localeCompare(b)),
); );
console.log('Add keys:', toAdd, 'for', lng); console.log("Add keys:", toAdd, "for", lng);
await Deno.writeTextFile( await Deno.writeTextFile(
translationPath, translationPath,
@ -35,12 +41,14 @@ async function updateFile() {
} }
keys.clear(); keys.clear();
} }
let delayId: number|null = null; let delayId: number | null = null;
router.post('/locales/add/:lng/:ns', async (context) => { router.post("/locales/add/:lng/:ns", async (context) => {
try { try {
// ns, lng is ignored // ns, lng is ignored
const body: Record<string,string> = await context.request.body({ type: 'json' }).value; const body: Record<string, string> = await context.request.body({
type: "json",
}).value;
for (const key of Object.keys(body)) { for (const key of Object.keys(body)) {
keys.add(key); keys.add(key);
} }
@ -51,7 +59,7 @@ router.post('/locales/add/:lng/:ns', async (context) => {
delayId = setTimeout(updateFile, 1000); delayId = setTimeout(updateFile, 1000);
context.response.status = 200; context.response.status = 200;
context.response.body = { message: 'Translation added.' }; context.response.body = { message: "Translation added." };
} catch (error) { } catch (error) {
context.response.status = 500; context.response.status = 500;
context.response.body = { message: error.message }; context.response.body = { message: error.message };
@ -61,5 +69,5 @@ router.post('/locales/add/:lng/:ns', async (context) => {
app.use(router.routes()); app.use(router.routes());
app.use(router.allowedMethods()); app.use(router.allowedMethods());
console.log(`Listening on port ${PORT}...`) console.log(`Listening on port ${PORT}...`);
await app.listen({ port: PORT }); await app.listen({ port: PORT });

View File

@ -1,36 +1,45 @@
import { Octokit } from 'npm:@octokit/rest@19.0.7'; import { Octokit } from "npm:@octokit/rest@19.0.7";
const TAG_PREFIX = 'gui-' const TAG_PREFIX = "gui-";
type Platform = 'darwin-x86_64' | 'darwin-aarch64' | 'linux-x86_64' | 'windows-x86_64' type Platform =
const PLATFORMS: Platform[] = ['darwin-x86_64', 'darwin-aarch64', 'linux-x86_64', 'windows-x86_64'] | "darwin-x86_64"
| "darwin-aarch64"
| "linux-x86_64"
| "windows-x86_64";
const PLATFORMS: Platform[] = [
"darwin-x86_64",
"darwin-aarch64",
"linux-x86_64",
"windows-x86_64",
];
const PlatformSuffix: Record<Platform, string> = { const PlatformSuffix: Record<Platform, string> = {
'darwin-x86_64': '.app.tar.gz', "darwin-x86_64": ".app.tar.gz",
'darwin-aarch64': '.app.tar.gz', "darwin-aarch64": ".app.tar.gz",
'linux-x86_64': '.AppImage.tar.gz', "linux-x86_64": ".AppImage.tar.gz",
'windows-x86_64': '.msi.zip', "windows-x86_64": ".msi.zip",
} };
type File = { type File = {
signature: string signature: string;
url: string url: string;
} };
type UpdateJson = { type UpdateJson = {
version: string version: string;
notes: string notes: string;
pub_date: string pub_date: string;
platforms: Record<Platform, File> platforms: Record<Platform, File>;
} };
const REPO = { const REPO = {
owner: 'spacemeowx2', owner: "spacemeowx2",
repo: 's3si.ts', repo: "s3si.ts",
} };
const octokit = new Octokit({ const octokit = new Octokit({
auth: Deno.env.get('GITHUB_TOKEN'), auth: Deno.env.get("GITHUB_TOKEN"),
}); });
async function findFirstGuiRelease() { async function findFirstGuiRelease() {
@ -39,15 +48,15 @@ async function findFirstGuiRelease() {
const { data: list } = await octokit.repos.listReleases({ const { data: list } = await octokit.repos.listReleases({
...REPO, ...REPO,
page, page,
}) });
if (list.length === 0) { if (list.length === 0) {
return undefined return undefined;
} }
for (const release of list) { for (const release of list) {
if (release.tag_name.startsWith(TAG_PREFIX)) { if (release.tag_name.startsWith(TAG_PREFIX)) {
return release return release;
} }
} }
@ -57,27 +66,26 @@ async function findFirstGuiRelease() {
const release = await findFirstGuiRelease(); const release = await findFirstGuiRelease();
const version = release?.tag_name.slice(TAG_PREFIX.length) ?? 'unknown'; const version = release?.tag_name.slice(TAG_PREFIX.length) ?? "unknown";
const notes = release?.body ?? 'unknown'; const notes = release?.body ?? "unknown";
const pub_date = release?.published_at ?? 'unknown'; const pub_date = release?.published_at ?? "unknown";
async function makePlatforms(r: typeof release) { async function makePlatforms(r: typeof release) {
const assets = r?.assets ?? []; const assets = r?.assets ?? [];
const platforms = Object.fromEntries(PLATFORMS.map(p => { const platforms = Object.fromEntries(PLATFORMS.map((p) => {
const asset = assets.find(i => i.name.endsWith(PlatformSuffix[p])); const asset = assets.find((i) => i.name.endsWith(PlatformSuffix[p]));
if (!asset) { if (!asset) {
throw new Error(`Asset not found for ${p}`) throw new Error(`Asset not found for ${p}`);
} }
return [p, { return [p, {
signature: asset.browser_download_url + '.sig', signature: asset.browser_download_url + ".sig",
url: asset.browser_download_url, url: asset.browser_download_url,
}] }];
})) as Record<Platform, File>; })) as Record<Platform, File>;
return platforms;
return platforms
} }
const updateJson: UpdateJson = { const updateJson: UpdateJson = {
@ -85,7 +93,7 @@ const updateJson: UpdateJson = {
notes, notes,
pub_date, pub_date,
platforms: await makePlatforms(release), platforms: await makePlatforms(release),
} };
// fetch signatures // fetch signatures
for (const platform of PLATFORMS) { for (const platform of PLATFORMS) {