-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrunt-symfony.js
131 lines (108 loc) · 3 KB
/
grunt-symfony.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
/**
* grunt-symfony
* @author vitre
* @licence MIT
* @version 1.1.23
* @url https://www.npmjs.org/package/grunt-symfony
*/
"use strict"
var fs = require('fs'),
path = require('path'),
extend = require('node.extend');
//---
var defaults = {
web: 'web',
src: 'src',
gruntFile: 'Gruntfile.js',
resources: 'Resources'
};
var options;
var bundles;
var grunt;
/**
* getBundles
* @param root
* @param r
*/
var getBundles = function (root, r) {
if (typeof root === 'undefined') {
root = options.src;
}
if (typeof r === 'undefined') {
r = [];
}
var files = fs.readdirSync(root);
for (var i in files) {
var path = root + '/' + files[i];
if (fs.statSync(path).isDirectory()) {
if (path.match(/Bundle$/)) {
var name = path.substr(options.src.length + 1, path.length);
var name_camelcase = name.replace(/\//g, '');
var name_dashed = name.replace(/\//g, '-');
var name_underscore = name.replace(/\//g, '_').replace(/-/g, '_');
var name_web = name_camelcase.toLowerCase().replace(/bundle$/, '');
var bundle = {
name: name,
name_camelcase: name_camelcase,
name_dashed: name_dashed,
name_underscore: name_underscore,
name_web: name_web,
path: path,
resources: path + '/' + options.resources,
web: options.web + '/bundles/' + name_web,
web_public: '/bundles/' + name_web,
gruntFile: path + '/' + options.gruntFile
};
if (fs.existsSync(bundle.gruntFile)) {
r.push(bundle);
}
}
getBundles(path, r);
}
}
return r;
};
/**
* importBundle
* @param bundle
* @param config
*/
var importBundle = function (bundle, config) {
var gruntFile = bundle.path + '/' + options.gruntFile;
if (fs.existsSync(gruntFile)) {
var filePath = path.resolve(gruntFile);
console.log('Importing bundle: ' + bundle.name + ' [' + gruntFile + ']');
require(filePath)(grunt, config, bundle, options);
}
};
/**
* importBundles
* @param config
*/
var importBundles = function (config) {
bundles = getBundles();
for (var i = 0; i < bundles.length; i++) {
config.symfony.bundles[bundles[i].name_underscore] = bundles[i];
importBundle(bundles[i], config);
}
};
/**
* Export importBundles
* @param _grunt
* @param config
* @param _options
*/
exports.importBundles = function (_grunt, config, _options) {
grunt = _grunt;
if (typeof _options === 'undefined') {
options = defaults;
} else {
options = extend(true, {}, defaults, _options);
}
config.symfony = extend(config.symfony, {
bundles: {},
dist_tasks: [],
dev_tasks: []
});
importBundles(config);
}