UNPKG

1.83 kBtext/coffeescriptView Raw
1_ = require 'lodash'
2fs = require 'fs-extra'
3path = require 'path'
4debug = require('debug')('html5-to-pdf:options')
5
6class 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 throw new Error 'Missing outputPath' unless options.outputPath?
22 defaults =
23 options:
24 landscape: false
25 pageSize: 'A4'
26 marginsType: 0
27 printBackground: false
28 renderDelay: 0
29 template: 'html5bp'
30
31 @options = _.defaultsDeep options, defaults
32
33 unless @options.templatePath?
34 @options.templatePath = @templatePath @options.template
35 @options.inputPath = @convertPath @options.inputPath unless @options.inputBody?
36 @options.outputPath = @convertPath @options.outputPath
37 @options.inputBody ?= fs.readFileSync(@options.inputPath)
38
39 @options.include = _.map @options.include, ({ type, filePath }) =>
40 throw new Error 'Invalid include item, must be type css or js' unless type in ['css', 'js']
41 return {
42 type,
43 filePath: @convertPath(filePath)
44 }
45
46 @options.include.push {
47 filePath: @templatePath('pdf.css')
48 type: 'css'
49 }
50 @options.include.push {
51 filePath: @templatePath('highlight.css')
52 type: 'css'
53 }
54
55 templatePath: (filePath) =>
56 return path.resolve path.join(__dirname, '../', 'templates', filePath)
57
58 convertPath: (filePath) =>
59 debug 'convertPath', filePath
60 return filePath if path.isAbsolute filePath
61 return path.resolve process.cwd(), filePath
62
63module.exports = Options