From abe6407cb936c14a8985ddf71b4eaf1935485ea1 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sat, 6 Jan 2018 22:52:09 -0600 Subject: [PATCH 1/9] taskkit --- index.js | 20 +++++++++----- package.json | 2 +- test/test.runkit-task.js | 57 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 697ceb5..896e57b 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,6 @@ 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); class TaskKitTask { constructor(name, options, kit = {}) { @@ -129,15 +128,22 @@ class TaskKitTask { } await mkdirp(outputDir); await this.writeFile(output, contents); - let size; + let numericSize; + let readableSize; if (typeof contents === 'string') { - size = bytesize.stringSize(contents, true); + numericSize = bytesize.stringSize(contents, false); + readableSize = bytesize.stringSize(contents, true); + } else if (this.options.gzipSize) { + numericSize = await bytesize.gzipSize(output, false); + readableSize = await bytesize.gzipSize(output, true); } else { - //TODO: needs bytesize update - //size = await fileSize(output, true); - size = '--'; + numericSize = await bytesize.fileSize(output, false); + readableSize = await bytesize.fileSize(output, true); } - this.log(`Writing file ${filename} (${size})`); + if (typeof this.options.sizeThreshold === 'number' && numericSize > this.options.sizeThreshold) { + this.log(['warning'], `File ${filename} size is ${readableSize} (${numericSize} bytes), exceeds threshold of ${this.options.sizeThreshold} bytes`); + } + this.log(`Writing file ${filename} (${readableSize})`); } } diff --git a/package.json b/package.json index 0e5266f..038b376 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "homepage": "https://github.com/firstandthird/taskkit-task#readme", "dependencies": { "aug": "^2.0.0", - "bytesize": "0.2.0", + "bytesize": "^1.0.0", "logr": "^2.1.0", "logr-cli-fancy": "^0.2.0", "logr-reporter-bell": "0.0.1", diff --git a/test/test.runkit-task.js b/test/test.runkit-task.js index 0341ee2..8ed3298 100644 --- a/test/test.runkit-task.js +++ b/test/test.runkit-task.js @@ -174,6 +174,63 @@ test('writes when contents is stream', async(t) => { 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'); + t.equal(results[0].indexOf('warning'), 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(); +}); + test('handles input as object', async(t) => { const task = new TaskKitTask('test', { files: { From cd777a3da0267129d4670a21374c870e6f9581c3 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sat, 6 Jan 2018 23:01:22 -0600 Subject: [PATCH 2/9] state if gzipped size --- index.js | 2 +- test/test.runkit-task.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 896e57b..0caf117 100644 --- a/index.js +++ b/index.js @@ -141,7 +141,7 @@ class TaskKitTask { readableSize = await bytesize.fileSize(output, true); } if (typeof this.options.sizeThreshold === 'number' && numericSize > this.options.sizeThreshold) { - this.log(['warning'], `File ${filename} size is ${readableSize} (${numericSize} bytes), exceeds threshold of ${this.options.sizeThreshold} bytes`); + 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} (${readableSize})`); } diff --git a/test/test.runkit-task.js b/test/test.runkit-task.js index 8ed3298..02b281a 100644 --- a/test/test.runkit-task.js +++ b/test/test.runkit-task.js @@ -197,6 +197,7 @@ test('will warn if sizeThreshold is specified and is exceeded ', async(t) => { }, {}); await task2.write('output.txt', 'contents'); console.log = oldLog; + console.log(results) 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(); @@ -226,7 +227,8 @@ test('will use gzip size for sizeThreshold comparisons if gzipSize is true ', as sizeThreshold: 200000 }, {}); await task2.write('output.txt', 'contents'); - t.equal(results[0].indexOf('warning'), 1, 'logs if gzipped file size exceeds sizeThreshold'); + 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(); }); From 7d6a3f3c3e8d7cb0c3e915dffe9e56d393320c18 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sun, 7 Jan 2018 01:03:14 -0600 Subject: [PATCH 3/9] always use gzip size when gzipSize is true --- index.js | 8 ++++---- test/test.runkit-task.js | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 0caf117..afef258 100644 --- a/index.js +++ b/index.js @@ -130,12 +130,12 @@ class TaskKitTask { await this.writeFile(output, contents); let numericSize; let readableSize; - if (typeof contents === 'string') { - numericSize = bytesize.stringSize(contents, false); - readableSize = bytesize.stringSize(contents, true); - } else if (this.options.gzipSize) { + if (this.options.gzipSize) { numericSize = await bytesize.gzipSize(output, false); readableSize = await bytesize.gzipSize(output, true); + } else if (typeof contents === 'string') { + numericSize = bytesize.stringSize(contents, false); + readableSize = bytesize.stringSize(contents, true); } else { numericSize = await bytesize.fileSize(output, false); readableSize = await bytesize.fileSize(output, true); diff --git a/test/test.runkit-task.js b/test/test.runkit-task.js index 02b281a..a4ae5b6 100644 --- a/test/test.runkit-task.js +++ b/test/test.runkit-task.js @@ -197,7 +197,6 @@ test('will warn if sizeThreshold is specified and is exceeded ', async(t) => { }, {}); await task2.write('output.txt', 'contents'); console.log = oldLog; - console.log(results) 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(); From b49cd45be1e973f6f0b247649bf81cd4d8d7df55 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sun, 7 Jan 2018 12:18:07 -0600 Subject: [PATCH 4/9] always convert to string --- index.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index afef258..6732802 100644 --- a/index.js +++ b/index.js @@ -5,7 +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 writeFileAsync = util.promisify(fs.writeFile); class TaskKitTask { constructor(name, options, kit = {}) { @@ -100,17 +100,17 @@ class TaskKitTask { } writeFile(filepath, contents) { - if (typeof contents === 'string') { - return writeFile(filepath, contents); - } return new Promise((resolve, reject) => { + if (typeof contents === 'string') { + return resolve(writeFileAsync(filepath, contents)); + } const fileStream = fs.createWriteStream(filepath); contents.on('error', (err) => { this.log(['error'], err); this.emit('end'); }); contents.on('close', () => { - resolve(); + return resolve(writeFileAsync(filepath, contents)); }); contents.pipe(fileStream); }); @@ -133,12 +133,9 @@ class TaskKitTask { if (this.options.gzipSize) { numericSize = await bytesize.gzipSize(output, false); readableSize = await bytesize.gzipSize(output, true); - } else if (typeof contents === 'string') { + } else { numericSize = bytesize.stringSize(contents, false); readableSize = bytesize.stringSize(contents, true); - } else { - numericSize = await bytesize.fileSize(output, false); - readableSize = await bytesize.fileSize(output, 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`); From 889b4f8cc3863a3d7510933c46ebe8a11af44e11 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sun, 7 Jan 2018 12:59:32 -0600 Subject: [PATCH 5/9] fix lint --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 6732802..b006f25 100644 --- a/index.js +++ b/index.js @@ -109,9 +109,7 @@ class TaskKitTask { this.log(['error'], err); this.emit('end'); }); - contents.on('close', () => { - return resolve(writeFileAsync(filepath, contents)); - }); + contents.on('close', () => resolve(writeFileAsync(filepath, contents))); contents.pipe(fileStream); }); } From 69dbfa82292b61828d9c2ce16fa422b3f4fb0cfd Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sun, 7 Jan 2018 14:57:29 -0600 Subject: [PATCH 6/9] convert to string, use gzipStringSize --- index.js | 28 ++++++++++++++-------------- test/test.runkit-task.js | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index b006f25..b3c89c3 100644 --- a/index.js +++ b/index.js @@ -100,24 +100,23 @@ class TaskKitTask { } writeFile(filepath, contents) { - return new Promise((resolve, reject) => { - if (typeof contents === 'string') { - return resolve(writeFileAsync(filepath, contents)); - } - const fileStream = fs.createWriteStream(filepath); - contents.on('error', (err) => { - this.log(['error'], err); - this.emit('end'); - }); - contents.on('close', () => resolve(writeFileAsync(filepath, contents))); - contents.pipe(fileStream); - }); + return writeFileAsync(filepath, contents); } async write(filename, contents) { if (!contents) { this.log(['warning'], `attempting to write empty string to ${filename}`); } + // 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('data', (data) => { chunks.push(data); }); + contents.on('close', () => resolve(Buffer.concat(chunks).toString())); + }); const output = path.join(this.options.dist || '', filename); const outputDir = path.dirname(output); @@ -129,8 +128,9 @@ class TaskKitTask { let numericSize; let readableSize; if (this.options.gzipSize) { - numericSize = await bytesize.gzipSize(output, false); - readableSize = await bytesize.gzipSize(output, true); + // needs to be gzipStringSize + numericSize = await bytesize.gzipStringSize(contents, false); + readableSize = await bytesize.gzipStringSize(contents, true); } else { numericSize = bytesize.stringSize(contents, false); readableSize = bytesize.stringSize(contents, true); diff --git a/test/test.runkit-task.js b/test/test.runkit-task.js index a4ae5b6..3f15d30 100644 --- a/test/test.runkit-task.js +++ b/test/test.runkit-task.js @@ -170,7 +170,7 @@ 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(); }); From ad31d2f938f3bfff647b58979f3d7fe1b7d69251 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Sun, 7 Jan 2018 15:00:46 -0600 Subject: [PATCH 7/9] remove comment --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index b3c89c3..e9b483d 100644 --- a/index.js +++ b/index.js @@ -128,7 +128,6 @@ class TaskKitTask { let numericSize; let readableSize; if (this.options.gzipSize) { - // needs to be gzipStringSize numericSize = await bytesize.gzipStringSize(contents, false); readableSize = await bytesize.gzipStringSize(contents, true); } else { From efcb225739c42f9ee21f52720300ae7a241229d0 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 8 Jan 2018 10:56:25 -0600 Subject: [PATCH 8/9] update bytesize --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 038b376..31c4b95 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "homepage": "https://github.com/firstandthird/taskkit-task#readme", "dependencies": { "aug": "^2.0.0", - "bytesize": "^1.0.0", + "bytesize": "^1.1.0", "logr": "^2.1.0", "logr-cli-fancy": "^0.2.0", "logr-reporter-bell": "0.0.1", From a42fb6fb29950088cda7a7b8be5b9111ef79b8bd Mon Sep 17 00:00:00 2001 From: Greg Allen Date: Mon, 8 Jan 2018 09:05:58 -0800 Subject: [PATCH 9/9] remove unnecessary function --- index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/index.js b/index.js index 99b0716..82ecada 100644 --- a/index.js +++ b/index.js @@ -100,10 +100,6 @@ class TaskKitTask { return Promise.all(promises); } - writeFile(filepath, contents) { - return writeFileAsync(filepath, contents); - } - async write(filename, contents) { if (!contents) { this.log(['warning'], `attempting to write empty string to ${filename}`); @@ -125,7 +121,7 @@ class TaskKitTask { return; } await mkdirp(outputDir); - await this.writeFile(output, contents); + await writeFileAsync(output, contents); let numericSize; let readableSize; if (this.options.gzipSize) {