From 1f3147fdad5cff2603632bf7bb5aa8d9ce22247b Mon Sep 17 00:00:00 2001 From: Skyler Dong Date: Thu, 23 Jul 2020 05:28:37 -0500 Subject: [PATCH] :wrench: Organize code & centralize helper functions --- .eslintrc.js | 6 +- .../documentReady.test.js | 29 +------ .../getElementById.test.js | 29 +------ __tests__/lib/testCommandOnTestCase.js | 30 +++++++ __tests__/main.test.js | 35 ++------ .../pendingTestChallenges/children.test.js | 29 +------ .../cloneElement.test.js | 29 +------ .../createElement.test.js | 29 +------ .../getElementsByClassName.test.js | 29 +------ .../getElementsByTagName.test.js | 29 +------ .../pendingTestChallenges/parent.test.js | 29 +------ .../pendingTestChallenges/selectAll.test.js | 29 +------ .../selectAllAttribute.test.js | 29 +------ .../selectAllPseudoClass.test.js | 29 +------ __tests__/testCases/getElementById.js | 4 +- bin/vaniquery.js | 6 +- lib/helpers.js | 84 +++++++++++++++++++ lib/loadFileToBuffer.js | 15 ---- lib/revert.js | 39 ++++++--- lib/vanilla.js | 35 ++------ package.json | 4 + yarn.lock | 33 +++++++- 22 files changed, 229 insertions(+), 381 deletions(-) create mode 100644 __tests__/lib/testCommandOnTestCase.js create mode 100644 lib/helpers.js delete mode 100644 lib/loadFileToBuffer.js diff --git a/.eslintrc.js b/.eslintrc.js index 2bc12a1..3e0ca5f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,10 @@ module.exports = { env: { commonjs: true, node: true, es2020: true, jest: true }, - extends: ['airbnb-base', 'plugin:prettier/recommended'], + extends: [ + 'airbnb-base', + 'plugin:node/recommended', + 'plugin:prettier/recommended', + ], globals: { Atomics: 'readonly', SharedArrayBuffer: 'readonly' }, parserOptions: { ecmaVersion: 2020, sourceType: 'module' }, rules: { diff --git a/__tests__/finishedTestChallenges/documentReady.test.js b/__tests__/finishedTestChallenges/documentReady.test.js index 7e65455..d52c2dd 100644 --- a/__tests__/finishedTestChallenges/documentReady.test.js +++ b/__tests__/finishedTestChallenges/documentReady.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'documentReady' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'documentReady'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'documentReady'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/finishedTestChallenges/getElementById.test.js b/__tests__/finishedTestChallenges/getElementById.test.js index c029928..d3ec873 100644 --- a/__tests__/finishedTestChallenges/getElementById.test.js +++ b/__tests__/finishedTestChallenges/getElementById.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'getElementById' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'getElementById'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'getElementById'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/lib/testCommandOnTestCase.js b/__tests__/lib/testCommandOnTestCase.js new file mode 100644 index 0000000..24e7886 --- /dev/null +++ b/__tests__/lib/testCommandOnTestCase.js @@ -0,0 +1,30 @@ +/** + * Test function vanilla on test case + */ + +const { loadFileToBuffer } = require('../../lib/helpers'); + +exports.testVanillaOnTestCase = (testCase) => { + const { vanilla } = require('../../lib/vanilla'); // eslint-disable-line global-require + + test(`Vanillaize '${testCase}'`, async () => { + // Input + const argv = { + _: ['vanilla', `./__tests__/testCases/${testCase}.js`], + C: true, + 'no-cache': true, + noCache: true, + $0: 'vaniquery', + }; + + // Output + const output = await vanilla(argv); + + // Answer key + const answerKeyFile = `./__tests__/testCases/${testCase}.answerkey.js`; + const answerKey = await loadFileToBuffer(answerKeyFile); + + // Expect + expect(output).toEqual(answerKey); + }); +}; diff --git a/__tests__/main.test.js b/__tests__/main.test.js index a083364..f7d5262 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -2,37 +2,14 @@ * Main test script */ -const fs = require('fs'); -const { vanilla } = require('../lib/vanilla'); -const loadFileToBuffer = require('../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('./lib/testCommandOnTestCase'); +const { listAllFilesInDir } = require('../lib/helpers'); const testDir = './__tests__/finishedTestChallenges/'; -const testNames = []; -fs.readdirSync(testDir).forEach((filename) => { - testNames.push(filename.substr(0, filename.indexOf('.test.js'))); -}); - -describe("Test command 'vanilla'", () => { - testNames.forEach((testName) => { - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); +const testCases = listAllFilesInDir(testDir); - // Expect - expect(output).toEqual(answerKey); - }); +describe("Test command 'vanilla' on several test cases", () => { + testCases.forEach((testCase) => { + testVanillaOnTestCase(testCase); }); }); diff --git a/__tests__/pendingTestChallenges/children.test.js b/__tests__/pendingTestChallenges/children.test.js index ff0ef62..cd6b478 100644 --- a/__tests__/pendingTestChallenges/children.test.js +++ b/__tests__/pendingTestChallenges/children.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'children' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'children'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'children'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/cloneElement.test.js b/__tests__/pendingTestChallenges/cloneElement.test.js index 910425c..0b3e578 100644 --- a/__tests__/pendingTestChallenges/cloneElement.test.js +++ b/__tests__/pendingTestChallenges/cloneElement.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'cloneElement' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'cloneElement'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'cloneElement'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/createElement.test.js b/__tests__/pendingTestChallenges/createElement.test.js index 425518a..52b1bb4 100644 --- a/__tests__/pendingTestChallenges/createElement.test.js +++ b/__tests__/pendingTestChallenges/createElement.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'createElement' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'createElement'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'createElement'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/getElementsByClassName.test.js b/__tests__/pendingTestChallenges/getElementsByClassName.test.js index d8da942..7af88d1 100644 --- a/__tests__/pendingTestChallenges/getElementsByClassName.test.js +++ b/__tests__/pendingTestChallenges/getElementsByClassName.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'getElementsByClassName' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'getElementsByClassName'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'getElementsByClassName'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/getElementsByTagName.test.js b/__tests__/pendingTestChallenges/getElementsByTagName.test.js index f4164da..97faf53 100644 --- a/__tests__/pendingTestChallenges/getElementsByTagName.test.js +++ b/__tests__/pendingTestChallenges/getElementsByTagName.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'getElementsByTagName' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'getElementsByTagName'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'getElementsByTagName'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/parent.test.js b/__tests__/pendingTestChallenges/parent.test.js index 32a4a84..9edd0e0 100644 --- a/__tests__/pendingTestChallenges/parent.test.js +++ b/__tests__/pendingTestChallenges/parent.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'parent' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'parent'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'parent'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/selectAll.test.js b/__tests__/pendingTestChallenges/selectAll.test.js index 350e5c6..a89a795 100644 --- a/__tests__/pendingTestChallenges/selectAll.test.js +++ b/__tests__/pendingTestChallenges/selectAll.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'selectAll' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'selectAll'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'selectAll'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/selectAllAttribute.test.js b/__tests__/pendingTestChallenges/selectAllAttribute.test.js index c976035..d1abfc9 100644 --- a/__tests__/pendingTestChallenges/selectAllAttribute.test.js +++ b/__tests__/pendingTestChallenges/selectAllAttribute.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'selectAllAttribute' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'selectAllAttribute'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'selectAllAttribute'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/pendingTestChallenges/selectAllPseudoClass.test.js b/__tests__/pendingTestChallenges/selectAllPseudoClass.test.js index 22d60e3..71f9f18 100644 --- a/__tests__/pendingTestChallenges/selectAllPseudoClass.test.js +++ b/__tests__/pendingTestChallenges/selectAllPseudoClass.test.js @@ -2,30 +2,7 @@ * Test function vanilla on case 'selectAllPseudoClass' */ -const { vanilla } = require('../../lib/vanilla'); -const loadFileToBuffer = require('../../lib/loadFileToBuffer'); +const { testVanillaOnTestCase } = require('../lib/testCommandOnTestCase'); -describe("Test command 'vanilla' on test challenges", () => { - const testName = 'selectAllPseudoClass'; - - test(`Vanillaize '${testName}'`, async () => { - // Input - const argv = { - _: ['vanilla', `./__tests__/testCases/${testName}.js`], - C: true, - 'no-cache': true, - noCache: true, - $0: 'vaniquery', - }; - - // Output - const output = await vanilla(argv); - - // Answer key - const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`; - const answerKey = await loadFileToBuffer(answerKeyFile); - - // Expect - expect(output).toEqual(answerKey); - }); -}); +const testCase = 'selectAllPseudoClass'; +testVanillaOnTestCase(testCase); diff --git a/__tests__/testCases/getElementById.js b/__tests__/testCases/getElementById.js index 6ed24f8..3de612a 100644 --- a/__tests__/testCases/getElementById.js +++ b/__tests__/testCases/getElementById.js @@ -1,3 +1,3 @@ -var footer = $('#footer'); +var footer = document.getElementById('footer'); -$('#something'); +document.getElementById('something'); diff --git a/bin/vaniquery.js b/bin/vaniquery.js index 899f90c..2942238 100755 --- a/bin/vaniquery.js +++ b/bin/vaniquery.js @@ -4,11 +4,9 @@ * Main script of vaniquery */ -const revert = require('../lib/revert'); +const { revert } = require('../lib/revert'); const { vanilla, vanillaOut } = require('../lib/vanilla'); - -const thisYear = new Date().getFullYear(); -const copyrightNotice = `Copyright (c) ${thisYear} Badwater Bay`; +const { copyrightNotice } = require('../lib/helpers'); require('yargs') // eslint-disable-line .usage('Usage: vaniquery [OPTIONS] COMMAND') diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 0000000..7f47b6e --- /dev/null +++ b/lib/helpers.js @@ -0,0 +1,84 @@ +/** + * A collection of minor helper functions + */ + +const fs = require('fs'); + +/** + * Return a string of copyright notice + */ +const copyrightNotice = (() => { + const thisYear = new Date().getFullYear(); + return `Copyright (c) ${thisYear} Badwater Bay (under MIT license)`; +})(); + +exports.copyrightNotice = copyrightNotice; + +/** + * Return an array of filenames in a directory + * @param {string} testDir + */ +const listAllFilesInDir = (testDir) => { + const testCases = []; + fs.readdirSync(testDir).forEach((filename) => { + testCases.push(filename.substr(0, filename.indexOf('.test.js'))); + }); + return testCases; +}; + +exports.listAllFilesInDir = listAllFilesInDir; + +/** + * Loading a file to stream buffer + * @param {string} answerKeyFile + */ +const loadFileToBuffer = async (answerKeyFile) => { + return new Promise((resolve) => { + const stream = fs.createReadStream(answerKeyFile); + stream.on('data', (buffer) => { + resolve(buffer.toString()); + }); + }); +}; + +exports.loadFileToBuffer = loadFileToBuffer; + +/** + * Return filename from argument values + * @param {Object} argv + */ +const parseFilename = (argv) => argv._[1]; + +exports.parseFilename = parseFilename; + +/** + * Read a file and write to another file + * @param {string} readFile + * @param {string} writeFile + */ +const readPipeWrite = (readFile, writeFile) => { + const readStream = fs.createReadStream(readFile); + const writeStream = fs.createWriteStream(writeFile); + readStream.pipe(writeStream); +}; + +exports.readPipeWrite = readPipeWrite; + +/** + * Save cache file + * @param {Object} argv + */ +const saveCacheFile = (argv) => { + try { + const file = parseFilename(argv); + const cacheFile = `${file}.vaniquerycache`; + readPipeWrite(file, cacheFile); + console.log('Cache file saved.'); + return true; + } catch (err) { + console.error(err); + return false; + } +}; + +exports.saveCacheFile = saveCacheFile; diff --git a/lib/loadFileToBuffer.js b/lib/loadFileToBuffer.js deleted file mode 100644 index 50b31ee..0000000 --- a/lib/loadFileToBuffer.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Loading a file to stream buffer - * @param {string} answerKeyFile - */ - -const fs = require('fs'); - -module.exports = async (answerKeyFile) => { - return new Promise((resolve) => { - const stream = fs.createReadStream(answerKeyFile); - stream.on('data', (buffer) => { - resolve(buffer.toString()); - }); - }); -}; diff --git a/lib/revert.js b/lib/revert.js index 55cee6c..b322c39 100644 --- a/lib/revert.js +++ b/lib/revert.js @@ -1,23 +1,24 @@ /** * Revert changes of vanillaizing a file, if its cached file is present - * @param {Object} argv */ const fs = require('fs'); +const { parseFilename, readPipeWrite } = require('./helpers'); -module.exports = (argv) => { +/** + * @param {Object} argv + */ +exports.revert = (argv) => { try { console.log('Reverting vanillaization...'); - const file = argv._[1]; - const cachedFile = `${file}.vaniquerycache`; + const file = parseFilename(argv); + const cacheFile = `${file}.vaniquerycache`; - if (fs.existsSync(cachedFile)) { + if (fs.existsSync(cacheFile)) { const revertFile = new Promise((resolve, reject) => { try { - const readStream = fs.createReadStream(cachedFile); - const writeStream = fs.createWriteStream(file); - readStream.pipe(writeStream); + readPipeWrite(cacheFile, file); resolve(); } catch (err) { console.error(err); @@ -25,15 +26,27 @@ module.exports = (argv) => { } }); - revertFile.then(fs.unlinkSync(cachedFile)).catch((err) => { - console.error('Something went wrong during reverting vanillaization.'); - console.error(err); - }); - console.log('Reverting vanillaization finished.'); + revertFile + .then(() => { + fs.unlinkSync(cacheFile); + return true; + }) + .catch((err) => { + console.error( + 'Something went wrong during reverting vanillaization.' + ); + console.error(err); + return false; + }) + .finally(console.log('Reverting vanillaization finished.')); } else { console.log('No cache file found. Cannot revert vanillaization.'); + return false; } } catch (err) { + console.error('Something went wrong during reverting vanillaization.'); console.error(err); + return false; } + return false; }; diff --git a/lib/vanilla.js b/lib/vanilla.js index 893d1cd..d6a60b2 100644 --- a/lib/vanilla.js +++ b/lib/vanilla.js @@ -4,30 +4,7 @@ const fs = require('fs'); const equivalentsLib = require('./equivalentsLib.js'); - -/** - * Get filename from argument values - * @param {Object} argv - */ -const parseFilename = (argv) => argv._[1]; - -/** - * Create cache file - * @param {Object} argv - */ -const saveCacheFile = (argv) => { - try { - const file = parseFilename(argv); - const readStream = fs.createReadStream(file); - const writeStream = fs.createWriteStream(`${file}.vaniquerycache`); - readStream.pipe(writeStream); - console.log('Cache file saved.'); - return true; - } catch (err) { - console.error(err); - return false; - } -}; +const { parseFilename, saveCacheFile } = require('./helpers'); /** * Vanillaize a file @@ -79,13 +56,17 @@ exports.vanillaOut = (argv, result) => { const file = parseFilename(argv); fs.writeFile(file, result, 'utf8', (err) => { if (err) { - throw err; + console.error('Something went wrong when writing vanillaized file.'); + console.error(err); + return false; } + console.log('Vanillaization finished.'); + return true; }); - console.log('Vanillaization finished.'); - return true; } catch (err) { + console.error('Something went wrong when writing vanillaized file.'); console.error(err); return false; } + return false; }; diff --git a/package.json b/package.json index 9a76b73..abdb9a4 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ "test": "jest ./__tests__/main.test.js", "refresh": "rm -rf ./node_modules/ && yarn ci" }, + "engines": { + "node": ">=8.0.0" + }, "husky": { "hooks": { "pre-commit": "lint-staged" @@ -34,6 +37,7 @@ "eslint-config-airbnb-base": "^14.2.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-import": "^2.22.0", + "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^3.1.4", "husky": "^4.2.5", "jest": "^26.1.0", diff --git a/yarn.lock b/yarn.lock index aaa9dbc..aebc081 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1394,6 +1394,14 @@ eslint-module-utils@^2.6.0: debug "^2.6.9" pkg-dir "^2.0.0" +eslint-plugin-es@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/eslint-plugin-es/-/eslint-plugin-es-3.0.1.tgz#75a7cdfdccddc0589934aeeb384175f221c57893" + integrity sha512-GUmAsJaN4Fc7Gbtl8uOBlayo2DqhwWvEzykMHSCZHU3XdJ+NSzzZcVhXh3VxX5icqQ+oQdIEawXX8xkR3mIFmQ== + dependencies: + eslint-utils "^2.0.0" + regexpp "^3.0.0" + eslint-plugin-import@^2.22.0: version "2.22.0" resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.22.0.tgz#92f7736fe1fde3e2de77623c838dd992ff5ffb7e" @@ -1413,6 +1421,18 @@ eslint-plugin-import@^2.22.0: resolve "^1.17.0" tsconfig-paths "^3.9.0" +eslint-plugin-node@^11.1.0: + version "11.1.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-11.1.0.tgz#c95544416ee4ada26740a30474eefc5402dc671d" + integrity sha512-oUwtPJ1W0SKD0Tr+wqu92c5xuCeQqB3hSCHasn/ZgjFdA9iDGNkNf2Zi9ztY7X+hNuMib23LNGRm6+uN+KLE3g== + dependencies: + eslint-plugin-es "^3.0.0" + eslint-utils "^2.0.0" + ignore "^5.1.1" + minimatch "^3.0.4" + resolve "^1.10.1" + semver "^6.1.0" + eslint-plugin-prettier@^3.1.4: version "3.1.4" resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-3.1.4.tgz#168ab43154e2ea57db992a2cd097c828171f75c2" @@ -1428,7 +1448,7 @@ eslint-scope@^5.1.0: esrecurse "^4.1.0" estraverse "^4.1.1" -eslint-utils@^2.1.0: +eslint-utils@^2.0.0, eslint-utils@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-2.1.0.tgz#d2de5e03424e707dc10c74068ddedae708741b27" integrity sha512-w94dQYoauyvlDc43XnGB8lU3Zt713vNChgt4EWwhXAP2XkBvndfxF0AgIqKOOasjPIPzj9JqgwkwbCYD0/V3Zg== @@ -1989,6 +2009,11 @@ ignore@^4.0.6: resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc" integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg== +ignore@^5.1.1: + version "5.1.8" + resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57" + integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw== + import-fresh@^3.0.0, import-fresh@^3.1.0: version "3.2.1" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.2.1.tgz#633ff618506e793af5ac91bf48b72677e15cbe66" @@ -3512,7 +3537,7 @@ regex-not@^1.0.0, regex-not@^1.0.2: extend-shallow "^3.0.2" safe-regex "^1.1.0" -regexpp@^3.1.0: +regexpp@^3.0.0, regexpp@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-3.1.0.tgz#206d0ad0a5648cffbdb8ae46438f3dc51c9f78e2" integrity sha512-ZOIzd8yVsQQA7j8GCSlPGXwg5PfmA1mrq0JP4nGhh54LaKN3xdai/vHUDu74pKwV8OxseMS65u2NImosQcSD0Q== @@ -3606,7 +3631,7 @@ resolve-url@^0.2.1: resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= -resolve@^1.10.0, resolve@^1.13.1, resolve@^1.17.0, resolve@^1.3.2: +resolve@^1.10.0, resolve@^1.10.1, resolve@^1.13.1, resolve@^1.17.0, resolve@^1.3.2: version "1.17.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444" integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w== @@ -3711,7 +3736,7 @@ semver-regex@^2.0.0: resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== -semver@^6.0.0, semver@^6.3.0: +semver@^6.0.0, semver@^6.1.0, semver@^6.3.0: version "6.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==