pax_global_header00006660000000000000000000000064127315461310014515gustar00rootroot0000000000000052 comment=42d4b6792bbaee919805b2cb9afc95233addb239 output-file-sync-1.1.2/000077500000000000000000000000001273154613100147455ustar00rootroot00000000000000output-file-sync-1.1.2/.editorconfig000077500000000000000000000002741273154613100174300ustar00rootroot00000000000000root = true [*] charset = utf-8 end_of_line = lf indent_style = space indent_size = 2 insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false output-file-sync-1.1.2/.gitattributes000066400000000000000000000000141273154613100176330ustar00rootroot00000000000000* text=auto output-file-sync-1.1.2/.gitignore000077500000000000000000000000431273154613100167350ustar00rootroot00000000000000coverage node_modules tmp tmp_file output-file-sync-1.1.2/.travis.yml000066400000000000000000000003661273154613100170630ustar00rootroot00000000000000sudo: false git: depth: 1 branches: except: - /^v\d/ language: node_js node_js: node script: npm run-script pretest && npm run-script coverage after_script: - npm install istanbul-coveralls - node node_modules/.bin/istanbul-coveralls output-file-sync-1.1.2/LICENSE000066400000000000000000000021051273154613100157500ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 - 2016 Shinnosuke Watanabe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. output-file-sync-1.1.2/README.md000066400000000000000000000077401273154613100162340ustar00rootroot00000000000000# 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://img.shields.io/coveralls/shinnn/output-file-sync.svg)](https://coveralls.io/r/shinnn/output-file-sync) [![Dependency Status](https://david-dm.org/shinnn/output-file-sync.svg)](https://david-dm.org/shinnn/output-file-sync) [![devDependency Status](https://david-dm.org/shinnn/output-file-sync/dev-status.svg)](https://david-dm.org/shinnn/output-file-sync#info=devDependencies) Synchronously write a file and create its ancestor directories if needed ```javascript const fs = require('fs'); const outputFileSync = require('output-file-sync'); outputFileSync('foo/bar/baz.txt', 'Hi!'); fs.readFileSync('foo/bar/baz.txt', 'utf8'); //=> 'Hi!' ``` ## Difference from [fs.outputFileSync](https://www.npmjs.com/package/fs-extra#outputfilefile-data-options-callback) This module is very similar to [fs-extra](https://github.com/jprichardson/node-fs-extra)'s [`fs.outputFileSync`](https://github.com/jprichardson/node-fs-extra#outputfilefile-data-options-callback) but they are 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 fs = require('fs'); const outputFileSync = require('output-file-sync'); outputFileSync('foo/bar', 'content', {mode: 33260}); fs.statSync('foo').mode; //=> 33260 ``` ## Installation [Use npm.](https://docs.npmjs.com/cli/install) ``` npm install output-file-sync ``` ## API ```javascript const outputFileSync = require('output-file-sync'); ``` ### outputFileSync(*path*, *data* [, *options*]) *path*: `String` *data*: `String` or [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer) *options*: `Object` or `String` (options for [fs.writeFileSync] and [mkdirp]) 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 fs = require('fs'); const outputFileSync = require('output-file-sync'); // When the directory `foo/bar` exists outputFileSync('foo/bar/baz/qux.txt', 'Hello', 'utf-8'); fs.statSync('foo/bar/baz').isDirectory(); //=> true fs.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 let dir = outputFileSync('foo/bar/baz.txt', 'Hello'); dir; //=> Same value as `path.resolve('foo')` ``` #### 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 modes of a file, overriding `mode` option. ##### options.dirMode Set 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 Copyright (c) 2014 - 2016 [Shinnosuke Watanabe](https://github.com/shinnn) Licensed under [the MIT License](./LICENSE). [fs.writeFileSync]: https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options [mkdirp]: https://github.com/substack/node-mkdirp output-file-sync-1.1.2/appveyor.yml000066400000000000000000000004721273154613100173400ustar00rootroot00000000000000shallow_clone: true version: '{build}' skip_tags: true install: - ps: Install-Product node Stable - npm uninstall --save-dev @shinnn/eslint-config-node-legacy eslint - npm install build: off test_script: node --strong_mode --throw-deprecation --track-heap-objects test.js cache: node_modules -> package.json output-file-sync-1.1.2/index.js000066400000000000000000000024101273154613100164070ustar00rootroot00000000000000/*! * output-file-sync | MIT (c) Shinnosuke Watanabe * https://github.com/shinnn/output-file-sync */ 'use strict'; var dirname = require('path').dirname; var writeFileSync = require('graceful-fs').writeFileSync; var inspect = require('util').inspect; var objectAssign = require('object-assign'); var mkdirpSync = require('mkdirp').sync; module.exports = function outputFileSync(filePath, data, options) { if (typeof filePath !== 'string') { throw new TypeError( inspect(filePath) + ' is not a string. Expected a file path to write a file.' ); } if (filePath === '') { throw new Error('Expected a file path to write a file, but received an empty string instead.'); } options = options || {}; var mkdirpOptions; if (typeof options === 'string') { mkdirpOptions = null; } else if (options.dirMode) { mkdirpOptions = objectAssign({}, options, {mode: options.dirMode}); } else { mkdirpOptions = options; } var writeFileOptions; if (options.fileMode) { writeFileOptions = objectAssign({}, options, {mode: options.fileMode}); } else { writeFileOptions = options; } var createdDirPath = mkdirpSync(dirname(filePath), mkdirpOptions); writeFileSync(filePath, data, writeFileOptions); return createdDirPath; }; output-file-sync-1.1.2/package.json000066400000000000000000000020651273154613100172360ustar00rootroot00000000000000{ "name": "output-file-sync", "version": "1.1.2", "description": "Synchronously write a file and create its ancestor directories if needed", "repository": "shinnn/output-file-sync", "author": "Shinnosuke Watanabe (https://github.com/shinnn)", "scripts": { "pretest": "eslint --fix --config @shinnn/node-legacy index.js test.js", "test": "node --strong_mode --throw-deprecation --track-heap-objects test.js | tap-spec", "coverage": "node --strong_mode --throw-deprecation --track-heap-objects node_modules/.bin/istanbul cover test.js" }, "license": "MIT", "files": [ "index.js" ], "keywords": [ "fs", "write", "sync", "synchronous", "output", "create", "file", "mkdir", "mkdirp" ], "dependencies": { "graceful-fs": "^4.1.4", "mkdirp": "^0.5.1", "object-assign": "^4.1.0" }, "devDependencies": { "@shinnn/eslint-config-node-legacy": "^2.0.0", "eslint": "^2.13.0", "istanbul": "^0.4.3", "read-remove-file": "^3.0.0", "tap-spec": "^4.1.1", "tape": "^4.5.1" } } output-file-sync-1.1.2/test.js000066400000000000000000000073221273154613100162660ustar00rootroot00000000000000'use strong'; const fs = require('graceful-fs'); const path = require('path'); const outputFileSync = require('.'); const readRemoveFile = require('read-remove-file'); const test = require('tape'); test('outputFileSync()', t => { t.plan(21); t.equal(outputFileSync.name, 'outputFileSync', 'should have a function name.'); t.equal( outputFileSync('tmp_file', 'foo', 'utf8'), null, 'should return null when it doesn\'t create any directories.' ); readRemoveFile('tmp_file', 'utf8', (...args) => { t.deepEqual( args, [null, 'foo'], 'should create a file into the existing directory.' ); }); t.equal( outputFileSync('tmp/foo', new Buffer('a'), {mode: '0744'}), path.resolve('tmp'), 'should return the path of the first created directory.' ); fs.stat('tmp/foo', (statErr, stat) => { t.strictEqual(statErr, null, 'should accept mkdirp\'s option.'); /* istanbul ignore next */ const expected = process.platform === 'win32' ? '100666' : '100744'; t.equal( stat.mode.toString(8), expected, 'should reflect `mode` option to the file mode.' ); readRemoveFile('tmp/foo', 'utf8', (...args) => { t.deepEqual( args, [null, 'a'], 'should create a file into the new directory.' ); }); }); fs.stat('tmp', (err, stat) => { t.strictEqual(err, null, 'should create a directory.'); /* istanbul ignore next */ const expected = process.platform === 'win32' ? '40666' : '40744'; t.equal( stat.mode.toString(8), expected, 'should reflect `mode` option to the directory mode.' ); }); outputFileSync('t/m/p', 'ə', { dirMode: '0745', fileMode: '0644', encoding: 'ascii' }); fs.stat('t/m', (err, stat) => { t.strictEqual(err, null, 'should create multiple directories.'); /* istanbul ignore next */ const expected = process.platform === 'win32' ? '40666' : '40745'; t.equal( stat.mode.toString(8), expected, 'should reflect `dirMode` option to the directory mode.' ); }); fs.stat('t/m/p', (statErr, stat) => { t.strictEqual(statErr, null, 'should create a file into the new directories.'); /* istanbul ignore next */ const expected = process.platform === 'win32' ? '100666' : '100644'; t.equal( stat.mode.toString(8), expected, 'should reflect `fileMode` option to the file mode.' ); readRemoveFile('t/m/p', 'utf8', (...args) => { t.deepEqual( args, [null, 'Y'], 'should accept fs.writeFile\'s option.' ); }); }); t.throws( () => outputFileSync('./', '0123456789'), /EISDIR/, 'should throw an error when fs.writeFile fails.' ); t.throws( () => outputFileSync('index.js/foo', ''), /EEXIST/, 'should throw an error when mkdirp fails.' ); t.throws( () => outputFileSync('foo', '', 'utf9'), /Unknown encoding.*utf9/, 'should throw an error when the option is not valid for fs.writeFile.' ); t.throws( () => outputFileSync('f/o/o', '', {fs: []}), /TypeError/, 'should throw a type error when the option is not valid for mkdirp.' ); t.throws( () => outputFileSync(['a', Buffer.from('b')], ''), /TypeError.*\[ 'a', \] is not a string\. Expected a file path to write a file\./, 'should throw a type error when the first argument is not a string.' ); t.throws( () => outputFileSync('', ''), /Error.*Expected a file path to write a file, but received an empty string instead\./, 'should throw an error when the first argument is an empty string.' ); t.throws( () => outputFileSync(), /TypeError.*path/, 'should throw a type error when it takes no arguments.' ); });