1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const metadata = require("read-metadata");
|
5 | const fs = require("fs");
|
6 | const gituser_1 = require("./utils/gituser");
|
7 | const validateName = require("validate-npm-package-name");
|
8 | const exists = fs.existsSync;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function 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 | }
|
28 | exports.default = options;
|
29 | ;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function 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 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function 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 | }
|
75 | function 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 |
|
\ | No newline at end of file |