1 | _ = require 'lodash'
|
2 | fs = require 'fs'
|
3 | path = require 'path'
|
4 | debug = require('debug')('html5-to-pdf:options')
|
5 |
|
6 | class Options
|
7 | constructor: (options) ->
|
8 | debug 'pre-options', options
|
9 | @_set options
|
10 | debug 'post-options', @options
|
11 |
|
12 | set: (key, value) =>
|
13 | return _.set @options, key, value
|
14 |
|
15 | get: (key) =>
|
16 | return _.get @options, key
|
17 |
|
18 | _set: (options={}) =>
|
19 | unless options.inputPath? or options.inputBody?
|
20 | throw new Error 'Missing inputPath or inputBody'
|
21 | defaults =
|
22 | options:
|
23 | host: 'localhost'
|
24 | landscape: false
|
25 | pageSize: 'A4'
|
26 | marginsType: 0
|
27 | port: 0
|
28 | printBackground: false
|
29 | renderDelay: 100
|
30 | template: 'html5bp'
|
31 |
|
32 | @options = _.defaultsDeep options, defaults
|
33 |
|
34 | unless @options.templatePath?
|
35 | @options.templatePath = @templatePath @options.template
|
36 | @options.inputPath = @convertPath @options.inputPath unless @options.inputBody?
|
37 | @options.outputPath = @convertPath @options.outputPath if @options.outputPath?
|
38 | @options.inputBody ?= fs.readFileSync(@options.inputPath)
|
39 | @options.renderDelay = _.parseInt(@options.renderDelay) if @options.renderDelay
|
40 |
|
41 | @options.include = _.map @options.include, ({ type, filePath }) =>
|
42 | throw new Error 'Invalid include item, must be type css or js' unless type in ['css', 'js']
|
43 | return {
|
44 | type,
|
45 | filePath: @convertPath(filePath)
|
46 | }
|
47 |
|
48 | @options.include.push {
|
49 | filePath: @templatePath('pdf.css')
|
50 | type: 'css'
|
51 | }
|
52 | @options.include.push {
|
53 | filePath: @templatePath('highlight.css')
|
54 | type: 'css'
|
55 | }
|
56 |
|
57 | templatePath: (filePath) =>
|
58 | return path.resolve path.join(__dirname, '../', 'templates', filePath)
|
59 |
|
60 | convertPath: (filePath) =>
|
61 | debug 'convertPath', filePath
|
62 | return filePath if path.isAbsolute filePath
|
63 | return path.resolve process.cwd(), filePath
|
64 |
|
65 | module.exports = Options
|