-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
348 lines (283 loc) · 9.55 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
var gux = require('node-unique-extensions'),
q = require('q'),
npmh = require('./helpers/NpmHelper'),
GruntHelper = require('./helpers/GruntHelper'),
utils = require('./helpers/Utils'),
args = require('minimist')(process.argv);
'use strict';
function Ya () {
this.directory = '.';
this.processedPromises = [];
this.extensions = [];
}
Ya.prototype.init = function (directory) {
this.directory = directory || this.directory;
// Lazily installed in case we want to dynamically determine
// the build engine later on
this.dependencies = [
'grunt',
'grunt-cli',
'load-grunt-tasks',
'grunt-contrib-watch',
'grunt-newer'
];
// Collection of folders that should be ignored so YA doesn't
// try to process them and get all confused
this.ignoredDirs = [
'node_modules',
'.git',
'.sass-cache',
'bower_components',
'vendor'
];
// Handles ya'ing a subdirectory
// this.ignoredDirs = this.ignoredDirs.map(function (dir) {
// return utils.slashDir(this.directory) + dir;
// }, this);
this.engine = new GruntHelper({
directory: this.directory,
ignoredDirs: this.ignoredDirs
});
this.engine.on('added', onAddedExtensions.bind(this));
this.engine.on('jsChanged', onJSChanged.bind(this));
utils.bindAll(this);
return q();
};
// Make sure YA's dependencies are installed
Ya.prototype.startup = function () {
return q()
.then(this.handleDefaultPackageJSON)
.then(this.installDependencies);
};
// Returns a promise that resolves with the settings for each
// processed/supported extension found in the working directory
Ya.prototype.yaExtensions = function () {
return q()
.then(this.findUsedExtensions)
.then(this.filterSupportedExtensions)
.then(this.setUsedExtensions)
.then(this.processExtensions);
};
// Takes in the settings of all processed/found extensions
// and generates a build engine configuration file
// Returns a promise that resolves with the build configuration object
Ya.prototype.generateBuildConfig = function (allSettings) {
var that = this;
this.setAllSettings(allSettings);
return this.generateConfig()
.then(function (config) {
return that.flushConfig(config).then(function () {
return config;
});
});
};
// Installs YA's npm dependencies
Ya.prototype.installDependencies = function () {
var that = this;
return q.all(this.dependencies.map(npmh.isLibInstalled))
.then(function (results) {
// Only install the libs that haven't already been installed
var notInstalled = that.dependencies.filter(function (dep, idx) {
return ! results[idx];
});
if (notInstalled.length) {
notInstalled.map(function (ni) {
console.log('Installing: ', ni);
});
}
return q.all(notInstalled.map(npmh.installLib));
});
};
// Setter to notify YA of the extensions to be used/processed
Ya.prototype.setUsedExtensions = function (extensions) {
this.extensions = extensions || [];
};
// Creates an empty package.json file in the working directory
// to allow YA to continue initialization and installation of
// (saved) dependencies.
Ya.prototype.handleDefaultPackageJSON = function () {
var that = this;
return npmh.hasPackageJsonFile(this.directory)
.then(function (hasFile) {
if (! hasFile) {
return npmh.createEmptyPackageJsonFile(that.directory);
}
});
};
// Initiates the build engine's watch task
Ya.prototype.watch = function () {
this.engine.watch();
};
// An extension is supported if we have a settings file for it
Ya.prototype.isExtensionSupported = function (ext) {
var deferred = q.defer();
if (! ext || args.preprocess && ext === '.js') {
deferred.resolve(false);
} else {
return utils.exists(this.getSettingsFilepath(ext));
}
return deferred.promise;
};
// Returns the path of the settings file for the given extension.
// Note: the settings file isn't guaranteed to exist
Ya.prototype.getSettingsFilepath = function (ext) {
var extension = ext[0] === '.' ? ext.slice(1) : ext;
return __dirname + '/settings/' + extension + '-settings.js';
};
// Resolves with a settings object for the given extension or
// null if the extension is not supported
Ya.prototype.getExtensionSettings = function (ext) {
return this.isExtensionSupported(ext)
.then(function (isSupported) {
if (isSupported) {
// Settings will either be an object literal
// for simple preprocessors that have static settings
// or a promise that resolves with the settings
// for preprocessors performing async to determine settings
return require(this.getSettingsFilepath(ext));
} else {
return null;
}
}.bind(this));
};
Ya.prototype.processExtensions = function () {
return q.all(this.extensions.map(this.processExtension));
};
// Installs all library dependencies for that extension
// and resolves with the build engine configuration settings
// Precond: ext is a supported extension (i.e., has settings)
Ya.prototype.processExtension = function (ext) {
return this.getExtensionSettings(ext)
.then(function (settings) {
// A preprocessor can require multiple libraries installed
var libs = settings.lib instanceof Array ? settings.lib : [settings.lib];
return q.all(libs.map(npmh.installIfNecessary.bind(npmh)))
.then(function () {
return settings;
});
});
};
// Adds the new extension to the processing pipeline and regenerates
// the build engine configuration
Ya.prototype.processAdditionalExtension = function (ext) {
var that = this;
this.extensions.push(ext);
this.processedPromises.push(this.processExtension(ext));
return this.generateConfig()
.then(function (config) {
return that.engine.flushConfig(config)
.then(function () {
return that.engine.compileTasks(config);
});
})
.then(function () {
that.engine.rewatch();
})
.done();
};
// Returns true if the given extension has already been processed
Ya.prototype.isExtensionAlreadyProcessed = function (ext) {
return this.extensions.indexOf(utils.dotExt(ext)) !== -1;
};
// Returns all extensions found in the current directory
Ya.prototype.findUsedExtensions = function () {
return gux({
path: this.directory,
exclusions: utils.ignoredDirs,
includeDotFiles: true
});
};
// Returns a subset of the given extensions that YA supports
Ya.prototype.filterSupportedExtensions = function (extensions) {
return q.all(extensions.map(this.isExtensionSupported.bind(this)))
.then(function (results) {
// Grab all extensions that have a truthy support value
var supported = extensions.filter(function (ext, idx) {
return results[idx];
});
return supported;
});
};
// Resolves with the build engine configuration for all
// processed extensions
Ya.prototype.generateConfig = function () {
return this.getAllSettings()
.then(function (targets) {
return this.engine.getConfig(targets, this.extensions);
}.bind(this));
};
// Flushes the build engine configuration to disk
Ya.prototype.flushConfig = function (config) {
return this.engine.flushConfig(config);
};
Ya.prototype.compileTasks = function (config) {
return this.engine.compileTasks(config);
};
// Returns a list of promises that resolve with the
// settings objects of all processed extensions
Ya.prototype.getAllSettings = function () {
return q.all(this.processedPromises);
};
// Sets the list of processed extensions' settings
Ya.prototype.setAllSettings = function (settingsList) {
this.processedPromises = settingsList;
};
///////////////
// Listeners
///////////////
// Flow for handling yet another extension on file addition
// Note: Assumes the extension is new and system-supported
function onAddedExtensions (extensions) {
console.log('Detected the following additions: ', extensions);
extensions.forEach(function (ext) {
if (this.isExtensionAlreadyProcessed(ext)) return;
this.isExtensionSupported(ext).done(function (isSupported) {
if (! isSupported) return;
this.processAdditionalExtension(ext);
}.bind(this));
}.bind(this));
}
// When to change/generate the grunt configuration
function onJSChanged() {
if (typeof this.jsh === 'undefined') {
var JSH = require('./helpers/JsHelper');
this.jsh = new JSH(this.directory);
}
var that = this;
// Cases to recompute:
// a root file changes: index.js could remove require of lib/index making index.js a root and lib/index a root
// a non-root file changes: b.js is the root and a.js changes to require b.js making it the new root
return this.jsh.getRoots().then(function (roots) {
return that.jsh.haveRootsChanged(roots);
})
.then(function (haveRootsChanged) {
if (! haveRootsChanged) {
console.log('roots haven\'t changed');
return;
}
console.log('An app root has changed');
// Need all of the targets to regenerate the gruntfile
return that.getAllSettings().then(function (targets) {
return that.engine.getConfig(targets, that.extensions);
})
.then(function (config) {
// Grab the targets for the apps and merge with the existing targets
return that.jsh.getSettings().then(function (settings) {
utils.shallowExtend(config, settings.target);
return config;
});
})
.then(function (config) {
// console.log('Config: ', config)
return that.engine.flushConfig(config)
.then(function () {
return that.engine.compileTasks(config);
});
});
});
}
/**
* Export the singleton
* @type {Ya}
*/
module.exports = new Ya();