# output-file-sync

> Synchronously write a file and create its ancestor directories if needed

Latest version **2.0.1** (published 2018-02-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install output-file-sync
pnpm add output-file-sync
yarn add output-file-sync
bun add output-file-sync
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2018-02-15 |
| First published | 2014-11-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Shinnosuke Watanabe |
| Maintainers | shinnn |
| Keywords | fs, write, sync, synchronous, output, create, file, mkdir, mkdirp |

## Links

- npm: https://www.npmjs.com/package/output-file-sync
- Repository: https://github.com/shinnn/output-file-sync
- Homepage: https://github.com/shinnn/output-file-sync#readme
- Issues: https://github.com/shinnn/output-file-sync/issues
- npm.io page: https://npm.io/package/output-file-sync

## Dependencies (3)

- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [graceful-fs](https://npm.io/package/graceful-fs.md) ^4.1.11
- [is-plain-obj](https://npm.io/package/is-plain-obj.md) ^1.1.0

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 2.0.1 (latest) — 2018-02-15
- 2.0.0 — 2017-03-08
- 1.1.2 — 2016-06-19
- 1.1.1 — 2015-06-07
- 1.1.0 — 2014-11-29
- 1.0.0 — 2014-11-13

## README

# output-file-sync

[![npm version](https://img.shields.io/npm/v/output-file-sync.svg)](https://www.npmjs.com/package/output-file-sync)
[![Build Status](https://travis-ci.org/shinnn/output-file-sync.svg?branch=master)](https://travis-ci.org/shinnn/output-file-sync)
[![Build status](https://ci.appveyor.com/api/projects/status/3qjn5ktuqb6w2cae?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/output-file-sync)
[![Coverage Status](https://coveralls.io/repos/github/shinnn/output-file-sync/badge.svg?branch=master)](https://coveralls.io/github/shinnn/output-file-sync?branch=master)

Synchronously write a file and create its ancestor directories if needed

```javascript
const {readFileSync} = require('fs');
const outputFileSync = require('output-file-sync');

outputFileSync('foo/bar/baz.txt', 'Hi!');
readFileSync('foo/bar/baz.txt', 'utf8'); //=> 'Hi!'
```

## Difference from [fs.outputFileSync](https://github.com/jprichardson/node-fs-extra/blob/master/docs/outputFile.md)

This module is very similar to [fs-extra](https://github.com/jprichardson/node-fs-extra)'s `fs.outputFileSync` method, but different in the following points:

1. *output-file-sync* returns the path of the directory created first. [See the API document for more details.](#outputfilesyncpath-data--options)
2. *output-file-sync* accepts [mkdirp] options.
   ```javascript
   const {statSync} = require('fs');
   const outputFileSync = require('output-file-sync');

   outputFileSync('foo/bar', 'content', {mode: 33260});
   statSync('foo').mode; //=> 33260
   ```
3. *output-file-sync* validates its arguments strictly, and prints highly informative error message.

## Installation

[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).

```
npm install output-file-sync
```

## API

```javascript
const outputFileSync = require('output-file-sync');
```

### outputFileSync(*path*, *data* [, *options*])

*path*: `string`  
*data*: `string`, `Buffer` or `Uint8Array`  
*options*: `Object` (options for [fs.writeFileSync] and [mkdirp]) or `string` (encoding)  
Return: `string` if it creates more than one directories, otherwise `null`

It writes the data to a file synchronously. If ancestor directories of the file don't exist, it creates the directories before writing the file.

```javascript
const {statSync} = require('fs');
const outputFileSync = require('output-file-sync');

// When the directory `foo/bar` exists
outputFileSync('foo/bar/baz/qux.txt', 'Hello', 'utf-8');

statSync('foo/bar/baz').isDirectory(); //=> true
statSync('foo/bar/baz/qux.txt').isFile(); //=> true
```

It returns the directory path just like [mkdirp.sync](https://github.com/substack/node-mkdirp#mkdirpsyncdir-opts):

> Returns the first directory that had to be created, if any.

```javascript
const dir = outputFileSync('foo/bar/baz.txt', 'Hello');
dir === path.resolve('foo'); //=> true
```

#### options

All options for [fs.writeFileSync] and [mkdirp] are available.

Additionally, you can pass [`fileMode`](#optionsfilemode) and [`dirMode`](#optionsdirmode) options to set different permission between the file and directories.

##### options.fileMode

Set the mode of a file, overriding `mode` option.

##### options.dirMode

Set the modes of directories, overriding `mode` option.

```javascript
outputFileSync('dir/file', 'content', {dirMode: '0745', fileMode: '0644'});
fs.statSync('dir').mode.toString(8); //=> '40745'
fs.statSync('dir/file').mode.toString(8); //=> '100644'
```

## Related project

* [output-file](https://github.com/shinnn/output-file) (asynchronous version)

## License

[ISC License](./LICENSE) © 2017 - 2018 Shinnosuke Watanabe

[fs.writeFileSync]: https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options
[mkdirp]: https://github.com/substack/node-mkdirp

---
_Source: https://npm.io/package/output-file-sync · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
