Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vue-reactivity/fs

Reactive filesystem powered by @vue/reactivity
https://github.com/vue-reactivity/fs

fs reactive-fs vue-reactivity vue3

Last synced: 2 days ago
JSON representation

Reactive filesystem powered by @vue/reactivity

Awesome Lists containing this project

README

        




Reactive filesystem powered by @vue/reactivity


npm
npm bundle size

## Install


npm i @vue-reactivity/fs

### Usage

> Work only in Node.js

#### Async usage

```ts
import { useFile } from '@vue-reactivity/fs'

const fileRef = await useFile('messages.txt').waitForReady()

console.log(fileRef.value) // output file content

fileRef.value += 'Hello World' // append to file

fileRef.value = 'Good Morning' // write to file
```

#### Callback usage

```ts
import { useFile } from '@vue-reactivity/fs'

useFile('messages.txt')
.waitForReady()
.then(fileRef => {
console.log(fileRef.value) // output file content
})
```

#### Watch for file changes (via [`chokidar`](https://github.com/paulmillr/chokidar))

```ts
const fileRef = useFile('messages.txt', { watchFileChanges: true })
```

#### `useJson`

```ts
import { useJSON } from '@vue-reactivity/fs'

const data = useJSON('data.json', { initialValue: { foo: 'bar' }})

console.log(data.value) // { foo: 'bar' }

data.value = { bar: 'foo' } // write to json file
```

#### Custom serializer

```ts
import YAML from 'js-yaml'
import { useFileWithSerializer } from '@vue-reactivity/fs'

export function useYAML(path: string, options: JSONSerializerOptions = {}) {
return useFileWithSerializer(
path,
{
...options,
serialize: v => YAML.safeDump(v)
deserialize: v => YAML.safeLoad(v),
},
)
}
```

## License

MIT