2021-10-14 10:54:54 -04:00

158 lines
4.3 KiB
JavaScript
Executable File

'use strict';
const path = require('path');
const fs = require('fs-extra');
const _ = require('lodash');
const moment = require('moment');
const { builApiEndpointPath } = require('../utils/builders');
const defaultConfig = require('../config/default-config');
module.exports = () => {
const docPlugin = strapi.plugin('documentation');
return {
getMergedDocumentationPath(version = this.getDocumentationVersion()) {
return path.join(
strapi.config.appPath,
'src',
'extensions',
'documentation',
'documentation',
version
);
},
getDocumentationVersion() {
return docPlugin.config('info.version');
},
getFullDocumentationPath() {
return path.join(
strapi.config.appPath,
'src',
'extensions',
'documentation',
'documentation'
);
},
getDocumentationVersions() {
return fs
.readdirSync(this.getFullDocumentationPath())
.map(version => {
try {
const doc = JSON.parse(
fs.readFileSync(
path.resolve(this.getFullDocumentationPath(), version, 'full_documentation.json')
)
);
const generatedDate = _.get(doc, ['info', 'x-generation-date'], null);
return { version, generatedDate, url: '' };
} catch (err) {
return null;
}
})
.filter(x => x);
},
async getDocumentationAccess() {
const config = await strapi
.store({
environment: '',
type: 'plugin',
name: 'documentation',
key: 'config',
})
.get();
return { restrictedAccess: config.restrictedAccess, password: config.password || '' };
},
/**
* @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.config.appPath, 'src', 'extensions', api.name, 'documentation');
}
return path.join(strapi.config.appPath, 'src', 'api', api.name, 'documentation');
},
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 = docPlugin.config('x-strapi-config.plugins');
const pluginsToDocument = plugins.map(plugin => {
return {
name: plugin,
getter: 'plugin',
ctNames: Object.keys(strapi.plugin(plugin).contentTypes),
};
});
const apisToDocument = Object.keys(strapi.api).map(api => {
return {
name: api,
getter: 'api',
ctNames: Object.keys(strapi.api[api].contentTypes),
};
});
return [...apisToDocument, ...pluginsToDocument];
},
/**
* @description - Creates the Swagger json files
*/
async generateFullDoc() {
let paths = {};
const apis = this.getPluginAndApiInfo();
for (const api of apis) {
const apiName = api.name;
const apiDirPath = path.join(
this.getApiDocumentationPath(api),
this.getDocumentationVersion()
);
const apiDocPath = path.join(apiDirPath, `${apiName}.json`);
await fs.ensureFile(apiDocPath);
const apiPathsObject = builApiEndpointPath(api);
await fs.writeJson(apiDocPath, apiPathsObject, { spaces: 2 });
paths = { ...paths, ...apiPathsObject.paths };
}
const fullDocJsonPath = path.join(
this.getFullDocumentationPath(),
this.getDocumentationVersion(),
'full_documentation.json'
);
const settings = _.cloneDeep(defaultConfig);
_.set(settings, ['info', 'x-generation-date'], moment().format('L LTS'));
_.set(settings, ['info', 'version'], this.getDocumentationVersion());
await fs.ensureFile(fullDocJsonPath);
await fs.writeJson(fullDocJsonPath, { ...settings, paths }, { spaces: 2 });
},
};
};