Skip to content

Commit

Permalink
use const, template literal and native Object.assign
Browse files Browse the repository at this point in the history
[BREAKING] drop support for Node.js v0.x

fix #1
  • Loading branch information
shinnn committed Feb 9, 2017
1 parent 7591645 commit ece73c3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
54 changes: 27 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@
*/
'use strict';

var dirname = require('path').dirname;
var writeFileSync = require('graceful-fs').writeFileSync;
var inspect = require('util').inspect;
const dirname = require('path').dirname;
const writeFileSync = require('graceful-fs').writeFileSync;
const inspect = require('util').inspect;

var objectAssign = require('object-assign');
var mkdirpSync = require('mkdirp').sync;
const isPlainObj = require('is-plain-obj');
const mkdirpSync = require('mkdirp').sync;

const PATH_ERROR = 'Expected a file path to write a file';

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.'
);
throw new TypeError(`${PATH_ERROR}, but got a non-string value ${inspect(filePath)}.`);
}

if (filePath === '') {
throw new Error('Expected a file path to write a file, but received an empty string instead.');
if (filePath.length === 0) {
throw new Error(`${PATH_ERROR}, but got '' (empty string).`);
}

options = options || {};

var mkdirpOptions;
if (typeof options === 'string') {
mkdirpOptions = null;
} else if (options.dirMode) {
mkdirpOptions = objectAssign({}, options, {mode: options.dirMode});
if (options !== null && options !== undefined) {
if (typeof options === 'string') {
options = {encoding: options};
} else if (!isPlainObj(options)) {
throw new TypeError(
'Expected a string to specify file encoding or ' +
`an object to specify output-file-sync options, but got ${inspect(options)}.`
);
}
} else {
mkdirpOptions = options;
options = {};
}

var writeFileOptions;
if (options.fileMode) {
writeFileOptions = objectAssign({}, options, {mode: options.fileMode});
} else {
writeFileOptions = options;
}
const createdDirPath = mkdirpSync(dirname(filePath), options.dirMode !== undefined ? Object.assign({}, options, {
mode: options.dirMode
}) : options);

writeFileSync(filePath, data, options.fileMode !== undefined ? Object.assign({}, options, {
mode: options.fileMode
}) : options);

var createdDirPath = mkdirpSync(dirname(filePath), mkdirpOptions);
writeFileSync(filePath, data, writeFileOptions);
return createdDirPath;
};
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "shinnn/output-file-sync",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --fix --config @shinnn/node-legacy index.js test.js",
"pretest": "eslint --fix --format=codeframe index.js test.js",
"test": "node --throw-deprecation test.js",
"coverage": "node --throw-deprecation node_modules/.bin/istanbul cover test.js"
},
Expand All @@ -26,16 +26,18 @@
],
"dependencies": {
"graceful-fs": "^4.1.11",
"mkdirp": "^0.5.1",
"object-assign": "^4.1.1"
"mkdirp": "^0.5.1"
},
"devDependencies": {
"@shinnn/eslint-config-node-legacy": "^2.0.0",
"eslint": "^2.13.0",
"istanbul": "^0.4.3",
"@shinnn/eslint-config-node": "^3.0.0",
"eslint": "^3.15.0",
"istanbul": "^0.4.5",
"lstat": "^0.1.0",
"read-utf8-file": "^1.0.0",
"rmfr": "^1.0.2",
"tape": "^4.6.3"
},
"eslintConfig": {
"extends": "@shinnn/node"
}
}
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,25 @@ test('outputFileSync()', t => {

t.throws(
() => outputFileSync('f/o/o', '', {fs: []}),
/TypeError/,
/^TypeError/,
'should throw a type error when the option is not valid for mkdirp.'
);

t.throws(
() => outputFileSync(['a', Buffer.from('b')], ''),
/TypeError.*\[ 'a', <Buffer 62> \] is not a string\. Expected a file path to write a file\./,
/^TypeError.*Expected a file path to write a file, but got a non-string value \[ 'a', <Buffer 62> ]\./,
'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\./,
/^Error.*Expected a file path to write a file, but got '' \(empty string\)\./,
'should throw an error when the first argument is an empty string.'
);

t.throws(
() => outputFileSync(),
/TypeError.*path/,
/^TypeError.*path/,
'should throw a type error when it takes no arguments.'
);
});

0 comments on commit ece73c3

Please sign in to comment.