1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var processArgv = require('./lib/argv');
|
11 | var debug = require('./lib/debug');
|
12 | var fields = require('./lib/fields');
|
13 | var utils = require('./lib/utils');
|
14 |
|
15 | module.exports = function(app, options) {
|
16 | debug.schema('initializing <%s>, called from <%s>', __filename, module.parent.id);
|
17 |
|
18 | if (typeof app.cwd !== 'string') {
|
19 | throw new Error('expected the base-cwd plugin to be registered');
|
20 | }
|
21 | if (typeof app.pkg === 'undefined') {
|
22 | throw new Error('expected the base-pkg plugin to be registered');
|
23 | }
|
24 | if (typeof app.argv !== 'function') {
|
25 | throw new Error('expected the base-argv plugin to be registered');
|
26 | }
|
27 |
|
28 | var opts = utils.merge({sortArrays: false, omitEmpty: true}, options);
|
29 | var schema = new utils.Schema(opts);
|
30 | schema.app = app;
|
31 |
|
32 |
|
33 | schema
|
34 | .field('config', ['boolean', 'object', 'string'], {
|
35 | normalize: fields.config(app, opts)
|
36 | })
|
37 | .field('data', ['boolean', 'object', 'string'], {
|
38 | normalize: fields.data(app, opts)
|
39 | })
|
40 | .field('disable', 'string', {
|
41 | normalize: fields.disable(app, opts)
|
42 | })
|
43 | .field('enable', 'string', {
|
44 | normalize: fields.enable(app, opts)
|
45 | })
|
46 | .field('options', ['boolean', 'object', 'string'], {
|
47 | normalize: fields.options(app, opts)
|
48 | })
|
49 | .field('option', ['boolean', 'object', 'string'], {
|
50 | normalize: fields.option(app, opts)
|
51 | });
|
52 |
|
53 |
|
54 | schema
|
55 | .field('asyncHelpers', ['array', 'string'], {
|
56 | normalize: fields.asyncHelpers(app, opts)
|
57 | })
|
58 | .field('helpers', ['array', 'string'], {
|
59 | normalize: fields.helpers(app, opts)
|
60 | })
|
61 | .field('cwd', ['boolean', 'string'], {
|
62 | normalize: fields.cwd(app, opts)
|
63 | })
|
64 | .field('dest', ['boolean', 'string'], {
|
65 | normalize: fields.dest(app, opts)
|
66 | })
|
67 | .field('emit', ['array', 'boolean', 'string'], {
|
68 | normalize: fields.emit(app, opts)
|
69 | })
|
70 | .field('pkg', ['boolean', 'object'], {
|
71 | normalize: fields.pkg(app, opts)
|
72 | })
|
73 | .field('reflinks', ['array', 'object', 'string'], {
|
74 | normalize: fields.reflinks(app, opts)
|
75 | })
|
76 | .field('related', ['array', 'object', 'string'], {
|
77 | normalize: fields.related(app, opts)
|
78 | })
|
79 | .field('save', ['object', 'string'], {
|
80 | normalize: fields.save(app, opts)
|
81 | })
|
82 | .field('tasks', ['array', 'string'], {
|
83 | normalize: fields.tasks(app, opts)
|
84 | })
|
85 | .field('toc', ['object', 'string', 'boolean'], {
|
86 | normalize: fields.toc(app, opts)
|
87 | });
|
88 |
|
89 |
|
90 | schema
|
91 | .field('layout', ['object', 'string', 'boolean', 'null'], {
|
92 | normalize: fields.layout(app, opts)
|
93 | });
|
94 |
|
95 | var normalizeField = schema.normalizeField;
|
96 | schema.normalizeField = function(key, value, config, options) {
|
97 | var field = schema.get(key);
|
98 | var isUndefined = typeof value === 'undefined'
|
99 | && typeof config[key] === 'undefined'
|
100 | && typeof field.default === 'undefined';
|
101 |
|
102 | if (isUndefined) {
|
103 | return;
|
104 | }
|
105 |
|
106 | debug.field(key, value);
|
107 | normalizeField.apply(schema, arguments);
|
108 |
|
109 | if (typeof config[key] !== 'undefined') {
|
110 | debug.results(key, config[key]);
|
111 | }
|
112 | return config[key];
|
113 | };
|
114 |
|
115 | var normalizeSchema = schema.normalize;
|
116 | schema.normalize = function(argv) {
|
117 | if (argv.isNormalized) {
|
118 | return argv;
|
119 | }
|
120 |
|
121 | var obj = pluralize(processArgv(app, argv));
|
122 | var res = normalizeSchema.call(schema, obj, opts);
|
123 |
|
124 | for (var key in utils.aliases) {
|
125 | if (res.hasOwnProperty(key)) {
|
126 | res[utils.aliases[key]] = res[key];
|
127 | }
|
128 | }
|
129 | utils.define(res, 'isNormalized', true);
|
130 | return res;
|
131 | };
|
132 | return schema;
|
133 | };
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function pluralize(obj) {
|
140 | var props = ['helper', 'asyncHelper', 'plugin', 'engine', 'task'];
|
141 | var res = {};
|
142 |
|
143 | for (var key in obj) {
|
144 | var val = obj[key];
|
145 | if (key === 'config' && utils.isObject(val)) {
|
146 | val = pluralize(val);
|
147 | }
|
148 | if (~props.indexOf(key)) {
|
149 | key += 's';
|
150 | }
|
151 | res[key] = val;
|
152 | }
|
153 | return res;
|
154 | }
|