-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.js
147 lines (134 loc) · 3.58 KB
/
core.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
const documentation = require("documentation");
const remark = require("remark");
const ansimd = require("ansimd");
const log = require("util").debuglog("zeroarg");
let md2cli = md => ansimd(remark().stringify(md));
function eatOptions(param) {
var options = {};
param.properties.forEach(property => {
options[property.name.replace(/.*\./g, "")] = Object.assign(
{},
property.description
? {
describe: md2cli(property.description).trim()
}
: {
describe: "..."
},
paramToType(property)
);
});
return options;
}
function typeToType(type) {
switch (type.type) {
case "NameExpression":
switch (type.name.toLowerCase()) {
case "string":
return "string";
case "number":
return "number";
case "boolean":
return "boolean";
}
case "TypeApplication":
if (type.expression && type.expression.name == "Array") {
return "array";
}
}
return "string";
}
function paramToType(property) {
var { type } = property;
var demandOption;
var choices;
var yargsType = {
type: "string"
};
var def;
if (property.default) {
try {
def = { default: eval(property.default) };
} catch (err) {
def = { default: property.default };
}
}
if (property.type) {
switch (type.type) {
case "NonNullableType":
type = type.expression;
demandOption = { demandOption: true };
break;
case "OptionalType":
type = type.expression;
break;
}
if (type.type == "UnionType") {
choices = {
choices: type.elements.map(element => {
return element.name;
})
};
}
yargsType = {
type: typeToType(type)
};
}
return Object.assign(yargsType, def, demandOption, choices);
}
function zeroarg(fn) {
return documentation
.build(
{
// Prefix with var to permit anonymous traditional functions
source: "var _ = " + fn.toString()
},
{
shallow: true
}
)
.then(([doc]) => {
var positionalArguments = [];
var positionalArgumentNames = [];
var options = {};
var positional = true;
var hasVariadic = false;
doc.params.forEach((param, i) => {
if (param.properties) {
if (i !== doc.params.length - 1) {
throw new Error("Options must be specified last");
}
Object.assign(options, eatOptions(param));
} else {
if (hasVariadic) {
throw new Error(
"A variadic positional argument must be the last positional argument"
);
}
if (
param.type &&
param.type.type === "TypeApplication" &&
param.type.expression.name === "Array"
) {
positionalArguments.push("[" + param.name + "..]");
hasVariadic = true;
} else if (param.type && param.type.type === "NonNullableType") {
positionalArguments.push("<" + param.name + ">");
} else {
positionalArguments.push("[" + param.name + "]");
}
positionalArgumentNames.push(param.name);
}
});
return {
options: options,
epilogue: md2cli(doc.description),
// one weird trick, since 'run ' means that this requires an argument.
command: ["run", positionalArguments.join(" ")]
.filter(Boolean)
.join(" "),
positionalArgumentNames: positionalArgumentNames
};
});
}
module.exports = zeroarg;