forked from guzart/gulp-ng-constant
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
139 lines (112 loc) · 3.91 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
'use strict';
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var _ = require('lodash');
var TEMPLATE_PATH = path.join(__dirname, 'tpls', 'constant.tpl.ejs');
var DEFAULT_WRAP_PATH = path.join(__dirname, 'tpls', 'default-wrapper.tpl.ejs');
var AMD_WRAP_PATH = path.join(__dirname, 'tpls', 'amd-wrapper.tpl.ejs');
var COMMONJS_WRAP_PATH = path.join(__dirname, 'tpls', 'commonjs-wrapper.tpl.ejs');
var defaultWrapper, amdWrapper, commonjsWrapper;
var defaults = {
space: '\t',
deps: null,
wrap: false,
template: undefined,
templatePath: TEMPLATE_PATH,
noFile: false,
interpolate: undefined
};
function ngConstantPlugin(opts) {
var options = _.merge({}, defaults, opts);
var template = options.template || readFile(options.templatePath);
var stream = through.obj(objectStream);
if (options.noFile) {
stream.end(new gutil.File({ path: options.dest | 'constants.js' }));
}
return stream;
function objectStream(file, enc, cb) {
/* jshint validthis: true */
var _this = this;
if (file.isStream()) {
_this.emit('error', pluginError('Streaming not supported'));
return cb();
}
try {
var data = file.isNull() ? {} : JSON.parse(file.contents);
var result = _.template(template)({
moduleName: options.name || data.name,
deps: isDepsFalse(options, data) ? false : options.deps || data.deps || [],
constants: getConstants(data, options)
});
// Handle wrapping
if (!options.wrap) { options.wrap = data.wrap; }
result = wrap(result, options)(_.merge({ '__ngModule': result }, options));
file.path = getFilePath(file.path, options);
file.contents = new Buffer(result);
_this.push(file);
} catch (err) {
err.fileName = file.path;
_this.emit('error', pluginError(err.stack));
}
cb();
}
}
function getConstants(data, options) {
var opts = options || {};
if (typeof opts.constants === 'string') {
opts.constants = JSON.parse(opts.constants);
}
var dataCnst = data.constants || data;
var input = _.extend({}, dataCnst, opts.constants);
var constants = _.map(input, function (value, name) {
return {
name: name,
value: stringify(value, opts.space)
};
});
return constants;
}
function getFilePath(filePath, options) {
if (!options.dest) {
return gutil.replaceExtension(filePath, '.js');
}
return path.join(path.dirname(filePath), options.dest);
}
function pluginError(msg) {
return new gutil.PluginError('gulp-tslint-log', msg);
}
function wrap(input, options) {
var wrapper = options.wrap || '<%= __ngModule %>';
if (wrapper === true) {
if (!defaultWrapper) { defaultWrapper = readFile(DEFAULT_WRAP_PATH); }
wrapper = defaultWrapper;
} else if (wrapper === 'amd') {
if (!amdWrapper) { amdWrapper = readFile(AMD_WRAP_PATH); }
wrapper = amdWrapper;
} else if (wrapper === 'commonjs') {
if (!commonjsWrapper) { commonjsWrapper = readFile(COMMONJS_WRAP_PATH); }
wrapper = commonjsWrapper;
}
return _.template(wrapper, options.interpolate ? { interpolate: options.interpolate } : {});
}
function readFile(filepath) {
return fs.readFileSync(filepath, 'utf8');
}
function stringify(value, space) {
return _.isUndefined(value) ? 'undefined' : JSON.stringify(value, null, space);
}
function isDepsFalse(options, data) {
if (options.deps === false || (!options.deps && data.deps === false)) {
return true;
} else {
return false;
}
}
_.extend(ngConstantPlugin, {
getConstants: getConstants,
getFilePath: getFilePath,
wrap: wrap
});
module.exports = ngConstantPlugin;