1

I'm on Nuxt 2.15.4 and i wanna check in my store codes if a file exist in nuxt directory by fs-extra package.

it is simple in modules because i can get files path by this code:

const path = require('path')
const fse = require('fs-extra');
const FilePath = path.join(this.options.rootDir, './static/myfile.json')
const fse = require('fs-extra');
fse.pathExists(FilePath, (err, exists) => {
    console.log(err) // => null
    console.log(exists) // => true
})

but in vuex store i dont have access to this.options.rootDir and this code is always return false:

export const actions = {
  async nuxtServerInit({dispatch, commit}) {
    if(process.server){
      const fse = require('fs-extra');
      fse.pathExists('~/static/myfile.json', (err, exists) => {
        console.log(err) // => null
        console.log(exists) // => false
      })
    }
  }
}

how can i get files fullpath or check if it exits??

#UPDATE

It looks like I had a little mistake in my file path so used ./static/myfile.json and the check is done!!

but got another problem!! I have another json file, when I'm trying to use Object.assign(mainfile, myfile) it won't work!!

here is a sample:

  async nuxtServerInit({dispatch, commit}) {
    let mainfile = require('../assets/mainfile.json')
    // if i use assign here it works and merge them together
    // let myfile = require('../assets/myfile.json')
    // Object.assign(mainfile, myfile)
    if(process.server){
      const fse = require('fs-extra');
      fse.pathExists('./static/myfile.json', (err, exists) => {
        if(exists){
          Object.assign(mainfile, myfile)
          commit('SET_FILE', mainfile); // this send the unmerged file to mutation
          console.log(mainfile); // but get the merged json here
        }
      })
      console.log(mainfile); // it is unmerged
    }
    console.log(mainfile); // it is unmerged
  }

Mojtaba Barari
  • 1,127
  • 1
  • 15
  • 49
  • ah my mistake!! should have used `./static/myfile.json` instead of `~/static/myfile.json` . was gonna delete my question but thought some one my need the solution. so don't down vote me plz :P – Mojtaba Barari May 02 '21 at 15:19
  • Best thing is to write an answer to your own question in this case (and to accept it). Totally fine. :) – kissu May 03 '21 at 08:48
  • @kissu , i updated my question. plz take a look – Mojtaba Barari May 04 '21 at 08:14

2 Answers2

1

For your updated question, be sure that exists is truthy, that you're entering in the loop and that mainfile is in the format that you're expecting.
Then, you could do

mainfile = {...mainfile, ...myfile} // rather than Object.assign
kissu
  • 40,416
  • 14
  • 65
  • 133
  • i tracked all steps by `console.log()` and it looks like `process.server` part is running after @kissu the codes before & after it. the exist statement is true and i can log the result but cant commit vuex mutation. actually `console.log()` gives me the merged json before and after mutation in `fse.pathExists()` callback but the same data is send to mutation and its not merged!! look at the code part in UPDATE – Mojtaba Barari May 04 '21 at 09:25
  • 1
    Commits are synchronous, you should use a vuex action and call it like `await yourFancyVuexAction(mainfile)`. – kissu May 04 '21 at 09:36
  • thank you for your hint !! finally found the problem – Mojtaba Barari May 04 '21 at 10:14
  • i took advantage of your kindness and emailed u about another issue. plz check this if u have time. tanx : https://stackoverflow.com/questions/67356435/how-to-copy-files-and-folders-using-fs-extra-in-nuxt – Mojtaba Barari May 04 '21 at 14:19
1

Ok, thanks to @kissu I found out the problem. As kissu mentioned in his answer's comment, commit is synchronous; I tried await action but didn't get the result; so I used pathExistsSync instead and done!!

  async nuxtServerInit({dispatch, commit}) {
    let myfile = {}
    let mainfile = require('../assets/mainfile.json')
    if(process.server){
      const fse = require('fs-extra');
      if(fse.pathExistsSync('./static/myfile.json')){
          myfile = require('../assets/myfile.json')
          Object.assign(mainfile, myfile)
      }
    }
    await dispatch('setMyFile', mainfile)
  }

#Update

require('../assets/mainfile.json') still throw error if file doesn't exist even with if(fse.pathExistsSync('./static/myfile.json')) statement so:

  async nuxtServerInit({dispatch, commit}) {
    let myfile = {}
    let mainfile = require('../assets/mainfile.json')
    if(process.server){
      const fse = require('fs-extra');
      if(fse.pathExistsSync('./static/myfile.json')){
          myfile = readJsonSync('./static/myfile.json')
          Object.assign(mainfile, myfile)
      }
    }
    await dispatch('setMyFile', mainfile)
  }
Mojtaba Barari
  • 1,127
  • 1
  • 15
  • 49