-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (117 loc) · 3.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
var merge = require('merge-stream');
var rimraf = require('rimraf');
var initConfig = function(config) {
if (!config) {
throw new Error('You need to define a config (argument #2)');
}
if (!config.dest) {
throw new Error("The property .dest needs to be defined in the config (argument #2)");
}
if (!config.root) {
throw new Error("The property .root needs to be defined in the config (argument #2)");
}
if (!config.hasOwnProperty('dev')) {
config.dev = true;
}
if (!config.hasOwnProperty('tmp')) {
config.tmp = 'files/cache/webforge-js-builder';
}
return config;
};
var specifySubTask = function(spec) {
if (typeof(spec) === 'string') {
spec = { name: spec };
}
if (!spec.type) {
spec.type = 'lib';
}
return spec;
};
module.exports = function(gulp, config, rootRequire) {
var SubTask = require('gulp-subtask')(gulp);
var that = this;
this.config = initConfig(config); // property is public
this.tasks = {};
this.configuredTasks = require('./configured-tasks');
this.gulpTasks = require('./gulp-tasks');
this.require = rootRequire;
this.resolvedModules = {};
if (!that.require) {
throw new Error('Please pass the require function from the gulpfile.js to the builder as third Argument');
}
this.add = function(lane, spec) {
spec = specifySubTask(spec);
var task = new SubTask(lane+':'+spec.name);
task.spec = spec;
if (!that.tasks[lane]) {
that.tasks[lane] = [];
}
that.tasks[lane].push(task);
return task;
};
that.dest = require('gulp-dest');
that.rename = require('gulp-rename');
this.addConfigured = function(lane, name, config) {
if (!that.configuredTasks[name]) {
throw new Error('The builder has no configuration for: '+name);
}
that.configuredTasks[name](that, config || {});
return that;
};
this.run = function(lane) {
var streams = [];
that.tasks[lane].forEach(function(task) {
streams.push(
task.run()
);
});
return merge.apply(this, streams);
};
this.registerTask = function(name, taskConfig) {
if (!that.gulpTasks[name]) {
throw new Error('The builder has no task named: '+name);
}
that.gulpTasks[name](gulp, that, taskConfig || {});
return that;
};
this.resolveModule = function(name, alternative) {
var path = require('path');
if (that.resolvedModules.hasOwnProperty(name)) {
return that.resolvedModules[name];
}
if (that.resolvedModules.hasOwnProperty(alternative)) {
return that.resolvedModules[alternative];
}
try {
return that.resolvedModules[name] = path.dirname(that.require.resolve(name));
} catch (exc) {
if (alternative) {
try {
return that.resolvedModules[alternative] = path.dirname(that.require.resolve(alternative));
} catch (exc2) {
}
} else {
// this does not have a "main" defined, so that it cannot be required()
var execSync = require('child_process').execSync;
var lspath;
try {
lspath = execSync('npm ls '+name+' --parseable', { cwd: that.config.root, stdio: ['pipe', 'pipe', 'ignore']});
} catch (e) {
if (e.status === 1) {
lspath = e.stdout;
} else {
throw e;
}
}
lspath = lspath.toString();
// take the first line
var match = lspath.match(/^(.*?)([\r\n]+|$)/);
if (match === null || match[1] == "") {
throw new Error('Could not parse ls output: '+lspath);
}
return that.resolvedModules[name] = match[1];
}
throw exc;
}
}
};