Skip to content

Commit

Permalink
Major uncontrolled refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
karlisup committed Jul 21, 2017
1 parent 05ef7c4 commit ddabfee
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 588 deletions.
83 changes: 83 additions & 0 deletions lib/CountComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict'

var fs = require('graceful-fs')
var path = require('path')
var Component = require('./compolib.Component')

function CountComponents (opts) {
this._opts = opts
this._possibleExtensions = getAllTemplateExtensions.call(this)
}

CountComponents.prototype.init = function () {
recursivelyWalkFiletree.call(this, this._opts.location.src)
}

function recursivelyWalkFiletree (dir) {
fs.readdirSync(dir).filter(function (file) {
if (file.charAt(0) === '.') {
return // ignore hidden files
}
if (/^.*\.demo\.[^.\n]+$/.test(file)) {
return // ignore *.demo.* files
}
if (isFolder(file)) {
var filePath = path.join(dir, file)
recursivelyWalkFiletree.call(this, filePath)
}
if (!isSupportedTemplateFile.call(this, file)) {
return
}

var component = {}
component.name = file.substr(0, file.indexOf('.'))
component.fullPath = dir // for fetching file
component.patternPath = dir.split(this._opts.location.src)[1] // folder

// there could be a situation when e.g. .twig template initializes component
// creation. When you see another template file (e.g. mustache) you
// shouldn't initialize it again.
if (componentExists.call(this, file, component.patternPath)) {
return
}

// if component !exist - create it.
var newComponent = new Component(this._opts, component, this._possibleExtensions)
newComponent.assemble()
}.bind(this))
}

/**
* Detect from filename if it's folder or a file.
*/
function isFolder (file) {
return path.extname(file) === ''
}

/**
* Look up a component with the same name and url
* @param title
* @param folder
* @returns {boolean}
*/
function componentExists (title, folder) {
for (var i = 0, l = this._opts.components.length; i < l; i++) {
var component = this._opts.templateEngine[i]
if (component.name === title && component.path === folder) {
return true
}
}
return false
}

/**
* Returns if file extension is among the ones passed in gulpfile.js config.
* @param file
* @returns {boolean}
*/
function isSupportedTemplateFile (file) {
var fileExtension = path.extname(file).toLowerCase()
return this._possibleExtensions.indexOf(fileExtension) > -1
}

module.exports = CountComponents
39 changes: 39 additions & 0 deletions lib/GetDataFromJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var fs = require('graceful-fs')
var path = require('path')

function GetDataFromJSON (opts) {
this._opts = opts
this._dataRegex = /["']data@(.*)["']/g
this._jsonContnets = ''
}

GetDataFromJSON.prototype.fetchData = function (jsonPath) {
if (!fs.existsSync(jsonPath)) {
console.error(jsonPath + ' file does not exist!')
return {}
}

this._jsonContents = fs.readFileSync(jsonPath, 'utf8')
if (hasDataFileReference(this._jsonContents)) {
var match = this._dataRegex.exec(this._jsonContents)
replaceReferencesWithData(match)
}
return this._jsonContents
}

function hasDataFileReference (data) {
return (data.indexOf('data@') !== -1)
}

function replaceReferencesWithData (match) {
while (match != null) {
var referencedFilename = match[1]
var fileContents = this.fetchData(this._opts,
path.join(this._opts.location.src, referencedFilename))
fileContents = fileContents || 'undefined'
this._jsonContnets = this._jsonContnets.replace(referencedFilename, fileContents)
match = this._dataRegex.exec(this._jsonContnets)
}
}

exports.GetDataFromJSON = GetDataFromJSON
33 changes: 0 additions & 33 deletions lib/LandingPage.js

This file was deleted.

22 changes: 22 additions & 0 deletions lib/RenderPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var helpers = require('./compolib.helpers')
var Twig = require('twig')
var twig = Twig.twig
Twig.cache(false)

function RenderPage (opts) {
//this._opts = opts
}

RenderPage.prototype.render = function (config, data, destPath) {
var dashboardHTML = renderHTML(config, data)
helpers.writeFile(destPath, dashboardHTML)
}

function renderHTML (config, data) {
var defaultConfig = {
async: false
}
return twig(Object.assign(defaultConfig, config)).render(data)
}

exports.RenderPage = RenderPage
122 changes: 63 additions & 59 deletions lib/compolib.Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,85 @@

var fs = require('graceful-fs')
var path = require('path')
// var console = require('better-console') // [optional]
var debug = false

/**
* Creating new component
* @param opts
* @param filePath
* @param component
*/
var Component = function Component (opts, filePath) {
// general information
this.path = '' // relateive url
this.name = '' // component name
this.code = [] // array of file markups
// component related files
this.tmpl = {} // ->path & ->compiled
this.info = {} // ->path & ->compiled
this.data = '' // .json
this.style = '' // .sass (and others)
this.js = '' // .js
function Component (opts, component, possibleExtensions) {
this._opts = opts
this._component = component

if (filePath) {
// TODO: if component is root split is returning undefined. Fix this in beautiful way
this.path = (path.dirname(filePath).split(opts.location.src)[1] === undefined) ? '/' : path.dirname(filePath).split(opts.location.src)[1]
// this.path = path.dirname(filePath).split(opts.location.src)[1] // relative folder url
this.name = path.basename(filePath, opts.extensions.template) // foo/bar/baz/asdf/quux.html ==> quux
this.tmpl.path = path.join(this.path, this.name + opts.extensions.template)
}
this._component.files = []
this._component.code = [] // array of file markups

// component related files
/*this._component.tmpl = [] // ->path & DEPRECATED: compiled
this._component.info = '' // ->path & DEPRECATED: compiled
this._component.data = '' // .json
this._component.style = '' // .sass (and others)
this._component.js = '' // .js*/
}
// TODO test
// - creating component with wierd extension
// - creating component on windows / mac
// - weird user passed template extension

/**
* Find other parts of component (data, info, etc.)
* @param component
* @return component
*/
Component.prototype.assemble = function assemble (opts) {
var _this = this
if (debug) console.info('Assembling component: ' + _this.name)
fs.readdirSync(opts.location.src + _this.path).filter(function (file) {
var extension = (path.extname(file) === undefined) ? 'folder' : path.extname(file).toLowerCase()
Component.prototype.assemble = function assemble () {
walkFolderFiles.call(this, this._component.fullPath)
}

function walkFolderFiles(folder) {
fs.readdirSync(folder).filter(function (file) {
var extension = path.extname(file).toLowerCase() || ''
var fileName = file.substr(0, file.indexOf('.'))

// only files with the same name as template or '_' in front
if (file.substr(0, file.indexOf('.')) === _this.name ||
file.substr(0, file.indexOf('.')) === '_' + _this.name) {

// Ad demo file to component if it exists.
if( RegExp(".*\.demo" + opts.extensions.template).test(file) ) {
_this.demo = path.join(_this.path, file)
} else {
switch (extension) {
case '.json':
_this.data = path.join(_this.path, file)
break
case '.md':
_this.info.path = path.join(_this.path, file)
break
case '.js':
_this.js = path.join(_this.path, file)
break
case '.scss':
_this.style = path.join(_this.path, file)
break
}
if (fileName === this._component.name || '_' + this._component.name) {
// Add demo file to component if it exists.
if ( RegExp('.*\.demo\..*').test(file)) {
addFile.call(this, 'demo', file)
return
}

switch (extension) {
case '.json':
addFile.call(this, 'data', file)
break
case '.md':
addFile.call(this, 'info', file)
break
case '.js':
addFile.call(this, 'js', file)
break
case '.scss':
addFile.call(this, 'style', file)
break
}
// if extension is among supported templating engines
if (this._opts.templateExtensions.indexOf(extension) !== -1) {
addFile.call(this, 'tmpl', file)
}
}
})
}.bind(this))
}

opts.components.push(_this)
return _this
function addFile (type, filename) {
var fileFullPath = path.join(this._component.fullPath, filename)
if (type === 'tmpl') {
this._component.tmpl.push(fileFullPath)
}
this._component[type] = fileFullPath
}
// TODO test
// - pass folder url and check if all files are registred in component object
// - try breaking readdirSync(opts.location.src + _this.path)

// if (filePath) {
// // if component is in root folder it's location becomes undefined
// var url = path.dirname(filePath).split(opts.location.src)[1] // path -> url
// this.path = (url === undefined) ? '/' : url
// this.name = path.basename(filePath, opts.extensions.template) // foo/bar/baz/asdf/quux.html ==> quux
// this.tmpl.path = path.join(this.path, this.name + opts.extensions.template)
// }



module.exports = Component
32 changes: 23 additions & 9 deletions lib/compolib.build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict'

// var util = require('util')
var path = require('path')
var console = require('better-console')
// var renderNav = require('./compolib.renderNav')
var filetreeToJSON = require('./compolib.filetreeToJSON')
var components = require('./compolib.components')
var CountComponents = require('./CountComponents')
// var components = require('./compolib.components')
var Components = require('./compolib.components').Components
var components = new Components()
var RenderPage = require('./RenderPage').RenderPage

var ncp = require('ncp').ncp

module.exports = function build (opts) {
Expand All @@ -14,12 +18,9 @@ module.exports = function build (opts) {
return console.error(err)
}
})

// TODO make sure that there are no components in dest folder before creating new
// could be the situation when .json files are missing/deleted, but dest component folder is still there
opts.components = [] // before build, reset components
opts.treeStructure['data'] = filetreeToJSON(opts, opts.location.src, true)
// renderNav(opts)
var countComponents = new CountComponents(opts)
countComponents.init()

// create simple list of components
// Saves data in opts.searchableItems
Expand All @@ -31,7 +32,20 @@ module.exports = function build (opts) {
// Saves data in opts.components
components.renderAll(opts)

components.renderDashboard(opts)
var landingPage = {
config: {
base: opts.location.styleguide,
path: opts.location.styleguide + 'dashboard.twig'
},
data: {
'root': opts.location.root,
'searchableItems': opts.searchableItems,
'filterItems': opts.filterItems
},
dest: path.join(opts.location.dest, 'index.html')
}
var renderPage = new RenderPage()
renderPage.render(landingPage.config, landingPage.data, landingPage.dest)

// console.log(util.inspect(opts.searchableItems, {showHidden: false, depth: null}))

Expand Down
Loading

0 comments on commit ddabfee

Please sign in to comment.