Vue.js 如何在Nuxt中使用fs extra复制文件和文件夹
我使用的是Nuxtjs 2.15.4,我正在尝试为我的应用程序创建多个主题 流程非常简单:我有一个Vue.js 如何在Nuxt中使用fs extra复制文件和文件夹,vue.js,nuxt.js,fs,chokidar,fs-extra,Vue.js,Nuxt.js,Fs,Chokidar,Fs Extra,我使用的是Nuxtjs 2.15.4,我正在尝试为我的应用程序创建多个主题 流程非常简单:我有一个themes文件夹,其中包含我的themes文件夹。在nuxt build/dev中,将调用一个模块: const path=require('path')) const chokidar=require('chokidar'); 常数fse=需要('fs-extra'); 导出默认异步函数(moduleOptions){ //moduleOptions.themeName='newtheme'&m
themes
文件夹,其中包含我的themes文件夹。在nuxt build/dev中,将调用一个模块:
const path=require('path'))
const chokidar=require('chokidar');
常数fse=需要('fs-extra');
导出默认异步函数(moduleOptions){
//moduleOptions.themeName='newtheme'&moduleOptions.CustomizeName='false'
const themeName=moduleOptions.themeName
const defaultThemeDir=path.join(this.options.rootDir'。/themes/main')
const newThemeDir=path.join(this.options.rootDir'./themes/'+moduleOptions.themeName)
const sourceDir=path.join(this.options.rootDir,'./'))
const emptySourceDir=async()=>{
emptyDir(path.join(this.options.rootDir,'assets'))
emptyDir(path.join(this.options.rootDir,'components'))
emptyDir(path.join(this.options.rootDir,'layouts'))
emptyDir(path.join(this.options.rootDir,'middleware'))
emptyDir(path.join(this.options.rootDir,'pages'))
emptyDir(path.join(this.options.rootDir,'plugins'))
emptyDir(path.join(this.options.rootDir,'static'))
}
const copyThemesDirectory=async()=>{
等待fse.copy(path.join(defaultThemeDir,'base')、sourceDir)
if(themeName!=“main”){
等待fse.copy(path.join(newThemeDir,'base'),sourceDir)
}
if(moduleOptions.CustomizeTime==='true'){
等待fse.copy(path.join(newThemeDir,'custom')、sourceDir)
}
}
const toTargetPath=(旧路径)=>{
让newPath=oldPath.replace(this.options.rootDir“”)
.replace(“\\themes\\main\\base\\”,“\\”)
.replace(“\\themes\\main\\custom\\”,“\\”)
.replace(“\\themes\\”+themeName+“\\base\\”,“\\”)
.replace(“\\themes\\”+themeName+“\\custom\\”,“\\”)
返回path.join(this.options.rootDir,newPath)
}
等待清空资源
等待copyThemesDirectory()
if(process.env.NODE_env===‘development’){
在('all',异步(事件,文件路径)=>{
如果(事件=='add'| |事件=='change'){
fse.copy(文件路径,toTargetPath(文件路径))
}
如果(事件=='unlink'){
删除(toTargetPath(文件路径))
}
})
}
}
它将清空nuxt根目录中的一些文件夹,然后将themes/main/base
复制到其中。之后,它将检查主题名是否不是“main”,并将themes/{themeName}/base
复制到根目录中。然后它将检查定制选项,如果为true,它将把theme/{themeName}/custom
文件夹复制到根目录
这部分做得没有问题!第二部分是开发
模式,它告诉我这样的错误:这些文件(添加到新主题或自定义中的新文件)不存在
在chokidar的帮助下,查看我的主题文件夹,如果有任何更改,它将删除或复制根目录中的内容。如果主主题库文件夹中存在相同的文件,则会显示错误
我甚至尝试了fse.copy(filePath,toTargetPath(filePath),{overwrite:true})
和fse.copySync(filePath,toTargetPath(filePath),{overwrite:true})
下面是一个错误示例:
错误enoint:没有这样的文件或目录,请取消链接“{my local directory}\components\global\footer\sub\links.vue”
有人知道它有什么问题吗??它还可以运行watchchange
或add
部分,即使是在第一次尝试时,也可以简单地从主题复制到根目录
#更新
看起来即使在构建中也有错误。我建造,它给我
Error: ENOENT: no such file or directory, mkdir '/var/www/static/images/folder'
然后,当我构建它时,再次运行,没有错误。这是我的主题文件夹结构:
-themes
|__ main
| |__base
| | |__assets
| | |__components
| | |__middleware
| | |__layouts
| | |__pages
| | |__plugins
| | |__static
| |
| |__custom
|
|__ newtheme
|__base
|__custom
真的不确定这里发生了什么(不用于这种结构重写),但是这里没有任何异步问题吗?比如说,如果一个文件夹没有创建完成,你就依赖它了?我发现问题在于重写。当存在重复文件时,它会抛出一个错误,即在目标文件夹中找不到该文件,并创建目录,然后在第二次尝试时,它将复制该文件。可能有一个标志,如bash中的
mkdir-p
。