-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
132 lines (108 loc) · 3.78 KB
/
main.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
/* jshint node:true */
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var Path = require('path');
var fs = require('fs');
var less = require('less');
var uglify = require("uglify-js");
var bowerConfig = require('bower-config');
var findConfig = require('find-config');
var missed = require('./missed.json');
var sequence = require('./sequence.json');
var PLUGIN_NAME = 'gulp-bootstrap-configurator';
function makeBootstrap(configText, type, callback) {
var data = [];
var config = JSON.parse(configText);
if (type == 'css') {
config.css = config.css.concat(missed.css);
for (var name in missed.vars)
config.vars[name] = missed.vars[name];
for (var name in config.vars)
data.push(name + ': ' + config.vars[name] + ";");
sequence.less.forEach(function(name){
name = name + '.less';
if (config.css.indexOf(name) > -1)
data.push('@import "' + name + "\";");
});
data = data.join("\n");
}
if (type == 'js') {
sequence.js.forEach(function(name){
name = name + '.js';
if (config.js.indexOf(name) > -1) data.push(name);
});
}
callback(null, data);
}
function constructor(type) {
return function(opt) {
opt = opt || {};
opt.bower = opt.bower || false;
if (! opt.base) opt.base = Path.dirname(module.parent.filename);
if(!opt.path && !opt.bower) {
opt.path = 'node_modules/bootstrap';
try {
var resolvedPath = require.resolve('bootstrap');
var packagePath = findConfig('package.json', { cwd: resolvedPath });
opt.path = (packagePath && Path.dirname(packagePath)) || opt.path;
} catch(e) { }
} else if(!opt.path) {
var bc = bowerConfig.create(opt.base).load().toObject();
opt.path = Path.join(bc.directory, 'bootstrap');
}
opt.compress = opt.compress || false;
if (! opt.name && opt.compress) opt.name = 'bootstrap.min.'+type;
if (! opt.name) opt.name = 'bootstrap.'+type;
return through.obj(function (file, encoding, callback){
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new gutil.PluginError(PLUGIN_NAME, 'doesn\'t support Streams'));
}
makeBootstrap(file.contents.toString(), type, function (err, data) {
if (err) {
return callback(new gutil.PluginError(PLUGIN_NAME, err));
}
var bsDir = Path.isAbsolute(opt.path) ? opt.path : Path.join(opt.base, opt.path);
if (type == 'css') {
var lessDir = Path.join(bsDir, 'less');
var cssFile = new gutil.File({
path: Path.join(opt.base, opt.name),
cwd: file.cwd,
});
less.render(data, {paths: [lessDir], compress: opt.compress}, function (e, output) {
if (e) callback(e);
else {
cssFile.contents = new Buffer(output.css);
callback(null, cssFile);
}
});
}
if (type == 'js') {
var jsDir = Path.join(bsDir, 'js');
var jsFile = new gutil.File({
path: Path.join(opt.base, opt.name),
cwd: file.cwd,
});
data.forEach(function(name, i){ data[i] = Path.join(jsDir, name); });
data.unshift(Path.join(__dirname, 'header.js'));
if (opt.compress) {
jsFile.contents = new Buffer(uglify.minify(data).code);
} else {
var jsData = data.map(function(filePath){
return fs.readFileSync(filePath);
});
jsFile.contents = new Buffer(jsData.join("\n"));
}
callback(null, jsFile);
}
});
});
};
}
module.exports = {
css: constructor('css'),
js: constructor('js')
};