UNPKG

4.06 kBJavaScriptView Raw
1/*!
2 * base-cli-schema <https://github.com/jonschlinkert/base-cli-schema>
3 *
4 * Copyright (c) 2016, Jon Schlinkert.
5 * Licensed under the MIT License.
6 */
7
8'use strict';
9
10var processArgv = require('./lib/argv');
11var debug = require('./lib/debug');
12var fields = require('./lib/fields');
13var utils = require('./lib/utils');
14
15module.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 // Configuration, settings and data
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 // misc
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('emit', ['array', 'boolean', 'string'], {
65 normalize: fields.emit(app, opts)
66 })
67 .field('pkg', ['boolean', 'object'], {
68 normalize: fields.pkg(app, opts)
69 })
70 .field('reflinks', ['array', 'object', 'string'], {
71 normalize: fields.reflinks(app, opts)
72 })
73 .field('related', ['array', 'object', 'string'], {
74 normalize: fields.related(app, opts)
75 })
76 .field('save', ['object', 'string'], {
77 normalize: fields.save(app, opts)
78 })
79 .field('tasks', ['array', 'string'], {
80 normalize: fields.tasks(app, opts)
81 })
82 .field('toc', ['object', 'string', 'boolean'], {
83 normalize: fields.toc(app, opts)
84 });
85
86 // template related
87 schema
88 .field('layout', ['object', 'string', 'boolean', 'null'], {
89 normalize: fields.layout(app, opts)
90 });
91
92 var normalizeField = schema.normalizeField;
93 schema.normalizeField = function(key, value, config, options) {
94 var field = schema.get(key);
95
96 var isUndefined = typeof val === 'undefined'
97 && typeof config[key] === 'undefined'
98 && typeof field.default === 'undefined';
99
100 if (isUndefined) {
101 return;
102 }
103
104 debug.field(key, value);
105 normalizeField.apply(schema, arguments);
106 if (typeof config[key] !== 'undefined') {
107 debug.results(key, config[key]);
108 }
109 };
110
111 var normalizeSchema = schema.normalize;
112 schema.normalize = function(argv) {
113 if (argv.isNormalized) {
114 return argv;
115 }
116
117 var obj = pluralize(processArgv(app, argv));
118 var res = normalizeSchema.call(schema, obj, opts);
119
120 for (var key in utils.aliases) {
121 if (res.hasOwnProperty(key)) {
122 res[utils.aliases[key]] = res[key];
123 }
124 }
125
126 utils.define(res, 'isNormalized', true);
127 return res;
128 };
129 return schema;
130};
131
132/**
133 * Ensure certain keys are pluralized (e.g. fix humans)
134 */
135
136function pluralize(obj) {
137 var props = ['helper', 'asyncHelper', 'plugin', 'engine', 'task'];
138 var res = {};
139
140 for (var key in obj) {
141 var val = obj[key];
142 if (key === 'config' && utils.isObject(val)) {
143 val = pluralize(val);
144 }
145 if (~props.indexOf(key)) {
146 key += 's';
147 }
148 res[key] = val;
149 }
150 return res;
151}