UNPKG

@strapi/strapi

Version:

An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite

83 lines (76 loc) 3.02 kB
'use strict'; var core = require('@strapi/core'); var openapi = require('@strapi/openapi'); var chalk = require('chalk'); var fse = require('fs-extra'); var path = require('path'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var openapi__namespace = /*#__PURE__*/_interopNamespace(openapi); var chalk__default = /*#__PURE__*/_interopDefault(chalk); var fse__default = /*#__PURE__*/_interopDefault(fse); var path__default = /*#__PURE__*/_interopDefault(path); const DEFAULT_OUTPUT = path__default.default.join(process.cwd(), 'specification.json'); const EXPERIMENTAL_MSG = chalk__default.default.yellow(` ⚠️ The OpenAPI generation feature is currently experimental. Its behavior and output might change in future releases without following semver. Please report any issues you encounter on https://github.com/strapi/strapi/issues/new?template=BUG_REPORT.yml. `); /** * @experimental */ const action = async (options)=>{ console.warn(EXPERIMENTAL_MSG); const filePath = options.output ?? DEFAULT_OUTPUT; const app = await createStrapiApp(); const { document, durationMs } = openapi__namespace.generate(app, { type: 'content-api' }); writeDocumentToFile(document, filePath); summarize(app, durationMs, filePath); await teardownStrapiApp(app); }; const createStrapiApp = async ()=>{ const appContext = await core.compileStrapi(); const app = core.createStrapi(appContext); // Make sure to not log Strapi debug info app.log.level = 'error'; // Load internals await app.load(); // Make sure the routes are mounted before generating the specification app.server.mount(); return app; }; const writeDocumentToFile = (document, filePath)=>{ fse__default.default.outputFileSync(filePath, JSON.stringify(document, null, 2)); }; const teardownStrapiApp = async (app)=>{ await app.destroy(); }; const summarize = (app, durationMs, filePath)=>{ const cwd = process.cwd(); const relativeFilePath = path__default.default.relative(cwd, filePath); const { name, version } = app.config.get('info'); const fName = chalk__default.default.cyan(name); const fVersion = chalk__default.default.cyan(`v${version}`); const fFile = chalk__default.default.magenta(relativeFilePath); const fTime = chalk__default.default.green(`${durationMs}ms`); console.log(chalk__default.default.bold(`Generated an OpenAPI specification for "${fName} ${fVersion}" at ${fFile} in ${fTime}`)); }; exports.action = action; //# sourceMappingURL=generate.js.map