@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
215 lines (211 loc) • 7.77 kB
JavaScript
;
var async = require('../../async.js');
var cli = require('../../cli.js');
require('../../error.js');
var event = require('../../event.js');
var fs = require('../../fs.js');
var fs_util = require('../../fs/util.js');
var object = require('../../object.js');
var path = require('../../path.js');
var runtime = require('../../runtime.js');
var dialog_cli_progress = require('./progress.js');
var dialog_cli_file_linux = require('./file/linux.js');
var dialog_cli_file_mac = require('./file/mac.js');
var dialog_cli_file_windows = require('./file/windows.js');
var error_Exception = require('../../error/Exception.js');
var cli_common = require('../../cli/common.js');
var error_common = require('../../error/common.js');
function throwUnsupportedPlatformError() {
throw new error_common.NotSupportedError("Unsupported platform");
}
async function pickFile(options = {}) {
const _platform = runtime.platform();
if (_platform === "darwin") {
return await dialog_cli_file_mac.macPickFile(options.title, {
type: options.type,
forSave: options === null || options === void 0 ? void 0 : options.forSave,
defaultName: options === null || options === void 0 ? void 0 : options.defaultName,
});
}
else if (_platform === "windows" || cli_common.isWSL()) {
return await dialog_cli_file_windows.windowsPickFile(options.title, {
type: options.type,
forSave: options === null || options === void 0 ? void 0 : options.forSave,
defaultName: options === null || options === void 0 ? void 0 : options.defaultName,
});
}
else if (_platform === "linux" || await cli.which("zenity")) {
return await dialog_cli_file_linux.linuxPickFile(options.title, {
type: options.type,
forSave: options === null || options === void 0 ? void 0 : options.forSave,
defaultName: options === null || options === void 0 ? void 0 : options.defaultName,
});
}
throwUnsupportedPlatformError();
}
async function pickFiles(options = {}) {
const _platform = runtime.platform();
if (_platform === "darwin") {
return await dialog_cli_file_mac.macPickFiles(options.title, options.type);
}
else if (_platform === "windows" || cli_common.isWSL()) {
return await dialog_cli_file_windows.windowsPickFiles(options.title, options.type);
}
else if (_platform === "linux" || await cli.which("zenity")) {
return await dialog_cli_file_linux.linuxPickFiles(options.title, options.type);
}
throwUnsupportedPlatformError();
}
async function pickDirectory(options = {}) {
const _platform = runtime.platform();
if (_platform === "darwin") {
return await dialog_cli_file_mac.macPickFolder(options.title);
}
else if (_platform === "windows" || cli_common.isWSL()) {
return await dialog_cli_file_windows.windowsPickFolder(options.title);
}
else if (_platform === "linux" || await cli.which("zenity")) {
return await dialog_cli_file_linux.linuxPickFolder(options.title);
}
throwUnsupportedPlatformError();
}
async function openFile(options) {
let filename = await pickFile(options);
if (filename) {
return await fs.readFileAsFile(filename);
}
else {
return null;
}
}
async function openFiles(options = {}) {
const filenames = await pickFiles(options);
return await Promise.all(filenames.map(path => fs.readFileAsFile(path)));
}
async function openDirectory(options = {}) {
const dirname = await pickDirectory(options);
if (dirname) {
const files = [];
for await (const entry of fs.readDir(dirname, { recursive: true })) {
if (entry.kind === "file") {
const path$1 = path.join(dirname, entry.relativePath);
const file = await fs.readFileAsFile(path$1);
Object.defineProperty(file, "webkitRelativePath", {
configurable: true,
enumerable: true,
writable: false,
value: entry.relativePath.replace(/\\/g, "/"),
});
files.push(fs_util.fixFileType(file));
}
}
return files;
}
else {
return [];
}
}
async function saveFile(file, options = {}) {
var _a;
const { title } = options;
let filename;
if (typeof Blob === "function" && file instanceof Blob) {
filename = await pickFile({
title,
type: options.type || file.type,
forSave: true,
defaultName: options.name || ((_a = object.as(file, File)) === null || _a === void 0 ? void 0 : _a.name),
});
}
else {
filename = await pickFile({
title,
type: options.type,
forSave: true,
defaultName: options.name,
});
}
if (filename) {
await fs.writeFile(filename, file, object.pick(options, ["signal"]));
}
}
async function downloadFile(url, options = {}) {
var _a;
const src = typeof url === "object" ? url.href : url;
const name = options.name || path.basename(src);
const dest = await pickFile({
title: options.title,
type: options.type,
forSave: true,
defaultName: name,
});
if (!dest) // user canceled
return;
const task = async.asyncTask();
let signal = (_a = options.signal) !== null && _a !== void 0 ? _a : null;
let result;
let updateProgress;
if (options.showProgress) {
const ctrl = new AbortController();
signal = ctrl.signal;
result = dialog_cli_progress.default("Downloading...", async (set) => {
updateProgress = set;
return await task;
}, () => {
ctrl.abort();
throw new error_Exception.default("Download canceled", { name: "AbortError" });
});
}
else {
result = task;
}
const res = await fetch(src, { signal });
if (!res.ok) {
throw new Error(`Failed to download: ${src}`);
}
const size = parseInt(res.headers.get("Content-Length") || "0", 10);
let stream = res.body;
if (options.onProgress || options.showProgress) {
const { onProgress } = options;
let loaded = 0;
const transform = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk);
loaded += chunk.byteLength;
if (onProgress) {
try {
onProgress === null || onProgress === void 0 ? void 0 : onProgress(event.createProgressEvent("progress", {
lengthComputable: !!size,
loaded,
total: size !== null && size !== void 0 ? size : 0,
}));
}
catch (_a) {
// ignore
}
}
if (updateProgress && size) {
updateProgress({
percent: loaded / size,
});
}
},
});
stream = stream.pipeThrough(transform);
}
fs.writeFile(dest, stream, { signal: signal }).then(() => {
task.resolve();
}).catch(err => {
task.reject(err);
});
await result;
}
exports.downloadFile = downloadFile;
exports.openDirectory = openDirectory;
exports.openFile = openFile;
exports.openFiles = openFiles;
exports.pickDirectory = pickDirectory;
exports.pickFile = pickFile;
exports.pickFiles = pickFiles;
exports.saveFile = saveFile;
//# sourceMappingURL=file.js.map