UNPKG

5.59 kBJavaScriptView Raw
1var path = require('path'),
2 fs = require('fs');
3
4var request = require('request'),
5 mkdirp = require('mkdirp'),
6 UglifyJS = require('uglify-js'),
7 isUtf8 = require('is-utf8');
8
9var unixify = require('unixify');
10
11var logger = require('note-down');
12logger.removeOption('showLogLine');
13var chalk = logger.chalk;
14
15var utils = {
16 // https://github.com/sindresorhus/strip-bom/blob/f01a9435b8e7d31bb2bd757e67436d0a1864db0e/index.js
17 // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
18 // conversion translates it to FEFF (UTF-16 BOM)
19 stripBom: function (string) {
20 if (string.charCodeAt(0) === 0xFEFF) {
21 return string.slice(1);
22 }
23 return string;
24 },
25
26 getEncoding: function(contents) {
27 return isUtf8(contents) ? 'utf8' : 'binary';
28 },
29 getColoredTypeString: function(encoding) {
30 switch (encoding) {
31 case 'remote': return chalk.cyan('remote');
32 case 'binary': return chalk.yellow('binary');
33 case 'utf8': return ' utf8 ';
34 default: return chalk.red(encoding);
35 }
36 },
37 isRemoteResource: function (resourcePath) {
38 if (
39 resourcePath.indexOf('https://') === 0 ||
40 resourcePath.indexOf('http://') === 0 ||
41 resourcePath.indexOf('ftp://') === 0
42 ) {
43 return true;
44 }
45 return false;
46 },
47 getRelativePath: function (wrt, fullPath) {
48 if (utils.isRemoteResource(fullPath)) {
49 return fullPath;
50 }
51 return unixify(path.relative(wrt, fullPath));
52 },
53
54 exitWithError: function (e, errMsg) {
55 if (errMsg) {
56 logger.log(chalk.magenta(errMsg));
57 }
58 if (e) {
59 logger.error(e);
60 }
61 process.exit(1);
62 },
63
64 // Returns true/false/<defaultValue>
65 booleanIntention: function (val, defaultValue) {
66 if (val === undefined) {
67 return defaultValue;
68 } else {
69 return !!val;
70 }
71 },
72
73 ensureDirectoryExistence: function(dirPath) {
74 var dirname = (
75 dirPath[dirPath.length-1] === '/' ?
76 unixify(path.normalize(dirPath)) :
77 unixify(path.dirname(dirPath))
78 );
79 if (!fs.existsSync(dirPath)) {
80 try {
81 mkdirp.sync(dirname);
82 } catch (e) {
83 logger.error('\n' + chalk.bold.underline('Error:'));
84 logger.error('Unable to create directory ' + dirname);
85
86 logger.error('\n' + chalk.bold.underline('Error details:'));
87 logger.error(e);
88
89 process.exit(1);
90 }
91 }
92 },
93
94 additionalProcessing: function (additionalOptions, code) {
95 var needsUglify = additionalOptions.needsUglify;
96 var removeSourceMappingURL = additionalOptions.removeSourceMappingURL;
97 var data = {};
98
99 if (removeSourceMappingURL) {
100 // LAZY: This approach is simple enough and seems to work well for the common known cases.
101 // As and when any issues are encountered, this code can be improved.
102 code = code.split('//# sourceMappingURL=')[0];
103 data.consoleCommand = data.consoleCommand || {};
104 data.consoleCommand.sourceMappingUrl = 'Note: Removed "sourceMappingURL"';
105 }
106
107 if (needsUglify) {
108 var result = UglifyJS.minify(
109 code,
110 // Equivalent to: uglifyjs <source> --compress sequences=false --beautify beautify=false,semicolons=false,comments=some --output <destination>
111 {
112 compress: {
113 sequences: false
114 },
115 mangle: false,
116 output: {
117 semicolons: false,
118 comments: 'some'
119 }
120 }
121 );
122 var consoleCommand = 'uglifyjs <source> --compress sequences=false --beautify beautify=false,semicolons=false,comments=some --output <destination>';
123
124 data.code = result.code || code;
125 data.consoleCommand = data.consoleCommand || {};
126 data.consoleCommand.uglifyJs = consoleCommand;
127 } else {
128 data.code = code;
129 }
130
131 return data;
132 },
133
134 readContents: function (sourceFullPath, cb) {
135 if (utils.isRemoteResource(sourceFullPath)) {
136 request(
137 {
138 uri: sourceFullPath,
139 encoding: null,
140 gzip: true,
141 timeout: 30000
142 },
143 function (err, response, body) {
144 if (err) {
145 cb(err);
146 } else {
147 if (response.statusCode === 200) {
148 cb(null, body, 'remote');
149 } else {
150 cb('Unexpected statusCode (' + response.statusCode + ') for response of: ' + sourceFullPath);
151 }
152 }
153 }
154 );
155 } else {
156 try {
157 var rawContents = fs.readFileSync(sourceFullPath);
158 var encoding = utils.getEncoding(rawContents);
159 var contents = encoding === 'binary'
160 ? rawContents
161 : rawContents.toString('utf8');
162 cb(null, contents, encoding);
163 } catch (e) {
164 cb(e);
165 }
166 }
167 }
168};
169
170module.exports = utils;