Skip to content

Add es8 features #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

var writeFile = require('./');
var Readable = require('stream').Readable;
const writeFile = require('./');
const Readable = require('stream').Readable;

function toStream(str) {
var stream = new Readable();
const stream = new Readable();
stream.push(str);
stream.push(null);
return stream;
}

toStream('fooo')
.pipe(writeFile.stream('tmp/a/b/c/foo.md'))
.on('close', function() {
.on('close', () => {
console.log('done');
})
});
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

'use strict';

var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');

/**
* Asynchronously writes data to a file, replacing the file if it already
Expand Down Expand Up @@ -59,8 +59,8 @@ function writeFile(filepath, data, options, cb) {
}

var preparedData = data;
if (options && options.ensureNewLine && data.slice(-1) !== "\n") {
preparedData += "\n";
if (options && options.ensureNewLine && data.slice(-1) !== '\n') {
preparedData += '\n';
}

fs.writeFile(filepath, preparedData, options, cb);
Expand Down
97 changes: 49 additions & 48 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@
'use strict';

require('mocha');
var fs = require('fs');
var assert = require('assert');
var Readable = require('stream').Readable;
var del = require('delete');
var each = require('async-each');
var writeFile = require('./');
var files = ['tmp/a.md', 'tmp/b.md', 'tmp/c.md', 'tmp/d.md', 'tmp/e.md'];
const fs = require('fs');
const assert = require('assert');
const Readable = require('stream').Readable;
const del = require('delete');
const each = require('async-each');
const writeFile = require('./');
const files = ['tmp/a.md', 'tmp/b.md', 'tmp/c.md', 'tmp/d.md', 'tmp/e.md'];

function toStream(str) {
var stream = new Readable();
const stream = new Readable();
stream.push(str);
stream.push(null);
return stream;
}

describe('write', function() {
afterEach(function(cb) {
each(files, function(fp, next) {
describe('write', () => {
afterEach((cb) => {
each(files, (fp, next) => {
fs.stat(fp, next);
}, function(err) {
}, (err) => {
if (err) return cb(err);
del('tmp', cb);
});
});

describe('End New Line', function () {
it('should just write given data by default', function (done) {
each(files, function (fp, next) {
writeFile(fp, 'Hello!', function () {
fs.readFile(fp, function (err, fileContent) {
describe('End New Line', () => {
it('should just write given data by default', (done) => {
each(files, (fp, next) => {
writeFile(fp, 'Hello!', () => {
fs.readFile(fp, (err, fileContent) => {
if (err) {
return next(err);
}
Expand All @@ -49,11 +49,11 @@ describe('write', function() {
}, done);
});

describe('With `ensureNewLine` option to true', function () {
it('should add a new line at the end of the file if none', function (done) {
each(files, function (fp, next) {
writeFile(fp, 'Hello!', { ensureNewLine: true }, function () {
fs.readFile(fp, function (err, fileContent) {
describe('With `ensureNewLine` option to true', () => {
it('should add a new line at the end of the file if none', (done) => {
each(files, (fp, next) => {
writeFile(fp, 'Hello!', { ensureNewLine: true }, () => {
fs.readFile(fp, (err, fileContent) => {
if (err) {
return next(err);
}
Expand All @@ -65,10 +65,10 @@ describe('write', function() {
}, done);
});

it('should not add a new line at the end of the file if there is already one', function (done) {
each(files, function (fp, next) {
writeFile(fp, "Hello!\n", { ensureNewLine: true }, function() {
fs.readFile(fp, function (err, fileContent) {
it('should not add a new line at the end of the file if there is already one', (done) => {
each(files, (fp, next) =>{
writeFile(fp, 'Hello!\n', { ensureNewLine: true }, () => {
fs.readFile(fp, (err, fileContent) => {
if (err) {
return next(err);
}
Expand All @@ -82,64 +82,65 @@ describe('write', function() {
});
});

describe('async', function() {
it('should write files', function(cb) {
each(files, function(fp, next) {
describe('async', () => {
it('should write files', (cb) => {
each(files, (fp, next) => {
writeFile(fp, 'content...', next);
}, cb);
});

it('should return a promise when no callback is given', function(cb) {
each(files, function(fp, next) {
it('should return a promise when no callback is given', (cb) => {
each(files, (fp, next) => {
writeFile(fp, 'content...')
.then(function() {
.then(() => {
next();
})
.catch(function(err) {
.catch((err) => {
next(err);
});
}, cb);
});
});

describe('promise', function() {
describe('promise', () => {
it('should write files using .promise', function(cb) {
each(files, function(fp, next) {
each(files, (fp, next) => {
writeFile.promise(fp, 'content...')
.then(function() {
.then(() => {
next();
})
.catch(function(err) {
.catch((err) => {
next(err);
});
}, cb);
});
});

describe('sync', function() {
it('should write files using .sync', function() {
files.forEach(function(fp) {
describe('sync', () => {
it('should write files using .sync', () => {
files.forEach((fp) => {
writeFile.sync(fp, '');
});
});
});

describe('stream', function() {
describe('stream', () => {
it('should write files using .stream', function(cb) {
each(files, function(fp, next) {
each(files, (fp, next) => {
toStream('this is content...')
.pipe(writeFile.stream(fp))
.on('close', next)
.on('close', next);
}, cb);
});

it('should overwrite an existing file', function(cb) {
var fixtures = files.slice().concat(['tmp/e.md', 'tmp/e.md']);

each(files, function(fp, next) {
it('should overwrite an existing file', cb => {
//fixtures never used
const fixtures = files.slice().concat(['tmp/e.md', 'tmp/e.md']);

each(files, (fp, next) => {
toStream('this is content...')
.pipe(writeFile.stream(fp))
.on('close', next)
.on('close', next);
}, cb);
});
});
Expand Down