116 lines
3.8 KiB
Plaintext
116 lines
3.8 KiB
Plaintext
"use strict";
|
|
const node_fs = require("node:fs");
|
|
const node_path = require("node:path");
|
|
const isWhatType = require("is-what-type");
|
|
const tinyglobby = require("tinyglobby");
|
|
const vpRuntimeHelper = require("vp-runtime-helper");
|
|
const promises = require("node:fs/promises");
|
|
const node_util = require("node:util");
|
|
const fsExtra = require("fs-extra");
|
|
const name = "vite-plugin-cp";
|
|
const PLUGIN_NAME = name;
|
|
const logger = vpRuntimeHelper.logFactory.getLogger(PLUGIN_NAME);
|
|
function stringify(value) {
|
|
return node_util.inspect(value, { breakLength: Infinity });
|
|
}
|
|
function changeName(name2, rename) {
|
|
if (typeof rename === "function") {
|
|
return rename(name2) || name2;
|
|
}
|
|
return rename || name2;
|
|
}
|
|
function makeCopy(transform) {
|
|
return typeof transform === "function" ? function(from, to) {
|
|
return promises.readFile(from).then((buf) => transform(buf, from)).then((data) => {
|
|
return fsExtra.outputFile(to, data);
|
|
});
|
|
} : fsExtra.copy;
|
|
}
|
|
async function doCopy(config, cwd, globOptions, copyOptions) {
|
|
const { src, rename, flatten, transform } = config;
|
|
let { dest } = config;
|
|
if (!src || !dest) {
|
|
throw new Error(`${stringify(config)} target must have "src" and "dest" properties.`);
|
|
}
|
|
dest = vpRuntimeHelper.toAbsolutePath(dest, cwd);
|
|
const cpFile = makeCopy(transform);
|
|
const run = async (source) => {
|
|
const absSource = vpRuntimeHelper.toAbsolutePath(source, cwd);
|
|
let isNotFlatten = false;
|
|
try {
|
|
isNotFlatten = node_fs.statSync(absSource).isDirectory() && !flatten;
|
|
} catch (e) {
|
|
}
|
|
const matchedPaths = await tinyglobby.glob(absSource, globOptions);
|
|
if (!matchedPaths.length) {
|
|
logger.warn(`Could not find files with "${source}".`);
|
|
return null;
|
|
}
|
|
await Promise.all(matchedPaths.map((matchedPath) => {
|
|
let targetFileName;
|
|
let destDir = dest;
|
|
if (isNotFlatten) {
|
|
const tmp = node_path.parse(node_path.relative(absSource, matchedPath));
|
|
targetFileName = tmp.base;
|
|
destDir = node_path.join(destDir, tmp.dir);
|
|
} else {
|
|
targetFileName = node_path.parse(matchedPath).base;
|
|
}
|
|
const destPath = node_path.join(destDir, changeName(targetFileName, rename));
|
|
return cpFile(matchedPath, destPath, copyOptions).then(() => {
|
|
logger.trace(`Copied "${matchedPath}" to "${destPath}".`);
|
|
}, (e) => {
|
|
logger.warn(`Could not copy "${matchedPath}" to "${destPath}".`, e);
|
|
});
|
|
}));
|
|
};
|
|
if (typeof src === "string") {
|
|
await run(src);
|
|
} else if (Array.isArray(src)) {
|
|
await Promise.all(src.map(run));
|
|
}
|
|
}
|
|
function pluginCp(opts) {
|
|
const { hook = "closeBundle", enforce, targets, cwd = process.cwd(), globOptions, copyOptions, logLevel, delay, enableBanner } = opts || {};
|
|
if (!Array.isArray(targets) || !targets.length) {
|
|
logger.warn("No targets specified.");
|
|
return;
|
|
}
|
|
if (enableBanner) {
|
|
vpRuntimeHelper.banner(PLUGIN_NAME);
|
|
}
|
|
if (logLevel) {
|
|
logger.level = logLevel;
|
|
}
|
|
const plugin = {
|
|
name: PLUGIN_NAME
|
|
};
|
|
if (enforce) {
|
|
plugin.enforce = enforce;
|
|
}
|
|
let called = false;
|
|
plugin[hook] = async function() {
|
|
if (called) {
|
|
return;
|
|
}
|
|
if (delay !== void 0) {
|
|
await vpRuntimeHelper.sleep(delay);
|
|
}
|
|
called = true;
|
|
const startTime = Date.now();
|
|
await Promise.all(targets.map((target) => {
|
|
if (!isWhatType.isObject(target)) {
|
|
throw new TypeError(`${stringify(target)} target must be an object`);
|
|
}
|
|
return doCopy(target, cwd, Object.assign({}, globOptions, target.globOptions, {
|
|
absolute: true,
|
|
expandDirectories: true,
|
|
onlyFiles: true
|
|
}), Object.assign({}, copyOptions, target.copyOptions));
|
|
}));
|
|
logger.info(`Done in ${vpRuntimeHelper.displayTime(Date.now() - startTime)}`);
|
|
};
|
|
return plugin;
|
|
}
|
|
module.exports = pluginCp;
|