-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
149 lines (126 loc) · 3.57 KB
/
generator.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
'use strict';
var utils = require('./utils');
module.exports = function plugin(app, base) {
if (!utils.isValid(app, 'generate-data')) return;
/**
* Clone `cache.data` before it's modified
*/
if (!app.has('cache.originalData')) {
app.set('cache.originalData', utils.clone(app.cache.data));
}
/**
* Use `base-data` plugin
*/
app.use(utils.data());
/**
* Update and normalize data
*/
app.cache.data.project = app.cache.data.project || {};
app.data({cwd: app.cwd});
app.data({year: new Date().getFullYear()});
app.data(app.pkg.data);
// restore original user-defined data
app.data(app.cache.originalData);
/**
* "Expand" fields using `expand-pkg`
*/
var config = expander(app);
app.data(config.expand(app.cache.data));
if (!app.has('cache.data.runner')) {
app.data('runner', {name: 'generate', homepage: 'https://github.com/generate/generate'});
}
set(app, 'name');
set(app, 'varname');
set(app, 'description');
set(app, 'alias');
set(app, 'owner');
return plugin;
};
/**
* Set data values on the `app.cache.data.project` object, which is
* merged onto the context at render time and is available in templats
* as `project`.
*/
function set(app, prop, val) {
var data = app.cache.data.project;
if (typeof val !== 'undefined') {
data[prop] = val;
} else if (typeof app.cache.data[prop] !== 'undefined') {
data[prop] = app.cache.data[prop];
}
}
/**
* Add custom fields to `expand-package`
*/
function expander(app) {
var config = new utils.Expand(app.options);
config.field('username', 'string', {
normalize: function(val, key, config, schema) {
if (utils.isString(val)) return val;
if (typeof val === 'undefined') {
schema.update('author', config);
config.author = config.author || {};
}
val = config[key] = utils.repo.username(config, schema.options);
if (utils.isString(val)) {
config.author.username = val;
return val;
}
}
});
config.field('twitter', 'string', {
normalize: function(val, key, config, schema) {
if (utils.isString(val)) return val;
if (typeof val === 'undefined') {
schema.update('username', config);
}
config.author = config.author || {};
val = schema.options.twitter || config.author.username;
if (utils.isString(val)) {
config.author.twitter = val;
return val;
}
}
});
config.field('alias', 'string', {
normalize: function(val, key, config, schema) {
var name = config.name || '';
var toAlias = schema.options.toAlias;
if (typeof toAlias === 'function') {
val = toAlias.call(config, name);
} else {
val = name.slice(name.lastIndexOf('-') + 1);
val = utils.camelcase(val);
}
config[key] = val;
set(app, key, val);
return val;
}
});
config.field('varname', 'string', {
normalize: function(val, key, config, schema) {
if (utils.isString(val)) {
set(app, key, val);
return val;
}
var name = config.name;
if (typeof schema.options.varname === 'function') {
config[key] = schema.option.varname(name, config);
} else {
config[key] = utils.namify(name);
}
val = app.cache.data[key] = config[key];
set(app, key, val);
return val;
}
});
if (app.options.config && app.options.config.fields) {
var fields = app.options.config.fields;
for (var key in fields) {
if (fields.hasOwnProperty(key)) {
config[key] = fields[key];
}
}
}
return config;
}