takin
Version:
Front end engineering base toolchain and scaffold
61 lines • 1.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.download = exports.getName = exports.supportProtocol = exports.parseOptions = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = __importDefault(require("lodash"));
const errors_1 = require("../errors");
const FILE_URL_REGEXP = /^(?:(?:file:\/?)|(\.\.\/)|(\.\/)|(\/)|(~\/))((?:[^/]+\/)*[^/]+\/?)$/;
/**
* 解析 file 链接或选项
* @param pathOrOptions - 链接或选项
* @returns 解析后的 file 链接或选项
*/
function parseOptions(pathOrOptions) {
let path;
let options = {};
if (typeof pathOrOptions === 'string') {
path = pathOrOptions;
}
else {
path = pathOrOptions.path;
options = { ...options, ...pathOrOptions };
}
if (!path)
throw new errors_1.DownloaderError('解析 file 选项错误: 缺少 path');
return { ...options, path };
}
exports.parseOptions = parseOptions;
/**
* 判断是否支持处理当前链接
* @param url - 链接
* @returns 是否支持该链接
*/
function supportProtocol(url) {
if (!url)
return false;
return FILE_URL_REGEXP.test(url);
}
exports.supportProtocol = supportProtocol;
/**
* 基于 file 链接选项获取名称
* @param fileOptions - file 链接选项
* @returns 名称
*/
function getName(fileOptions) {
return (fileOptions.path || '').trim().replace(/^\//, '');
}
exports.getName = getName;
/**
* 下载 file 链接到指定目录
* @param fileOptions - file 链接选项
* @param dest - 指定目录地址
*/
async function download(fileOptions, dest) {
await fs_extra_1.default.remove(dest);
await fs_extra_1.default.copy(fileOptions.path, dest, lodash_1.default.omit(fileOptions, 'path'));
}
exports.download = download;
//# sourceMappingURL=file.js.map