isomorphic-git / lightning-fs

A lean and fast 'fs' for the browser
MIT License
477 stars 47 forks source link

ENOENT on existing resource #6

Closed snoop244 closed 5 years ago

snoop244 commented 5 years ago

Hi - thanks for the great projects.

I'm having trouble with readFile. I'm getting the following error:

Error: ENOENT: /TypescriptGeneratorTest
    at CacheFS._lookup (CacheFS.js:105)
    at CacheFS.writeFile (CacheFS.js:154)
    at superblockPromise.then (index.js:146)

with the code posted below. Note: I tried the utf8 option directly from a string and, per the code below, I'm now encoding the data to Uint8Array. The path fed to the writeFile is actually /TypescriptGeneratorTest/index.js thought the error only shows the directory - which definitely exists. What am I missing?


export function writeFileToFS (contentMetaData, content) {
    var filePath = contentMetaData.filePath
    if (!("TextEncoder" in window)) {
        alert("Sorry, this browser does not support TextEncoder...")
    } else {
        var enc = new TextEncoder(); // always utf-8
        var encodedContent = enc.encode(content)
        console.log('encoded content: ')
        console.log(encodedContent)
        window.pfs.writeFile(filePath, encodedContent, ((err) => {
            if(err) {
                console.error('could not write file to file system due to the following error: ')
                console.log(err)
            } else { // if successful, replace the rawContentItem in the store with the new one from the filesystem
                console.log('the following file was written succesfully to the file system: ' + filePath)
                readFileToStore(filePath)
            }
        }))
    }
}
billiegoose commented 5 years ago

You're mixing pfs which uses promises with fs which uses callbacks maybe? I'm not sure since I can't tell what window.pfs is.

snoop244 commented 5 years ago

Yes, I've wrapped window.fs in pify, per your example. My readFile works fine and uses the same approach as my posted code, above. I'm a JS newbie.

I switched it to fs with the callback. Same error. I switched it back to pfs, removed the callback and caught the same error in the .then() clause. I confess that promises are confusing to me, so if this is just me not knowing javascript, go ahead and close this.

How I initialize the file system

export function initFS () {
    // Initialize isomorphic-git with isomorphic-git's home-built file system
    window.fs = new LightningFS('fs')
    git.plugins.set('fs', window.fs)
    // make a Promisified version for convenience
    window.pfs = pify(window.fs)
}

and the changed code:

export function writeFileToFS (contentMetaData, content) {
    var filePath = contentMetaData.filePath
    if (!("TextEncoder" in window)) {
        alert("Sorry, this browser does not support TextEncoder...")
    } else {
        var enc = new TextEncoder(); // always utf-8
        var encodedContent = enc.encode(content)
        console.log('encoded content: ')
        console.log(encodedContent)
        window.pfs.writeFile(filePath, encodedContent).then((err) => {
            if(err) {
                console.error('could not write file to file system due to the following error: ')
                console.log(err)
            } else { // if successful, replace the rawContentItem in the store with the new one from the filesystem
                console.log('the following file was written succesfully to the file system: ' + filePath)
                readFileToStore(filePath)
            }
        })
    }
}
snoop244 commented 5 years ago

Closing. I'm unclear on how I fixed this, but it seems that the filePath I was feeding it was malformed through some unknown magic that I rendered upstream of this call. Apologies. When I simply grab the global window.dir and append the file name, it works.