This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.js
167 lines (135 loc) · 4.74 KB
/
utils.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// # Utils package for `lookback:emails`
const fs = Npm.require('fs');
const path = Npm.require('path');
const htmlToText = Npm.require('html-to-text');
const TAG = 'mailer-utils';
// 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
const isDevEnv = () => process.env.NODE_ENV === 'development';
const developmentPrivateDir = () => {
if (!isDevEnv())
return;
// In development, using pwd is fine. Remove the .meteor/foo/bar stuff though.
const meteorRoot = process.cwd().replace(/(\.meteor.*)/g, '');
return path.join(meteorRoot, 'private');
};
const productionPrivateDir = () => {
if (isDevEnv())
return;
const meteorRoot = fs.realpathSync(process.cwd() + '/../');
return fs.realpathSync(meteorRoot + '/../');
};
const privateDir = process.env.BUNDLE_PATH || process.env.APP_DIR || productionPrivateDir();
let ROOT = privateDir && path.join(privateDir, 'programs', 'server', 'assets', 'app');
ROOT = ROOT || developmentPrivateDir();
Utils = {
// Takes an HTML string and outputs a text version of it. Catches and logs errors.
toText(html, opts = {}) {
try {
return htmlToText.fromString(html, opts);
} catch (ex) {
return Utils.Logger.error(`Could not make plain-text version from html: ${ex.message}`);
}
},
capitalizeFirstChar(string) {
return 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) {
const defaults = {
suppressInfo: false
};
opts = _.extend({}, defaults, opts);
const res = ['info', 'warn', 'error'].map(function(method) {
if (!_.has(logger, method)) {
console.warn(`The injected logger must support the ${method} method.`);
return false;
}
return true;
});
if (_.compact(res).length === 0) {
console.warn('Falling back to the native logger.');
this.Logger = console;
} else {
this.Logger = logger;
}
// Just do a noop for the `info` method
// if we're in silent mode.
if (opts.suppressInfo === true) {
return this.Logger.info = function() {};
}
},
joinUrl(base, path) {
// Remove any trailing slashes.
const root = base.replace(/\/$/, '');
// Add front slash if not exist already.
if (!/^\//.test(path)) {
path = '/' + path;
}
return root + path;
},
addStylesheets(template, html, juiceOpts = {}) {
check(template, Match.ObjectIncluding({
name: String,
css: Match.Optional(String),
scss: Match.Optional(String)
}));
try {
let content = html;
if (template.css) {
const css = Utils.readFile(template.css);
content = juice.inlineContent(content, css, juiceOpts);
}
if (template.scss) {
const scss = Utils.toCSS(template.scss);
content = juice.inlineContent(content, scss, juiceOpts);
}
return content;
} catch (ex) {
Utils.Logger.error(`Could not add CSS to ${template.name}: ${ex.message}`, TAG);
return html;
}
},
addDoctype(html) {
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' + html;
},
readFile: function(relativePathFromApp) {
const 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: function(scss) {
if (!Package['chrisbutler:node-sass']) {
Utils.Logger.warn('Sass support is opt-in since lookback:[email protected]. Please add chrisbutler:node-sass from Atmosphere and try again.', TAG);
return Utils.readFile(scss);
}
const file = path.join(ROOT, scss);
const sass = Package['chrisbutler:node-sass'].sass;
try {
return sass.renderSync({
file: file,
sourceMap: false
}).css.toString();
} catch (ex) {
console.error(`Sass failed to compile: ${ex.message}`);
return console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`);
}
}
};