forked from atticoos/gulp-ng-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulp-ng-config.js
More file actions
118 lines (99 loc) · 3.53 KB
/
gulp-ng-config.js
File metadata and controls
118 lines (99 loc) · 3.53 KB
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
var through = require('through2'),
gutil = require('gulp-util'),
_ = require('lodash'),
fs = require('fs'),
jsYaml = require('js-yaml'),
templateFilePath = __dirname + '/template.html',
PluginError = gutil.PluginError;
const PLUGIN_NAME = 'gulp-ng-config',
WRAP_TEMPLATE = '(function () { \n return <%= module %>\n})();\n';
function gulpNgConfig (moduleName, configuration) {
var templateFile, stream, defaults;
defaults = {
createModule: true,
wrap: false,
environment: null,
parser: null,
pretty: false
};
if (!moduleName) {
throw new PluginError(PLUGIN_NAME, 'Missing required moduleName option for gulp-ng-config');
}
templateFile = fs.readFileSync(templateFilePath, 'utf8');
configuration = configuration || {};
configuration = _.merge({}, defaults, configuration);
stream = through.obj(function (file, encoding, callback) {
var constants = [],
templateOutput,
jsonObj,
wrapTemplate;
if (!configuration.parser && (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml'))) {
configuration.parser = 'yml';
}
if (!configuration.parser) {
configuration.parser = 'json';
}
if (configuration.parser === 'json') {
try {
jsonObj = file.isNull() ? {} : JSON.parse(file.contents.toString('utf8'));
} catch (e) {
this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided'));
}
} else if (configuration.parser === 'yml' || configuration.parser === 'yaml') {
try {
jsonObj = jsYaml.safeLoad(file.contents);
} catch (e) {
this.emit('error', new PluginError(PLUGIN_NAME, 'invaild YML file provided'));
}
} else {
this.emit('error', new PluginError(PLUGIN_NAME, configuration.parser + ' is not supported as a valid parser'));
}
if (!_.isPlainObject(jsonObj)) {
this.emit('error', new PluginError(PLUGIN_NAME, 'invalid JSON object provided'));
}
// select the environment in the configuration
if (configuration.environment && jsonObj.hasOwnProperty(configuration.environment)) {
jsonObj = jsonObj[configuration.environment];
}
jsonObj = _.merge({}, jsonObj, configuration.constants || {});
var spaces = 0;
if (configuration.pretty) {
if (configuration.pretty === true) {
spaces = 2;
} else if (configuration.pretty === +configuration.pretty) {
if (configuration.pretty !== parseInt(configuration.pretty, 10) || !Number.isFinite(configuration.pretty)) {
var message = 'invalid \'pretty\' value. Should be boolean value or an integer number';
return this.emit('error', new PluginError(PLUGIN_NAME, message));
}
spaces = configuration.pretty;
}
}
_.each(jsonObj, function (value, key) {
constants.push({
name: key,
value: JSON.stringify(value, null, spaces)
});
});
templateOutput = _.template(templateFile)({
createModule: configuration.createModule,
moduleName: moduleName,
constants: constants
});
if (configuration.wrap) {
if (typeof configuration.wrap === 'string') {
wrapTemplate = configuration.wrap;
} else {
wrapTemplate = WRAP_TEMPLATE;
}
templateOutput = _.template(wrapTemplate)({
module: templateOutput
});
}
file.path = gutil.replaceExtension(file.path, '.js');
file.contents = new Buffer(templateOutput);
this.push(file);
callback();
});
return stream;
}
module.exports = gulpNgConfig;