Skip to content

Commit

Permalink
adding correct iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
shershen08 committed Jan 26, 2017
1 parent 63359e6 commit 3c43456
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 153 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@ Now only basic proof of concept is available which can load folder with .vue fil

Target is to have some tool with at least some of the [React version](https://github.com/styleguidist/react-styleguidist) capabilities.

**Very early alpha mode**
**EARLY ALPHA MODE NOTICE!**

### NPM version available
### Setup

An NPM version available:

- ```npm install vue-styleguide-generator --save-dev```
- add to you package.json in script section - ```"build-components": "node ./node_modules/vue-styleguide-generator/"``` then run ```npm run build-components```
- You can also manually (or using Gulp/Grunt) run ```node ./node_modules/vue-styleguide-generator/```


### CLI options
### Usage

#### CLI options

- src : String, source dir; __default: 'src'__
- dest : String, destination output dir; __default: 'components-preview'__
- exclude : RegExp, file mask to exclude; __default: '/^\./'__
- locale : String; __default: 'en'__
- src : String, source dir; __default: 'src'__;
- dest : String, destination output dir; __default: 'components-preview'__;
- exclude : RegExp, file mask to exclude; __default: '/^\./'__;
- locale : String; __default: 'en'__;

E.g.: ```node ./node_modules/vue-styleguide-generator/ --src components --dest preview``` will read components from PROJECT_ROOT/components folder and provide a html page into PROJECT_ROOT/preview folder

#### Who the components are iterated (Why I don't see it in the list ?!)

If you want to see more information about the component - add a .md file in the same directory.
While retrieving the files the following assumptions are made:
- if there are files with the **same name** in one folder (e.g. ```/scr/partials/TabBar.vue``` and ```/scr/partials/TabBar.md```) it's assumed to be one component;
- if there are **only two** files in the folder and one is .vue and another is .md (e.g. ```/scr/TabBar/codez.vue``` and ```/scr/partials/readers.md```) it's assumed to be one component;
- If there are >1 .vue files in the folder and .md file that does not have the same name as one of the .vue files - this .md file is ignored
- Components with no props, no computed and no methods defined are considered to be a simple wrappers and not outputed


Found a bug or have a better proposal - please create an issue or tweet me @legkoletat!

### Output UI demo

Expand All @@ -31,7 +46,6 @@ Target is to have some tool with at least some of the [React version](https://gi
### Todos

- testing and cleanup
- core: merge component file with .md file in the same directory
- core: move the demo-page to use Vue so that components can be generated from its declaration
- ui: search in list of components
- ui: output extra component parameters (computable, data)
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><script src="https://code.getmdl.io/1.3.0/material.min.js" defer></script><link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css"><link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"><link rel="stylesheet" href="https://getmdl.io/templates/article/styles.css"><link rel="stylesheet" href="custom-styles.css"><body class="component-lib"><div class="mdl-layout mdl-js-layout mdl-layout--fixed-drawer"><div class="mdl-layout__drawer"><form><div class="mdl-textfield mdl-js-textfield search-area"><input class="mdl-textfield__input js-component-search" type="text" placeholder="Search..."></div></form><span class="mdl-layout-title">Collection</span><nav class="mdl-navigation"><a class="mdl-navigation__link" title="LoremIpsum.vue" href="#LoremIpsum">LoremIpsum.vue</a><a class="mdl-navigation__link" title="ScrollProgress.vue" href="#ScrollProgress">ScrollProgress.vue</a><a class="mdl-navigation__link" title="VueSound.vue" href="#VueSound">VueSound.vue</a></nav></div><main class="mdl-layout__content"><div class="page-content"><div class="demo-content mdl-color--white mdl-shadow--4dp content mdl-color-text--grey-800"><section id="LoremIpsum"><div class="demo-crumbs mdl-color-text--grey-500">src-test/lorem-generator/LoremIpsum.vue</div><h3>lorem</h3><!--Properties--><h4>Properties</h4><ul><li>add</li><li>extraClass</li></ul><!--Methods--><h4>Methods</h4><ul><li>createAndPutLoremText</li><li>selectCorrectRef</li></ul><!--Methods--><h4>Methods</h4><ul><li>createAndPutLoremText</li><li>selectCorrectRef</li></ul><!--Usage--><h4>Usage</h4><code>&lt;lorem&gt;&lt;/lorem&gt;</code><!--Rendered--><!--h4 Rendered view--><!--.result-wrapper--><!--!=renderedHtml--></section>,<section id="ScrollProgress"><div class="demo-crumbs mdl-color-text--grey-500">src-test/scroll-progress/ScrollProgress.vue</div><h3>content-scroll-progress</h3><!--Properties--><h4>Properties</h4><ul><li>spy</li><li>color</li><li>extraClass</li></ul><!--Methods--><h4>Methods</h4><ul><li>getChildrenHeight</li><li>iterateChildren</li><li>calculateScrollRatio</li><li>handleScroll</li><li>handleInitialProps</li></ul><!--Methods--><h4>Methods</h4><ul><li>getChildrenHeight</li><li>iterateChildren</li><li>calculateScrollRatio</li><li>handleScroll</li><li>handleInitialProps</li></ul><!--Usage--><h4>Usage</h4><code>&lt;content-scroll-progress&gt;&lt;/content-scroll-progress&gt;</code><!--Rendered--><!--h4 Rendered view--><!--.result-wrapper--><!--!=renderedHtml--></section>,<section id="VueSound"><div class="demo-crumbs mdl-color-text--grey-500">src-test/vue-sound/src/VueSound.vue</div><h3>vue-audio</h3><!--Properties--><h4>Properties</h4><ul><li>file</li><li>autoPlay</li></ul><!--Methods--><h4>Methods</h4><ul><li>stop</li><li>play</li><li>pause</li><li>download</li><li>mute</li><li>_handleLoaded</li><li>_handlePlayingUI</li><li>_handlePlayPause</li><li>init</li><li>getAudio</li></ul><!--Methods--><h4>Methods</h4><ul><li>stop</li><li>play</li><li>pause</li><li>download</li><li>mute</li><li>_handleLoaded</li><li>_handlePlayingUI</li><li>_handlePlayPause</li><li>init</li><li>getAudio</li></ul><!--Usage--><h4>Usage</h4><code>&lt;vue-audio&gt;&lt;/vue-audio&gt;</code><!--Rendered--><!--h4 Rendered view--><!--.result-wrapper--><!--!=renderedHtml--></section></div></div></main></div><footer class="footer"><p>Vue component library &copy; 2017</p></footer><style type="text/css">.component-lib code{padding:20px;background:#f8f8f8;width:90%;display:block;}.component-lib h3{color:#42b983;}.component-lib .mdl-layout__drawer .mdl-navigation .mdl-navigation__link:hover{background:#a8eacc;}.component-lib .search-area input{margin:10px 0px 0px 20px;}.component-lib footer{position:absolute;bottom:0;width:100%;z-index:10000;background:#fff;}.component-lib footer p{padding:10px 0px 10px 20px;margin:0;color:#ccc;}</style></body></html>
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var libGenerator = require('./src/main');
var argv = require('minimist')(process.argv.slice(2));

const DEFAULT_SOURCE_DIR = 'src';
const DEFAULT_SOURCE_DIR = 'src-test';
const DEFAULT_DIST_DIR = 'components-preview';
const DEFAULT_LOCALE = 'en';

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"license": "MIT",
"dependencies": {
"consolidate": "^0.14.0",
"css": "^2.2.1",
"hash-sum": "^1.0.2",
"js-beautify": "^1.6.3",
"loader-utils": "^0.2.10",
Expand Down
178 changes: 178 additions & 0 deletions src/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
var path = require('path')
var fs = require('fs')
var markdown = require( "markdown" ).markdown;
var Q = require('Q');
var css = require('css');

var fileProcessor = require('./processor');
var drawer = require('./drawer');
var utils = require('./utils');
var walker = require('./walker');

const COMPONENTS_FOLDER = 'example-components'
const OUTPUT_FOLDER = 'collection-preview'
const OUTPUT_FILENAME = 'index.html'
const CUSTOM_CSS = 'style/custom-styles.css';

let outputPath;
let runOptions;

module.exports = {
iterateComponentsFolder : (folderFromName, folderToName, options) => {

setVariables(folderFromName, folderToName, options);

walker.walk(folderFromName, runOptions, function(result){
sortOutResultingList(result);
})
}
}
const sortOutResultingList = (list) => {
var nonEmpty = list.filter((x) => x.files.length);
let flatFileList = [];

nonEmpty.forEach(function(elem){

if(elem.files.length === 1){
flatFileList.push({file: elem.files[0]});
} else if(elem.files.length === 2){ // && fileNamesAreComplimentary(elem.files)
flatFileList = flatFileList.concat({
file: elem.files[0],
readme: elem.files[1]
});
} else {
flatFileList = flatFileList.concat(elem.files.map((f) => {
return {file: f}
}));
}
});
console.log(flatFileList);
generateFiles(flatFileList);
}

const fileNamesAreComplimentary = (filesArray) => {
const f1 = path.basename(filesArray[0]);
const f2 = path.basename(filesArray[1]);
if((f1.split('.')[0] == f2.split('.')[0]) && (f1.split('.')[1] != f2.split('.')[1])) return true;
return false;
}

const setVariables = (folderFromName, folderToName, options) => {
runOptions = options;
folderFromName = folderFromName || COMPONENTS_FOLDER;
outputPath = folderToName || OUTPUT_FOLDER;
}

const generateFullPage = (treeArray) => {

let links = drawer.generateLinkList(treeArray.map((x) => x.link))
let comps = treeArray.map((x) => drawer.generateComponentDescription(x.comp))

let data = {
links,
comps,
inlineCss : addInlinedCSS(CUSTOM_CSS)
}
/*
* assumption, script path: PROJECT_ROOT/node_modules/vue-styleguide-generator/
* assumption, output assumption PROJECT_ROOT/<outputPath>
*/
var dirPath = path.resolve(__dirname, '..', '..', '..', outputPath)
var pagePath = path.resolve(__dirname, '..', OUTPUT_FILENAME)//path.resolve(__dirname, '..', '..', '..', outputPath, OUTPUT_FILENAME)
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
fs.writeFileSync(pagePath, drawer.generatePage(data))
}
const generateFiles = (files) => {

if(files.length) {

items = files.map(function(fileItem){
return {
comp: fileItem.readme ? processWithReadmeFiles(fileItem) : processSingleFiles(fileItem),
link: fileItem.file
};
});

generateFullPage(modifyComponentsTree(items));

logResult(files.length, runOptions.i18n.console_processed);
} else {
logError(runOptions.i18n.console_no_files_found);
}
}
const modifyComponentsTree = (list) => {
let filtered = list.filter((x) => !x.comp._isWrapper)
return filtered;
}

const getFile = (filename) => {
return path.resolve(componentsFolder, filename);
}

const addInlinedCSS = (cssPath) => {
const cssfilePath = path.resolve(cssPath);
const cssFile = fs.readFileSync(cssfilePath, { encoding: "utf8" });
const obj = css.parse(cssFile, {});

return css.stringify(obj, {
inputSourcemaps: false,
compress: true
});
}

const processSingleFiles = (fileObject) => {
if(path.basename(fileObject.file).split('.')[1] === 'md') return; //folder with single .md file
return readComponent(fileObject);
}
const processWithReadmeFiles = (fileObject) => {
console.log('processWithReadmeFiles', fileObject);
// var ext = file.split('.')[1];
// if(ext === 'md'){
// return readMDfile(file)
// } else {
//
// }
}
const isSimpleWrapperComponent = (obj) => {
if(!obj.methods.length && !obj.props.length && !obj.computed.length) return true;
return false;
}

const readComponent = (fileObject) => {
const loadFile = fileObject.file;
let vueFile = fs.readFileSync(loadFile, {encoding: 'utf-8'})
let componentObject = fileProcessor.processComponent(vueFile);
let componentCode = utils.componentCodeFromName(componentObject);

let data = {
_isWrapper: false,
itemTitle: componentObject.name || path.basename(loadFile).split('.')[0],
fileName: loadFile,
compInitialData : (componentObject.data ? componentObject.data() : ''),
computed: utils.showIfAny(componentObject.computed),
props: utils.showIfAny(componentObject.props),
methods: utils.showIfAny(componentObject.methods),
componentCode,
htmlBlockId: path.basename(loadFile).split('.')[0]
};

if(isSimpleWrapperComponent(data)){
data._isWrapper = true;
}

return data;
}

const readMDfile = (loadFile) => {
let mdFile = fs.readFileSync(loadFile, {encoding: 'utf-8'});
return markdown.toHTML(mdFile);
}

const logResult = (text, suffix) => {
console.log(text + suffix)
}
const logError = (text) => {
console.log(text)
}
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var path = require('path');
var walker = require('./walker');
var generator = require('./generator');

const getLocaleFile = (localeValue) => {
const localeFileName = localeValue + '.json'
Expand All @@ -10,7 +10,7 @@ const mainMethod = (callOptions) => {
const options = Object.assign({}, callOptions, {
i18n: getLocaleFile(callOptions.locale)
})
walker.iterateComponentsFolder(options.src, options.dest, options);
generator.iterateComponentsFolder(options.src, options.dest, options);
}

module.exports = {
Expand Down
38 changes: 38 additions & 0 deletions src/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var Vue = require('vue')
var renderer = require('vue-server-renderer').createRenderer()
var path = require('path');
var parse = require('./parser')
var fs = require('fs')

var app = new Vue({
render: function (h) {
return h('p', 'hello world')
}
})


const renderFunction = (component, callback) => {
let piece;
var componentPath = path.resolve(__dirname, '..', component.fileName)
var file = fs.readFileSync(componentPath, {encoding: 'utf-8'});
var vueFile = parse(file, 'name', false)
console.log(vueFile.script);
// var stream = renderer.renderToString(vueFile, function(html){
// console.log(html);
// });
// stream.on('data', function (chunk) {
// piece += chunk
// })
// stream.on('end', function () {
// callback(piece)
// })

// renderer.renderToString(app, function (error, html) {
// if (error) throw error
// callback(html)
// })
}

module.exports = {
render : renderFunction
}
4 changes: 3 additions & 1 deletion src/templates/pageTemplate.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
doctype html
html
include partials/header.pug
body
body.component-lib
include partials/head.pug
.mdl-layout.mdl-js-layout.mdl-layout--fixed-drawer
div(class="mdl-layout__drawer")
Expand All @@ -15,3 +15,5 @@ html
.demo-content.mdl-color--white.mdl-shadow--4dp.content.mdl-color-text--grey-800
!=comps
include partials/footer.pug
style(type='text/css')
!=inlineCss
16 changes: 16 additions & 0 deletions src/templates/renderer-usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var renderer = require('./renderer');

const deferredRenderer = (comp) => {
var deferred = Q.defer();
renderer.render(comp, function(html){
deferred.resolve(html);
})
return deferred.promise;
}


//let views = treeArray.map((x) => deferredRenderer(x.comp));
console.log(views)
Q.spread(views, function () {
console.log(agruments)
});
Loading

0 comments on commit 3c43456

Please sign in to comment.