Skip to content

Commit

Permalink
refactoring; refining getStructureAndTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Oct 29, 2016
1 parent e89f078 commit 24a602a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
22 changes: 14 additions & 8 deletions src/cli/cms/templates/template.js
Expand Up @@ -291,21 +291,27 @@ export function getStructureAndTemplates() {
var site = cmsData.revision.filePathInfos(config.root)
var result = {'structure': [], 'templates': []}

let structure = path.join(site.path, config.structure.url)
let templates = path.join(site.path, config.templates.url)
const pathStructure = path.join(site.path, config.structure.url)
const pathTemplates = path.join(site.path, config.templates.url)
try {
var directoryStructure = fse.lstatSync(structure)
let directoryStructure = fse.lstatSync(pathStructure)
if (directoryStructure.isDirectory()) {
result.structure = cmsData.file.getFolders(structure, false)
result.structure = cmsData.file.getFolders(pathStructure, false)
}
} catch (e) {
}
try {
var directoryTemplate = fse.lstatSync(templates)
let directoryTemplate = fse.lstatSync(pathTemplates)
if (directoryTemplate.isDirectory()) {
var resultTemplates = result.templates.concat(cmsData.file.getFiles(templates, true, 10, new RegExp(`.${config.files.templates.extension}`)))
result.templates = resultTemplates.filter(function (resultTemplate) {
return resultTemplate.path.indexOf(config.partials) < 0
let extension = '.' + config.files.templates.extension
let resultTemplates = []
let templatePaths = coreUtils.file.getFilesSync(pathTemplates, true, extension)
Array.prototype.forEach.call(templatePaths, (templatePath) => {
let additionalPath = path.dirname(templatePath).replace(pathTemplates,'')
if(additionalPath !== '') additionalPath = additionalPath.substring(1)
let name = path.join(additionalPath,path.basename(templatePath,extension))
let template = {'path':templatePath, 'cleanPath':templatePath, 'cleanNameNoExt':name}
result.templates.push(template)
})
}
} catch (e) {
Expand Down
39 changes: 22 additions & 17 deletions src/cli/core/manager/Manager.js
Expand Up @@ -60,24 +60,29 @@ class Manager {
structure: new events.EventEmitter(0)
}

this._watchTemplateFolder = watch.createMonitor(this._pathTemplate, (monitor) => {
monitor.on('created', (f, stat) => {
this.getKeysFromSelect()
this.updateStructureAndTemplates()
this.events.template.emit('update')
})
monitor.on('changed', (f, curr, prev) => {
this.getKeysFromSelect()
this.updateStructureAndTemplates()
this.events.template.emit('update')

})
monitor.on('removed', (f, stat) => {
this.getKeysFromSelect()
this.updateStructureAndTemplates()
this.events.template.emit('update')
try {
fse.accessSync(this._pathTemplate, fse.F_OK)
this._watchTemplateFolder = watch.createMonitor(this._pathTemplate, (monitor) => {
monitor.on('created', (f, stat) => {
this.getKeysFromSelect()
this.updateStructureAndTemplates()
this.events.template.emit('update')
})
monitor.on('changed', (f, curr, prev) => {
this.getKeysFromSelect()
this.updateStructureAndTemplates()
this.events.template.emit('update')

})
monitor.on('removed', (f, stat) => {
this.getKeysFromSelect()
this.updateStructureAndTemplates()
this.events.template.emit('update')
})
})
})
} catch (e) {
console.log('the directory ' + this._pathTemplate + ' does not exist')
}

try {
fse.accessSync(this._pathStructure, fse.F_OK)
Expand Down
6 changes: 3 additions & 3 deletions src/server/views/partials/create-form-template.html
Expand Up @@ -5,12 +5,12 @@
<option></option>
{{#each manager.list.templates}}
{{#if isHome}}
<option value="{{cleanPath}}" clean-value="{{website}}">{{cleanPath}}</option>
<option value="{{cleanPath}}" clean-value="{{website}}">{{cleanNameNoExt}}</option>
{{else}}
{{#ifCond @root.json.abe_meta.template cleanNameNoExt}}
<option value="{{cleanPath}}" clean-value="{{website}}" selected="selected">{{cleanPath}}</option>
<option value="{{cleanPath}}" clean-value="{{website}}" selected="selected">{{cleanNameNoExt}}</option>
{{else}}
<option value="{{cleanPath}}" clean-value="{{website}}">{{cleanPath}}</option>
<option value="{{cleanPath}}" clean-value="{{website}}">{{cleanNameNoExt}}</option>
{{/ifCond}}
{{/if}}
{{/each}}
Expand Down

0 comments on commit 24a602a

Please sign in to comment.