This commit is contained in:
2017-03-19 17:14:28 +01:00
parent 9297711793
commit 34cfe010f7
18 changed files with 191 additions and 121 deletions

View File

@ -1,54 +1,7 @@
import 'typings-global'
import * as handlebars from 'handlebars'
import * as smartfile from 'smartfile'
import * as smartq from 'smartq'
import * as path from 'path'
import * as plugins from './smarthbs.plugins'
export type TTemplateStringType = 'filePath' | 'code'
/**
* registers a directory of partials to make them available within handlebars compilation
*/
export let registerPartialDir = (dirPathArg: string) => {
let done = smartq.defer()
smartfile.fs.listFileTree(dirPathArg, '**/*.hbs').then(hbsFileArrayArg => {
for (let hbsFilePath of hbsFileArrayArg) {
let parsedPath = path.parse(hbsFilePath)
let hbsFileString = smartfile.fs.toStringSync(path.join(dirPathArg, hbsFilePath))
if (parsedPath.dir === '') {
parsedPath.name = '/' + parsedPath.name
}
let partialName = `partials${parsedPath.dir}${parsedPath.name}`
handlebars.registerPartial(partialName, hbsFileString)
done.resolve()
}
})
return done.promise
}
/**
* compiles a directory and outputs it
*/
export let compileDirectory = async (
originDirPathArg: string,
destinationDirPathArg: string,
dataFileNameArg: string
) => {
let hbsFilePathArray = smartfile.fs.listFilesSync(originDirPathArg, /.hbs/)
let data = smartfile.fs.toObjectSync(path.join(originDirPathArg, dataFileNameArg))
for (let hbsFilePath of hbsFilePathArray) {
let parsedPath = path.parse(hbsFilePath)
let hbsFileString = smartfile.fs.toStringSync(path.join(originDirPathArg, hbsFilePath))
let template = handlebars.compile(hbsFileString)
let output = template(data)
console.log('hi ' + output + ' hi')
smartfile.memory.toFsSync(output, path.join(destinationDirPathArg, parsedPath.name + '.html'))
}
}
/**
* get a template for a file on disk
*/
export let getTemplateForFile = async (filePathArg: string) => {
let filePathAbsolute = path.resolve(filePathArg)
return handlebars.compile(smartfile.fs.toStringSync(filePathAbsolute))
}
export * from './smarthbs.compile'
export * from './smarthbs.helpers'
export * from './smarthbs.partials'
export * from './smarthbs.template'

21
ts/smarthbs.compile.ts Normal file
View File

@ -0,0 +1,21 @@
import * as plugins from './smarthbs.plugins'
/**
* compiles a directory and outputs it
*/
export let compileDirectory = async (
originDirPathArg: string,
destinationDirPathArg: string,
dataFileNameArg: string
) => {
let hbsFilePathArray = plugins.smartfile.fs.listFilesSync(originDirPathArg, /.hbs/)
let data = plugins.smartfile.fs.toObjectSync(plugins.path.join(originDirPathArg, dataFileNameArg))
for (let hbsFilePath of hbsFilePathArray) {
let parsedPath = plugins.path.parse(hbsFilePath)
let hbsFileString = plugins.smartfile.fs.toStringSync(plugins.path.join(originDirPathArg, hbsFilePath))
let template = plugins.handlebars.compile(hbsFileString)
let output = template(data)
console.log('hi ' + output + ' hi')
plugins.smartfile.memory.toFsSync(output, plugins.path.join(destinationDirPathArg, parsedPath.name + '.html'))
}
}

4
ts/smarthbs.helpers.ts Normal file
View File

@ -0,0 +1,4 @@
import * as plugins from './smarthbs.plugins'
export let registerHelper = plugins.handlebars.registerHelper

21
ts/smarthbs.partials.ts Normal file
View File

@ -0,0 +1,21 @@
import * as plugins from './smarthbs.plugins'
/**
* registers a directory of partials to make them available within handlebars compilation
*/
export let registerPartialDir = (dirPathArg: string) => {
let done = plugins.smartq.defer()
plugins.smartfile.fs.listFileTree(dirPathArg, '**/*.hbs').then(hbsFileArrayArg => {
for (let hbsFilePath of hbsFileArrayArg) {
let parsedPath = plugins.path.parse(hbsFilePath)
let hbsFileString = plugins.smartfile.fs.toStringSync(plugins.path.join(dirPathArg, hbsFilePath))
if (parsedPath.dir === '') {
parsedPath.name = '/' + parsedPath.name
}
let partialName = `partials${parsedPath.dir}${parsedPath.name}`
plugins.handlebars.registerPartial(partialName, hbsFileString)
done.resolve()
}
})
return done.promise
}

12
ts/smarthbs.plugins.ts Normal file
View File

@ -0,0 +1,12 @@
import 'typings-global'
import * as handlebars from 'handlebars'
import * as smartfile from 'smartfile'
import * as smartq from 'smartq'
import * as path from 'path'
export {
handlebars,
smartfile,
smartq,
path
}

9
ts/smarthbs.template.ts Normal file
View File

@ -0,0 +1,9 @@
import * as plugins from './smarthbs.plugins'
/**
* get a template for a file on disk
*/
export let getTemplateForFile = async (filePathArg: string) => {
let filePathAbsolute = plugins.path.resolve(filePathArg)
return plugins.handlebars.compile(plugins.smartfile.fs.toStringSync(filePathAbsolute))
}