@devbie/apits 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -2
- package/bin/apits +4 -0
- package/dist/cjs/core/cli.d.ts +29 -0
- package/dist/cjs/core/cli.js +396 -0
- package/dist/cjs/core/generator.js +301 -0
- package/dist/cjs/core/writeRequestClient.js +93 -0
- package/dist/cjs/index.js +25 -0
- package/dist/cjs/types.js +90 -0
- package/dist/cjs/utils/console.js +79 -0
- package/dist/cjs/utils/getOutputPath.js +43 -0
- package/dist/cjs/utils/helpers.js +166 -0
- package/dist/cjs/utils/openApiToInterfaces.js +229 -0
- package/dist/cjs/utils/spinner.js +69 -0
- package/dist/cjs/utils/utils.d.ts +56 -0
- package/dist/cjs/utils/utils.js +355 -0
- package/dist/cjs/utils/vtilsLite.js +123 -0
- package/dist/esm/core/cli.d.mts +29 -0
- package/dist/esm/core/cli.mjs +713 -0
- package/dist/esm/core/generator.mjs +463 -0
- package/dist/esm/core/writeRequestClient.mjs +54 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/types.mjs +94 -0
- package/dist/esm/utils/console.mjs +65 -0
- package/dist/esm/utils/getOutputPath.mjs +12 -0
- package/dist/esm/utils/helpers.mjs +226 -0
- package/dist/esm/utils/openApiToInterfaces.mjs +326 -0
- package/dist/esm/utils/spinner.mjs +58 -0
- package/dist/esm/utils/utils.d.mts +56 -0
- package/dist/esm/utils/utils.mjs +545 -0
- package/dist/esm/utils/vtilsLite.mjs +142 -0
- package/package.json +68 -4
|
@@ -0,0 +1,713 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
var _templateObject;
|
|
3
|
+
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
|
4
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
5
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
6
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
7
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
8
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
9
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
10
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
11
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
12
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
13
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
14
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
18
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
19
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
20
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
21
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
22
|
+
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
23
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
24
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
25
|
+
function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
|
|
26
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
27
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
28
|
+
import { register } from "tsx/cjs/api";
|
|
29
|
+
import fs from "fs-extra";
|
|
30
|
+
import path from "path";
|
|
31
|
+
import prompt from "prompts";
|
|
32
|
+
import yargs from "yargs";
|
|
33
|
+
import { dedent } from "../utils/vtilsLite.mjs";
|
|
34
|
+
import { Generator } from "./generator.mjs";
|
|
35
|
+
import chalk from "chalk";
|
|
36
|
+
import * as conso from "../utils/console.mjs";
|
|
37
|
+
import { formatContent } from "../utils/utils.mjs";
|
|
38
|
+
import { spinnerInstance } from "../utils/spinner.mjs";
|
|
39
|
+
import { asyncFnArrayOrderRun, defineConfig } from "../utils/helpers.mjs";
|
|
40
|
+
|
|
41
|
+
// Register the tsx loader so apits.config.ts / apits.config.local.ts can be
|
|
42
|
+
// required at runtime. tsx transpiles via esbuild (no type-checking) and
|
|
43
|
+
// reads tsconfig.json automatically, so no compilerOptions are needed here.
|
|
44
|
+
register();
|
|
45
|
+
|
|
46
|
+
/** Flags accepted by the `gen` command. */
|
|
47
|
+
|
|
48
|
+
// Config file candidates in priority order; the first match wins.
|
|
49
|
+
var CONFIG_FILE_CANDIDATES = ["apits.config.local.ts", "apits.config.ts", "apits.config.local.json", "apits.config.json"];
|
|
50
|
+
/** Raised when neither a CLI input nor a config file can be found. */
|
|
51
|
+
var NoInputError = /*#__PURE__*/function (_Error) {
|
|
52
|
+
_inherits(NoInputError, _Error);
|
|
53
|
+
var _super = _createSuper(NoInputError);
|
|
54
|
+
function NoInputError() {
|
|
55
|
+
_classCallCheck(this, NoInputError);
|
|
56
|
+
return _super.apply(this, arguments);
|
|
57
|
+
}
|
|
58
|
+
return _createClass(NoInputError);
|
|
59
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
60
|
+
function isHttpInput(input) {
|
|
61
|
+
return /^https?:\/\//i.test(input);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Normalize a single config or an array into a config array. */
|
|
65
|
+
function normalizeConfig(raw) {
|
|
66
|
+
return defineConfig(Array.isArray(raw) ? raw : [raw]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Discover the optional config file. Supports TS/JSON config files and the
|
|
71
|
+
* `apits` field inside package.json. Returns undefined when nothing exists.
|
|
72
|
+
*/
|
|
73
|
+
function discoverConfigFile() {
|
|
74
|
+
return _discoverConfigFile.apply(this, arguments);
|
|
75
|
+
}
|
|
76
|
+
/** Load configs from a discovered file. */
|
|
77
|
+
function _discoverConfigFile() {
|
|
78
|
+
_discoverConfigFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
79
|
+
var cwd,
|
|
80
|
+
_iterator,
|
|
81
|
+
_step,
|
|
82
|
+
fileName,
|
|
83
|
+
filePath,
|
|
84
|
+
packageJsonPath,
|
|
85
|
+
pkg,
|
|
86
|
+
_args5 = arguments;
|
|
87
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
88
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
89
|
+
case 0:
|
|
90
|
+
cwd = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : process.cwd();
|
|
91
|
+
_iterator = _createForOfIteratorHelper(CONFIG_FILE_CANDIDATES);
|
|
92
|
+
_context5.prev = 2;
|
|
93
|
+
_iterator.s();
|
|
94
|
+
case 4:
|
|
95
|
+
if ((_step = _iterator.n()).done) {
|
|
96
|
+
_context5.next = 13;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
fileName = _step.value;
|
|
100
|
+
filePath = path.join(cwd, fileName);
|
|
101
|
+
_context5.next = 9;
|
|
102
|
+
return fs.pathExists(filePath);
|
|
103
|
+
case 9:
|
|
104
|
+
if (!_context5.sent) {
|
|
105
|
+
_context5.next = 11;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
return _context5.abrupt("return", {
|
|
109
|
+
filePath: filePath,
|
|
110
|
+
kind: fileName.endsWith(".json") ? "json" : "ts"
|
|
111
|
+
});
|
|
112
|
+
case 11:
|
|
113
|
+
_context5.next = 4;
|
|
114
|
+
break;
|
|
115
|
+
case 13:
|
|
116
|
+
_context5.next = 18;
|
|
117
|
+
break;
|
|
118
|
+
case 15:
|
|
119
|
+
_context5.prev = 15;
|
|
120
|
+
_context5.t0 = _context5["catch"](2);
|
|
121
|
+
_iterator.e(_context5.t0);
|
|
122
|
+
case 18:
|
|
123
|
+
_context5.prev = 18;
|
|
124
|
+
_iterator.f();
|
|
125
|
+
return _context5.finish(18);
|
|
126
|
+
case 21:
|
|
127
|
+
packageJsonPath = path.join(cwd, "package.json");
|
|
128
|
+
_context5.next = 24;
|
|
129
|
+
return fs.pathExists(packageJsonPath);
|
|
130
|
+
case 24:
|
|
131
|
+
if (!_context5.sent) {
|
|
132
|
+
_context5.next = 37;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
_context5.prev = 25;
|
|
136
|
+
_context5.t1 = JSON;
|
|
137
|
+
_context5.next = 29;
|
|
138
|
+
return fs.readFile(packageJsonPath, "utf-8");
|
|
139
|
+
case 29:
|
|
140
|
+
_context5.t2 = _context5.sent;
|
|
141
|
+
pkg = _context5.t1.parse.call(_context5.t1, _context5.t2);
|
|
142
|
+
if (!pkg.apits) {
|
|
143
|
+
_context5.next = 33;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
return _context5.abrupt("return", {
|
|
147
|
+
filePath: packageJsonPath,
|
|
148
|
+
kind: "package"
|
|
149
|
+
});
|
|
150
|
+
case 33:
|
|
151
|
+
_context5.next = 37;
|
|
152
|
+
break;
|
|
153
|
+
case 35:
|
|
154
|
+
_context5.prev = 35;
|
|
155
|
+
_context5.t3 = _context5["catch"](25);
|
|
156
|
+
case 37:
|
|
157
|
+
return _context5.abrupt("return", undefined);
|
|
158
|
+
case 38:
|
|
159
|
+
case "end":
|
|
160
|
+
return _context5.stop();
|
|
161
|
+
}
|
|
162
|
+
}, _callee5, null, [[2, 15, 18, 21], [25, 35]]);
|
|
163
|
+
}));
|
|
164
|
+
return _discoverConfigFile.apply(this, arguments);
|
|
165
|
+
}
|
|
166
|
+
function loadConfigFile(discovered) {
|
|
167
|
+
var filePath = discovered.filePath,
|
|
168
|
+
kind = discovered.kind;
|
|
169
|
+
if (kind === "ts") {
|
|
170
|
+
var _mod$default;
|
|
171
|
+
// tsx is registered at module load time.
|
|
172
|
+
var mod = require(filePath);
|
|
173
|
+
return normalizeConfig((_mod$default = mod.default) !== null && _mod$default !== void 0 ? _mod$default : mod);
|
|
174
|
+
}
|
|
175
|
+
if (kind === "json") {
|
|
176
|
+
return normalizeConfig(JSON.parse(fs.readFileSync(filePath, "utf-8")));
|
|
177
|
+
}
|
|
178
|
+
var pkg = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
179
|
+
return normalizeConfig(pkg.apits);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Build a single ad-hoc config from CLI flags (no config file needed). */
|
|
183
|
+
function buildCliConfig(flags) {
|
|
184
|
+
var _flags$client;
|
|
185
|
+
var config = {
|
|
186
|
+
input: flags.input,
|
|
187
|
+
output: flags.output || "src/api"
|
|
188
|
+
};
|
|
189
|
+
if (flags.name) {
|
|
190
|
+
config.name = flags.name;
|
|
191
|
+
}
|
|
192
|
+
if (flags.baseUrl !== undefined) {
|
|
193
|
+
config.baseURL = flags.baseUrl;
|
|
194
|
+
}
|
|
195
|
+
// CLI mode has no custom import template, so the scaffolded client is on
|
|
196
|
+
// unless `--no-client` was passed. Defaults to true when unspecified.
|
|
197
|
+
config.client = (_flags$client = flags.client) !== null && _flags$client !== void 0 ? _flags$client : true;
|
|
198
|
+
return defineConfig(config)[0];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Ensure every config has a name. Explicit names win; missing names are
|
|
203
|
+
* derived from the input. A duplicated derived name gets an index suffix
|
|
204
|
+
* so generated files never overwrite each other.
|
|
205
|
+
*/
|
|
206
|
+
function assignUniqueNames(configs) {
|
|
207
|
+
var counts = new Map();
|
|
208
|
+
configs.forEach(function (item) {
|
|
209
|
+
var _counts$get;
|
|
210
|
+
var base = item.name || Generator.deriveName(item.input);
|
|
211
|
+
var seen = (_counts$get = counts.get(base)) !== null && _counts$get !== void 0 ? _counts$get : 0;
|
|
212
|
+
counts.set(base, seen + 1);
|
|
213
|
+
if (!item.name) {
|
|
214
|
+
item.name = seen === 0 ? base : "".concat(base, "_").concat(seen + 1);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Resolve the effective config list.
|
|
221
|
+
* Precedence: CLI flags > config file > built-in defaults.
|
|
222
|
+
*/
|
|
223
|
+
function resolveConfigs(_x) {
|
|
224
|
+
return _resolveConfigs.apply(this, arguments);
|
|
225
|
+
}
|
|
226
|
+
/** Print an actionable hint when no input is available. */
|
|
227
|
+
function _resolveConfigs() {
|
|
228
|
+
_resolveConfigs = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(flags) {
|
|
229
|
+
var configs, discovered, _configs;
|
|
230
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
231
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
232
|
+
case 0:
|
|
233
|
+
if (!flags.input) {
|
|
234
|
+
_context6.next = 4;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
configs = [buildCliConfig(flags)];
|
|
238
|
+
assignUniqueNames(configs);
|
|
239
|
+
return _context6.abrupt("return", {
|
|
240
|
+
configs: configs,
|
|
241
|
+
source: "cli"
|
|
242
|
+
});
|
|
243
|
+
case 4:
|
|
244
|
+
_context6.next = 6;
|
|
245
|
+
return discoverConfigFile();
|
|
246
|
+
case 6:
|
|
247
|
+
discovered = _context6.sent;
|
|
248
|
+
if (!discovered) {
|
|
249
|
+
_context6.next = 16;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
_configs = loadConfigFile(discovered); // In config-file mode `--name` filters which source(s) to run.
|
|
253
|
+
if (!flags.name) {
|
|
254
|
+
_context6.next = 13;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
_configs = _configs.filter(function (item) {
|
|
258
|
+
return item.name === flags.name;
|
|
259
|
+
});
|
|
260
|
+
if (!(_configs.length === 0)) {
|
|
261
|
+
_context6.next = 13;
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
throw new Error("No config found with name: ".concat(flags.name));
|
|
265
|
+
case 13:
|
|
266
|
+
// CLI flags override config-file values. `--client` forces true,
|
|
267
|
+
// `--no-client` forces false; omitting the flag keeps the config value.
|
|
268
|
+
_configs = _configs.map(function (item) {
|
|
269
|
+
return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, item), flags.output ? {
|
|
270
|
+
output: flags.output
|
|
271
|
+
} : {}), flags.baseUrl !== undefined ? {
|
|
272
|
+
baseURL: flags.baseUrl
|
|
273
|
+
} : {}), flags.client !== undefined ? {
|
|
274
|
+
client: flags.client
|
|
275
|
+
} : {});
|
|
276
|
+
});
|
|
277
|
+
assignUniqueNames(_configs);
|
|
278
|
+
return _context6.abrupt("return", {
|
|
279
|
+
configs: _configs,
|
|
280
|
+
source: "file",
|
|
281
|
+
configFile: discovered.filePath
|
|
282
|
+
});
|
|
283
|
+
case 16:
|
|
284
|
+
throw new NoInputError();
|
|
285
|
+
case 17:
|
|
286
|
+
case "end":
|
|
287
|
+
return _context6.stop();
|
|
288
|
+
}
|
|
289
|
+
}, _callee6);
|
|
290
|
+
}));
|
|
291
|
+
return _resolveConfigs.apply(this, arguments);
|
|
292
|
+
}
|
|
293
|
+
function printNoInputHint() {
|
|
294
|
+
conso.error(["No OpenAPI input found. Use one of:", "", " 1. Pass document URL or path directly (no config file needed):", " apits gen http://localhost:3041/api-json -o src/api", " apits gen ./openapi.yaml", "", " 2. Generate a config file (multi-source / function-based customization):", " apits init"].join("\n"));
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Scaffold an optional apits.config.ts template. */
|
|
298
|
+
export function genConfig(_x2) {
|
|
299
|
+
return _genConfig.apply(this, arguments);
|
|
300
|
+
}
|
|
301
|
+
function _genConfig() {
|
|
302
|
+
_genConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(prefill) {
|
|
303
|
+
var cwd, configTSFile, answers, configAnswers, nameLine;
|
|
304
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
305
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
306
|
+
case 0:
|
|
307
|
+
cwd = process.cwd();
|
|
308
|
+
configTSFile = path.join(cwd, "apits.config.ts");
|
|
309
|
+
_context7.next = 4;
|
|
310
|
+
return fs.pathExists(configTSFile);
|
|
311
|
+
case 4:
|
|
312
|
+
if (!_context7.sent) {
|
|
313
|
+
_context7.next = 11;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
conso.tips("Config file already exists: ".concat(configTSFile));
|
|
317
|
+
_context7.next = 8;
|
|
318
|
+
return prompt({
|
|
319
|
+
message: "Overwrite existing config file?",
|
|
320
|
+
name: "override",
|
|
321
|
+
type: "confirm"
|
|
322
|
+
});
|
|
323
|
+
case 8:
|
|
324
|
+
answers = _context7.sent;
|
|
325
|
+
if (answers.override) {
|
|
326
|
+
_context7.next = 11;
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
return _context7.abrupt("return");
|
|
330
|
+
case 11:
|
|
331
|
+
_context7.next = 13;
|
|
332
|
+
return prompt([{
|
|
333
|
+
message: "API document URL or local JSON/YAML file path",
|
|
334
|
+
name: "input",
|
|
335
|
+
type: "text",
|
|
336
|
+
initial: (prefill === null || prefill === void 0 ? void 0 : prefill.input) || ""
|
|
337
|
+
}, {
|
|
338
|
+
message: "Generated file name (optional, derived from input if blank)",
|
|
339
|
+
name: "name",
|
|
340
|
+
type: "text",
|
|
341
|
+
initial: ""
|
|
342
|
+
}]);
|
|
343
|
+
case 13:
|
|
344
|
+
configAnswers = _context7.sent;
|
|
345
|
+
if (!(!configAnswers || !configAnswers.input)) {
|
|
346
|
+
_context7.next = 16;
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
return _context7.abrupt("return", conso.tips("Cancelled, config file not written"));
|
|
350
|
+
case 16:
|
|
351
|
+
nameLine = configAnswers !== null && configAnswers !== void 0 && configAnswers.name ? " name: '".concat(configAnswers.name, "',\n") : "";
|
|
352
|
+
_context7.t0 = fs;
|
|
353
|
+
_context7.t1 = configTSFile;
|
|
354
|
+
_context7.next = 21;
|
|
355
|
+
return formatContent(dedent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n import { defineConfig } from '@devbie/apits'\n\n export default defineConfig([{\n input: '", "',\n output: 'src/api',\n ", " baseURL: '[code]:process.env.BASE_API_URL',\n clientImportTemplate: () => \"", "\",\n // Keep the scaffolded request.ts because the import above points at it.\n client: true,\n }])\n "])), (configAnswers === null || configAnswers === void 0 ? void 0 : configAnswers.input) || "", nameLine, "import request from './request'"));
|
|
356
|
+
case 21:
|
|
357
|
+
_context7.t2 = _context7.sent;
|
|
358
|
+
_context7.next = 24;
|
|
359
|
+
return _context7.t0.outputFile.call(_context7.t0, _context7.t1, _context7.t2);
|
|
360
|
+
case 24:
|
|
361
|
+
conso.success("Config file written");
|
|
362
|
+
case 25:
|
|
363
|
+
case "end":
|
|
364
|
+
return _context7.stop();
|
|
365
|
+
}
|
|
366
|
+
}, _callee7);
|
|
367
|
+
}));
|
|
368
|
+
return _genConfig.apply(this, arguments);
|
|
369
|
+
}
|
|
370
|
+
function startGenerate(_x3) {
|
|
371
|
+
return _startGenerate.apply(this, arguments);
|
|
372
|
+
}
|
|
373
|
+
/** Run generation once for every resolved config. */
|
|
374
|
+
function _startGenerate() {
|
|
375
|
+
_startGenerate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(config) {
|
|
376
|
+
var index,
|
|
377
|
+
outputDir,
|
|
378
|
+
label,
|
|
379
|
+
generator,
|
|
380
|
+
output,
|
|
381
|
+
_args8 = arguments;
|
|
382
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
383
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
384
|
+
case 0:
|
|
385
|
+
index = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : 0;
|
|
386
|
+
outputDir = config.output;
|
|
387
|
+
label = chalk.green("".concat(config.input, " elapsed"));
|
|
388
|
+
console.time(label);
|
|
389
|
+
spinnerInstance.start();
|
|
390
|
+
generator = new Generator(config);
|
|
391
|
+
_context8.next = 8;
|
|
392
|
+
return generator.generate();
|
|
393
|
+
case 8:
|
|
394
|
+
output = _context8.sent;
|
|
395
|
+
_context8.next = 11;
|
|
396
|
+
return generator.write(output);
|
|
397
|
+
case 11:
|
|
398
|
+
spinnerInstance.clear();
|
|
399
|
+
conso.log(chalk.yellowBright("\n".concat(index + 1, ".-------------------------")));
|
|
400
|
+
conso.success("Code generated, output: ".concat(outputDir));
|
|
401
|
+
console.timeEnd(label);
|
|
402
|
+
conso.log(chalk.yellowBright("---------------------------\n"));
|
|
403
|
+
return _context8.abrupt("return", true);
|
|
404
|
+
case 17:
|
|
405
|
+
case "end":
|
|
406
|
+
return _context8.stop();
|
|
407
|
+
}
|
|
408
|
+
}, _callee8);
|
|
409
|
+
}));
|
|
410
|
+
return _startGenerate.apply(this, arguments);
|
|
411
|
+
}
|
|
412
|
+
function runAll(_x4) {
|
|
413
|
+
return _runAll.apply(this, arguments);
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Watch local input files and regenerate the affected config on change.
|
|
417
|
+
* Remote inputs are not watchable; a hint is printed instead.
|
|
418
|
+
*/
|
|
419
|
+
function _runAll() {
|
|
420
|
+
_runAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(configs) {
|
|
421
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
422
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
423
|
+
case 0:
|
|
424
|
+
_context10.next = 2;
|
|
425
|
+
return asyncFnArrayOrderRun(configs.map(function (configItem, index) {
|
|
426
|
+
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
|
|
427
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
428
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
429
|
+
case 0:
|
|
430
|
+
_context9.next = 2;
|
|
431
|
+
return startGenerate(configItem, index);
|
|
432
|
+
case 2:
|
|
433
|
+
case "end":
|
|
434
|
+
return _context9.stop();
|
|
435
|
+
}
|
|
436
|
+
}, _callee9);
|
|
437
|
+
}));
|
|
438
|
+
}));
|
|
439
|
+
case 2:
|
|
440
|
+
case "end":
|
|
441
|
+
return _context10.stop();
|
|
442
|
+
}
|
|
443
|
+
}, _callee10);
|
|
444
|
+
}));
|
|
445
|
+
return _runAll.apply(this, arguments);
|
|
446
|
+
}
|
|
447
|
+
function watchConfigs(configs) {
|
|
448
|
+
var localConfigs = configs.filter(function (item) {
|
|
449
|
+
return !isHttpInput(item.input);
|
|
450
|
+
});
|
|
451
|
+
if (localConfigs.length === 0) {
|
|
452
|
+
conso.tips("--watch only supports local files; remote URLs are ignored");
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Simple debounce so editors that save twice only trigger one rebuild.
|
|
457
|
+
var timers = new Map();
|
|
458
|
+
localConfigs.forEach(function (configItem) {
|
|
459
|
+
var filePath = path.resolve(configItem.input);
|
|
460
|
+
fs.watch(filePath, /*#__PURE__*/function () {
|
|
461
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(eventType) {
|
|
462
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
463
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
464
|
+
case 0:
|
|
465
|
+
// Handle both 'change' and 'rename' — macOS atomic save (e.g. vim's
|
|
466
|
+
// write-and-rename) triggers 'rename', and the watcher may need to be
|
|
467
|
+
// re-established after the inode changes. Re-create the watcher on
|
|
468
|
+
// rename to avoid silently losing subsequent changes.
|
|
469
|
+
if (eventType === "rename") {
|
|
470
|
+
try {
|
|
471
|
+
fs.watch(filePath, /*#__PURE__*/function () {
|
|
472
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ev) {
|
|
473
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
474
|
+
while (1) switch (_context.prev = _context.next) {
|
|
475
|
+
case 0:
|
|
476
|
+
scheduleRebuild(filePath, ev);
|
|
477
|
+
case 1:
|
|
478
|
+
case "end":
|
|
479
|
+
return _context.stop();
|
|
480
|
+
}
|
|
481
|
+
}, _callee);
|
|
482
|
+
}));
|
|
483
|
+
return function (_x6) {
|
|
484
|
+
return _ref2.apply(this, arguments);
|
|
485
|
+
};
|
|
486
|
+
}());
|
|
487
|
+
} catch (_unused) {
|
|
488
|
+
// File may not exist yet during the rename window; the next save
|
|
489
|
+
// will re-establish the watcher.
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
scheduleRebuild(filePath, eventType);
|
|
493
|
+
case 2:
|
|
494
|
+
case "end":
|
|
495
|
+
return _context2.stop();
|
|
496
|
+
}
|
|
497
|
+
}, _callee2);
|
|
498
|
+
}));
|
|
499
|
+
return function (_x5) {
|
|
500
|
+
return _ref.apply(this, arguments);
|
|
501
|
+
};
|
|
502
|
+
}());
|
|
503
|
+
});
|
|
504
|
+
function scheduleRebuild(filePath, eventType) {
|
|
505
|
+
if (eventType !== "change" && eventType !== "rename") return;
|
|
506
|
+
var oldTimer = timers.get(filePath);
|
|
507
|
+
if (oldTimer) clearTimeout(oldTimer);
|
|
508
|
+
timers.set(filePath, setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
509
|
+
var configItem, idx;
|
|
510
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
511
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
512
|
+
case 0:
|
|
513
|
+
configItem = localConfigs.find(function (c) {
|
|
514
|
+
return path.resolve(c.input) === filePath;
|
|
515
|
+
});
|
|
516
|
+
if (configItem) {
|
|
517
|
+
_context3.next = 3;
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
return _context3.abrupt("return");
|
|
521
|
+
case 3:
|
|
522
|
+
idx = localConfigs.indexOf(configItem);
|
|
523
|
+
conso.tips("Detected change, regenerating: ".concat(filePath));
|
|
524
|
+
_context3.prev = 5;
|
|
525
|
+
_context3.next = 8;
|
|
526
|
+
return startGenerate(configItem, idx);
|
|
527
|
+
case 8:
|
|
528
|
+
_context3.next = 13;
|
|
529
|
+
break;
|
|
530
|
+
case 10:
|
|
531
|
+
_context3.prev = 10;
|
|
532
|
+
_context3.t0 = _context3["catch"](5);
|
|
533
|
+
conso.error("Regeneration failed: ".concat(_context3.t0.message || _context3.t0));
|
|
534
|
+
case 13:
|
|
535
|
+
case "end":
|
|
536
|
+
return _context3.stop();
|
|
537
|
+
}
|
|
538
|
+
}, _callee3, null, [[5, 10]]);
|
|
539
|
+
})), 200));
|
|
540
|
+
}
|
|
541
|
+
conso.tips("Watching ".concat(localConfigs.length, " local document(s), Ctrl+C to exit"));
|
|
542
|
+
}
|
|
543
|
+
export function start() {
|
|
544
|
+
return _start.apply(this, arguments);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** Map parsed yargs argv into GenFlags. */
|
|
548
|
+
function _start() {
|
|
549
|
+
_start = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
|
|
550
|
+
var flags,
|
|
551
|
+
timeLabel,
|
|
552
|
+
resolved,
|
|
553
|
+
_args11 = arguments;
|
|
554
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
555
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
556
|
+
case 0:
|
|
557
|
+
flags = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : {};
|
|
558
|
+
timeLabel = chalk.green("Total elapsed");
|
|
559
|
+
console.time(timeLabel);
|
|
560
|
+
_context11.prev = 3;
|
|
561
|
+
_context11.next = 6;
|
|
562
|
+
return resolveConfigs(flags);
|
|
563
|
+
case 6:
|
|
564
|
+
resolved = _context11.sent;
|
|
565
|
+
_context11.next = 17;
|
|
566
|
+
break;
|
|
567
|
+
case 9:
|
|
568
|
+
_context11.prev = 9;
|
|
569
|
+
_context11.t0 = _context11["catch"](3);
|
|
570
|
+
if (!(_context11.t0 instanceof NoInputError)) {
|
|
571
|
+
_context11.next = 14;
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
printNoInputHint();
|
|
575
|
+
return _context11.abrupt("return");
|
|
576
|
+
case 14:
|
|
577
|
+
spinnerInstance.stop();
|
|
578
|
+
conso.error("Config resolution failed: ".concat(_context11.t0.message || _context11.t0));
|
|
579
|
+
return _context11.abrupt("return");
|
|
580
|
+
case 17:
|
|
581
|
+
conso.tips(resolved.source === "cli" ? "Using CLI input: ".concat(flags.input) : "Found config file: ".concat(resolved.configFile));
|
|
582
|
+
_context11.prev = 18;
|
|
583
|
+
spinnerInstance.start("Generating code...\n");
|
|
584
|
+
_context11.next = 22;
|
|
585
|
+
return runAll(resolved.configs);
|
|
586
|
+
case 22:
|
|
587
|
+
spinnerInstance.stop();
|
|
588
|
+
_context11.next = 33;
|
|
589
|
+
break;
|
|
590
|
+
case 25:
|
|
591
|
+
_context11.prev = 25;
|
|
592
|
+
_context11.t1 = _context11["catch"](18);
|
|
593
|
+
spinnerInstance.stop();
|
|
594
|
+
conso.error("Execution failed:");
|
|
595
|
+
conso.error("Error: ".concat(_context11.t1.message || _context11.t1));
|
|
596
|
+
if (_context11.t1.stack) {
|
|
597
|
+
conso.error("Stack: ".concat(_context11.t1.stack));
|
|
598
|
+
}
|
|
599
|
+
if (_context11.t1.cause) {
|
|
600
|
+
conso.error("Cause: ".concat(_context11.t1.cause));
|
|
601
|
+
}
|
|
602
|
+
return _context11.abrupt("return", conso.error("Code generation failed, see error details above"));
|
|
603
|
+
case 33:
|
|
604
|
+
console.timeEnd(timeLabel);
|
|
605
|
+
if (flags.watch) {
|
|
606
|
+
watchConfigs(resolved.configs);
|
|
607
|
+
}
|
|
608
|
+
return _context11.abrupt("return", null);
|
|
609
|
+
case 36:
|
|
610
|
+
case "end":
|
|
611
|
+
return _context11.stop();
|
|
612
|
+
}
|
|
613
|
+
}, _callee11, null, [[3, 9], [18, 25]]);
|
|
614
|
+
}));
|
|
615
|
+
return _start.apply(this, arguments);
|
|
616
|
+
}
|
|
617
|
+
function toFlags(argv) {
|
|
618
|
+
return {
|
|
619
|
+
input: typeof argv.input === "string" && argv.input ? argv.input : undefined,
|
|
620
|
+
output: typeof argv.output === "string" ? argv.output : undefined,
|
|
621
|
+
name: typeof argv.name === "string" ? argv.name : undefined,
|
|
622
|
+
baseUrl: typeof argv.baseUrl === "string" ? argv.baseUrl : undefined,
|
|
623
|
+
client: typeof argv.client === "boolean" ? argv.client : undefined,
|
|
624
|
+
watch: Boolean(argv.watch)
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
var CLI = /*#__PURE__*/function () {
|
|
628
|
+
function CLI() {
|
|
629
|
+
_classCallCheck(this, CLI);
|
|
630
|
+
}
|
|
631
|
+
_createClass(CLI, [{
|
|
632
|
+
key: "run",
|
|
633
|
+
value: function run(args) {
|
|
634
|
+
var cli = this.init();
|
|
635
|
+
if (args.length === 0) {
|
|
636
|
+
cli.showHelp();
|
|
637
|
+
}
|
|
638
|
+
return cli.parse(args);
|
|
639
|
+
}
|
|
640
|
+
}, {
|
|
641
|
+
key: "genBuilder",
|
|
642
|
+
value: function genBuilder(y) {
|
|
643
|
+
return y.positional("input", {
|
|
644
|
+
type: "string",
|
|
645
|
+
describe: "OpenAPI document URL or local JSON/YAML file path"
|
|
646
|
+
}).option("output", {
|
|
647
|
+
alias: "o",
|
|
648
|
+
type: "string",
|
|
649
|
+
describe: "Output directory (default: src/api)"
|
|
650
|
+
}).option("name", {
|
|
651
|
+
alias: "n",
|
|
652
|
+
type: "string",
|
|
653
|
+
describe: "Generated file name; filters by name in config-file mode"
|
|
654
|
+
}).option("base-url", {
|
|
655
|
+
type: "string",
|
|
656
|
+
describe: "Runtime baseURL (supports [code]: prefix for code emission)"
|
|
657
|
+
}).option("client", {
|
|
658
|
+
type: "boolean",
|
|
659
|
+
describe: "Generate default request.ts (use --no-client to disable, --client to force-enable)"
|
|
660
|
+
}).option("watch", {
|
|
661
|
+
alias: "w",
|
|
662
|
+
type: "boolean",
|
|
663
|
+
default: false,
|
|
664
|
+
describe: "Watch local document for changes and auto-regenerate"
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}, {
|
|
668
|
+
key: "init",
|
|
669
|
+
value: function init() {
|
|
670
|
+
var _this = this;
|
|
671
|
+
return yargs.scriptName("apits").usage("Usage: $0 [gen] [input] [options]")
|
|
672
|
+
// `init` must be declared before the default command so it is not
|
|
673
|
+
// captured as the `input` positional.
|
|
674
|
+
.command("init [input]", "Scaffold an optional apits.config.ts file", function (y) {
|
|
675
|
+
y.positional("input", {
|
|
676
|
+
type: "string",
|
|
677
|
+
describe: "Prefilled document URL or local file path"
|
|
678
|
+
});
|
|
679
|
+
}, /*#__PURE__*/function () {
|
|
680
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(argv) {
|
|
681
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
682
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
683
|
+
case 0:
|
|
684
|
+
_context4.next = 2;
|
|
685
|
+
return genConfig({
|
|
686
|
+
input: typeof argv.input === "string" ? argv.input : undefined
|
|
687
|
+
});
|
|
688
|
+
case 2:
|
|
689
|
+
case "end":
|
|
690
|
+
return _context4.stop();
|
|
691
|
+
}
|
|
692
|
+
}, _callee4);
|
|
693
|
+
}));
|
|
694
|
+
return function (_x7) {
|
|
695
|
+
return _ref4.apply(this, arguments);
|
|
696
|
+
};
|
|
697
|
+
}())
|
|
698
|
+
// `gen` is also the default command: `apits <input>` works.
|
|
699
|
+
// The default-command alias is hidden from help to keep it clean.
|
|
700
|
+
.command("gen [input]", "Generate TypeScript types and request functions from an OpenAPI document", function (y) {
|
|
701
|
+
return _this.genBuilder(y);
|
|
702
|
+
}, function (argv) {
|
|
703
|
+
start(toFlags(argv));
|
|
704
|
+
}).command("$0 [input]", false, function (y) {
|
|
705
|
+
return _this.genBuilder(y);
|
|
706
|
+
}, function (argv) {
|
|
707
|
+
start(toFlags(argv));
|
|
708
|
+
}).help();
|
|
709
|
+
}
|
|
710
|
+
}]);
|
|
711
|
+
return CLI;
|
|
712
|
+
}();
|
|
713
|
+
export { CLI as default };
|