Files
NapCatQQ/.pnpm-store/v3/files/68/f5c5cd26e409e47e99a701cf594d5f5f5bd4882e503da6095d8f78f12f1e9a6e0c829dbb2e1afe9b9399177e8d03362ecfac2f6c4988afd4bbbb8e899ce7f7
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

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;