forked from SBoudrias/gruntfile-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (129 loc) · 4.51 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
'use strict';
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var Tree = require('ast-query');
/**
* A class managing the edition of the project Gruntfile content. Editing the
* Gruntfile using this class allow easier Generator composability as they can
* work and add parts to the same Gruntfile without having to parse the
* Gruntfile AST themselves.
* @constructor
* @param {String} gruntfileContent - The actual `Gruntfile.js` content
*/
var GruntfileEditor = module.exports = function (gruntfileContent) {
gruntfileContent = gruntfileContent ||
fs.readFileSync(path.join(__dirname, 'default-gruntfile.js'));
this.gruntfile = new Tree(gruntfileContent.toString());
};
/**
* Insert a configuration section inside the `Gruntfile.js` initConfig() block
* @param {String} name - Key name of the configuration block
* @param {String} config - Configuration content code as a string.
* @return {this}
*/
GruntfileEditor.prototype.insertConfig = function (name, config) {
name = _.isString(name) && name.trim() || false;
config = _.isString(config) && config.trim() || false;
assert(name, 'You must provide a task name');
assert(config, 'You must provide a task configuration body as a String');
this.gruntfile.callExpression('grunt.initConfig').arguments.at(0)
.key(name).value(config);
return this;
};
/**
* Load a Grunt plugin
* @param {String} pluginName - name of the plugin to load (ex: 'grunt-sass')
* @return {this}
*/
GruntfileEditor.prototype.loadNpmTasks = function (pluginName) {
pluginName = _.isString(pluginName) && pluginName.trim() || false;
assert(pluginName, 'You must provide a plugin name');
this.gruntfile.assignment('module.exports').value().body.prepend(
'grunt.loadNpmTasks("' + pluginName + '");'
);
return this;
};
/**
* Register a task inside a named task group
*
* Can optionally take an object to specify options:
* * duplicates (false): Allow the same task to be inserted multiple times
*
* @param {String} name - Task group name
* @param {String|Array[String]} tasks - Tasks name to insert in the group
* @param {Object} options - An optional object to specify options
* @return {this}
*/
GruntfileEditor.prototype.registerTask = function (name, tasks, options) {
name = _.isString(name) && name.trim() ? name : false;
assert(name, 'You must provide a task group name');
if (_.isString(tasks)) {
tasks = tasks.trim() || false;
}
if (_.isArray(tasks)) {
tasks = tasks.length > 0 && tasks || false;
}
if (!(_.isString(tasks) || _.isArray(tasks))) {
tasks = false;
}
assert(tasks, 'You must provide a task or an array of tasks');
if (options != null && !_.isPlainObject(options)) {
assert(false, 'If you provide options, they must be as an Object');
}
options = _.merge({
duplicates: false
}, options);
var current = this.gruntfile.callExpression('grunt.registerTask')
.filter(function (node) {
return node.arguments[0].value === name;
});
tasks = _.isArray(tasks) ? tasks : tasks.replace(/ /g, '').split(',');
if (!current.length) {
this.gruntfile.assignment('module.exports').value().body.append(
'grunt.registerTask("' + name + '", ["' + tasks.join('","') + '"])'
);
}
else {
var argList = current.arguments.at(1);
var currentTasks = argList.nodes[0].map(function (list) {
return list.value;
});
tasks.forEach(function (task) {
if (currentTasks.indexOf(task) === -1 || options.duplicates) {
argList.push('"' + task + '"');
}
});
}
return this;
};
/**
* Add a variable declaration to the Gruntfile
* @param {String} name - Variable name
* @param {String} value - Variable value
*
* @return {this}
*/
GruntfileEditor.prototype.insertVariable = function (name, value) {
name = _.isString(name) && name.trim() || false;
assert(name, 'You must provide a variable name');
value = _.isString(value) && value.trim() || false;
assert(value, 'You must provide a variable value as a String');
var current = this.gruntfile.var(name);
if (current.length) {
current.value(value);
} else {
this.gruntfile.assignment('module.exports').value().body
.prepend('var ' + name + ' = ' + value);
}
return this;
};
/**
* Return the Gruntfile representation as a string suitable for writing to an
* actual `Gruntfile.js`
* @return {String} gruntfileContent - The actual `Gruntfile.js` content
*/
GruntfileEditor.prototype.toString = function () {
return this.gruntfile.toString();
};