test: add tests
This commit is contained in:
Generated
+1969
-2832
File diff suppressed because it is too large
Load Diff
+12
-7
@@ -14,7 +14,8 @@
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
"dev": "tsdown --watch",
|
||||
"typecheck": "tsc --noEmit"
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "node --test"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -30,12 +31,12 @@
|
||||
"dependencies": {
|
||||
"ansi-colors": "^1.1.0",
|
||||
"async": "^2.6.0",
|
||||
"cli-progress": "^3.0.0",
|
||||
"cli-progress": "^3.12.0",
|
||||
"dateformat": "^5.0.4",
|
||||
"fancy-log": "^1.3.2",
|
||||
"fs-extra": "^5.0.0",
|
||||
"got": "^9.6.0",
|
||||
"gulp": "^4.0.0",
|
||||
"got": "^16.1.0",
|
||||
"gulp": "^5.0.1",
|
||||
"gulp-clean": "^0.4.0",
|
||||
"gulp-debian": "^0.1.9",
|
||||
"gulp-gzip": "^1.4.2",
|
||||
@@ -49,19 +50,23 @@
|
||||
"plugin-error": "^1.0.1",
|
||||
"promise-streams": "^2.1.1",
|
||||
"tail": "^1.2.3",
|
||||
"tar": "^4.4.10",
|
||||
"tar": "^7.5.22",
|
||||
"through2": "^2.0.3",
|
||||
"time-stamp": "^1.1.0",
|
||||
"tmp-file": "^2.0.1",
|
||||
"unzip-stream": "^0.3.0",
|
||||
"unzip-stream": "^0.3.4",
|
||||
"vinyl": "^2.1.0",
|
||||
"vinyl-fs": "^3.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cli-progress": "^3.11.6",
|
||||
"@types/fs-extra": "^11.0.4",
|
||||
"@types/gulp": "^4.0.18",
|
||||
"@types/gulp-rename": "^2.0.7",
|
||||
"@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",
|
||||
"eslint": "^10.12.0",
|
||||
"prettier": "^3.9.10",
|
||||
|
||||
@@ -75,17 +75,8 @@ export class Config {
|
||||
this.flags = [];
|
||||
this.icon = null;
|
||||
this.meta = {
|
||||
title: null,
|
||||
filename: null,
|
||||
icon: null,
|
||||
version: null,
|
||||
pack: null,
|
||||
author: null,
|
||||
company: null,
|
||||
width: 800,
|
||||
height: 600,
|
||||
mobileWidth: null,
|
||||
mobileHeight: null,
|
||||
fps: 60,
|
||||
};
|
||||
this.key = null;
|
||||
|
||||
@@ -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
@@ -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 path from "path";
|
||||
import ps from "promise-streams";
|
||||
import tar from "tar";
|
||||
import unzip from "unzip-stream";
|
||||
import { Log } from "../common/log";
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
import { Downloader } from "./download";
|
||||
|
||||
export class Sdk {
|
||||
static _dir: string;
|
||||
|
||||
+2
-1
@@ -6,7 +6,8 @@ import { Env } from "../common/env";
|
||||
import exec from "../common/exec";
|
||||
import { System } from "../common/system";
|
||||
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";
|
||||
|
||||
export class Android extends Sdk {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from "./common/system";
|
||||
export * from "./core/config";
|
||||
export * from "./core/download";
|
||||
export * from "./core/sdk";
|
||||
export * from "./core/type";
|
||||
export * from "./impl/android";
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import gulp from "gulp";
|
||||
import type { Task, TaskFunction } from "undertaker";
|
||||
import { Config } from "../core/config";
|
||||
import { BuildSystem, Platform } from "../core/type";
|
||||
import { Builder } from "./core/builder";
|
||||
@@ -7,8 +8,6 @@ import { Runner } from "./core/runner";
|
||||
import { RuffleRunner } from "./impl/runner/ruffle";
|
||||
export * from "./core";
|
||||
|
||||
export type Task = any; // TODO: gulp task type
|
||||
|
||||
export class Project {
|
||||
// TODO: compatibility
|
||||
static Config = Config;
|
||||
@@ -56,8 +55,8 @@ export class Project {
|
||||
return [packer.prepare.bind(packer), packer.call.bind(packer)];
|
||||
}
|
||||
|
||||
compile(external_gulp?: any): Record<string, Task> {
|
||||
const result: Record<string, Task> = {};
|
||||
compile(external_gulp?: gulp.Gulp): Record<string, TaskFunction> {
|
||||
const result: Record<string, TaskFunction> = {};
|
||||
const _gulp = external_gulp || gulp;
|
||||
for (const platform of this.platforms) {
|
||||
result[`${this.config.name}:${platform}:build`] = _gulp.series(this.build(platform));
|
||||
@@ -74,7 +73,7 @@ export class Project {
|
||||
return result;
|
||||
}
|
||||
|
||||
register(external_gulp?: any) {
|
||||
register(external_gulp?: gulp.Gulp) {
|
||||
const _gulp = external_gulp || gulp;
|
||||
for (const [k, v] of Object.entries(this.compile(_gulp))) {
|
||||
_gulp.task(k, v);
|
||||
|
||||
+17
-16
@@ -3,6 +3,7 @@ import gulp from "gulp";
|
||||
import gulpClean from "gulp-clean";
|
||||
import template from "lodash.template";
|
||||
import os from "os";
|
||||
import type { Task } from "undertaker";
|
||||
import { Builder, Project } from ".";
|
||||
import { System } from "../common/system";
|
||||
import { Config, type ConfigParams } from "../core/config";
|
||||
@@ -65,7 +66,7 @@ export class Preset {
|
||||
return projects;
|
||||
}
|
||||
|
||||
register(external_gulp?: any, additional_tasks?: string[]) {
|
||||
register(external_gulp?: gulp.Gulp, additional_tasks?: string[]) {
|
||||
const _gulp = external_gulp || gulp;
|
||||
|
||||
const resolvePath = (path: string) => path.replace("$HOME", os.homedir());
|
||||
@@ -114,15 +115,15 @@ export class Preset {
|
||||
project.register(_gulp);
|
||||
}
|
||||
|
||||
const defaultSeries = [_gulp.task("clean"), _gulp.task("setup")];
|
||||
const defaultSeries: Task[] = [_gulp.task("clean")!, _gulp.task("setup")!];
|
||||
|
||||
if (protohx) {
|
||||
defaultSeries.push("protohx:generate");
|
||||
defaultSeries.push(_gulp.task("protohx:generate")!);
|
||||
}
|
||||
|
||||
if (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) {
|
||||
switch (platform) {
|
||||
case Platform.FLASH:
|
||||
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}:build`)!);
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:html`)!);
|
||||
break;
|
||||
case Platform.HTML5:
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`));
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
|
||||
break;
|
||||
case Platform.LINUX:
|
||||
if (System.isLinux) {
|
||||
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}:deb`));
|
||||
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}:deb`)!);
|
||||
}
|
||||
break;
|
||||
case Platform.CPP:
|
||||
if (System.isLinux) {
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`));
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
|
||||
}
|
||||
break;
|
||||
case Platform.NEKO:
|
||||
if (System.isLinux) {
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`));
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
|
||||
}
|
||||
break;
|
||||
case Platform.ANDROID:
|
||||
if (System.isLinux) {
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`));
|
||||
defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)!);
|
||||
}
|
||||
break;
|
||||
case Platform.WINDOWS:
|
||||
if (System.isWindows) {
|
||||
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}:installer`));
|
||||
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}:installer`)!);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -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)));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user