@starduv/lil-schemy
Version:
Lil' Schemy is a cli tool that enables code first schema generation from TypeScript
71 lines (70 loc) • 3.78 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = void 0;
const commander_1 = require("commander");
const path_1 = __importDefault(require("path"));
const generator_1 = require("../generator");
const utils_1 = require("../utils");
const generate = (cwd, options) => {
var _a, _b, _c;
const { openApi } = options;
const files = (0, utils_1.getRootFiles)(cwd, (_a = openApi === null || openApi === void 0 ? void 0 : openApi.entry) !== null && _a !== void 0 ? _a : []);
console.debug("Searching for api paths in files %o", files);
const result = (0, generator_1.generateSchemas)({
openApi: {
base: JSON.stringify((_b = openApi === null || openApi === void 0 ? void 0 : openApi.base) !== null && _b !== void 0 ? _b : {}),
entry: files,
output: openApi === null || openApi === void 0 ? void 0 : openApi.output
}
});
if ((_c = result.openApi) === null || _c === void 0 ? void 0 : _c.filepath) {
console.info("OpenApi schema written to %s", result.openApi.filepath);
}
return result;
};
exports.generate = generate;
exports.default = new commander_1.Command('generate')
.description('Generate one or more schemas')
.option('-c, --config <config>', 'configuration module', 'schemy-config')
.action((_, command) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
var _b, _c;
let parentOptions = (_b = command.parent) === null || _b === void 0 ? void 0 : _b.opts();
const config = yield (_a = path_1.default.resolve(parentOptions === null || parentOptions === void 0 ? void 0 : parentOptions.cwd, command.getOptionValue('config')), Promise.resolve().then(() => __importStar(require(_a))));
(0, exports.generate)(parentOptions === null || parentOptions === void 0 ? void 0 : parentOptions.cwd, (_c = config.default) !== null && _c !== void 0 ? _c : config);
}));