-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
211 lines (185 loc) · 5.62 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
var assign = require( 'object.assign' );
var escodegen = require( 'escodegen' );
var esprima = require( 'esprima' );
var fs = require( 'fs' );
var chalk = require( 'chalk' );
var path = require( 'path' );
var strip = require( './lib/strip' );
/**
* @typedef {Object} MinErrStripOptions
* @property {String} docsUrl
* @property {String} configDest - The file that should be created to contain the stripped error messages
* @property {RegExp} [urlReplacement]
* @property {String} [productionTemplate]
* @property {String} [parsedFileFormat] - The format object passed to {@link escodegen.generate} when
* generating the final version of processed module templates
* @property {Function} [logger] - This should have log and error functions. If not provided
* the one from the gulp utility package will be used.
* @property {Object} [configDetails] - Additional configuration properties to be added to the output config
*/
/**
*
* @param {MinErrStripOptions} options
* @constructor
*/
var MinErrStrip = function MinErrStrip( options ) {
if ( !options ) {
throw new Error( 'An options object must be passed into the MinErrStrip constructor' );
}
var defaultLogger = {
log: console.log,
error: function (msg) {
console.error( chalk.white.bgRed.bold( ( 'ERROR: ' + msg ) ) );
}
};
/**
* Merge the supplied options with the defaults.
*
* @type {MinErrStripOptions}
*/
this.options = assign(
{
logger: defaultLogger,
productionTemplate: path.resolve( __dirname + '/lib/minErr.tpl.js' ),
parsedFileFormat: {
indent: {
style: ' ',
base: 0
}
},
configDetails: {
generated: new Date().toString()
},
urlReplacement: /MINERR_URL/
},
options
);
if ( !this.options.docsUrl ) {
throw new Error( 'options.docsUrl must be set in the MinErrStrip options' );
}
if ( !this.options.configDest ) {
throw new Error( 'options.configDest must be set in the MinErrStrip options' );
} else if ( false === fs.existsSync( path.dirname( this.options.configDest ) ) ) {
throw new Error( 'The directory for the file defined in options.configDest must exist' );
}
/**
* Storage for the strip tool so that we are only building it once
*
* @type {strip}
*/
this.strip = null;
/**
* Storage for the error templates that are stripped from modules. These should be dumped
* to a json config array after the last module is parsed.
*
* @type {Object}
*/
this.strippedErrorStorage = {};
};
/**
* Return the string content of the template specified in {@link MinErrStripOptions.productionTemplate} with
* {@link MinErrStripOptions.urlReplacement} replaced with {@link MinErrStripOptions.docsUrl}
*
* @private
*
* @returns {String}
*/
MinErrStrip.prototype.getProductionSource = function getProductionSource() {
return fs
.readFileSync( this.options.productionTemplate, 'utf8' )
.replace( this.options.urlReplacement, this.options.docsUrl );
};
/**
* @private
*
* Instantiate the stripping utility if a previously cached one is not present then return
* the {@link strip} instance.
*
* @returns {strip}
*/
MinErrStrip.prototype.getStripUtil = function getStripUtil() {
if ( null === this.strip ) {
this.strip = strip( {
logger: this.options.logger,
minErrAst: this.stringToAST( this.getProductionSource() ).body[0]
} );
}
return this.strip;
};
/**
* @private
*
* @param {String} contents - The string file contents
* @param {Object} [options] - Optional options to pass to the parser
*
* @returns {*} - The template in AST form
*/
MinErrStrip.prototype.stringToAST = function stringToAST( contents, options ) {
options = options || {};
return esprima.parse( contents, options );
};
/**
* Strips the error templates from the file AST and stores them for later
* writing to the error config file.
*
* @private
*
* @param {Syntax.Program} fileAST
*
* @returns {Syntax.Program} - The parsed AST
*/
MinErrStrip.prototype.stripErrorsFromAST = function stripErrors( fileAST ) {
var strip = this.getStripUtil();
return strip( fileAST, this.strippedErrorStorage );
};
/**
* @private
*
* @param {Syntax.Program} fileAST
*
* @returns {String} - The AST converted back to a string
*/
MinErrStrip.prototype.astToString = function astToString( fileAST ) {
return escodegen.generate( fileAST, {format: this.options.parsedFileFormat} );
};
/**
* @param {String} fileContents
*
* @returns {String} - The file contents with the error strings replaced and the error function patched
*/
MinErrStrip.prototype.processModule = function processModule( fileContents ) {
var parsedAST = this.stripErrorsFromAST(
this.stringToAST( fileContents, {loc: true} )
);
return this.astToString( parsedAST );
};
/**
* Construct the options object then write it out to the configured configuration file
*/
MinErrStrip.prototype.flushErrorConfig = function getStrippedErrors() {
var errorOptions = assign(
this.options.configDetails,
{errors: this.strippedErrorStorage}
);
this.writeConfigFile(
this.options.configDest,
errorOptions
);
// reset storage
this.strippedErrorStorage = {};
};
/**
* @private
*
* @param {String} writeConfigTo - The path that the config should be written to
* @param {object} data - An object containing the data to write to the config
*/
MinErrStrip.prototype.writeConfigFile = function writeConfigFile( writeConfigTo, data ) {
fs.writeFile( path.resolve( writeConfigTo ), JSON.stringify( data ), function ( err ) {
if ( err ) {
throw new Error( err );
}
} );
};
module.exports = MinErrStrip;