1 | var path = require('path'),
|
2 | fs = require('fs');
|
3 |
|
4 | var request = require('request'),
|
5 | mkdirp = require('mkdirp'),
|
6 | UglifyJS = require('uglify-js'),
|
7 | isUtf8 = require('is-utf8');
|
8 |
|
9 | var unixify = require('unixify');
|
10 |
|
11 | var logger = require('note-down');
|
12 | logger.removeOption('showLogLine');
|
13 | var chalk = logger.chalk;
|
14 |
|
15 | var utils = {
|
16 |
|
17 |
|
18 |
|
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 |
|
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 |
|
101 |
|
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 |
|
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 |
|
170 | module.exports = utils;
|