Skip to content
Merged
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
46 changes: 21 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const aug = require('aug');
const bytesize = require('bytesize');
const mkdirp = require('mkdirp-promise');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);
//const fileSize = util.promisify(bytesize.fileSize);
const writeFileAsync = util.promisify(fs.writeFile);

class TaskKitTask {
constructor(name, options, kit = {}) {
Expand Down Expand Up @@ -101,44 +100,41 @@ class TaskKitTask {
return Promise.all(promises);
}

writeFile(filepath, contents) {
if (typeof contents === 'string') {
return writeFile(filepath, contents);
async write(filename, contents) {
if (!contents) {
this.log(['warning'], `attempting to write empty string to ${filename}`);
}
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(filepath);
// if contents is a stream then get it as a string:
contents = typeof contents === 'string' ? contents : await new Promise((resolve, reject) => {
const chunks = [];
contents.on('error', (err) => {
this.log(['error'], err);
this.emit('end');
});
contents.on('close', () => {
resolve();
});
contents.pipe(fileStream);
contents.on('data', (data) => { chunks.push(data); });
contents.on('close', () => resolve(Buffer.concat(chunks).toString()));
});
}

async write(filename, contents) {
if (!contents) {
this.log(['warning'], `attempting to write empty string to ${filename}`);
}
const output = path.join(this.options.dist || '', filename);
const outputDir = path.dirname(output);

if (!outputDir) {
return;
}
await mkdirp(outputDir);
await this.writeFile(output, contents);
let size;
if (typeof contents === 'string') {
size = bytesize.stringSize(contents, true);
await writeFileAsync(output, contents);
let numericSize;
let readableSize;
if (this.options.gzipSize) {
numericSize = await bytesize.gzipStringSize(contents, false);
readableSize = await bytesize.gzipStringSize(contents, true);
} else {
//TODO: needs bytesize update
//size = await fileSize(output, true);
size = '--';
numericSize = bytesize.stringSize(contents, false);
readableSize = bytesize.stringSize(contents, true);
}
if (typeof this.options.sizeThreshold === 'number' && numericSize > this.options.sizeThreshold) {
this.log(['warning'], `File ${filename} ${this.options.gzipSize ? 'gzipped' : ''} size is ${readableSize} (${numericSize} bytes), exceeds threshold of ${this.options.sizeThreshold} bytes`);
}
this.log(`Writing file ${filename} (${size})`);
this.log(`Writing file ${filename} (${readableSize})`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"homepage": "https://github.com/firstandthird/taskkit-task#readme",
"dependencies": {
"aug": "^2.0.0",
"bytesize": "0.2.0",
"bytesize": "^1.1.0",
"logr": "^2.1.0",
"logr-cli-fancy": "^0.2.0",
"logr-reporter-bell": "0.0.1",
Expand Down
60 changes: 59 additions & 1 deletion test/test.runkit-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,65 @@ test('writes when contents is stream', async(t) => {
await task.write('stream.txt', fs.createReadStream(`${__dirname}/fixtures/stream.txt`));
t.equal(fs.existsSync('test/dist/stream.txt'), true);
const data = await readFile('test/dist/stream.txt');
t.equal(data.toString(), 'contents\n');
t.equal(data.toString().startsWith('contents'), true);
t.end();
});

test('will warn if sizeThreshold is specified and is exceeded ', async(t) => {
const oldLog = console.log;
const results = [];
console.log = (input) => {
results.push(input);
};
const task = new TaskKitTask('test', {
dist: 'test/dist',
items: {
output1: 'input1'
},
sizeThreshold: 1
}, {});
await task.write('output.txt', 'contents');
const task2 = new TaskKitTask('test', {
dist: 'test/dist',
items: {
output1: 'input1'
},
sizeThreshold: 200000
}, {});
await task2.write('output.txt', 'contents');
console.log = oldLog;
t.equal(results[0].indexOf('warning'), 1, 'logs if file size exceeds sizeThreshold');
t.equal(results.length, 3, 'does not log warning if file size does not exceed');
t.end();
});

test('will use gzip size for sizeThreshold comparisons if gzipSize is true ', async(t) => {
const oldLog = console.log;
const results = [];
console.log = (input) => {
results.push(input);
};
const task = new TaskKitTask('test', {
dist: 'test/dist',
items: {
output1: 'input1'
},
gzipSize: true,
sizeThreshold: 1
}, {});
await task.write('output.txt', 'contents');
const task2 = new TaskKitTask('test', {
dist: 'test/dist',
items: {
output1: 'input1'
},
gzipSize: true,
sizeThreshold: 200000
}, {});
await task2.write('output.txt', 'contents');
console.log = oldLog;
t.notEqual(results[0].indexOf('gzipped size is'), -1, 'logs if gzipped file size exceeds sizeThreshold');
t.equal(results.length, 3, 'does not log warning if gzipped file size does not exceed');
t.end();
});

Expand Down