Skip to content

Commit

Permalink
refactoring: almost kill file-utils -> renamed as file
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 7, 2016
1 parent 4669239 commit 375b663
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/cli/cms/data/revision.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import fse from 'fs-extra'

import {
FileParser,
fileUtils,
config,
cmsData,
coreUtils,
Manager
} from '../../'

Expand All @@ -18,7 +18,7 @@ export function getFilesRevision(urls, fileName) {
var publishDate = new Date()
var json = null

if(fileUtils.isFile(tplUrl.publish.json)) {
if(coreUtils.file.exist(tplUrl.publish.json)) {
json = FileParser.getJson(tplUrl.publish.json)
if(typeof json !== 'undefined' && json !== null
&& typeof json[config.meta.name] !== 'undefined' && json[config.meta.name] !== null) {
Expand Down Expand Up @@ -48,7 +48,7 @@ export function getFilesRevision(urls, fileName) {
number = number + 1

var tplUrlObj = FileParser.getFileDataFromUrl(urlObj.path)
if(fileUtils.isFile(tplUrlObj.publish.json)) {
if(coreUtils.file.exist(tplUrlObj.publish.json)) {
var jsonObj = FileParser.getJson(tplUrlObj.publish.json)
urlObj[config.meta.name] = jsonObj[config.meta.name]
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/cms/editor/handlebars/abeImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'

import {
Plugins
,fileUtils
,coreUtils
,Hooks
} from '../../../'

Expand All @@ -22,7 +22,7 @@ export default function abeImport (file, config, ctx) {
catch(e){
pathToPartial = `${defaultPartials}/${file}.html`
}
if (fileUtils.isFile(pathToPartial)) {
if (coreUtils.file.exist(pathToPartial)) {
var html = fse.readFileSync(pathToPartial, 'utf8')
}else {
html = ''
Expand All @@ -31,7 +31,7 @@ export default function abeImport (file, config, ctx) {
var pluginsPartials = Plugins.instance.getPartials()
Array.prototype.forEach.call(pluginsPartials, (pluginPartials) => {
var checkFile = path.join(pluginPartials, `${file}.html`)
if (fileUtils.isFile(checkFile)) {
if (coreUtils.file.exist(checkFile)) {
html += fse.readFileSync(checkFile, 'utf8')
}
})
Expand Down
3 changes: 1 addition & 2 deletions src/cli/cms/operations/create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'path'
import {
fileUtils,
FileParser,
coreUtils,
cmsTemplate,
Expand All @@ -23,7 +22,7 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
if(templatePath !== null && filePath !== null) {
var tplUrl = FileParser.getFileDataFromUrl(filePath)

if(!fileUtils.isFile(tplUrl.json.path)) {
if(!coreUtils.file.exist(tplUrl.json.path)) {
var json = (forceJson) ? forceJson : {}
var tpl = templatePath
var text = cmsTemplate.template.getTemplate(tpl)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cms/operations/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export function save(url, tplPath, json = null, text = '', type = '', previousSa
complete: 0,
type: type
}

let meta = config.meta.name
json[meta] = extend(json[meta], ext)
var date = cmsData.fileAttr.get(pathIso.jsonPath).d
Expand All @@ -103,6 +102,7 @@ export function save(url, tplPath, json = null, text = '', type = '', previousSa
date = new Date(date)
}
}

cmsData.meta.add(tpl, json, type, {}, date, realType)

if(typeof text === 'undefined' || text === null || text === '') {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/cms/templates/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Promise} from 'es6-promise'
import path from 'path'
import {
config
,fileUtils
,coreUtils
,cmsData
,Hooks
} from '../../'
Expand Down Expand Up @@ -77,7 +77,7 @@ export function includePartials(text) {
var file = obj.file
var partial = ''
file = path.join(config.root, config.partials, file)
if(fileUtils.isFile(file)) {
if(coreUtils.file.exist(file)) {
partial = includePartials(fse.readFileSync(file, 'utf8'))
}
text = text.replace(cmsData.regex.escapeTextToRegex(abeImport, 'g'), partial)
Expand Down Expand Up @@ -140,7 +140,7 @@ export function getTemplate (file) {
file = file.replace(/\..+$/, '')
}
file = path.join(config.root, config.templates.url, file + '.' + config.files.templates.extension)
if(fileUtils.isFile(file)) {
if(coreUtils.file.exist(file)) {
text = fse.readFileSync(file, 'utf8')
text = includePartials(text)
text = translate(text)
Expand Down
10 changes: 5 additions & 5 deletions src/cli/core/utils/file-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import path from 'path'

import {
cmsData
,cmsOperations
,fileUtils
,cmsOperations
,coreUtils
,config
,Hooks
,Manager
Expand Down Expand Up @@ -426,18 +426,18 @@ export default class FileParser {

// TODO : change the signature of this method to removeFile(file)
static removeFile(file, json) {
if(fileUtils.isFile(file)) {
if(coreUtils.file.exist(file)) {
fse.removeSync(file)
}

if(fileUtils.isFile(json)) {
if(coreUtils.file.exist(json)) {
fse.removeSync(json)
}
}

static unpublishFile(filePath) {
var tplUrl = FileParser.getFileDataFromUrl(path.join(config.publish.url, filePath))
if(fileUtils.isFile(tplUrl.json.path)) {
if(coreUtils.file.exist(tplUrl.json.path)) {
var json = JSON.parse(JSON.stringify(FileParser.getJson(tplUrl.json.path)))
if(typeof json.abe_meta.publish !== 'undefined' && json.abe_meta.publish !== null) {
delete json.abe_meta.publish
Expand Down
38 changes: 0 additions & 38 deletions src/cli/core/utils/file-utils.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/cli/core/utils/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fse from 'fs-extra'

export function exist(pathFile) {
try{
fse.statSync(pathFile)
return true
}catch(e){
return false
}

return false
}

/**
* This method checks that the path leads to a file and return the content as UTF-8 content
* @param {string} path The path
* @return {string} The content of the UTF-8 file
*/
export function getContent(pathFile) {
var res = null
if(typeof pathFile !== 'undefined' && pathFile !== null && pathFile !== '') {
if (exist(pathFile)) {
res = fse.readFileSync(pathFile, 'utf8')
}
}
return res
}
2 changes: 2 additions & 0 deletions src/cli/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as sort from './sort'
import * as text from './text'
import locales from './locales'
import * as slug from './slug'
import * as file from './file'

export {
sort
,text
,locales
,slug
,file
}
4 changes: 2 additions & 2 deletions src/cli/extend/abe-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import hooksDefault from '../../hooks/hooks'

import {
config
,fileUtils
,coreUtils
,Plugins
} from '../'

Expand All @@ -19,7 +19,7 @@ class Hooks {
constructor(enforcer) {
if(enforcer != singletonEnforcer) throw 'Cannot construct Json singleton'

if(fileUtils.isFile(path.join(config.root, config.hooks.url, 'hooks.js'))){
if(coreUtils.file.exist(path.join(config.root, config.hooks.url, 'hooks.js'))){
var h = require(path.join(config.root, config.hooks.url, 'hooks.js'))
this.fn = extend(true, hooksDefault, h.default)
}
Expand Down
2 changes: 0 additions & 2 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import Manager from './core/manager/Manager'
import Page from './cms/Page'

import FileParser from './core/utils/file-parser'
import fileUtils from './core/utils/file-utils'

// import {getTemplate} from './cms/templates/abe-template'
import Create from './cms/Create'
Expand Down Expand Up @@ -66,7 +65,6 @@ export {
,Handlebars
,clc
,FileParser
,fileUtils
,printInput
,abeImport
,math
Expand Down
10 changes: 5 additions & 5 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import uuid from 'node-uuid'

import {
config,
coreUtils,
FileParser,
fileUtils,
printInput,
abeImport,
testObj,
Expand Down Expand Up @@ -71,7 +71,7 @@ var html = exphbs.create({
})

var opts = {}
if (fileUtils.isFile(path.join(config.root, 'cert.pem'))) {
if (coreUtils.file.exist(path.join(config.root, 'cert.pem'))) {
opts = {
key: fse.readFileSync( path.join(config.root, 'key.pem')),
cert: fse.readFileSync( path.join(config.root, 'cert.pem'))
Expand Down Expand Up @@ -151,13 +151,13 @@ app.use(express.static(publish))
// app.use(express.static(publish))

if(config.partials !== '') {
if (fileUtils.isFile(path.join(config.root, config.partials))) {
if (coreUtils.file.exist(path.join(config.root, config.partials))) {
app.use(express.static(path.join(config.root, config.partials)))
}
}

if(config.custom !== '') {
if (fileUtils.isFile(path.join(config.root, config.custom))) {
if (coreUtils.file.exist(path.join(config.root, config.custom))) {
app.use(express.static(path.join(config.root, config.custom)))
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ app.use(session({

Hooks.instance.trigger('afterExpress', app, express)

if (fileUtils.isFile(path.join(config.root, 'cert.pem'))) {
if (coreUtils.file.exist(path.join(config.root, 'cert.pem'))) {
var server = https.createServer(opts, app)
server.listen(port, function() {
console.log(clc.green(`\nserver running at https://localhost:${port}/`))
Expand Down
4 changes: 2 additions & 2 deletions src/server/controllers/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'
import {
cmsData,
cmsEditor,
fileUtils,
coreUtils,
abeEngine,
cmsTemplate,
FileParser,
Expand Down Expand Up @@ -224,7 +224,7 @@ export function editor(fileName, jsonPath, documentLink) {
var json

json = {}
if(fileUtils.isFile(jsonPath)) {
if(coreUtils.file.exist(jsonPath)) {
json = FileParser.getJson(jsonPath, 'utf8')
}

Expand Down
1 change: 0 additions & 1 deletion src/server/helpers/abe-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import path from 'path'

import {
FileParser,
fileUtils,
config
} from '../../cli'

Expand Down
3 changes: 1 addition & 2 deletions src/server/helpers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from 'path'
import {
cmsData,
FileParser,
fileUtils,
config,
Page,
cmsTemplate,
Expand Down Expand Up @@ -36,7 +35,7 @@ var page = function (req, res, next) {
linkPath = filePathTest.abe_meta.link
}

if(jsonPath === null || !fileUtils.isFile(jsonPath)) {
if(jsonPath === null || !coreUtils.file.exist(jsonPath)) {
res.status(404).send('Not found')
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/middlewares/website.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import pkg from '../../../package'
import {
Util,
FileParser,
fileUtils,
coreUtils,
config,
Page,
abeProcess,
Expand Down Expand Up @@ -74,8 +74,8 @@ var middleware = function(req, res, next) {
var html = ''

var page = path.join(config.root, config.publish.url, req.originalUrl)
if (fileUtils.isFile(page)) {
html = fileUtils.getFileContent(page)
if (coreUtils.file.exist(page)) {
html = coreUtils.file.getContent(page)

}else {
return next()
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/get-delete-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import fse from 'fs-extra'

import {
FileParser,
fileUtils,
coreUtils,
config
} from '../../cli'

var route = function(req, res, next){
var file = path.join(config.root, 'logs', `${req.params[0]}.log`)
var html = ''
if (fileUtils.isFile(file)) {
if (coreUtils.file.exist(file)) {
fse.removeSync(file)
res.redirect('/abe/delete-logs/')
}else {
Expand Down
Loading

0 comments on commit 375b663

Please sign in to comment.