基本上,作为清理任务的一部分,我想自动删除根文件夹中存在于src文件夹中的所有文件。因为根目录中有不在src中的文件,所以我不能删除所有这些文件,而且我希望保持这个过程是动态的(自动确定匹配的文件),而不是手动的(显式列出要删除的文件)。
现在,我正在手动执行此操作:
const deleteViewFiles = done => {
del.sync([
'./page-*.php',
'./header.php',
]);
done();
}但我想要的是,查看./src/views/,找到./src/views/的所有直系后代(不是子文件夹),并在根目录中删除它们。
例如:假设在./src/views/中有page-1.php、header.php、my-dog.php、mermaid.html,我想从根目录删除所有这些文件,而不是在删除任务中列出它们。
发布于 2020-02-12 03:05:51
好的。这就是我最终要做的。
我使用glob将src目录中的所有文件存储到一个数组中。
然后,我将视图文件夹的基本路径替换为根基本路径。然后我使用这个数组作为我的del函数的参数。
(I stored paths in a config file, here's a snippet (the real object is much bigger):)
paths = {
views: {
src: `${dirs.src}views/`,
dest: `${dirs.base}views/`,
rootFiles: `${dirs.src}views/*.*`,
folders: `${dirs.src}views/*/*`
}
}
import { paths } from '../config';
const clean = done => {
// get all files at root of views directory
const viewsDirRootFiles = glob.sync(paths.views.rootFiles);
// replace views dir in base of filepaths with root
const rootFiles = viewsDirRootFiles.map(f => f.replace(paths.views.src, paths.dirs.base));
// combine root view files with other folders/files to delete to generate final deletion-target array
const filesToDelete = [
...rootFiles,
'./views',
];
del.sync(filesToDelete);
done();
}https://stackoverflow.com/questions/60174643
复制相似问题