This commit is contained in:
Philipp Kunz 2017-02-19 04:12:17 +01:00
commit 25c959d12d
15 changed files with 190 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules/
coverage/
public/
pages/
.nogit/

4
.npmignore Normal file
View File

@ -0,0 +1,4 @@
coverage/
node_modules/
test/
ts/

38
README.md Normal file
View File

@ -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)

4
dist/index.d.ts vendored Normal file
View File

@ -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;

31
dist/index.js vendored Normal file
View File

@ -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 +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FBIn0=

33
package.json Normal file
View File

@ -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"
}
}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{{> partials/header}}

View File

View File

@ -0,0 +1 @@
<head></head>

1
test/test.d.ts vendored Normal file
View File

@ -0,0 +1 @@
import 'typings-test';

16
test/test.js Normal file
View File

@ -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=

18
test/test.ts Normal file
View File

@ -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')
})
})

View File

@ -0,0 +1 @@
<head></head>

36
ts/index.ts Normal file
View File

@ -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'))
}
}