Skip to content

Instantly share code, notes, and snippets.

@dabbott
Last active April 18, 2020 03:23
Show Gist options
  • Save dabbott/a7c23da95b90ef34374144ff5d61670e to your computer and use it in GitHub Desktop.
Save dabbott/a7c23da95b90ef34374144ff5d61670e to your computer and use it in GitHub Desktop.
Lona: memfs + unionfs proof of concept
import { FSWrapper } from '@lona/compiler/lib/helpers/fs'
import { Volume, createFsFromVolume } from 'memfs'
import { Union } from 'unionfs'
import fs from 'fs'
import path from 'path'
import { Volume as VolumeType } from 'memfs/lib/volume'
// Create a filesystem wrapper.
// Reading files: First try to read from the in-memory FS, falling back to the disk if a file is missing.
// Writing files: All files are written to the in-memory FS. We can write all files to disk later.
export const createFileSystem: () => {
volume: VolumeType
wrapper: FSWrapper
} = () => {
const volume = new Volume()
const mem = createFsFromVolume(volume)
const union = new Union()
union.use(fs)
union.use(mem as any) // Cast type as temp workaround for https://github.com/streamich/unionfs/issues/453
return {
volume,
wrapper: {
readFile: (filePath: string) => {
return Promise.resolve(union.readFileSync(filePath).toString('utf8'))
},
writeFile: (filePath: string, data: string) => {
mem.mkdirpSync(path.dirname(filePath))
return volume.promises.writeFile(filePath, data)
},
copyDir: (dirPath: string, output?: string) => {
mem.mkdirpSync(path.dirname(dirPath))
return volume.promises.copyFile(dirPath, output || '')
},
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment