gpt4 book ai didi

node.js - 使用 `ncp` 复制文件抛出 : no such file or directory, mkdir

转载 作者:行者123 更新时间:2023-12-04 08:04:56 26 4
gpt4 key购买 nike

我正在使用 ncp复制文件如下:

import ncp from "ncp";
import { promisify } from "util";

const ncpPromise = promisify(ncp);
const copyAssets = async (exportFolderName, includeSourceMaps) => {
const assets = glob.sync("**/", { cwd: distPath });
const options = { clobber: true, stopOnErr: true };
if (!includeSourceMaps) {
options.filter = (f) => {
return !f.endsWith(".map");
};
}
return Promise.all(
assets.map((asset) => {
return ncpPromise(
path.join(distPath, asset),
path.join(exportPath, exportFolderName, asset),
options
);
})
);
};
但这有时会失败并出现以下错误:
"ENOENT: no such file or directory, mkdir '/path/to/folder'"
我该如何解决这个问题?

最佳答案

我猜您正在尝试复制与给定 glob 匹配的所有文件,因此您需要执行以下操作:

const assets = glob.sync("**/*.*", { cwd: distPath }); // note the *.*
例如,您当前有问题的 glob 将导致:
[
'folder1/',
'folder2/',
]
而这个答案中的 glob 将导致(这就是你想要的):
[
'folder1/file1.txt',
'folder1/file2.txt',
'folder2/anotherfile.txt',
]

替代:
好像 ncp没有被维护。因此,您可以使用 fs-extra ,它也可以复制文件和目录:
const glob = require("glob");
const path = require("path");
const fs = require("fs-extra");

const copyAssets = async (exportFolderName, includeSourceMaps) => {
const assets = glob.sync("**/*.*", { cwd: distPath });

const options = { overwrite: true };

if (!includeSourceMaps) {
options.filter = (f) => {
return !f.endsWith(".map");
};
}

return Promise.all(
assets.map((asset) => {
return fs
.copy(
path.join(distPath, asset),
path.join(exportPath, exportFolderName, asset),
options
)
.catch((e) => console.log(e));
})
);
};

关于node.js - 使用 `ncp` 复制文件抛出 : no such file or directory, mkdir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66264194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com