2025-02-28 19:43:11 +08:00

82 lines
2.5 KiB
JavaScript

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var promises = require('fs/promises');
var fs = require('fs');
var path = require('path');
var fg = require('fast-glob');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var fg__default = /*#__PURE__*/_interopDefaultLegacy(fg);
async function prepare_dir(dir) {
const parents = path.dirname(dir);
await promises.access(parents, fs.constants.F_OK).catch(async (err) => {
if (err.code == 'ENOENT') await prepare_dir(parents);
});
await promises.access(dir, fs.constants.F_OK).catch(async (err) => {
if (err.code == 'ENOENT') await promises.mkdir(dir).catch(err => {
if (err.code !== 'EEXIST') console.log(err);
});
});
}
async function copyToDir(src, tarDir) {
await prepare_dir(tarDir);
let notExsit = false;
const fstat = await promises.stat(src).catch(err => {
if (err.code == 'ENOENT') {
console.log(tarDir + ' is not exist');
notExsit = true;
}
});
if (notExsit) return;
const tarPath = path.join(tarDir, path.basename(src));
if (fstat.isDirectory()) {
for (const file of await promises.readdir(src)) {
const srcPath = path.join(src, file);
copyToDir(srcPath, tarPath);
}
} else {
await promises.copyFile(src, tarPath).catch(err => {
console.log(`The file "${src}" could not be copied`);
});
}
}
function copy(copyList, options = {}) {
const { hook = 'buildEnd' } = options;
copyList = Array.isArray(copyList) && copyList.length
? copyList
: [];
return {
name: 'copy',
apply: 'build',
[hook]: async () => {
for (const { src, dest } of copyList) {
const matchedSrcs = await fg__default['default'](src, {
expandDirectories: false,
onlyFiles: false,
});
if (matchedSrcs.length > 0) {
for (const src of matchedSrcs) {
if (Array.isArray(dest)) {
for (const dest of tasks) {
await copyToDir(src, dest);
}
} else {
await copyToDir(src, dest);
}
}
}
}
}
}
}
exports.copy = copy;