-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
263 lines (229 loc) · 5.45 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
/*!
* scaffold <https://github.com/jonschlinkert/scaffold>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var Base = require('base');
var util = require('expand-utils');
var utils = require('./utils');
/**
* Create a new Scaffold with the given `options`
*
* ```js
* var scaffold = new Scaffold({cwd: 'src'});
* scaffold.addTargets({
* site: {src: ['*.hbs']},
* blog: {src: ['*.md']}
* });
* ```
* @param {Object} `options`
* @api public
*/
function Scaffold(options) {
if (!(this instanceof Scaffold)) {
return new Scaffold(options);
}
Base.call(this);
this.use(utils.plugins());
this.is('scaffold');
options = options || {};
this.options = options;
this.targets = {};
if (Scaffold.isScaffold(options)) {
this.options = {};
foward(this, options);
this.addTargets(options);
return this;
} else {
foward(this, options);
}
}
/**
* Inherit `Base`
*/
Base.extend(Scaffold);
/**
* Static method, returns `true` if the given value is an
* instance of `Scaffold` or appears to be a valid `scaffold`
* configuration object.
*
* ```js
* Scaffold.isScaffold({});
* //=> false
*
* var blog = new Scaffold({
* post: {
* src: 'content/post.md',
* dest: 'src/posts/'
* }
* });
* Scaffold.isScaffold(blog);
* //=> true
* ```
* @static
* @param {Object} `val` The value to check
* @return {Boolean}
* @api public
*/
Scaffold.isScaffold = function(val) {
return util.isTask(val) || utils.isScaffold(val);
};
/**
* Add targets to the scaffold, while also normalizing src-dest mappings and
* expanding glob patterns in each target.
*
* ```js
* scaffold.addTargets({
* site: {src: '*.hbs', dest: 'templates/'},
* docs: {src: '*.md', dest: 'content/'}
* });
* ```
* @param {Object} `targets` Object of targets, `options`, or arbitrary properties.
* @return {Object}
* @api public
*/
Scaffold.prototype.addTargets = function(config) {
if (!utils.isObject(config)) {
throw new TypeError('expected an object');
}
if (util.isTarget(config)) {
return this.addTarget(config.name, config);
}
for (var key in config) {
if (config.hasOwnProperty(key)) {
var val = config[key];
if (util.isTarget(val)) {
this.addTarget(key, val);
} else {
this[key] = val;
}
}
}
return this;
};
/**
* Add a single target to the scaffold, while also normalizing src-dest mappings and
* expanding glob patterns in the target.
*
* ```js
* scaffold.addTarget('foo', {
* src: 'templates/*.hbs',
* dest: 'site'
* });
*
* // other configurations are possible
* scaffold.addTarget('foo', {
* options: {cwd: 'templates'}
* files: [
* {src: '*.hbs', dest: 'site'},
* {src: '*.md', dest: 'site'}
* ]
* });
* ```
* @param {String} `name`
* @param {Object} `config`
* @return {Object}
* @api public
*/
Scaffold.prototype.addTarget = function(name, config) {
if (typeof name !== 'string') {
throw new TypeError('expected name to be a string');
}
if (!utils.isObject(config)) {
throw new TypeError('expected an object');
}
var self = this;
var Target = this.get('Target');
var target = new Target(this.options);
utils.define(target, 'parent', this);
utils.define(target, 'name', name);
utils.define(target, 'key', name);
target.on('files', function(stage, files) {
utils.define(files, 'parent', target);
self.emit('files', stage, files);
});
target.options = utils.extend({}, target.options, config.options);
this.emit('target', target);
this.run(target);
target.addFiles(config);
this.targets[name] = target;
return target;
};
/**
* Getter/setter for the `Target` constructor to use for creating new targets.
*
* ```js
* var Target = scaffold.get('Target');
* var target = new Target();
* ```
* @name .Target
* @return {Function} Returns the `Target` constructor to use for creating new targets.
* @api public
*/
Object.defineProperty(Scaffold.prototype, 'Target', {
configurable: true,
set: function(Target) {
utils.define(this, '_Target', Target);
},
get: function() {
return this._Target || this.options.Target || utils.Target;
}
});
/**
* Getter/setter for `scaffold.name`. The `name` property can be set on the options or
* directly on the instance.
*
* ```js
* var scaffold = new Scaffold({name: 'foo'});
* console.log(scaffold.name);
* //=> 'foo'
*
* // or
* var scaffold = new Scaffold();
* scaffold.options.name = 'bar';
* console.log(scaffold.name);
* //=> 'bar'
*
* // or
* var scaffold = new Scaffold();
* scaffold.name = 'baz';
* console.log(scaffold.name);
* //=> 'baz'
* ```
* @name .name
* @return {Function} Returns the `Target` constructor to use for creating new targets.
* @api public
*/
Object.defineProperty(Scaffold.prototype, 'name', {
configurable: true,
set: function(val) {
utils.define(this, '_scaffoldName', val);
},
get: function() {
return this._scaffoldName || this.options.name;
}
});
/**
* Forward events from the scaffold instance to the `Scaffold` constructor
*/
function foward(app, options) {
if (typeof options.name === 'string') {
app.name = options.name;
delete options.name;
}
Scaffold.emit('scaffold', app);
emit('target', app, Scaffold);
emit('files', app, Scaffold);
}
/**
* Forward events from emitter `a` to emitter `b`
*/
function emit(name, a, b) {
a.on(name, b.emit.bind(b, name));
}
/**
* Expose `Scaffold`
*/
module.exports = Scaffold;