How to copy files and folders using fs-extra in Nuxt

1k views Asked by At

i'm on Nuxtjs 2.15.4 and I'm trying to create multi themes for my app.

The flow is very simple: I have a themes folder with my themes folder within. At nuxt build/dev a module will be called:

const path = require('path')
const chokidar = require('chokidar');
const fse = require('fs-extra');

export default async function (moduleOptions) {
    // moduleOptions.themeName = 'newtheme' &  moduleOptions.customizeTheme = '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 () => {
        fse.emptyDir(path.join(this.options.rootDir, 'assets'))
        fse.emptyDir(path.join(this.options.rootDir, 'components'))
        fse.emptyDir(path.join(this.options.rootDir, 'layouts'))
        fse.emptyDir(path.join(this.options.rootDir, 'middleware'))
        fse.emptyDir(path.join(this.options.rootDir, 'pages'))
        fse.emptyDir(path.join(this.options.rootDir, 'plugins'))
        fse.emptyDir(path.join(this.options.rootDir, 'static'))
    }
    const copyThemesDirectory = async () => {
        await fse.copy(path.join(defaultThemeDir, 'base'), sourceDir)
        if(themeName !== 'main'){
            await fse.copy(path.join(newThemeDir, 'base'), sourceDir)
        }
        if(moduleOptions.customizeTheme === 'true'){
            await fse.copy(path.join(newThemeDir, 'custom'), sourceDir)
        }
    }
    const toTargetPath = (oldPath) => {
        let newPath = oldPath.replace(this.options.rootDir, '')
        .replace('\\themes\\main\\base\\', '\\')
        .replace('\\themes\\main\\custom\\', '\\')
        .replace('\\themes\\'+themeName+'\\base\\', '\\')
        .replace('\\themes\\'+themeName+'\\custom\\', '\\')
        return path.join(this.options.rootDir, newPath)
    }

    await emptySourceDir()
    await copyThemesDirectory()

    if(process.env.NODE_ENV === 'development'){
        chokidar.watch([defaultThemeDir, newThemeDir]).on('all', async (event, filePath) => {
            if (event === 'add' || event === 'change') {
                fse.copy(filePath, toTargetPath(filePath))
            }
            if (event === 'unlink') {
                fse.remove(toTargetPath(filePath))
            }
        })
    }
}

it will empty the some folders in nuxt root directory, and then copy themes/main/base into them. after that it will check if theme name is not "main" it will copy themes/{themeName}/base into root directory. then it will check for customization option and if true, it will copy theme/{themeName}/custom folders to root Directory.

this part is done without problem! the second part that is for development mode, gives me error that such files (new files that added to new theme or custom) don't exist.

that part with the help of chokidar watch my themes folder and if anything is changed it will remove or copy that on root directory. the error is shown if the same file exits in the main theme base folders.

I even tried fse.copy(filePath, toTargetPath(filePath),{overwrite: true}) and fse.copySync(filePath, toTargetPath(filePath),{overwrite: true})

here is an example of errors:

ERROR ENOENT: no such file or directory, unlink '{my-local-directory}\components\global\footer\sub\links.vue'

anyone know what's wrong with it?? it also run the watch change or add part even on first try when simply copying from themes to root.

#UPDATE

looks like there is error even in build. I build and it gives me

Error: ENOENT: no such file or directory, mkdir '/var/www/static/images/folder'

then again when i'm building it run without error. this is my themes folder structure:

-themes
|__ main
|   |__base
|   |   |__assets
|   |   |__components
|   |   |__middleware
|   |   |__layouts
|   |   |__pages
|   |   |__plugins
|   |   |__static
|   |
|   |__custom
|
|__ newtheme
    |__base
    |__custom
0

There are 0 answers