From 94fae3cc5fa06325d81f6e038fbc209124a54dc8 Mon Sep 17 00:00:00 2001 From: Artuom Kukharenko Date: Thu, 18 Jan 2018 14:40:11 +0300 Subject: [PATCH] [+] Add partials --- client/index.js | 30 +++++---------- client/lib/api.js | 33 +++++++++------- client/lib/webpackTask.js | 5 +-- client/package.json | 2 +- client/samples/bob/index.js | 6 ++- client/samples/harvestReport/index.js | 52 ++++++++++++++++++------- client/samples/invoiceReceipt/index.js | 53 ++++++++++++++------------ client/test/test.js | 51 +++++++++++++------------ client/watch.js | 6 ++- 9 files changed, 134 insertions(+), 104 deletions(-) diff --git a/client/index.js b/client/index.js index dca108a..57f2dce 100755 --- a/client/index.js +++ b/client/index.js @@ -20,8 +20,7 @@ module.exports = class PdfService { const { pdfOptions = {}, headers = {}, - templateParams = {}, - templateHelpers = {}, + templateSystem = {}, } = params; try { @@ -37,8 +36,7 @@ module.exports = class PdfService { pdfOptions, headers, type: 'pdf', - templateParams, - templateHelpers, + templateSystem, serverUrl: this.serverUrl, mode: this.mode, }); @@ -55,8 +53,7 @@ module.exports = class PdfService { const { pdfOptions = {}, headers = {}, - templateParams = {}, - templateHelpers = {}, + templateSystem = {}, } = params; try { @@ -64,8 +61,7 @@ module.exports = class PdfService { pdfOptions, headers, type: 'pdf', - templateParams, - templateHelpers, + templateSystem, content, serverUrl: this.serverUrl, }); @@ -81,8 +77,7 @@ module.exports = class PdfService { const { imgOptions = {}, headers = {}, - templateParams = {}, - templateHelpers = {}, + templateSystem = {}, } = params; try { @@ -97,8 +92,7 @@ module.exports = class PdfService { outPaths: { htmlPath, staticFilePath: `${staticFilePath}.${imgOptions.type || 'png'}` }, imgOptions, headers, - templateParams, - templateHelpers, + templateSystem, type: imgOptions.type || 'png', serverUrl: this.serverUrl, mode: this.mode, @@ -116,16 +110,14 @@ module.exports = class PdfService { const { imgOptions = {}, headers = {}, - templateParams = {}, - templateHelpers = {}, + templateSystem = {}, } = params; try { return getStaticFileByContent({ imgOptions, headers, - templateParams, - templateHelpers, + templateSystem, content, type: imgOptions.type || 'png', serverUrl: this.serverUrl, @@ -140,8 +132,7 @@ module.exports = class PdfService { async watch(pagePath, params) { const { - templateParams = {}, - templateHelpers = {}, + templateSystem = {}, pdfOptions = {}, } = params; @@ -150,8 +141,7 @@ module.exports = class PdfService { const watchParams = { paths, pdfOptions, - templateParams, - templateHelpers, + templateSystem, serverUrl: this.serverUrl, mode: this.mode, buildPdf: getStaticFileFromHtml, diff --git a/client/lib/api.js b/client/lib/api.js index 8d1fc41..883800b 100644 --- a/client/lib/api.js +++ b/client/lib/api.js @@ -5,20 +5,30 @@ const PassThrough = require('stream').PassThrough; const logger = require('./logger'); const Handlebars = require('handlebars'); -const compileHtml = (html, templateParams, templateHelpers) => { - Object.keys(templateHelpers).forEach((helperName) => { - Handlebars.registerHelper(helperName, templateHelpers[helperName](Handlebars)); +const compileHtml = (html, templateSystem) => { + const { + params = {}, + helpers = {}, + partials = {}, + } = templateSystem; + + Object.keys(helpers).forEach((helperName) => { + Handlebars.registerHelper(helperName, helpers[helperName](Handlebars)); + }); + + Object.keys(partials).forEach((partialName) => { + Handlebars.registerPartial(partialName, partials[partialName]); }); const compiledHtml = Handlebars.compile(html); - return compiledHtml(templateParams); + return compiledHtml(params); }; -const readFile = async (filePath, templateParams, templateHelpers) => { +const readFile = async (filePath, templateSystem) => { try { const html = (await fs.readFile(filePath)).toString('binary'); - return compileHtml(html, templateParams, templateHelpers); + return compileHtml(html, templateSystem); } catch (err) { logger.error('Something irreparable happened !!!\n', 'When read file'); throw err; @@ -66,15 +76,14 @@ const getStaticFileFromHtml = async ({ outPaths, pdfOptions, headers, - templateHelpers, - templateParams, + templateSystem, serverUrl, type, mode = 'development', watch, }) => { const { htmlPath, staticFilePath } = outPaths; - const html = await readFile(htmlPath, templateParams, templateHelpers); + const html = await readFile(htmlPath, templateSystem); if (watch) { await fs.writeFile(htmlPath, html); @@ -99,13 +108,11 @@ const getStaticFileByContent = async ({ content, pdfOptions, headers, - templateHelpers, - templateParams, + templateSystem, serverUrl, type, }) => { - const html = compileHtml(content, templateParams, templateHelpers); - console.log(html); + const html = compileHtml(content, templateSystem); const pdfStream = type === 'pdf' ? getPdf(html, pdfOptions, headers, serverUrl) : getImg(html, pdfOptions, headers, serverUrl); diff --git a/client/lib/webpackTask.js b/client/lib/webpackTask.js index 74488cd..7473d82 100644 --- a/client/lib/webpackTask.js +++ b/client/lib/webpackTask.js @@ -76,7 +76,7 @@ const build = ({ paths }) => { }); }; -const watch = ({ paths, templateParams, templateHelpers, serverUrl, buildPdf, mode }) => { +const watch = ({ paths, templateSystem, serverUrl, buildPdf, mode }) => { const config = getConfig({ paths }); const compiler = webpack(config); return new Promise((resolve, reject) => { @@ -88,9 +88,8 @@ const watch = ({ paths, templateParams, templateHelpers, serverUrl, buildPdf, mo await buildPdf({ outPaths: { htmlPath, pdfPath }, - templateParams, + templateSystem, type: 'pdf', - templateHelpers, serverUrl, mode, watch: true, diff --git a/client/package.json b/client/package.json index 03a7965..e6a6fa1 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "@paralect/pdf-service-client", - "version": "0.2.5", + "version": "0.3.0", "description": "Pdf service client for https://hub.docker.com/r/paralect/pdf-service/", "main": "index.js", "bin": { diff --git a/client/samples/bob/index.js b/client/samples/bob/index.js index 26fd987..e455140 100644 --- a/client/samples/bob/index.js +++ b/client/samples/bob/index.js @@ -9,7 +9,9 @@ pdfService.generatePdf(`${__dirname}/src/index.html`, { pdfOptions: { format: 'Letter', }, - templateParams: { - tagline: 'Future is near!!!', + templateSystem: { + params: { + tagline: 'Future is near!!!', + }, }, }); diff --git a/client/samples/harvestReport/index.js b/client/samples/harvestReport/index.js index c158d0e..6ea5d54 100644 --- a/client/samples/harvestReport/index.js +++ b/client/samples/harvestReport/index.js @@ -9,22 +9,46 @@ pdfService.generatePdf(`${__dirname}/src/index.html`, { pdfOptions: { format: 'Letter', }, - templateParams: { - tasks: [{ billableHours: 80, noneBillableHours: 0, totalAmount: 0, taskName: 'Programming', uninvoicedAmount: 0, items: [{ billableHours: 80, noneBillableHours: 0, totalAmount: 0, fullName: 'Andrew Orsich', avatar: 'https://cache.harvestapp.com/assets/profile_images/abraj_albait_towers.png?1498516481', totalHours: 80, percentFromMax: '100', billablePercent: '100' }], totalHours: 80, percentFromMax: '100', billablePercent: '100' }], projects: [{ billableHours: 80, noneBillableHours: 0, totalAmount: 0, projectName: 'Paralect Care', totalHours: 80, percentFromMax: '100', billablePercent: '100' }], detailsEntries: [{ totalHours: 80, fullName: 'Andrew Orsich', notes: [{ taskId: 8372101, spentOn: 'Monday, August 7th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Tuesday, August 8th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Wednesday, August 9th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Thursday, August 10th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Friday, August 11th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Monday, August 14th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Tuesday, August 15th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Wednesday, August 16th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Thursday, August 17th', projectId: 14838809, notes: 'Programming biiiiiig stuff', hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Friday, August 18th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }] }], totalBillableHours: 80, totalUnbillableHours: 0, totalHours: 80, totalAmount: 0, totalUninvoicedAmount: 0, client: { id: 5928851, name: 'Paralect', active: true, currency: 'United States Dollar - USD', updated_at: '2017-08-25T15:48:41Z', created_at: '2017-08-25T15:43:35Z', statement_key: '0aef226a68331872cf3368f4f5e4aa5e', default_invoice_kind: 'project', default_invoice_timeframe: '20170801,20170831', address: null, delete_at: null, currency_symbol: '$', details: null, last_invoice_kind: 'project' }, moreThanOneProject: false, from: 'Aug 01', to: 'Aug 31, 2017', reportName: 'Paralect Team Monthly Time Report', rangeParagraph: 'August', rangeEmailString: 'I have prepared a one-month report for', emailTitle: 'Monthly Report', fileName: 'Paralect Team Monthly Time Report ((Aug 01 - Aug 31, 2017)).pdf', templateName: 'monthly_report', - }, - templateHelpers: { - currency: Handlebars => (amount) => { - const htmlData = parseFloat(Handlebars.escapeExpression(amount)) - .toFixed(2) - .replace(/./g, (c, i, a) => (i && c !== '.' && ((a.length - i) % 3 === 0) ? `,${c}` : c)) - .concat(' USD'); - - return new Handlebars.SafeString(htmlData); + templateSystem: { + params: { + tasks: [{ + billableHours: 80, + noneBillableHours: 0, + totalAmount: 0, + taskName: 'Programming', + uninvoicedAmount: 0, + items: [{ + billableHours: 80, + noneBillableHours: 0, + totalAmount: 0, + fullName: 'Andrew Orsich', + avatar: 'https://cache.harvestapp.com/assets/profile_images/abraj_albait_towers.png?1498516481', + totalHours: 80, + percentFromMax: '100', + billablePercent: '100', + }], + totalHours: 80, + percentFromMax: '100', + billablePercent: '100', + }], }, - hours: Handlebars => (hours) => { - const htmlData = parseFloat(Handlebars.escapeExpression(hours)).toFixed(2); + helpers: { + currency: Handlebars => (amount) => { + const htmlData = parseFloat(Handlebars.escapeExpression(amount)) + .toFixed(2) + .replace(/./g, (c, i, a) => (i && c !== '.' && ((a.length - i) % 3 === 0) ? `,${c}` : c)) + .concat(' USD'); - return new Handlebars.SafeString(htmlData); + return new Handlebars.SafeString(htmlData); + }, + hours: Handlebars => (hours) => { + const htmlData = parseFloat(Handlebars.escapeExpression(hours)).toFixed(2); + + return new Handlebars.SafeString(htmlData); + }, + }, + partials: { + hello: '

Hello!

', }, }, }); diff --git a/client/samples/invoiceReceipt/index.js b/client/samples/invoiceReceipt/index.js index 2654c1d..bc4617f 100644 --- a/client/samples/invoiceReceipt/index.js +++ b/client/samples/invoiceReceipt/index.js @@ -2,7 +2,7 @@ const PdfService = require('./../../index'); const pdfService = new PdfService({ serverUrl: 'http://localhost:4444', - mode: 'production', + mode: 'development', }); pdfService.generatePdf(`${__dirname}/view/index.html`, { @@ -15,31 +15,34 @@ pdfService.generatePdf(`${__dirname}/view/index.html`, { bottom: '0mm', }, }, - templateParams: { - invoice: { - paidOn: '2017-08-22', - totalAmount: 100000, - creditCard: { - type: 'VISA', - last4: '4357', - }, - activeUsers: 'Artem', - transaction: { - amount: 12000, - _id: 123, - }, - appName: 'Pdf Service', - from: '2017-07-22', - to: '2017-08-22', - items: [ - { - description: 'Spinners', - amount: 13000, + templateSystem: { + params: { + invoice: { + paidOn: '2017-08-22', + totalAmount: 100000, + creditCard: { + type: 'VISA', + last4: '4357', }, - { - description: 'Vape', + activeUsers: 'Artem', + transaction: { amount: 12000, + _id: 123, }, - ], - } }, + appName: 'Pdf Service', + from: '2017-07-22', + to: '2017-08-22', + items: [ + { + description: 'Spinners', + amount: 13000, + }, + { + description: 'Vape', + amount: 12000, + }, + ], + }, + }, + }, }); diff --git a/client/test/test.js b/client/test/test.js index ff3eed6..0f9299b 100644 --- a/client/test/test.js +++ b/client/test/test.js @@ -16,33 +16,36 @@ const pdfParams = { bottom: '0mm', }, }, - templateParams: { - invoice: { - paidOn: '2017-08-22', - totalAmount: 100000, - creditCard: { - type: 'VISA', - last4: '4357', - }, - activeUsers: 'Artem', - transaction: { - amount: 12000, - _id: 123, - }, - appName: 'Pdf Service', - from: '2017-07-22', - to: '2017-08-22', - items: [ - { - description: 'Spinners', - amount: 13000, + templateSystem: { + params: { + invoice: { + paidOn: '2017-08-22', + totalAmount: 100000, + creditCard: { + type: 'VISA', + last4: '4357', }, - { - description: 'Vape', + activeUsers: 'Artem', + transaction: { amount: 12000, + _id: 123, }, - ], - } }, + appName: 'Pdf Service', + from: '2017-07-22', + to: '2017-08-22', + items: [ + { + description: 'Spinners', + amount: 13000, + }, + { + description: 'Vape', + amount: 12000, + }, + ], + }, + }, + }, }; const testDirPath = `${__dirname}/test-waste`; diff --git a/client/watch.js b/client/watch.js index 6a1615f..be577bd 100755 --- a/client/watch.js +++ b/client/watch.js @@ -12,12 +12,14 @@ const optionDefinitions = [ const options = commandLineArgs(optionDefinitions); +options.templateSystem = {}; + if (options.templateParams) { - options.templateParams = require(path.resolve(options.templateParams)); // eslint-disable-line + options.templateSystem.params = require(path.resolve(options.templateParams)); // eslint-disable-line } if (options.templateHelpers) { - options.templateHelpers = require(path.resolve(options.templateHelpers)); // eslint-disable-line + options.templateSystem.helpers = require(path.resolve(options.templateHelpers)); // eslint-disable-line } const pdfService = new PdfService(options);