@spinajs/templates-pug 2.0.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `email`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const email = require('email');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/lib/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import * as pugTemplate from 'pug';
2
+ import { TemplateRenderer } from '@spinajs/templates';
3
+ export declare class PugRenderer extends TemplateRenderer {
4
+ protected Options: pugTemplate.Options;
5
+ protected Templates: Map<string, pugTemplate.compileTemplate>;
6
+ constructor();
7
+ get Type(): string;
8
+ get Extension(): string;
9
+ renderToFile(template: string, model: unknown, filePath: string, language?: string): Promise<void>;
10
+ render(templateName: string, model: unknown, language?: string): Promise<string>;
11
+ protected compile(templateName: string, path: string): Promise<void>;
12
+ }
package/lib/index.js ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ var __metadata = (this && this.__metadata) || function (k, v) {
32
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.PugRenderer = void 0;
39
+ const intl_1 = require("@spinajs/intl");
40
+ const exceptions_1 = require("@spinajs/exceptions");
41
+ const fs = __importStar(require("fs"));
42
+ const pugTemplate = __importStar(require("pug"));
43
+ const path = __importStar(require("path"));
44
+ const lodash_1 = __importDefault(require("lodash"));
45
+ const templates_1 = require("@spinajs/templates");
46
+ const configuration_1 = require("@spinajs/configuration");
47
+ const di_1 = require("@spinajs/di");
48
+ const path_1 = require("path");
49
+ let PugRenderer = class PugRenderer extends templates_1.TemplateRenderer {
50
+ constructor() {
51
+ super();
52
+ this.Templates = new Map();
53
+ }
54
+ get Type() {
55
+ return 'pug';
56
+ }
57
+ get Extension() {
58
+ return '.pug';
59
+ }
60
+ async renderToFile(template, model, filePath, language) {
61
+ const content = await this.render(template, model, language);
62
+ const dir = path.dirname(filePath);
63
+ if (!fs.existsSync(dir)) {
64
+ fs.mkdirSync(dir, { recursive: true });
65
+ }
66
+ fs.writeFileSync(filePath, content);
67
+ }
68
+ async render(templateName, model, language) {
69
+ this.Log.trace(`Rendering template ${templateName}`);
70
+ this.Log.timeStart(`PugTemplate${templateName}`);
71
+ if (!templateName) {
72
+ throw new exceptions_1.InvalidArgument('template parameter cannot be null or empty');
73
+ }
74
+ const fTemplate = this.Templates.get((0, path_1.normalize)(templateName));
75
+ if (!fTemplate) {
76
+ throw new exceptions_1.InvalidOperation(`Template ${templateName} is not found ( check if exists & compiled )`);
77
+ }
78
+ const lang = language ? language : (0, intl_1.guessLanguage)();
79
+ const tLang = lang !== null && lang !== void 0 ? lang : (0, intl_1.defaultLanguage)();
80
+ const content = fTemplate(lodash_1.default.merge(model !== null && model !== void 0 ? model : {}, {
81
+ __: (0, intl_1.__translate)(tLang),
82
+ __n: (0, intl_1.__translateNumber)(tLang),
83
+ __l: intl_1.__translateL,
84
+ __h: intl_1.__translateH,
85
+ }));
86
+ const time = this.Log.timeEnd(`PugTemplate${templateName}`);
87
+ this.Log.trace(`Rendering template ${templateName} ended, (${time} ms)`);
88
+ return Promise.resolve(content);
89
+ }
90
+ async compile(templateName, path) {
91
+ const tCompiled = pugTemplate.compileFile(path, this.Options);
92
+ const pNormalized = (0, path_1.normalize)(templateName);
93
+ if (!tCompiled) {
94
+ throw new exceptions_1.IOFail(`Cannot compile handlebars template ${pNormalized} from path ${path}`);
95
+ }
96
+ this.Templates.set(pNormalized, tCompiled);
97
+ }
98
+ };
99
+ __decorate([
100
+ (0, configuration_1.Config)('templates.pug'),
101
+ __metadata("design:type", Object)
102
+ ], PugRenderer.prototype, "Options", void 0);
103
+ PugRenderer = __decorate([
104
+ (0, di_1.Injectable)(templates_1.TemplateRenderer),
105
+ __metadata("design:paramtypes", [])
106
+ ], PugRenderer);
107
+ exports.PugRenderer = PugRenderer;
108
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAA2H;AAC3H,oDAA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EC;IADC,IAAA,sBAAM,EAAC,eAAe,CAAC;;4CACe;AAF5B,WAAW;IADvB,IAAA,eAAU,EAAC,4BAAgB,CAAC;;GAChB,WAAW,CAqEvB;AArEY,kCAAW"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@spinajs/templates-pug",
3
+ "version": "2.0.38",
4
+ "description": "templates support for pug files eg. html file generation",
5
+ "main": "lib/index.js",
6
+ "private": false,
7
+ "scripts": {
8
+ "build": "npm run clean && npm run compile",
9
+ "compile": "tsc -p tsconfig.build.json",
10
+ "clean": "",
11
+ "test": "ts-mocha -p tsconfig.json test/**/*.test.ts",
12
+ "coverage": "nyc npm run test",
13
+ "build-docs": "rimraf docs && typedoc --options typedoc.json src/",
14
+ "prepare": "npm run build",
15
+ "format": "prettier --write \"src/**/*.ts\"",
16
+ "lint": "eslint -c .eslintrc.js --ext .ts src --fix",
17
+ "prepublishOnly": "npm test && npm run lint",
18
+ "preversion": "npm run lint",
19
+ "version": "npm run format && git add -A src",
20
+ "postversion": "git push && git push --tags"
21
+ },
22
+ "files": [
23
+ "lib/**/*"
24
+ ],
25
+ "types": "lib",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/spinajs/main.git"
29
+ },
30
+ "keywords": [
31
+ "spinajs",
32
+ "http"
33
+ ],
34
+ "author": "SpinaJS <spinajs@coderush.pl> (https://github.com/spinajs/main)",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/spinajs/main/issues"
38
+ },
39
+ "homepage": "https://github.com/spinajs/main#readme",
40
+ "devDependencies": {
41
+ "@types/luxon": "^2.3.1",
42
+ "@types/mocha": "9.1.1",
43
+ "@types/node": "^16.11.11",
44
+ "@types/pug": "^2.0.4"
45
+ },
46
+ "dependencies": {
47
+ "@spinajs/configuration": "^2.0.38",
48
+ "@spinajs/di": "^2.0.38",
49
+ "@spinajs/exceptions": "^2.0.12",
50
+ "@spinajs/internal-logger": "^2.0.38",
51
+ "@spinajs/intl": "^2.0.38",
52
+ "@spinajs/log": "^2.0.38",
53
+ "@spinajs/templates": "^2.0.38",
54
+ "pug": "^3.0.0",
55
+ "tempfile": "3.0.0"
56
+ },
57
+ "gitHead": "5ea5440ee9db49595f531592ebdbc6d69f457082"
58
+ }