REFACTOR
This commit is contained in:
parent
866adcd94d
commit
0063ccd9fc
0
GruntfilePdf.js
Normal file → Executable file
0
GruntfilePdf.js
Normal file → Executable file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0
public/javascript.js
Normal file → Executable file
0
public/javascript.js
Normal file → Executable file
37
src/app.js
37
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();
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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');
|
||||
});
|
||||
});
|
||||
@ -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
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -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()
|
||||
});
|
||||
111
test/Util.js
111
test/Util.js
@ -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));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
8
test/node/Config.js
Normal file
8
test/node/Config.js
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user