test: add tests

This commit is contained in:
2026-10-16 00:43:09 +03:00
parent c3c0b07d32
commit 0edfb12473
10 changed files with 2112 additions and 2952 deletions
+1969 -2832
View File
File diff suppressed because it is too large Load Diff
+12 -7
View File
@@ -14,7 +14,8 @@
"scripts": { "scripts": {
"build": "tsdown", "build": "tsdown",
"dev": "tsdown --watch", "dev": "tsdown --watch",
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit",
"test": "node --test"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -30,12 +31,12 @@
"dependencies": { "dependencies": {
"ansi-colors": "^1.1.0", "ansi-colors": "^1.1.0",
"async": "^2.6.0", "async": "^2.6.0",
"cli-progress": "^3.0.0", "cli-progress": "^3.12.0",
"dateformat": "^5.0.4", "dateformat": "^5.0.4",
"fancy-log": "^1.3.2", "fancy-log": "^1.3.2",
"fs-extra": "^5.0.0", "fs-extra": "^5.0.0",
"got": "^9.6.0", "got": "^16.1.0",
"gulp": "^4.0.0", "gulp": "^5.0.1",
"gulp-clean": "^0.4.0", "gulp-clean": "^0.4.0",
"gulp-debian": "^0.1.9", "gulp-debian": "^0.1.9",
"gulp-gzip": "^1.4.2", "gulp-gzip": "^1.4.2",
@@ -49,19 +50,23 @@
"plugin-error": "^1.0.1", "plugin-error": "^1.0.1",
"promise-streams": "^2.1.1", "promise-streams": "^2.1.1",
"tail": "^1.2.3", "tail": "^1.2.3",
"tar": "^4.4.10", "tar": "^7.5.22",
"through2": "^2.0.3", "through2": "^2.0.3",
"time-stamp": "^1.1.0", "time-stamp": "^1.1.0",
"tmp-file": "^2.0.1", "tmp-file": "^2.0.1",
"unzip-stream": "^0.3.0", "unzip-stream": "^0.3.4",
"vinyl": "^2.1.0", "vinyl": "^2.1.0",
"vinyl-fs": "^3.0.2" "vinyl-fs": "^3.0.2"
}, },
"devDependencies": { "devDependencies": {
"@types/cli-progress": "^3.11.6",
"@types/fs-extra": "^11.0.4", "@types/fs-extra": "^11.0.4",
"@types/gulp": "^4.0.18",
"@types/gulp-rename": "^2.0.7", "@types/gulp-rename": "^2.0.7",
"@types/lodash.template": "^4.5.3", "@types/lodash.template": "^4.5.3",
"@types/node": "^26.1.1", "@types/node": "^26.6.5",
"@types/tar": "^7.0.87",
"@types/unzip-stream": "^0.3.4",
"@types/vinyl-fs": "^3.0.8", "@types/vinyl-fs": "^3.0.8",
"eslint": "^10.12.0", "eslint": "^10.12.0",
"prettier": "^3.9.10", "prettier": "^3.9.10",
-9
View File
@@ -75,17 +75,8 @@ export class Config {
this.flags = []; this.flags = [];
this.icon = null; this.icon = null;
this.meta = { this.meta = {
title: null,
filename: null,
icon: null,
version: null,
pack: null,
author: null,
company: null,
width: 800, width: 800,
height: 600, height: 600,
mobileWidth: null,
mobileHeight: null,
fps: 60, fps: 60,
}; };
this.key = null; this.key = null;
+84
View File
@@ -0,0 +1,84 @@
import progress from "cli-progress";
import fs from "fs";
import fse from "fs-extra";
import got from "got";
import path from "path";
import ps from "promise-streams";
import type Stream from "stream";
import * as tar from "tar";
import unzip from "unzip-stream";
export class Downloader {
static bar: progress.MultiBar | null;
static createProgressBar() {
if (Downloader.bar == null) {
Downloader.bar = new progress.MultiBar(
{
clearOnComplete: true,
},
progress.Presets.shades_grey,
);
}
return Downloader.bar.create(0, 0);
}
static releaseProgressBar(bar: progress.SingleBar) {
bar.stop();
if (Downloader.bar != null) {
Downloader.bar.remove(bar);
if (!Downloader.bar.isActive) {
Downloader.bar.stop();
Downloader.bar = null;
}
}
}
static download(url: string, dest: string, strip = 1, executable = false) {
fse.ensureDirSync(dest);
const bar = Downloader.createProgressBar();
let stream: Stream = got.stream(url);
stream = stream
.on("request", (r: any) => {
if (bar) bar.start(1, 0);
})
.on("downloadProgress", (p: any) => {
if (bar) {
bar.setTotal(p.total);
bar.update(p.transferred);
if (p.total === p.transferred) {
Downloader.releaseProgressBar(bar);
}
}
});
if (url.endsWith(".zip")) {
stream = stream.pipe(unzip.Parse()).on("entry", (entry: any) => {
const filePath = entry.path.split("/").slice(strip).join(path.sep);
if (filePath.length > 0) {
if (entry.type === "Directory") {
fse.ensureDirSync(path.join(dest, path.normalize(filePath)));
} else if (entry.type === "File") {
const fullFilePath = path.join(dest, path.normalize(filePath));
const dirPath = path.dirname(fullFilePath);
if (!fs.existsSync(dirPath)) {
fse.ensureDirSync(dirPath);
}
entry.pipe(
fs.createWriteStream(path.join(dest, path.normalize(filePath)), {
// ToDo: zip entry file mode?
mode: executable ? 0o755 : undefined,
}),
);
}
} else {
entry.autodrain();
}
});
} else if (url.endsWith("tar.gz")) {
stream = stream.pipe(tar.x({ C: dest, strip }));
} else {
stream = stream.pipe(fs.createWriteStream(path.join(dest, url.split("/").pop()!)));
}
return ps.wait(stream);
}
}
+1 -82
View File
@@ -1,88 +1,7 @@
import progress from "cli-progress";
import fs from "fs";
import fse from "fs-extra";
import got from "got";
import os from "os"; import os from "os";
import path from "path"; import path from "path";
import ps from "promise-streams";
import tar from "tar";
import unzip from "unzip-stream";
import { Log } from "../common/log"; import { Log } from "../common/log";
import { Downloader } from "./download";
export class Downloader {
static bar: progress.MultiBar;
static createProgressBar() {
if (Downloader.bar == null) {
Downloader.bar = new progress.MultiBar(
{
clearOnComplete: true,
},
progress.Presets.shades_grey,
);
}
return Downloader.bar.create();
}
static releaseProgressBar(bar: progress.MultiBar) {
bar.stop();
if (Downloader.bar != null) {
Downloader.bar.remove(bar);
if (Downloader.bar.bars.length === 0) {
Downloader.bar.stop();
Downloader.bar = null;
}
}
}
static download(url: string, dest: string, strip = 1, executable = false) {
fse.ensureDirSync(dest);
const bar = Downloader.createProgressBar();
let stream = got.stream(url);
stream = stream
.on("request", (r: any) => {
if (bar) bar.start(1, 0);
})
.on("downloadProgress", (p: any) => {
if (bar) {
bar.setTotal(p.total);
bar.update(p.transferred);
if (p.total === p.transferred) {
Downloader.releaseProgressBar(bar);
}
}
});
if (url.endsWith(".zip")) {
stream = stream.pipe(unzip.Parse()).on("entry", (entry: any) => {
const filePath = entry.path.split("/").slice(strip).join(path.sep);
if (filePath.length > 0) {
if (entry.type === "Directory") {
fse.ensureDirSync(path.join(dest, path.normalize(filePath)));
} else if (entry.type === "File") {
const fullFilePath = path.join(dest, path.normalize(filePath));
const dirPath = path.dirname(fullFilePath);
if (!fs.existsSync(dirPath)) {
fse.ensureDirSync(dirPath);
}
entry.pipe(
fs.createWriteStream(path.join(dest, path.normalize(filePath)), {
// ToDo: zip entry file mode?
mode: executable ? 0o755 : undefined,
}),
);
}
} else {
entry.autodrain();
}
});
} else if (url.endsWith("tar.gz")) {
stream = stream.pipe(tar.x({ C: dest, strip: strip }));
} else {
stream = stream.pipe(fs.createWriteStream(path.join(dest, url.split("/").pop()!)));
}
return ps.wait(stream);
}
}
export class Sdk { export class Sdk {
static _dir: string; static _dir: string;
+2 -1
View File
@@ -6,7 +6,8 @@ import { Env } from "../common/env";
import exec from "../common/exec"; import exec from "../common/exec";
import { System } from "../common/system"; import { System } from "../common/system";
import { StringWritable } from "../common/tail"; import { StringWritable } from "../common/tail";
import { Downloader, Sdk } from "../core/sdk"; import { Downloader } from "../core/download";
import { Sdk } from "../core/sdk";
import { Command } from "../run/command"; import { Command } from "../run/command";
export class Android extends Sdk { export class Android extends Sdk {
+1
View File
@@ -1,5 +1,6 @@
export * from "./common/system"; export * from "./common/system";
export * from "./core/config"; export * from "./core/config";
export * from "./core/download";
export * from "./core/sdk"; export * from "./core/sdk";
export * from "./core/type"; export * from "./core/type";
export * from "./impl/android"; export * from "./impl/android";
+4 -5
View File
@@ -1,4 +1,5 @@
import gulp from "gulp"; import gulp from "gulp";
import type { Task, TaskFunction } from "undertaker";
import { Config } from "../core/config"; import { Config } from "../core/config";
import { BuildSystem, Platform } from "../core/type"; import { BuildSystem, Platform } from "../core/type";
import { Builder } from "./core/builder"; import { Builder } from "./core/builder";
@@ -7,8 +8,6 @@ import { Runner } from "./core/runner";
import { RuffleRunner } from "./impl/runner/ruffle"; import { RuffleRunner } from "./impl/runner/ruffle";
export * from "./core"; export * from "./core";
export type Task = any; // TODO: gulp task type
export class Project { export class Project {
// TODO: compatibility // TODO: compatibility
static Config = Config; static Config = Config;
@@ -56,8 +55,8 @@ export class Project {
return [packer.prepare.bind(packer), packer.call.bind(packer)]; return [packer.prepare.bind(packer), packer.call.bind(packer)];
} }
compile(external_gulp?: any): Record<string, Task> { compile(external_gulp?: gulp.Gulp): Record<string, TaskFunction> {
const result: Record<string, Task> = {}; const result: Record<string, TaskFunction> = {};
const _gulp = external_gulp || gulp; const _gulp = external_gulp || gulp;
for (const platform of this.platforms) { for (const platform of this.platforms) {
result[`${this.config.name}:${platform}:build`] = _gulp.series(this.build(platform)); result[`${this.config.name}:${platform}:build`] = _gulp.series(this.build(platform));
@@ -74,7 +73,7 @@ export class Project {
return result; return result;
} }
register(external_gulp?: any) { register(external_gulp?: gulp.Gulp) {
const _gulp = external_gulp || gulp; const _gulp = external_gulp || gulp;
for (const [k, v] of Object.entries(this.compile(_gulp))) { for (const [k, v] of Object.entries(this.compile(_gulp))) {
_gulp.task(k, v); _gulp.task(k, v);
+17 -16
View File
@@ -3,6 +3,7 @@ import gulp from "gulp";
import gulpClean from "gulp-clean"; import gulpClean from "gulp-clean";
import template from "lodash.template"; import template from "lodash.template";
import os from "os"; import os from "os";
import type { Task } from "undertaker";
import { Builder, Project } from "."; import { Builder, Project } from ".";
import { System } from "../common/system"; import { System } from "../common/system";
import { Config, type ConfigParams } from "../core/config"; import { Config, type ConfigParams } from "../core/config";
@@ -65,7 +66,7 @@ export class Preset {
return projects; return projects;
} }
register(external_gulp?: any, additional_tasks?: string[]) { register(external_gulp?: gulp.Gulp, additional_tasks?: string[]) {
const _gulp = external_gulp || gulp; const _gulp = external_gulp || gulp;
const resolvePath = (path: string) => path.replace("$HOME", os.homedir()); const resolvePath = (path: string) => path.replace("$HOME", os.homedir());
@@ -114,15 +115,15 @@ export class Preset {
project.register(_gulp); project.register(_gulp);
} }
const defaultSeries = [_gulp.task("clean"), _gulp.task("setup")]; const defaultSeries: Task[] = [_gulp.task("clean")!, _gulp.task("setup")!];
if (protohx) { if (protohx) {
defaultSeries.push("protohx:generate"); defaultSeries.push(_gulp.task("protohx:generate")!);
} }
if (additional_tasks) { if (additional_tasks) {
for (const task of additional_tasks) { for (const task of additional_tasks) {
defaultSeries.push(_gulp.task(task)); defaultSeries.push(_gulp.task(task)!);
} }
} }
@@ -130,39 +131,39 @@ export class Preset {
for (const platform of project.platforms) { for (const platform of project.platforms) {
switch (platform) { switch (platform) {
case Platform.FLASH: case Platform.FLASH:
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:html`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:html`)!);
break; break;
case Platform.HTML5: case Platform.HTML5:
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
break; break;
case Platform.LINUX: case Platform.LINUX:
if (System.isLinux) { if (System.isLinux) {
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:archive`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:archive`)!);
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:deb`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:deb`)!);
} }
break; break;
case Platform.CPP: case Platform.CPP:
if (System.isLinux) { if (System.isLinux) {
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
} }
break; break;
case Platform.NEKO: case Platform.NEKO:
if (System.isLinux) { if (System.isLinux) {
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
} }
break; break;
case Platform.ANDROID: case Platform.ANDROID:
if (System.isLinux) { if (System.isLinux) {
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
} }
break; break;
case Platform.WINDOWS: case Platform.WINDOWS:
if (System.isWindows) { if (System.isWindows) {
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:archive`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:archive`)!);
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:installer`)); defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:installer`)!);
} }
break; break;
} }
+22
View File
@@ -0,0 +1,22 @@
import assert from "node:assert/strict";
import { existsSync, mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { Downloader } from "../dist/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",
};
for (const [url, file] of Object.entries(TEST_DATA)) {
test(`Test download: ${url}`, async () => {
const target = resolveTargetDir();
await Downloader.download(url, target);
assert(existsSync(join(target, file)));
});
}