forked from lookback/meteor-emails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.coffee
87 lines (69 loc) · 2.76 KB
/
utils.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Utils package for `lookback:emails`.
fs = Npm.require 'fs'
path = Npm.require 'path'
# This package assumes that assets (templates, SCSS, CSS ..) are
# stored in the `private` directory. Thanks to that, Meteor won't
# touch the HTML and CSS, which are non-JS files.
#
# However, since the file paths are screwed up when bundling and
# deploying Meteor apps, we need to set the BUNDLE_PATH env var
# to keep track of where the bundle lives.
#
# When deployed, set the BUNDLE_PATH env var to the location, perhaps:
# /var/www/app/bundle
# For Modulus, you need to use the APP_DIR variable, which you do NOT need to set
if process.env.BUNDLE_PATH
ROOT = path.join(process.env.BUNDLE_PATH, 'programs', 'server', 'assets', 'app')
else if process.env.APP_DIR
ROOT = path.join(process.env.APP_DIR, 'programs','server', 'assets', 'app')
else
# In development, using PWD is fine.
ROOT = path.join(process.env.PWD, 'private')
# Export the object on `share`, since CoffeeScript.
share.MailerUtils =
capitalizeFirstChar: (string) ->
string.charAt(0).toUpperCase() + string.slice(1)
# Set up a logger to use through `MailerUtils.Logger`. Verify
# that necessary methods exists on the injected `logger` and
# fallback if not.
setupLogger: (logger, opts) ->
defaults =
suppressInfo: false
opts = _.extend({}, defaults, opts)
res = ['info', 'warn', 'error'].map (method) ->
if not method in logger
console.warn "The injected logger does not support the #{method} method."
return false
return true
if _.compact(res).length is 0
console.warn 'Falling back to the native logger.'
@Logger = console
else
@Logger = logger
# Just do a noop for the `info` method
# if we're in silent mode.
if opts.suppressInfo is true
@Logger.info = -> #noop
joinUrl: (base, path) ->
# Remove any trailing slashes.
base = base.replace(/\/$/, '')
# Add front slash if not exist already.
unless /^\//.test(path)
path = '/' + path
return base + path
addDoctype: (html) ->
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' + html
readFile: (relativePathFromApp) ->
file = path.join(ROOT, relativePathFromApp)
try
return fs.readFileSync(file, encoding: 'utf8')
catch ex
throw new Meteor.Error 500, 'Could not find file: ' + file, ex.message
# Take a path to a SCSS file and compiles it to CSS with `node-sass`.
toCSS: (scss) ->
file = path.join(ROOT, scss)
try
return sass.renderSync(file: file, sourceMap: false).css.toString()
catch ex
console.error 'Sass failed to compile: ' + ex.message
console.error "In #{ex.file or scss} at line #{ex.line}, column #{ex.column}"