UNPKG

2.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const metadata = require("read-metadata");
5const fs = require("fs");
6const gituser_1 = require("./utils/gituser");
7const validateName = require("validate-npm-package-name");
8const exists = fs.existsSync;
9/**
10 * Read prompts metadata.
11 *
12 * @param {String} dir
13 * @return {Object}
14 */
15function options(name, dir, opt) {
16 const opts = getMetadata(dir);
17 setDefault(opts, 'name', name);
18 setValidateName(opts);
19 const author = gituser_1.default();
20 if (author) {
21 setDefault(opts, 'author', author);
22 }
23 for (const key in opt) {
24 setDefault(opts, key, opt[key]);
25 }
26 return opts;
27}
28exports.default = options;
29;
30/**
31 * Gets the metadata from either a meta.json or meta.js file.
32 *
33 * @param {String} dir
34 * @return {Object}
35 */
36function getMetadata(dir) {
37 const json = path.join(dir, 'meta.json');
38 const js = path.join(dir, 'meta.js');
39 let opts = {};
40 if (exists(json)) {
41 opts = metadata.sync(json);
42 }
43 else if (exists(js)) {
44 const req = require(path.resolve(js));
45 if (req !== Object(req)) {
46 throw new Error('meta.js needs to expose an object');
47 }
48 opts = req;
49 }
50 return opts;
51}
52/**
53 * Set the default value for a prompt question
54 *
55 * @param {Object} opts
56 * @param {String} key
57 * @param {String} val
58 */
59function setDefault(opts, key, val) {
60 if (opts.schema) {
61 opts.prompts = opts.schema;
62 delete opts.schema;
63 }
64 const prompts = opts.prompts || (opts.prompts = {});
65 if (!prompts[key] || typeof prompts[key] !== 'object') {
66 prompts[key] = {
67 'type': 'string',
68 'default': val
69 };
70 }
71 else {
72 prompts[key]['default'] = val;
73 }
74}
75function setValidateName(opts) {
76 const name = opts.prompts.name;
77 const customValidate = name.validate;
78 name.validate = name => {
79 const its = validateName(name);
80 if (!its.validForNewPackages) {
81 const errors = (its.errors || []).concat(its.warnings || []);
82 return 'Sorry, ' + errors.join(' and ') + '.';
83 }
84 if (typeof customValidate === 'function')
85 return customValidate(name);
86 return true;
87 };
88}
89//# sourceMappingURL=options.js.map
\No newline at end of file