Skip to content

Commit

Permalink
Merge pull request #49 from danmademe/feature/better_tests
Browse files Browse the repository at this point in the history
Feature/better tests
  • Loading branch information
danielcherubini committed Mar 10, 2017
2 parents 4745615 + e785eb8 commit faec74c
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[ignore]
.*/node_modules/vue/src/*.
.*/dist/*.
.*/node_modules/vue/src/*.
19 changes: 0 additions & 19 deletions lib/defaults.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare function expressVue(componentPath: string, options: Object, callback: Function): void;

export = expressVue;
export = expressVue;
67 changes: 44 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
// @flow
'use strict';

import {
Defaults,
Types
} from './defaults';
} from './models';
import {
layoutParser,
componentParser
} from './parser';
import {renderHtmlUtil} from './utils';

function expressVue(componentPath, options, callback) {
let types = new Types();
let promiseArray = [];

let defaults = new Defaults(options.settings.vue, options.settings.views);
let types = new Types();
defaults.layoutPath = defaults.layoutsDir + defaults.defaultLayout + '.vue';
defaults.options = options;

let componentArray = [
layoutParser(defaults.layoutPath, defaults, types.LAYOUT),
componentParser(componentPath, defaults, types.COMPONENT)
];
function setupComponentArray(componentPath: string, defaults: Defaults) {
let array = [];
array.push(layoutParser(defaults.layoutPath, defaults, types.LAYOUT));
array.push(componentParser(componentPath, defaults, types.COMPONENT));

if (defaults.options.vue && defaults.options.vue.components) {
for (var component in defaults.options.vue.components) {
if (defaults.options.vue.components.hasOwnProperty(component)) {
const componentFile = defaults.componentsDir + defaults.options.vue.components[component] + '.vue';
componentArray.push(componentParser(componentFile, defaults, types.SUBCOMPONENT));
array.push(componentParser(componentFile, defaults, types.SUBCOMPONENT));
}
}
}
Promise.all(componentArray).then(function(components) {
renderHtmlUtil(components, defaults).then(function(html) {
callback(null, html);
}).catch(function(error) {
console.error(error);
callback(new Error(error));
});
}, function(error) {
console.error(error);
callback(new Error(error));
return array;
}

function renderError(error: string, callback: Function) {
console.error(error);
callback(new Error(error));
}

function renderComponents(components: Array<Object>, defaults: Defaults, callback: Function) {
renderHtmlUtil(components, defaults).then(function(html) {
callback(null, html);
})
.catch(function(error) {
renderError(error, callback);
});
}

function expressVue(componentPath: string, options: Object, callback: Function) {

let defaults = new Defaults(options);
promiseArray = setupComponentArray(componentPath, defaults);

Promise.all(promiseArray)
.then(function(components) {
renderComponents(components, defaults, callback);
})
.catch(function(error) {
renderError(error, callback);
});
}

export default expressVue;
export {
expressVue,
expressVue as default,
renderError,
setupComponentArray,
renderComponents
};
20 changes: 20 additions & 0 deletions lib/models/dataObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @flow
import Types from './types';

const types = new Types();

class DataObject {
data: Object;
constructor(componentData: Object, defaultData: Object, type: Types) {
switch (type) {
case types.COMPONENT:
this.data = Object.assign({}, componentData, defaultData);
break;
case types.SUBCOMPONENT:
this.data = componentData;
break;
}
}
}

export default DataObject;
23 changes: 23 additions & 0 deletions lib/models/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @flow
class Defaults {
rootPath: string;
layoutsDir: string;
componentsDir: string;
defaultLayout: string;
options: Object;
backupLayout: string;
layoutPath: string;
options: Object;
constructor(options: Object) {
this.rootPath = options.settings.vue.rootPath === undefined ? options.settings.views + '/' : options.settings.vue.rootPath + '/';
this.layoutsDir = options.settings.vue.layoutsDir === undefined ? '' : this.rootPath + options.settings.vue.layoutsDir + '/';
this.componentsDir = options.settings.vue.componentsDir === undefined ? '' : options.settings.vue.componentsDir + '/';
this.defaultLayout = options.settings.vue.defaultLayout === undefined ? '' : options.settings.vue.layoutsDir === undefined ? this.rootPath + options.settings.vue.defaultLayout : options.settings.vue.defaultLayout;
this.options = options.settings.vue.options === undefined ? {} : options.settings.vue.options;
this.backupLayout = '<template><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><script src="https://unpkg.com/vue/dist/vue.js"></script></head><body>{{{app}}}{{{script}}}</body></html></template><script></script><style></style>';
this.layoutPath = this.layoutsDir + this.defaultLayout + '.vue';
this.options = options;
}
}

export default Defaults;
9 changes: 9 additions & 0 deletions lib/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Types from './types';
import Defaults from './defaults';
import DataObject from './dataObject';

export {
Types,
Defaults,
DataObject
};
14 changes: 14 additions & 0 deletions lib/models/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @flow

class Types {
COMPONENT: string;
SUBCOMPONENT: string;
LAYOUT: string;
constructor() {
this.COMPONENT = 'COMPONENT';
this.SUBCOMPONENT = 'SUBCOMPONENT';
this.LAYOUT = 'LAYOUT';
}
}

export default Types;
69 changes: 38 additions & 31 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// @flow
import fs from 'fs';
import minify from 'html-minifier';
import {Types} from '../defaults';
import {DataObject, Types} from '../models';
import requireFromString from 'require-from-string';
import pug from 'pug';

const htmlMinifier = minify.minify;
const htmlRegex = /(<template.*?>)([\s\S]*?)(<\/template>)/gm;
const scriptRegex = /(<script.*?>)([\s\S]*?)(<\/script>)/gm;
const types = new Types();
const htmlMinifier = minify.minify;
const htmlRegex = /(<template.*?>)([\s\S]*?)(<\/template>)/gm;
const scriptRegex = /(<script.*?>)([\s\S]*?)(<\/script>)/gm;
const templateRegex = /\w*\.vue/g;

function htmlParser(body, minify) {
let bodyString = body.match(htmlRegex)[0];
function htmlParser(body: string, minify: boolean) {
const bodyArray = body.match(htmlRegex) || [];
let bodyString = bodyArray[0];
const templateLang = bodyString.replace(htmlRegex, '$1');
if (bodyString) {
bodyString = bodyString.replace(htmlRegex, '$2');
Expand All @@ -28,20 +30,8 @@ function htmlParser(body, minify) {
return bodyString;
}

class DataObject {
constructor(componentData, defaultData, type) {
switch (type) {
case types.COMPONENT:
this.data = Object.assign({}, componentData, defaultData);
break;
case types.SUBCOMPONENT:
this.data = componentData;
break;
}
}
}

function dataParser(script, defaults, type) {
function dataParser(script: Object, defaults: Object, type: Types) {
let finalScript = {};
for (var element in script) {
if (script.hasOwnProperty(element)) {
Expand All @@ -56,18 +46,24 @@ function dataParser(script, defaults, type) {
return finalScript;
}

function scriptParser(script, defaults, type) {
function scriptParser(script: string, defaults: Object, type: Types) {
const options = {
'presets': ['es2015']
};
let scriptString = script.match(scriptRegex)[0].replace(scriptRegex, '$2');
let babelScript = require('babel-core').transform(scriptString, options);
let evalScript = requireFromString(babelScript.code);
let finalScript = dataParser(evalScript.default, defaults, type);
const scriptArray = script.match(scriptRegex) || [];
if (scriptArray.length === 0) {
let error = `I had an error processing this script.\n${script}`;
console.error(error);
return null;
}
let scriptString = scriptArray[0].replace(scriptRegex, '$2');
let babelScript = require('babel-core').transform(scriptString, options);
let evalScript = requireFromString(babelScript.code);
let finalScript = dataParser(evalScript.default, defaults, type);
return finalScript;
}

function layoutParser(layoutPath, defaults, type) {
function layoutParser(layoutPath: string, defaults: Object, type: Types) {
return new Promise(function(resolve) {
fs.readFile(layoutPath, 'utf-8', function (err, content) {
if (err) {
Expand All @@ -79,7 +75,7 @@ function layoutParser(layoutPath, defaults, type) {
// console.warn(error)
}

const body = htmlParser(content);
const body = htmlParser(content, false);
content = content.replace(htmlRegex, '');
const script = scriptParser(content, defaults, type);

Expand All @@ -92,7 +88,7 @@ function layoutParser(layoutPath, defaults, type) {
});
}

function componentParser(templatePath, defaults, type) {
function componentParser(templatePath: string, defaults: Object, type: Types) {
return new Promise(function(resolve, reject) {
fs.readFile(templatePath, 'utf-8', function (err, content) {
if (err) {
Expand All @@ -103,13 +99,21 @@ function componentParser(templatePath, defaults, type) {
const body = htmlParser(content, true);
content = content.replace(htmlRegex, '');
const script = scriptParser(content, defaults, type);
const templateArray = templatePath.match(templateRegex) || [];

if (templateArray.length === 0) {
let error = `I had an error processing component templates. in this file \n${templatePath}`;
console.error(error);
reject(error);
}

let componentScript = script;
let templateName = templateArray[0].replace('\.vue', '');
let componentScript = script || {};
componentScript.template = body;

resolve({
type: type,
name: templatePath.match(/\w*\.vue/g)[0].replace('\.vue', ''),
name: templateName,
script: componentScript
});
}
Expand All @@ -119,5 +123,8 @@ function componentParser(templatePath, defaults, type) {

export {
componentParser,
layoutParser
layoutParser,
scriptParser,
dataParser,
htmlParser
};
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
renderVueComponents,
renderVueMixins
} from './render';
import {scriptToString} from './string';
import scriptToString from './string';
import headUtil from './head';

export {
Expand Down
Loading

0 comments on commit faec74c

Please sign in to comment.