commit 25c959d12d81164f25b7b14e4d520c553f51dadd Author: Phil Kunz Date: Sun Feb 19 04:12:17 2017 +0100 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f812b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +coverage/ +public/ +pages/ +.nogit/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..71a5d26 --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +coverage/ +node_modules/ +test/ +ts/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..9aeb459 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# smarthbs +handlebars with better fs support + +## Availabililty +[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smarthbs) +[![git](https://push.rocks/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smarthbs) +[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smarthbs) +[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smarthbs/) + +## Status for master +[![build status](https://GitLab.com/pushrocks/smarthbs/badges/master/build.svg)](https://GitLab.com/pushrocks/smarthbs/commits/master) +[![coverage report](https://GitLab.com/pushrocks/smarthbs/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smarthbs/commits/master) +[![npm downloads per month](https://img.shields.io/npm/dm/smarthbs.svg)](https://www.npmjs.com/package/smarthbs) +[![Dependency Status](https://david-dm.org/pushrocks/smarthbs.svg)](https://david-dm.org/pushrocks/smarthbs) +[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smarthbs/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smarthbs/master/dependencies/npm) +[![bitHound Code](https://www.bithound.io/github/pushrocks/smarthbs/badges/code.svg)](https://www.bithound.io/github/pushrocks/smarthbs) +[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) +[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) +[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) + +## Usage +Use TypeScript for best in class instellisense. + +> Note: Why did we decide against a class based architecture? +Easy: handlebars.js is already pretty determined how things are handled internally, namely a global partial template registry +It doesn't make sense to then introduce a scoped partial template approach. + +```javascript +import * as smarthbs from 'smarthbs' + +// read all .hbs files in a directory and any child directories and use relative path as partial string identifier +smarthbs.registerPartialDir(testPartialDir) + +// read all .hbs files in a particular directory and level, output them to a destination and specify a .json file to read any referenced data +smarthbs.compileDirectory(testHbsDir, testResultDir, 'data.json') +``` + +[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks) diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..55823b5 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,4 @@ +import 'typings-global'; +export declare type TTemplateStringType = 'filePath' | 'code'; +export declare let registerPartialDir: (dirPathArg: string) => void; +export declare let compileDirectory: (originDirPathArg: string, destinationDirPathArg: string, dataFileNameArg: string) => void; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..cf9abaf --- /dev/null +++ b/dist/index.js @@ -0,0 +1,31 @@ +"use strict"; +require("typings-global"); +const handlebars = require("handlebars"); +const smartfile = require("smartfile"); +const path = require("path"); +exports.registerPartialDir = (dirPathArg) => { + 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); + } + }); +}; +exports.compileDirectory = (originDirPathArg, destinationDirPathArg, dataFileNameArg) => { + 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')); + } +}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLHlDQUF3QztBQUN4Qyx1Q0FBc0M7QUFDdEMsNkJBQTRCO0FBR2pCLFFBQUEsa0JBQWtCLEdBQUcsQ0FBQyxVQUFrQjtJQUMvQyxTQUFTLENBQUMsRUFBRSxDQUFDLFlBQVksQ0FBQyxVQUFVLEVBQUUsVUFBVSxDQUFDLENBQUMsSUFBSSxDQUFDLGVBQWU7UUFDbEUsR0FBRyxDQUFDLENBQUMsSUFBSSxXQUFXLElBQUksZUFBZSxDQUFDLENBQUMsQ0FBQztZQUN0QyxJQUFJLFVBQVUsR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDLFdBQVcsQ0FBQyxDQUFBO1lBQ3hDLElBQUksYUFBYSxHQUFHLFNBQVMsQ0FBQyxFQUFFLENBQUMsWUFBWSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsVUFBVSxFQUFFLFdBQVcsQ0FBQyxDQUFDLENBQUE7WUFDakYsRUFBRSxDQUFDLENBQUMsVUFBVSxDQUFDLEdBQUcsS0FBSyxFQUFFLENBQUMsQ0FBQyxDQUFDO2dCQUN4QixVQUFVLENBQUMsSUFBSSxHQUFHLEdBQUcsR0FBRyxVQUFVLENBQUMsSUFB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UFLLEdBQUcsTUFBTSxHQUFHLEtBQUssQ0FBQyxDQUFBO1FBQ25DLFNBQVMsQ0FBQyxNQUFNLENBQUMsUUFBUSxDQUFDLE1BQU0sRUFBRSxJQUFJLENBQUMsSUFBSSxDQUFDLHFCQUFxQixFQUFFLFVBQVUsQ0FBQyxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQTtJQUNsRyxDQUFDO0FBQ0wsQ0FBQyxDQUFBIn0= \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d909cc5 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "smarthbs", + "version": "1.0.2", + "description": "handlebars with better fs support", + "main": "dist/index.js", + "typings": "dist/index.d.ts", + "scripts": { + "test": "(npmts)" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@gitlab.com/pkunz/smarthbs.git" + }, + "keywords": [ + "handlebars" + ], + "author": "Lossless GmbH", + "license": "MIT", + "bugs": { + "url": "https://gitlab.com/pkunz/smarthbs/issues" + }, + "homepage": "https://gitlab.com/pkunz/smarthbs#README", + "dependencies": { + "@types/handlebars": "^4.0.31", + "handlebars": "^4.0.6", + "smartfile": "^4.1.4", + "smartq": "^1.0.4", + "typings-global": "^1.0.14" + }, + "devDependencies": { + "typings-test": "^1.0.3" + } +} diff --git a/test/hbs_testfiles/data.json b/test/hbs_testfiles/data.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/hbs_testfiles/data.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/hbs_testfiles/index.hbs b/test/hbs_testfiles/index.hbs new file mode 100644 index 0000000..7b83553 --- /dev/null +++ b/test/hbs_testfiles/index.hbs @@ -0,0 +1 @@ +{{> partials/header}} \ No newline at end of file diff --git a/test/hbs_testfiles/partials/footer.hbs b/test/hbs_testfiles/partials/footer.hbs new file mode 100644 index 0000000..e69de29 diff --git a/test/hbs_testfiles/partials/header.hbs b/test/hbs_testfiles/partials/header.hbs new file mode 100644 index 0000000..abbb30c --- /dev/null +++ b/test/hbs_testfiles/partials/header.hbs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/test.d.ts b/test/test.d.ts new file mode 100644 index 0000000..2fd432a --- /dev/null +++ b/test/test.d.ts @@ -0,0 +1 @@ +import 'typings-test'; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..52e21b0 --- /dev/null +++ b/test/test.js @@ -0,0 +1,16 @@ +"use strict"; +require("typings-test"); +const smarthbs = require("../dist/index"); +const path = require("path"); +let testHbsDir = path.join(__dirname, 'hbs_testfiles'); +let testPartialDir = path.join(testHbsDir, 'partials'); +let testResultDir = path.join(__dirname, 'testresult'); +describe('smarthbs', function () { + it('should create partials', function () { + smarthbs.registerPartialDir(testPartialDir); + }); + it('should compile a directory', function () { + smarthbs.compileDirectory(testHbsDir, testResultDir, 'data.json'); + }); +}); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQiwwQ0FBeUM7QUFDekMsNkJBQTRCO0FBRTVCLElBQUksVUFBVSxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsU0FBUyxFQUFFLGVBQWUsQ0FBQyxDQUFBO0FBQ3RELElBQUksY0FBYyxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsVUFBVSxFQUFFLFVBQVUsQ0FBQyxDQUFBO0FBQ3RELElBQUksYUFBYSxHQUFHLElBQUksQ0FBRSxJQUFJLENBQUMsU0FBUyxFQUFFLFlBQVksQ0FBQyxDQUFBO0FBQ3ZELFFBQVEsQ0FBQyxVQUFVLEVBQUU7SUFFakIsRUFBRSxDQUFDLHdCQUF3QixFQUFFO1FBQ3pCLFFBQVEsQ0FBQyxrQkFBa0IsQ0FBQyxjQUFjLENBQUMsQ0FBQTtJQUMvQyxDQUFDLENBQUMsQ0FBQTtJQUVGLEVBQUUsQ0FBQyw0QkFBNEIsRUFBRTtRQUM3QixRQUFRLENBQUMsZ0JBQWdCLENBQUMsVUFBVSxFQUFFLGFBQWEsRUFBRSxXQUFXLENBQUMsQ0FBQTtJQUNyRSxDQUFDLENBQUMsQ0FBQTtBQUVOLENBQUMsQ0FBQyxDQUFBIn0= \ No newline at end of file diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..262b99a --- /dev/null +++ b/test/test.ts @@ -0,0 +1,18 @@ +import 'typings-test' +import * as smarthbs from '../dist/index' +import * as path from 'path' + +let testHbsDir = path.join(__dirname, 'hbs_testfiles') +let testPartialDir = path.join(testHbsDir, 'partials') +let testResultDir = path .join(__dirname, 'testresult') +describe('smarthbs', function() { + + it('should create partials', function(){ + smarthbs.registerPartialDir(testPartialDir) + }) + + it('should compile a directory', function() { + smarthbs.compileDirectory(testHbsDir, testResultDir, 'data.json') + }) + +}) \ No newline at end of file diff --git a/test/testresult/index.html b/test/testresult/index.html new file mode 100644 index 0000000..abbb30c --- /dev/null +++ b/test/testresult/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts new file mode 100644 index 0000000..41814b8 --- /dev/null +++ b/ts/index.ts @@ -0,0 +1,36 @@ +import 'typings-global' +import * as handlebars from 'handlebars' +import * as smartfile from 'smartfile' +import * as path from 'path' +export type TTemplateStringType = 'filePath' | 'code' + +export let registerPartialDir = (dirPathArg: string) => { + 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) + } + }) +} + +export let compileDirectory = ( + 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')) + } +} \ No newline at end of file