From 24a602a68791da796435e8720f6157cabc72a32a Mon Sep 17 00:00:00 2001 From: gregorybesson Date: Sat, 29 Oct 2016 23:39:24 +0200 Subject: [PATCH] refactoring; refining getStructureAndTemplates --- src/cli/cms/templates/template.js | 22 +++++++---- src/cli/core/manager/Manager.js | 39 +++++++++++-------- .../views/partials/create-form-template.html | 6 +-- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/cli/cms/templates/template.js b/src/cli/cms/templates/template.js index 9f8a80ec..9821909d 100755 --- a/src/cli/cms/templates/template.js +++ b/src/cli/cms/templates/template.js @@ -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) { diff --git a/src/cli/core/manager/Manager.js b/src/cli/core/manager/Manager.js index 2e8eeab7..ffafbeee 100644 --- a/src/cli/core/manager/Manager.js +++ b/src/cli/core/manager/Manager.js @@ -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) diff --git a/src/server/views/partials/create-form-template.html b/src/server/views/partials/create-form-template.html index e2ed5ed3..f792f5f2 100644 --- a/src/server/views/partials/create-form-template.html +++ b/src/server/views/partials/create-form-template.html @@ -5,12 +5,12 @@ {{#each manager.list.templates}} {{#if isHome}} - + {{else}} {{#ifCond @root.json.abe_meta.template cleanNameNoExt}} - + {{else}} - + {{/ifCond}} {{/if}} {{/each}}