0

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
Mojtaba Barari
  • 1,127
  • 1
  • 15
  • 49
  • Really not sure what is happening here (not used to work with this kind of structure rewrite), but isn't there any async issue here? Like if a folder is not done creating and you're depending on it? – kissu May 05 '21 at 13:13
  • what i found out is that the problem is with rewrite. when there is a duplicate file it throw an error that such file is not found on destination folder and create the directory then on second try it will copy – Mojtaba Barari May 06 '21 at 08:00
  • There is maybe a flag there, as `mkdir -p` in bash. – kissu May 06 '21 at 08:07

0 Answers0