From 0063ccd9fc8ade87cb946996f7748d1dbb269bfd Mon Sep 17 00:00:00 2001 From: unpregnant Date: Mon, 20 Feb 2017 20:01:31 +0100 Subject: [PATCH] REFACTOR --- GruntfilePdf.js | 0 pdf/resume-grey-boxes.pdf | Bin 90968 -> 90968 bytes pdf/resume-left-right.pdf | Bin 95593 -> 95593 bytes pdf/resume-oblique.pdf | Bin 55661 -> 55661 bytes pdf/resume-side-bar.pdf | Bin 50993 -> 50993 bytes pdf/resume-spotify.pdf | Bin 52127 -> 52127 bytes pdf/resume-wanted.pdf | Bin 36518 -> 36518 bytes public/javascript.js | 0 src/app.js | 37 ++++++++-- src/{ => node}/Config.js | 0 src/{ => node}/ResumeToPdf.js | 18 +++-- src/{ => node}/Server.js | 38 +++++++--- src/{ => node}/StyleCompiler.js | 26 ++++--- src/{ => node}/Util.js | 17 +++-- test/Config.js | 9 --- test/ResumeToPdf.js | 18 ----- test/Server.js | 0 test/StyleCompiler.js | 64 ---------------- test/Util.js | 111 ---------------------------- test/node/Config.js | 8 ++ test/{ => node}/files/readFile.txt | 0 test/{ => node}/files/writeFile.txt | 0 22 files changed, 105 insertions(+), 241 deletions(-) mode change 100644 => 100755 GruntfilePdf.js mode change 100644 => 100755 public/javascript.js rename src/{ => node}/Config.js (100%) rename src/{ => node}/ResumeToPdf.js (76%) rename src/{ => node}/Server.js (78%) rename src/{ => node}/StyleCompiler.js (73%) rename src/{ => node}/Util.js (88%) delete mode 100755 test/Config.js delete mode 100755 test/ResumeToPdf.js delete mode 100755 test/Server.js delete mode 100755 test/StyleCompiler.js delete mode 100755 test/Util.js create mode 100644 test/node/Config.js rename test/{ => node}/files/readFile.txt (100%) rename test/{ => node}/files/writeFile.txt (100%) diff --git a/GruntfilePdf.js b/GruntfilePdf.js old mode 100644 new mode 100755 diff --git a/pdf/resume-grey-boxes.pdf b/pdf/resume-grey-boxes.pdf index 9e62885760c54897040e2908583cfe7b75e2d4c2..47092ce7a2a6d7d1ce95fa8c7c96ad7848de3f70 100755 GIT binary patch delta 58 zcmca{jP=Gb)(LT1mPUpqCZ?9!1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&H#*s-;P H@j?;+17Hyt delta 58 zcmca{jP=Gb)(LT17KY{~CPqfu1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&H#*s-;P H@j?;+0zwfC diff --git a/pdf/resume-left-right.pdf b/pdf/resume-left-right.pdf index acdad96ce881e8cd9f9dd3fe0a82f720481a1e12..92c626f2f81e4ecf1b37de1d768b4f67692b3dad 100755 GIT binary patch delta 58 zcmaF)iuL6y)(LT1mPUpqrUnMu1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&H#*s-;P H@nH!77!(nU delta 58 zcmaF)iuL6y)(LT17KY{~CPv2E1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&H#*s-;P H@nH!78O9Nt diff --git a/pdf/resume-oblique.pdf b/pdf/resume-oblique.pdf index e76b533a1e017e648ba6bce4039fa83fc7d4c702..1f588c289892050ebd95d7eb851482c4f172cfd5 100755 GIT binary patch delta 55 zcmaF6iTUj&<_U3HmPUpqrUr)E1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&HVvE%6t E0MM`ydjJ3c delta 55 zcmaF6iTUj&<_U3H7KY{~CPpUO1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&HVvE%6t E0MarLivR!s diff --git a/pdf/resume-side-bar.pdf b/pdf/resume-side-bar.pdf index 4115d9a6ca546249b6f7efb80053c9273ed8c41c..2af00ede148dc12091857700ebb5b89b930cf9c4 100755 GIT binary patch delta 55 zcmdnk$GowRc|x3)rIDeDsezHUfq^;@X>#fN=BKzMmZU0ZxL6q(7@8Xx8A6q8>{xjW E0H}KpZvX%Q delta 55 zcmdnk$GowRc|x3)g`v5LiIJ(cfq^;@X>#fN=BKzMmZU0ZxL6q(7@8Xx8A6q8>{xjW E0IB^Ce*gdg diff --git a/pdf/resume-spotify.pdf b/pdf/resume-spotify.pdf index 0e99e62ed08d6f6b4a8fcfcd660921901dca72e3..2f40e8bef86e48a49e7cd93a908c804d6e86be94 100755 GIT binary patch delta 55 zcmbO~oq7Ir<_U3HmPUpqrUu5^1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&HVu|xbc E0Hd@H(f|Me delta 55 zcmbO~oq7Ir<_U3H7KY{~CPrr31_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&HVu|xbc E0Hrn#;s5{u diff --git a/pdf/resume-wanted.pdf b/pdf/resume-wanted.pdf index 0cff4ef48051079a9251cddb007ab706daecbb4b..57fa6230f6430cd502756ec93c318bae940c1bbc 100755 GIT binary patch delta 55 zcmZ2BmucBtrU`LcmPUpqrUu5^1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&HVu|uv8 E0H}cuv;Y7A delta 55 zcmZ2BmucBtrU`Lc7KY{~CPwDk1_tUtq{*f4o1fy6Sdyxs;bLWEU}$b&WC&HVu|uv8 E0IDz!#sB~S diff --git a/public/javascript.js b/public/javascript.js old mode 100644 new mode 100755 diff --git a/src/app.js b/src/app.js index b6c014a..d2862bc 100755 --- a/src/app.js +++ b/src/app.js @@ -1,14 +1,41 @@ -const StyleCompiler = require('./StyleCompiler'); -const ResumeToPdf = require('./ResumeToPdf'); -const Server = require('./Server'); +const path = require('path'); +const Config = require('./node/Config'); +const Util = require('./node/Util')( + path, + require('write'), + require('fs'), + require('child_process').exec +); +const Server = require('./node/Server')( + path, + require('express'), + require('request-promise'), + Config, + Util, + require('./person.js'), + require('http'), + require('reload') +); +const StyleCompiler = require('./node/StyleCompiler'); +const ResumeToPdf = require('./node/ResumeToPdf'); process.argv.forEach(argument => { switch (argument) { case 'less': - StyleCompiler.run(); + StyleCompiler( + require('less'), + path, + require('clean-css'), + Util + ); break; case 'pdf': - ResumeToPdf.convert(); + ResumeToPdf( + path, + Config, + Util, + Server + ); break; case 'server': Server.run(); diff --git a/src/Config.js b/src/node/Config.js similarity index 100% rename from src/Config.js rename to src/node/Config.js diff --git a/src/ResumeToPdf.js b/src/node/ResumeToPdf.js similarity index 76% rename from src/ResumeToPdf.js rename to src/node/ResumeToPdf.js index 6e16a64..6ccdb92 100755 --- a/src/ResumeToPdf.js +++ b/src/node/ResumeToPdf.js @@ -1,7 +1,4 @@ -const path = require('path'); -const Config = require('./Config'); -const Util = require('./Util'); -const Server = require('./Server'); +let path, Config, Util, Server; const ResumeToPdf = { /** @@ -10,7 +7,7 @@ const ResumeToPdf = { * @return {string} electroshot command */ electroshotScript: function(resume) { - const dir = path.join(__dirname, '../pdf'); + const dir = path.join(__dirname, '../../pdf'); return 'electroshot localhost:' + Config.port + '/' + resume + ' 2481x3508 --pdf-margin none --format pdf --out ' + dir + ' --filename "' + resume + '.pdf" --pdf-background; '; @@ -29,4 +26,13 @@ const ResumeToPdf = { await Server.kill(); } }; -module.exports = ResumeToPdf; + +const mod = function(pathD, ConfigD, UtilD, ServerD) { + path = pathD; + Config = ConfigD; + Util = UtilD; + Server = ServerD; + return ResumeToPdf.convert(); +}; + +module.exports = mod; diff --git a/src/Server.js b/src/node/Server.js similarity index 78% rename from src/Server.js rename to src/node/Server.js index d92eb7c..e469fb0 100755 --- a/src/Server.js +++ b/src/node/Server.js @@ -1,11 +1,4 @@ -const path = require('path'); -const express = require('express'); -const request = require('request-promise'); -const Config = require('./Config'); -const Util = require('./Util'); -const person = require('./person.js'); -const http = require('http'); -const reload = require('reload'); +let path, express, request, Config, Util, person, http, reload; let app, resumes; const Server = { @@ -14,11 +7,11 @@ const Server = { */ setup: function() { if (!app) app = express(); - app.set('views', path.join(__dirname, '../resumes')); + app.set('views', path.join(__dirname, '../../resumes')); app.engine('mustache', require('hogan-express')); app.set('view engine', 'mustache'); - app.use(express.static(path.join(__dirname, '../public'))); - app.use(express.static(path.join(__dirname, '../node_modules'))); + app.use(express.static(path.join(__dirname, '../../public'))); + app.use(express.static(path.join(__dirname, '../../node_modules'))); }, /** * starts up express app @@ -91,4 +84,25 @@ const Server = { } }; -module.exports = Server; +const mod = function( + pathD, + expressD, + requestD, + ConfigD, + UtilD, + personD, + httpD, + reloadD +) { + path = pathD; + express = expressD; + request = requestD; + Config = ConfigD; + Util = UtilD; + person = personD; + http = httpD; + reload = reloadD; + return Server; +}; + +module.exports = mod; diff --git a/src/StyleCompiler.js b/src/node/StyleCompiler.js similarity index 73% rename from src/StyleCompiler.js rename to src/node/StyleCompiler.js index 3ec0625..8b01120 100755 --- a/src/StyleCompiler.js +++ b/src/node/StyleCompiler.js @@ -1,7 +1,4 @@ -const less = require('less'); -const path = require('path'); -const CleanCSS = require('clean-css'); -const Util = require('./Util'); +let less, path, CleanCSS, Util; const StyleCompiler = { /** @@ -10,7 +7,7 @@ const StyleCompiler = { * @return {string} css */ compile: function(lessContent) { - let lessDir = path.join(__dirname, '../less'); + let lessDir = path.join(__dirname, '../../less'); return new Promise((res, rej) => { less.render(lessContent, { paths: [lessDir, lessDir + '/fonts'], @@ -38,10 +35,10 @@ const StyleCompiler = { * @return {Promise} */ run: async function() { - const styleLess = await Util.readFileContent(path.join(__dirname, '../less/style.less')); + const styleLess = await Util.readFileContent(path.join(__dirname, '../../less/style.less')); const styleCss = await StyleCompiler.compile(styleLess); const styleMinified = await StyleCompiler.minify(styleCss); - const stylePath = path.join(__dirname, '../public/styles/style.min.css'); + const stylePath = path.join(__dirname, '../../public/styles/style.min.css'); await Util.writeFile(stylePath, styleMinified.styles); const directories = Util.getResumesFromDirectories(); @@ -49,16 +46,25 @@ const StyleCompiler = { directories .map(async(resume) => { const resumeLess = await Util.readFileContent( - path.join(__dirname, '../resumes/' + resume.path + '/style.less') + path.join(__dirname, '../../resumes/' + resume.path + '/style.less') ); const resumeCss = await StyleCompiler.compile(resumeLess); const resumeMinified = await StyleCompiler.minify(resumeCss); //write file - const resumePath = path.join(__dirname, '../public/styles/' + resume.path + '.min.css'); + const resumePath = path.join(__dirname, '../../public/styles/' + resume.path + '.min.css'); await Util.writeFile(resumePath, resumeMinified.styles); }) ); } }; -module.exports = StyleCompiler; + +const mod = function(lessD, pathD, CleanCSSD, UtilD) { + less = lessD; + path = pathD; + CleanCSS = CleanCSSD; + Util = UtilD; + return StyleCompiler.run(); +}; + +module.exports = mod; diff --git a/src/Util.js b/src/node/Util.js similarity index 88% rename from src/Util.js rename to src/node/Util.js index 783c9fb..0490bb1 100755 --- a/src/Util.js +++ b/src/node/Util.js @@ -1,7 +1,4 @@ -const path = require('path'); -const writeFile = require('write'); -const fs = require('fs'); -var exec = require('child_process').exec; +let path, writeFile, fs, exec; const Util = { /** @@ -9,7 +6,7 @@ const Util = { * @return {[]} */ getDirectories: function() { - const srcpath = path.join(__dirname, '../resumes'); + const srcpath = path.join(__dirname, '../../resumes'); return fs.readdirSync(srcpath) .filter(file => file.includes('resume-')); }, @@ -80,4 +77,12 @@ const Util = { } }; -module.exports = Util; +const mod = function(pathD, writeFileD, fsD, execD) { + path = pathD; + writeFile = writeFileD; + fs = fsD; + exec = execD; + return Util; +}; + +module.exports = mod; diff --git a/test/Config.js b/test/Config.js deleted file mode 100755 index 479a587..0000000 --- a/test/Config.js +++ /dev/null @@ -1,9 +0,0 @@ -/* global it, describe */ -const assert = require('assert'); -const Config = require('../src/Config'); -describe('Config', () => { - it('should have property port', () => { - assert(Config.port != null); - assert(typeof Config.port == 'number'); - }); -}); diff --git a/test/ResumeToPdf.js b/test/ResumeToPdf.js deleted file mode 100755 index 52c6768..0000000 --- a/test/ResumeToPdf.js +++ /dev/null @@ -1,18 +0,0 @@ -/* global it, describe */ -const assert = require('assert'); -const ResumeToPdf = require('../src/ResumeToPdf'); -describe('ResumeToPdf', () => { - describe('#electroshotScript()', () => { - it('should return a string to execute', () => { - const resume = 'resume-any'; - const exec = ResumeToPdf.electroshotScript(resume); - assert(exec != null); - assert(exec.includes(resume)); - }); - }); - describe('#convert()', () => { - it('should return a string to execute', () => { - // TODO write test with spy - }); - }); -}); diff --git a/test/Server.js b/test/Server.js deleted file mode 100755 index e69de29..0000000 diff --git a/test/StyleCompiler.js b/test/StyleCompiler.js deleted file mode 100755 index 6d951ab..0000000 --- a/test/StyleCompiler.js +++ /dev/null @@ -1,64 +0,0 @@ -/* global it, describe */ -const assert = require('assert'); -const StyleCompiler = require('../src/StyleCompiler'); - -describe('StyleCompiler', () => { - describe('#compile()', () => { - it('should return a promise', () => { - const less = '@color: blue; h1{ color: @color; }'; - const promise = StyleCompiler.compile(less); - assert(promise instanceof Promise); - }); - describe('positive', () => { - it('should compile less to css', async() => { - const less = '@color: blue; h1{ color: @color; }'; - const expected = 'h1 {\n color: blue;\n}\n'; - const css = await StyleCompiler.compile(less); - assert.equal(css, expected); - - }); - it('should not throw an error when compiling to css', () => { - const less = '@color: blue; h1{ color: @color; }'; - StyleCompiler.compile(less) - .catch(err => assert(err == null)); - }); - }); - describe('negative', () => { - it('should throw an error if less is not correct', () => { - const less = '@color: blue; h1#asd+yx[]{ color: @color; }'; - StyleCompiler.compile(less) - .then(css => assert(css == null)) - .catch(err => assert(err != null)); - }); - }); - }); - describe('#minify()', () => { - it('should return a promise', () => { - const less = '@color: blue; h1{ color: @color; }'; - const promise = StyleCompiler.compile(less); - assert(promise instanceof Promise); - }); - describe('positive', () => { - it('should minify css', async() => { - const css = 'h1 {\n color: blue;\n}\n'; - const expected = 'h1{color:#00f}'; - const minified = await StyleCompiler.minify(css); - assert.equal(minified.styles, expected); - }); - it('should not throw an error when minifying css', () => { - const css = 'h1 {\n color: blue;\n}\n'; - StyleCompiler.compile(css) - .catch(err => assert(err == null)); - }); - }); - describe('negative', () => { - it('should throw an error if css is not correct', () => { - const css = 'h1 {\n color: #yxcöklljasd+blue;\n}\n'; - StyleCompiler.compile(css) - .then(minified => assert(minified == null)) - .catch(err => assert(err != null)); - }); - }); - }); - // TODO test for .run() -}); diff --git a/test/Util.js b/test/Util.js deleted file mode 100755 index 489b736..0000000 --- a/test/Util.js +++ /dev/null @@ -1,111 +0,0 @@ -/* global it, describe */ -const assert = require('assert'); -const Util = require('../src/Util'); -const path = require('path'); -describe('Util', () => { - describe('#getDirectories()', () => { - it('should get directories starting with "resume-"', () => { - const directories = Util.getDirectories(); - assert(directories.length > 0); - }); - }); - describe('#getResumesFromDirectories()', () => { - it('should get resumes names from directories', () => { - const resumes = Util.getResumesFromDirectories(); - const directories = Util.getDirectories(); - assert.equal(directories.length, resumes.length); - assert(directories.length > 0); - assert(directories[0].includes(resumes[0].path)); - }); - it('should get create resume names from filenames (removing -)', () => { - const resumes = Util.getResumesFromDirectories(); - assert(!resumes[0].name.includes('-')); - }); - }); - describe('#setTimeout', () => { - it('should return a promise', () => { - const promise = Util.setTimeout(200); - assert(promise instanceof Promise); - }); - it('should resolve promise after given amount of time', () => { - const ms = 100; - let resolved = false; - Util.setTimeout(ms) - .then(() => resolved = true); - setTimeout(() => { - assert(resolved); - }, ms + 100); - }); - it('should not reject promise', () => { - const ms = 100; - Util.setTimeout(ms) - .then() - .catch(err => assert(err === null)); - }); - }); - describe('#readFileContent', async() => { - it('should return a promise', () => { - const p = path.join(__dirname, './files/readFile.txt'); - const promise = Util.readFileContent(p); - assert(promise instanceof Promise); - }); - describe('positive', () => { - it('should return file content if file exists', async() => { - const p = path.join(__dirname, './files/readFile.txt'); - const content = await Util.readFileContent(p); - assert(content.includes('any content to be read')); - }); - it('should not return any error if file exists', async() => { - const p = path.join(__dirname, './files/readFile.txt'); - Util.readFileContent(p) - .catch(err => assert(err == null)); - }); - }); - describe('negative', () => { - it('should reject promise and return error if file does not exist', () => { - const p = 'asdasdasd'; - Util.readFileContent(p) - .then(content => assert(content === null)) - .catch(err => assert(err != null)); - }); - }); - }); - describe('#writeFile', () => { - it('should return a promise', () => { - const p = path.join(__dirname, './files/writeFile.txt'); - const promise = Util.writeFile(p, 'any content'); - assert(promise instanceof Promise); - }); - describe('positive', () => { - it('should write file content if file exists and not throw any error', () => { - const p = path.join(__dirname, './files/writeFile.txt'); - const content = 'alskldjaksldjaklsjdla' + Math.random(); - Util.writeFile(p, content) - .then(async() => { - const c = await Util.readFileContent(p); - assert.equal(c, content); - }) - .catch(err => assert(err === null)); - }); - }); - }); - describe('#execBash', () => { - it('should return a promise', () => { - const promise = Util.execBash('echo "Hello World"'); - assert(promise instanceof Promise); - }); - describe('positive', () => { - it('should execute bash script and resolve promise', () => { - Util.execBash('echo "Hello world"') - .then() - .catch(err => assert(err === null)); - }); - }); - describe('negative', () => { - it('should try to execute false bash script and reject promise', () => { - Util.execBash('asdlkajsd!+##') - .catch(err => assert(err != null)); - }); - }); - }); -}); diff --git a/test/node/Config.js b/test/node/Config.js new file mode 100644 index 0000000..c5e4696 --- /dev/null +++ b/test/node/Config.js @@ -0,0 +1,8 @@ +/* global it, describe */ +const assert = require('assert'); +const Config = require('../../src/node/Config'); +describe('Config', () => { + it('should contain a valid port number', () => { + assert(Config.port > 0); + }); +}); diff --git a/test/files/readFile.txt b/test/node/files/readFile.txt similarity index 100% rename from test/files/readFile.txt rename to test/node/files/readFile.txt diff --git a/test/files/writeFile.txt b/test/node/files/writeFile.txt similarity index 100% rename from test/files/writeFile.txt rename to test/node/files/writeFile.txt