From 345ab1fea16b1d25b3f3cef9b4a35daacc975481 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 2 Nov 2026 01:26:42 +0300 Subject: [PATCH] feat: add link module --- src/common/template.ts | 4 ++- src/impl/neko.ts | 2 +- src/index.ts | 2 ++ src/v2/link.ts | 64 ++++++++++++++++++++++++++++++++++++++++++ test/download.test.js | 22 ++++++--------- test/link.test.js | 37 ++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 src/v2/link.ts create mode 100644 test/link.test.js diff --git a/src/common/template.ts b/src/common/template.ts index 53d5ea4..1085b0e 100644 --- a/src/common/template.ts +++ b/src/common/template.ts @@ -5,10 +5,12 @@ import { fileURLToPath } from "url"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -export default new Eta({ +export const template = new Eta({ autoFilter: true, escapeFunction: (val: unknown): string => String(val), filterFunction: (val: unknown): string => (val === null || val === undefined ? "" : String(val)), useWith: true, views: path.join(__dirname, "template"), }); + +export default template; diff --git a/src/impl/neko.ts b/src/impl/neko.ts index 50a20f2..95974c7 100644 --- a/src/impl/neko.ts +++ b/src/impl/neko.ts @@ -5,7 +5,7 @@ import env from "../common/env"; import { Sdk } from "../core/sdk"; import run from "../run/index"; -export class NekoLink { +class NekoLink { static get type() { switch (os.type()) { case "Linux": diff --git a/src/index.ts b/src/index.ts index fdb5cbb..c802c02 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ export * from "./common/download"; export * from "./common/log"; export * from "./common/output"; export * from "./common/system"; +export * from "./common/template"; export * from "./core/config"; export * from "./core/sdk"; export * from "./core/type"; @@ -14,3 +15,4 @@ export * from "./impl/ruffle"; export * from "./preset"; export * from "./project"; export * from "./project/impl"; +export * from "./v2/link"; diff --git a/src/v2/link.ts b/src/v2/link.ts new file mode 100644 index 0000000..058b8bd --- /dev/null +++ b/src/v2/link.ts @@ -0,0 +1,64 @@ +import os from "os"; +import template from "../common/template"; + +export type Params = Record; + +export interface Link { + name: string; + version: string; + url: string; +} + +export const NekoLink: Link = { + name: "neko", + version: "2.4.1", + url: "https://github.com/HaxeFoundation/neko/releases/download/v<%= version.split('.').join('-') %>/neko-<%= version %>-<%= os_name %><%= os_arch %>.<%= os_archive_ext %>", +}; + +export const HaxeLink: Link = { + name: "haxe", + version: "3.4.2", + url: "https://github.com/HaxeFoundation/haxe/releases/download/<%= version %>/haxe-<%= version %>-<%= os_name %><%= os_arch %>.<%= os_archive_ext %>", +}; + +export const FlashplayerLink: Link = { + name: "flashplayer", + version: "32", + url: "https://fpdownload.macromedia.com/pub/flashplayer/updaters/<%= version %>/<%= {linux:'flash_player_sa_linux' + (debug ? '_debug' : '') +'.x86_64.tar.gz','win':'flashplayer_'+version+'_sa'+(debug ? '_debug' : '')+'.exe'}[os_name] %>", +}; + +export const AndroidLink: Link = { + name: "android", + version: "7302050", + url: "https://dl.google.com/android/repository/commandlinetools-<%= os_name %>-<%= version %>_latest.zip", +}; + +export const DefaultParams: string[][] = [ + ["debug", "<%= false %>"], + ["os_name", "<%= {Linux:'linux',Windows_NT:'win'}[os.type()] %>"], + ["os_arch", "<%= {x64:'64',ia32:''}[os.arch()] %>"], + ["os_archive_ext", "<%= {linux:'tar.gz',win:'zip'}[os_name] %>"], +]; + +export const resolveParams = (source: Params, custom?: Params): Params => { + const params: Params = { ...source, os, debug: false }; + if (custom) { + for (const [k, v] of Object.entries(custom)) { + params[k] = template.renderString(v, params); + } + } + for (const [k, v] of DefaultParams) { + if (!Object.hasOwn(params, k)) { + params[k] = template.renderString(v, params); + } + } + return params; +}; + +export const resolveLink = (link: Link | string, params?: Params): string => { + if (typeof link === "string") { + return template.renderString(link, resolveParams({}, params)); + } else { + return template.renderString(link.url, resolveParams(link, params)); + } +}; diff --git a/test/download.test.js b/test/download.test.js index f39c490..c47e6ad 100644 --- a/test/download.test.js +++ b/test/download.test.js @@ -5,18 +5,14 @@ import { join } from "node:path"; import test from "node:test"; import { download } from "../dist/lib/index.mjs"; -const resolveTargetDir = () => mkdtempSync(join(tmpdir(), "test-")); - -const TEST_DATA = { - "https://home.shmyga.ru/public/sdk/haxe/haxe-4.2.5-win.zip": "haxe.exe", - "https://home.shmyga.ru/public/sdk/haxe/haxe-4.2.5-linux64.tar.gz": "haxe", - "https://home.shmyga.ru/public/test.txt": "test.txt", +const testUrl = async (url, file) => { + const target = mkdtempSync(join(tmpdir(), "test-")); + await download(url, target); + assert(existsSync(join(target, file))); }; -for (const [url, file] of Object.entries(TEST_DATA)) { - test(`Test download: ${url}`, async () => { - const target = resolveTargetDir(); - await download(url, target); - assert(existsSync(join(target, file))); - }); -} +test("zip", async () => await testUrl("https://home.shmyga.ru/public/sdk/haxe/haxe-4.2.5-win.zip", "haxe.exe")); + +test("tar.gz", async () => await testUrl("https://home.shmyga.ru/public/sdk/haxe/haxe-4.2.5-linux64.tar.gz", "haxe")); + +test("plain", async () => await testUrl("https://home.shmyga.ru/public/test.txt", "test.txt")); diff --git a/test/link.test.js b/test/link.test.js new file mode 100644 index 0000000..61e4d07 --- /dev/null +++ b/test/link.test.js @@ -0,0 +1,37 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { AndroidLink, FlashplayerLink, HaxeLink, NekoLink, resolveLink } from "../dist/lib/index.mjs"; + +const testLink = (link, args) => { + for (const [params, targetUrl] of args) { + const url = resolveLink(link, params); + assert.equal(url, targetUrl); + } +}; + +test("neko", () => + testLink(NekoLink, [ + [{ os_name: "linux" }, "https://github.com/HaxeFoundation/neko/releases/download/v2-4-1/neko-2.4.1-linux64.tar.gz"], + [{ os_name: "win" }, "https://github.com/HaxeFoundation/neko/releases/download/v2-4-1/neko-2.4.1-win64.zip"], + ])); + +test("haxe", () => + testLink(HaxeLink, [ + [{ os_name: "linux" }, "https://github.com/HaxeFoundation/haxe/releases/download/3.4.2/haxe-3.4.2-linux64.tar.gz"], + [{ os_name: "win" }, "https://github.com/HaxeFoundation/haxe/releases/download/3.4.2/haxe-3.4.2-win64.zip"], + ])); + +test("flashplayer", () => + testLink(FlashplayerLink, [ + [ + { os_name: "linux" }, + "https://fpdownload.macromedia.com/pub/flashplayer/updaters/32/flash_player_sa_linux.x86_64.tar.gz", + ], + [{ os_name: "win" }, "https://fpdownload.macromedia.com/pub/flashplayer/updaters/32/flashplayer_32_sa.exe"], + ])); + +test("android", () => + testLink(AndroidLink, [ + [{ os_name: "linux" }, "https://dl.google.com/android/repository/commandlinetools-linux-7302050_latest.zip"], + [{ os_name: "win" }, "https://dl.google.com/android/repository/commandlinetools-win-7302050_latest.zip"], + ]));