All Projects → jonschlinkert → Write

jonschlinkert / Write

Licence: mit
Write data to the file system, creating any intermediate directories if they don't already exist. Used by flat-cache and many others!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Write

write-yaml
Basic node.js utility for converting JSON to YAML and writing formatting YAML files to disk.
Stars: ✭ 38 (-44.12%)
Mutual labels:  disk, fs, file, file-system
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (-2.94%)
Mutual labels:  async, promise, sync
Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (+141.18%)
Mutual labels:  async, promise, sync
Bach
Compose your async functions with elegance.
Stars: ✭ 117 (+72.06%)
Mutual labels:  async, stream, promise
Download
Download and extract files
Stars: ✭ 1,064 (+1464.71%)
Mutual labels:  async, stream, promise
TLightFileStream
Implements a lightweight, high-performance, non-allocating advanced-record-based wrapper around the SysUtils file handling routines as an alternative to Classes.TFileStream.
Stars: ✭ 21 (-69.12%)
Mutual labels:  stream, file, file-system
fs-utils
Generalized file and path utils for Node.js projects.
Stars: ✭ 33 (-51.47%)
Mutual labels:  fs, file, file-system
Nodespider
[DEPRECATED] Simple, flexible, delightful web crawler/spider package
Stars: ✭ 33 (-51.47%)
Mutual labels:  async, promise
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-50%)
Mutual labels:  async, promise
Fritzbox.js
☎️ The leading AVM Fritz!Box API for NodeJS and JavaScript.
Stars: ✭ 36 (-47.06%)
Mutual labels:  async, promise
Node Qiniu Sdk
七牛云SDK,使用 ES2017 async functions 来操作七牛云,接口名称与官方接口对应,轻松上手,文档齐全
Stars: ✭ 44 (-35.29%)
Mutual labels:  async, promise
Request.swift
A tiny HTTP client written in swift. URLSession alternative
Stars: ✭ 14 (-79.41%)
Mutual labels:  async, sync
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (-66.18%)
Mutual labels:  async, promise
Promised Pipe
A ramda.pipe-like utility that handles promises internally with zero dependencies
Stars: ✭ 64 (-5.88%)
Mutual labels:  async, promise
Then
🎬 Tame async code with battle-tested promises
Stars: ✭ 908 (+1235.29%)
Mutual labels:  async, promise
Breeze
Javascript async flow control manager
Stars: ✭ 38 (-44.12%)
Mutual labels:  async, promise
Node Ntfs
Windows NT File System (NTFS) file system driver
Stars: ✭ 18 (-73.53%)
Mutual labels:  fs, disk
Futures Batch
An adapter for futures, which chunks up elements and flushes them after a timeout, or when the buffer is full. (Formerly known as tokio-batch.)
Stars: ✭ 37 (-45.59%)
Mutual labels:  async, buffer
Brawlstats
(A)sync python wrapper for the Brawl Stars API
Stars: ✭ 68 (+0%)
Mutual labels:  async, sync
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-27.94%)
Mutual labels:  async, promise

write Donate NPM version NPM monthly downloads NPM total downloads Build Status

Write data to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Thin wrapper around node's native fs methods.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm (requires Node.js >=10):

$ npm install --save write

Usage

const write = require('write');

Options

The following options may be used with any method.

options.newline

Type: boolean

Default: undefined

Ensure that contents has a trailing newline before writing it to the file system.

write.sync('foo.txt', 'some data...', { newline: true }); 

options.overwrite

Type: boolean

Default: undefined

Set to false to prevent existing files from being overwritten. See increment for a less severe alternative.

write.sync('foo.txt', 'some data...', { overwrite: false });

options.increment

Type: boolean

Default: undefined

Set to true to automatically rename files by appending an increment, like foo (2).txt, to prevent foo.txt from being overwritten. This is useful when writing log files, or other information where the file name is less important than the contents being written.

write.sync('foo.txt', 'some data...', { increment: true });
// if "foo.txt" exists, the file will be renamed to "foo (2).txt"

API

write

Asynchronously writes data to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Data can be a string or a buffer. Returns a promise if a callback function is not passed.

Params

  • filepath {String}: file path.
  • data {String|Buffer|Uint8Array}: Data to write.
  • options {Object}: Options to pass to fs.writeFile
  • callback {Function}: (optional) If no callback is provided, a promise is returned.
  • returns {Object}: Returns an object with the path and contents of the file that was written to the file system. This is useful for debugging when options.increment is used and the path might have been modified.

Example

const write = require('write');

// async/await
(async () => {
  await write('foo.txt', 'This is content...');
})();

// promise
write('foo.txt', 'This is content...')
  .then(() => {
    // do stuff
  });

// callback
write('foo.txt', 'This is content...', err => {
  // do stuff with err
});

.sync

The synchronous version of write. Returns undefined.

Params

  • filepath {String}: file path.
  • data {String|Buffer|Uint8Array}: Data to write.
  • options {Object}: Options to pass to fs.writeFileSync
  • returns {Object}: Returns an object with the path and contents of the file that was written to the file system. This is useful for debugging when options.increment is used and the path might have been modified.

Example

const write = require('write');
write.sync('foo.txt', 'This is content...');

.stream

Returns a new WriteStream object. Uses fs.createWriteStream to write data to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Data can be a string or a buffer.

Params

Example

const fs = require('fs');
const write = require('write');
fs.createReadStream('README.md')
  .pipe(write.stream('a/b/c/other-file.md'))
  .on('close', () => {
    // do stuff
  });

Release history

See [CHANGELOG.md].

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
42 jonschlinkert
2 jpetitcolas
1 tunnckoCore

Author

Jon Schlinkert

License

Copyright © 2019, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on September 04, 2019.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].