152 lines
4.2 KiB
JavaScript
Raw Normal View History

'use strict';
const path = require('path');
2021-09-02 11:25:24 +02:00
const fs = require('fs-extra');
const _ = require('lodash');
const { getAbsoluteServerUrl } = require('@strapi/utils');
2021-09-02 11:25:24 +02:00
const { builApiEndpointPath } = require('../utils/builders');
const defaultConfig = require('../config/default-config');
module.exports = ({ strapi }) => {
const config = strapi.config.get('plugin.documentation');
2021-09-02 11:25:24 +02:00
return {
getDocumentationVersion() {
return _.get(config, 'info.version');
2021-09-02 11:25:24 +02:00
},
2021-09-02 11:25:24 +02:00
getFullDocumentationPath() {
return path.join(strapi.dirs.extensions, 'documentation', 'documentation');
2021-09-02 11:25:24 +02:00
},
2021-09-02 11:25:24 +02:00
getDocumentationVersions() {
return fs
.readdirSync(this.getFullDocumentationPath())
.map(version => {
try {
2021-09-02 11:25:24 +02:00
const doc = JSON.parse(
fs.readFileSync(
path.resolve(this.getFullDocumentationPath(), version, 'full_documentation.json')
)
);
2021-09-02 11:25:24 +02:00
const generatedDate = _.get(doc, ['info', 'x-generation-date'], null);
2021-09-02 11:25:24 +02:00
return { version, generatedDate, url: '' };
} catch (err) {
2021-09-02 11:25:24 +02:00
return null;
}
2021-09-02 11:25:24 +02:00
})
.filter(x => x);
},
/**
* Returns settings stored in core-store
*/
async getDocumentationAccess() {
const { restrictedAccess } = await strapi
2021-09-02 11:25:24 +02:00
.store({
environment: '',
type: 'plugin',
name: 'documentation',
key: 'config',
})
.get();
return { restrictedAccess };
2021-09-02 11:25:24 +02:00
},
/**
* @description - Gets the path for an api or plugin
*
* @param {object} api
* @property {string} api.name - Name of the api
* @property {string} api.getter - api | plugin
*
* @returns path to the api | plugin
*/
getApiDocumentationPath(api) {
if (api.getter === 'plugin') {
return path.join(strapi.dirs.extensions, api.name, 'documentation');
2021-09-02 11:25:24 +02:00
}
return path.join(strapi.dirs.api, api.name, 'documentation');
2021-09-02 11:25:24 +02:00
},
async deleteDocumentation(version) {
const apis = this.getPluginAndApiInfo();
for (const api of apis) {
await fs.remove(path.join(this.getApiDocumentationPath(api), version));
}
await fs.remove(path.join(this.getFullDocumentationPath(), version));
},
getPluginAndApiInfo() {
const plugins = _.get(config, 'x-strapi-config.plugins');
2021-09-02 11:25:24 +02:00
const pluginsToDocument = plugins.map(plugin => {
return {
name: plugin,
getter: 'plugin',
ctNames: Object.keys(strapi.plugin(plugin).contentTypes),
};
});
2021-09-02 11:25:24 +02:00
const apisToDocument = Object.keys(strapi.api).map(api => {
return {
name: api,
getter: 'api',
ctNames: Object.keys(strapi.api[api].contentTypes),
};
});
2021-09-02 11:25:24 +02:00
return [...apisToDocument, ...pluginsToDocument];
},
/**
* @description - Creates the Swagger json files
*/
async generateFullDoc(version = this.getDocumentationVersion()) {
2021-09-02 11:25:24 +02:00
let paths = {};
const apis = this.getPluginAndApiInfo();
for (const api of apis) {
const apiName = api.name;
const apiDirPath = path.join(this.getApiDocumentationPath(api), version);
2021-09-02 11:25:24 +02:00
const apiDocPath = path.join(apiDirPath, `${apiName}.json`);
2021-09-02 11:25:24 +02:00
await fs.ensureFile(apiDocPath);
const apiPathsObject = builApiEndpointPath(api);
2021-09-02 11:25:24 +02:00
await fs.writeJson(apiDocPath, apiPathsObject, { spaces: 2 });
paths = { ...paths, ...apiPathsObject.paths };
}
2021-09-02 11:25:24 +02:00
const fullDocJsonPath = path.join(
this.getFullDocumentationPath(),
version,
2021-09-02 11:25:24 +02:00
'full_documentation.json'
);
2021-10-05 14:46:25 -04:00
const settings = _.cloneDeep(defaultConfig);
const serverUrl = getAbsoluteServerUrl(strapi.config);
const apiPath = strapi.config.get('api.rest.prefix');
_.set(settings, 'servers', [
{
url: `${serverUrl}${apiPath}`,
description: 'Development server',
},
]);
_.set(settings, ['info', 'x-generation-date'], new Date().toISOString());
_.set(settings, ['info', 'version'], version);
2021-10-05 14:46:25 -04:00
2021-09-02 11:25:24 +02:00
await fs.ensureFile(fullDocJsonPath);
2021-10-05 14:46:25 -04:00
await fs.writeJson(fullDocJsonPath, { ...settings, paths }, { spaces: 2 });
2021-09-02 11:25:24 +02:00
},
};
};