UNPKG

@arkts/sdk-downloader

Version:
119 lines (116 loc) 4.48 kB
const require_download = require('./download-CUyFpN2j.cjs'); const node_fs = require_download.__toESM(require("node:fs")); const node_path = require_download.__toESM(require("node:path")); const node_process = require_download.__toESM(require("node:process")); const pino = require_download.__toESM(require("pino")); const pino_pretty = require_download.__toESM(require("pino-pretty")); //#region src/errors/cli.ts var CliError = class extends Error { constructor(message, cliOptions) { super(message); this.cliOptions = cliOptions; } }; //#endregion //#region src/command-line.ts function isLogType(logType) { return [ "explicit", "full", "silent" ].includes(logType); } async function logSdkDirStructure(logger, unResolveSdkDir) { const sdkDir = node_path.default.resolve(unResolveSdkDir); const sdkDirContents = node_fs.default.readdirSync(sdkDir); logger.info({ sdkDirContents }, `SDK directory structure:`); sdkDirContents.forEach((item) => { const ohUniPackageJson = node_path.default.resolve(sdkDir, item, "oh-uni-package.json"); logger.info({ msg: `|- ${node_path.default.resolve(sdkDir, item)}`, ohUniPackageJson: node_fs.default.existsSync(ohUniPackageJson) ? JSON.parse(node_fs.default.readFileSync(ohUniPackageJson, "utf-8")) : null }); }); } async function runCommandLineDownload(options) { if (!options.apiVersion || !options.arch || !options.os) throw new CliError("Please provide --api-version, --arch, and --os options.", options); if (!isLogType(options.logType)) throw new CliError(`Invalid log type: ${options.logType}. Valid options are 'explicit', 'full', or 'silent'.`, options); const url = require_download.getSdkUrl(require_download.SdkVersion[options.apiVersion], require_download.SdkArch[options.arch], require_download.SdkOS[options.os]); if (!url) throw new CliError(`No SDK found for version ${options.apiVersion}, architecture ${options.arch}, and OS ${options.os}.`, options); const logger = options.logger ?? (0, pino.default)((0, pino_pretty.default)({ colorize: true, colorizeObjects: true, singleLine: true })); logger.info(options, `CLI Options:`); const abortController = new AbortController(); node_process.default.on("exit", () => abortController.abort()); const downloader = await require_download.createDownloader({ url, cacheDir: options.cacheDir, targetDir: options.targetDir, resumeDownload: true, signal: abortController.signal }); downloader.on("*", (ev, data) => { if (options.logType === "silent") return; if (options.logType === "full") return logger.info({ event: ev, data }, `Event: ${ev}`); }); if (options.logType === "explicit") { let lastLogTime = 0; downloader.on("download-progress", (progress) => { const now = Date.now(); if (now - lastLogTime >= options.logTimeout) { logger.info({ ...progress, msg: `Percentage: ${progress.percentage.toFixed(2)}%, current speed: ${progress.network}${progress.unit}/s` }); lastLogTime = now; } }); downloader.on("zip-extracted", (entry) => { const now = Date.now(); if (now - lastLogTime >= options.logTimeout) { logger.info(entry, `Extracted file in zip: ${entry.path}...`); lastLogTime = now; } }); downloader.on("tar-extracted", (entry) => { const now = Date.now(); if (now - lastLogTime >= options.logTimeout) { logger.info(entry, `Extracted file in tar.gz: ${entry.path}...`); lastLogTime = now; } }); } logger.info(`Starting download from ${url}`); await downloader.startDownload(); logger.info(`Download completed and saved to ${options.targetDir}, starting SHA256 check...`); await downloader.checkSha256(); logger.info(`SHA256 check passed, SDK is ready in ${options.targetDir}, starting extract tar...`); await downloader.extractTar(); logger.info(`SDK extracted successfully, starting extract zip...`); await downloader.extractZip(); logger.info(`SDK extracted successfully, cleanup...`); await downloader.clean(); logger.info(`Cleanup completed, SDK is ready in ${options.targetDir}.`); await logSdkDirStructure(logger, options.targetDir); return { logger }; } //#endregion Object.defineProperty(exports, 'logSdkDirStructure', { enumerable: true, get: function () { return logSdkDirStructure; } }); Object.defineProperty(exports, 'runCommandLineDownload', { enumerable: true, get: function () { return runCommandLineDownload; } }); //# sourceMappingURL=command-line-CruY3d41.cjs.map