https://github.com/ngoylufo/filer
Filer is a simple file reader/writer for node.
https://github.com/ngoylufo/filer
file-reader file-writer filer reader writer
Last synced: 5 months ago
JSON representation
Filer is a simple file reader/writer for node.
- Host: GitHub
- URL: https://github.com/ngoylufo/filer
- Owner: ngoylufo
- License: mit
- Created: 2020-02-09T15:49:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-18T01:55:04.000Z (about 6 years ago)
- Last Synced: 2025-10-28T15:53:49.646Z (8 months ago)
- Topics: file-reader, file-writer, filer, reader, writer
- Language: TypeScript
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Filer
Filer is a simple file reader/writer for node.
## Installation
If you happened to have stumbled upon this and have some time to kill, installing filer isn't too complicated. Also I couldn't think of a different name.
```sh
npm install @ngoylufo/filer
```
## Usage
Filer only has two use cases, reading and writing to files. If want to order pizza whilst on the toilet, you've come to the wrong place.
### Require filer, preferably, at the top of your file
```js
const filer = require('@ngoylufo/filer');
```
Now you can read and write files.
```js
const package = filer.readFileSync({ filename: `${__dirname}/package.json` });
console.log(package); //
filer
.readFile({
filename: `${__dirname}/app/index.html`
})
.then(text => {
console.log(text); //
})
.catch(err => {
console.log('Something went wrong');
});
// Returns a .
filer.readFileSync('sample.py');
```
### [Optional] Register some formats for default behaviour
```js
// How to deal with .json files.
filer.formats.register({
extension: '.json',
attributes: {
reader: {
options: { encoding: 'utf8' }
coerce: buffer => JSON.parse(buffer.toString())
},
writer: {
coerce: data => typeof data === 'string' ? data : JSON.stringify(data)
}
}
});
// Multiple formats following the same rules.
filer.formats.register({
extension: ['.html', '.css', '.txt'],
attributes: {
reader: { options: { encoding: 'utf8' } },
writer: { options: { encoding: 'utf8' } },
}
});
const package = filer.readFileSync({ filename: `${__dirname}/package.json` });
console.log(package.name); // @ngoylufo/filer
filer.writeFile({
filename: `${__dirname}/index.html`,
data: ''
}).then(() => {
console.log('Wrote to file, now reading from file...');
filer
.readFile({ filename: `${__dirname}/index.html` })
.then(data => {
console.log(data); //
}).catch((contents) => {
// contents === undefined not an error. At least not yet.
console.log('Something went wrong!');
});
});
// Unregistered format, returns a , if file exists.
filer.readFileSync('sample.py');
```
## API
Coming soon...
## License
This project is under the MIT License - see the [LICENSE](LICENSE) details.