forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpHelpers.js
183 lines (162 loc) · 5.54 KB
/
gulpHelpers.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
// this will have all of a copy of the normal fs methods as well
const fs = require('fs.extra');
const path = require('path');
const argv = require('yargs').argv;
const MANIFEST = 'package.json';
const through = require('through2');
const _ = require('lodash');
const gutil = require('gulp-util');
const submodules = require('./modules/.submodules.json').parentModules;
const MODULE_PATH = './modules';
const BUILD_PATH = './build/dist';
const DEV_PATH = './build/dev';
const ANALYTICS_PATH = '../analytics';
// get only subdirectories that contain package.json with 'main' property
function isModuleDirectory(filePath) {
try {
const manifestPath = path.join(filePath, MANIFEST);
if (fs.statSync(manifestPath).isFile()) {
const module = require(manifestPath);
return module && module.main;
}
} catch (error) {}
}
module.exports = {
parseBrowserArgs: function (argv) {
return (argv.browsers) ? argv.browsers.split(',') : [];
},
toCapitalCase: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
jsonifyHTML: function (str) {
return str.replace(/\n/g, '')
.replace(/<\//g, '<\\/')
.replace(/\/>/g, '\\/>');
},
getArgModules() {
var modules = (argv.modules || '')
.split(',')
.filter(module => !!module);
try {
if (modules.length === 1 && path.extname(modules[0]).toLowerCase() === '.json') {
var moduleFile = modules[0];
modules = JSON.parse(
fs.readFileSync(moduleFile, 'utf8')
);
}
} catch (e) {
throw new gutil.PluginError({
plugin: 'modules',
message: 'failed reading: ' + argv.modules
});
}
// we need to forcefuly include the parentModule if the subModule is present in modules list and parentModule is not present in modules list
Object.keys(submodules).forEach(parentModule => {
if (
!modules.includes(parentModule) &&
modules.some(module => submodules[parentModule].includes(module))
) {
modules.unshift(parentModule);
}
});
return modules;
},
getModules: _.memoize(function(externalModules) {
externalModules = externalModules || [];
var internalModules;
try {
var absoluteModulePath = path.join(__dirname, MODULE_PATH);
internalModules = fs.readdirSync(absoluteModulePath)
.filter(file => (/^[^\.]+(\.js)?$/).test(file))
.reduce((memo, file) => {
var moduleName = file.split(new RegExp('[.\\' + path.sep + ']'))[0];
var modulePath = path.join(absoluteModulePath, file);
if (fs.lstatSync(modulePath).isDirectory()) {
modulePath = path.join(modulePath, 'index.js')
}
if (fs.existsSync(modulePath)) {
memo[modulePath] = moduleName;
}
return memo;
}, {});
} catch (err) {
internalModules = {};
}
return Object.assign(externalModules.reduce((memo, module) => {
try {
// prefer internal project modules before looking at project dependencies
var modulePath = require.resolve(module, {paths: ['./modules']});
if (modulePath === '') modulePath = require.resolve(module);
memo[modulePath] = module;
} catch (err) {
// do something
}
return memo;
}, internalModules));
}),
getBuiltPath(dev, assetPath) {
return path.join(__dirname, dev ? DEV_PATH : BUILD_PATH, assetPath)
},
getBuiltModules: function(dev, externalModules) {
var modules = this.getModuleNames(externalModules);
if (Array.isArray(externalModules)) {
modules = _.intersection(modules, externalModules);
}
return modules.map(name => this.getBuiltPath(dev, name + '.js'));
},
getBuiltPrebidCoreFile: function(dev) {
return this.getBuiltPath(dev, 'prebid-core.js')
},
getModulePaths: function(externalModules) {
var modules = this.getModules(externalModules);
return Object.keys(modules);
},
getModuleNames: function(externalModules) {
return _.values(this.getModules(externalModules));
},
nameModules: function(externalModules) {
var modules = this.getModules(externalModules);
return through.obj(function(file, enc, done) {
file.named = modules[file.path] ? modules[file.path] : 'prebid';
this.push(file);
done();
})
},
/*
* Get source files for analytics subdirectories in top-level `analytics`
* directory adjacent to Prebid.js.
* Invoke with gulp <task> --analytics
* Returns an array of source files for inclusion in build process
*/
getAnalyticsSources: function() {
if (!argv.analytics) { return []; } // empty arrays won't affect a standard build
const directoryContents = fs.readdirSync(ANALYTICS_PATH);
return directoryContents
.filter(file => isModuleDirectory(path.join(ANALYTICS_PATH, file)))
.map(moduleDirectory => {
const module = require(path.join(ANALYTICS_PATH, moduleDirectory, MANIFEST));
return path.join(ANALYTICS_PATH, moduleDirectory, module.main);
});
},
/*
* Returns the babel options object necessary for allowing analytics packages
* to have their own configs. Gets added to prebid's webpack config with the
* flag --analytics
*/
getAnalyticsOptions: function() {
let options;
if (argv.analytics) {
// https://babeljs.io/docs/en/options#babelrcroots
options = {
babelrcRoots: ['.', ANALYTICS_PATH],
}
}
return options;
},
getDisabledFeatures() {
return (argv.disable || '')
.split(',')
.map((s) => s.trim())
.filter((s) => s);
},
};