diff --git a/index.js b/index.js index e9b483d..959e10d 100644 --- a/index.js +++ b/index.js @@ -87,10 +87,8 @@ class TaskKitTask { return results; } + // tasks should override process to do any work: process(input, output, options) { - if (!options) { - options = {}; - } return; } @@ -112,17 +110,14 @@ class TaskKitTask { const chunks = []; contents.on('error', (err) => { this.log(['error'], err); - this.emit('end'); + reject(err); }); 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); - - if (!outputDir) { - return; - } + // mkdirp will create any output directory if it doesn't exist: await mkdirp(outputDir); await this.writeFile(output, contents); let numericSize; diff --git a/test/test.runkit-task.js b/test/test.runkit-task.js index 3f15d30..e57483b 100644 --- a/test/test.runkit-task.js +++ b/test/test.runkit-task.js @@ -99,6 +99,24 @@ test('execute -- will not fire if no items / files passed', async(t) => { t.pass(); }); +test('execute -- will warn if no items / files passed', async(t) => { + const task = new TaskKitTask('test', { + }, {}); + task.process = () => { + t.fail(); + }; + task.log = (tags, input) => { + t.equal(tags[0], 'warning'); + t.equal(input, 'No input files, skipping'); + t.end(); + }; + try { + await task.execute(); + } catch (e) { + // do nothing + } +}); + test('execute -- can be disabled', async(t) => { class DisabledTask extends TaskKitTask { process() { @@ -174,6 +192,24 @@ test('writes when contents is stream', async(t) => { t.end(); }); +test('logs and throw error if there is a stream error', async(t) => { + const task = new TaskKitTask('test', { + dist: 'test/dist', + items: { + } + }, {}); + task.log = (tags) => { + t.equal(tags[0], 'error'); + }; + try { + await task.write('stream.txt', fs.createReadStream(`${__dirname}/fixtures/doesNotExist.txt`)); + t.fail(); + } catch (e) { + t.notEqual(e.toString().indexOf('no such file or directory'), -1); + t.end(); + } +}); + test('will warn if sizeThreshold is specified and is exceeded ', async(t) => { const oldLog = console.log; const results = []; @@ -280,5 +316,32 @@ test('writeMany files to dist directory ', async(t) => { t.equal(data2.toString(), 'contents2'); }); -test('can override logger'); +test('use the taskkit logger if provided', (t) => { + const task = new TaskKitTask('test', { + dist: 'test/dist', + }, { + logger: (input) => { + t.end(); + } + }); + task.log(); +}); + +test('warns if write called with empty content', async(t) => { + // t.plan(2); + const task = new TaskKitTask('test', { + dist: 'test/dist', + }, { + logger: (tags, input) => { + t.equal(tags[0], 'warning'); + t.equal(input.startsWith('attempting to write empty string to file'), true); + } + }); + try { + await task.write('file', undefined); + } catch (e) { + t.end(); + } +}); + test('can pass in full config');