Closed snoop244 closed 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.
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)
}
})
}
}
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.
Hi - thanks for the great projects.
I'm having trouble with readFile. I'm getting the following error:
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?