Skip to content

Commit

Permalink
Merge pull request #1786 from qdraw/feature/202410_code_smells_25
Browse files Browse the repository at this point in the history
Feature/202410 code smells 25
  • Loading branch information
qdraw authored Oct 25, 2024
2 parents 967ff2c + d3e93d6 commit e398c20
Show file tree
Hide file tree
Showing 16 changed files with 748 additions and 687 deletions.
151 changes: 75 additions & 76 deletions documentation/scripts/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,117 +14,116 @@ const fs = require('fs');
const swagger = require('../static/openapi/openapi.json');

function parseSwagger() {
let output = "";
let output = "";

const pathLen = 50;
const operationLen = 6;
const summaryLen = 80;
const wordBoundCorrection = 25;
const pathLen = 50;
const operationLen = 6;
const summaryLen = 80;
const wordBoundCorrection = 25;

output += `| Path${' '.repeat(pathLen -4)}| Type | Description ${' '.repeat(summaryLen -12)}| \r\n`;
output += "|" + '-'.repeat(pathLen +1) + "|" + '-'.repeat(operationLen +1) + "|" + '-'.repeat(summaryLen+1) + "|\r\n";
output += `| Path${' '.repeat(pathLen - 4)}| Type | Description ${' '.repeat(summaryLen - 12)}| \r\n`;
output += "|" + '-'.repeat(pathLen + 1) + "|" + '-'.repeat(operationLen + 1) + "|" + '-'.repeat(summaryLen + 1) + "|\r\n";


for (var path in swagger.paths) {
for (const path in swagger.paths) {

const pathObject = swagger.paths[path]
const pathObject = swagger.paths[path]


for (var operation in pathObject.operations) {
for (const operation in pathObject.operations) {

if (operation === "Head") {
continue;
}
if (operation === "Head") {
continue;
}

let pathContent = `__${path}__`
// for mdx markdown
pathContent = pathContent.replace(/{/ig, "\\{").replace(/}/ig, "\\}");
let pathContent = `__${path}__`
// for mdx markdown
pathContent = pathContent.replace(/{/ig, "\\{").replace(/}/ig, "\\}");

const rightPathSpace = ' '.repeat(pathLen - pathContent.length)
const rightOperationSpace = ' '.repeat(operationLen - operation.length)
const rightPathSpace = ' '.repeat(pathLen - pathContent.length)
const rightOperationSpace = ' '.repeat(operationLen - operation.length)

let summary = "Missing summary"
if (pathObject.operations[operation].summary) {
summary = pathObject.operations[operation].summary.replace(/(\n|\r\n)/ig,"");
}
let summary = "Missing summary"
if (pathObject.operations[operation].summary) {
summary = pathObject.operations[operation].summary.replace(/(\n|\r\n)/ig, "");
}

const trimmedSummary = trimString(summary, summaryLen);
const rightSummarySpace = ' '.repeat(summaryLen - trimmedSummary.length);
const trimmedSummary = trimString(summary, summaryLen);
const rightSummarySpace = ' '.repeat(summaryLen - trimmedSummary.length);

output += `| ${pathContent}${rightPathSpace}| ${operation.toUpperCase()}${rightOperationSpace}` +
`| ${trimmedSummary}${rightSummarySpace}|\r\n`;
output += `| ${pathContent}${rightPathSpace}| ${operation.toUpperCase()}${rightOperationSpace}` +
`| ${trimmedSummary}${rightSummarySpace}|\r\n`;


let parametersDefaultValue = 'Parameters: '
let parametersContent = parametersDefaultValue;
for (const parameterIndex in pathObject.operations[operation].parameters) {
const parameter = pathObject.operations[operation].parameters[parameterIndex];

let parametersDefaultValue = 'Parameters: '
let parametersContent = parametersDefaultValue;
for (const parameterIndex in pathObject.operations[operation].parameters) {
const parameter = pathObject.operations[operation].parameters[parameterIndex];
parametersContent += `${parameter.name}`;
if (parameter.description) {
parametersContent += ` (${parameter.description})`
}
if (parameterIndex != pathObject.operations[operation].parameters.length - 1) {
parametersContent += ", "
}
}

parametersContent += `${parameter.name}`;
if (parameter.description) {
parametersContent += ` (${parameter.description})`
}
if (parameterIndex != pathObject.operations[operation].parameters.length-1 ) {
parametersContent += ", "
}
}
if (parametersContent && parametersContent !== parametersDefaultValue) {

if (parametersContent && parametersContent !== parametersDefaultValue) {
const regex = new RegExp(`(?!\\s).{${pathLen + operationLen + summaryLen - wordBoundCorrection},}?(?=\\s|$)`, "g");
const matches = parametersContent.match(regex);

const regex = new RegExp(`(?!\\s).{${pathLen + operationLen + summaryLen - wordBoundCorrection},}?(?=\\s|$)`, "g");
const matches = parametersContent.match(regex);
if (matches) {
let parameterOutputDescription = "";
for (const splitedContent of matches) {
const value = (pathLen + operationLen + summaryLen) - splitedContent.length;

if (matches) {
let parameterOutputDescription = "";
for (const splitedContent of matches) {
const value = (pathLen + operationLen + summaryLen) - splitedContent.length;
const rightParameterSpace = ' '.repeat(value);
parameterOutputDescription += `| _${splitedContent}${rightParameterSpace} _ |\r\n`;
}

const rightParameterSpace = ' '.repeat(value);
parameterOutputDescription += `| _${splitedContent}${rightParameterSpace} _ |\r\n`;
}
if (matches.length >= 1) {
const lastContentInMatch = matches[matches.length - 1];
let index = parametersContent.indexOf(lastContentInMatch) + lastContentInMatch.length;
const splitedContent = parametersContent.substring(index, parametersContent.length);
if (splitedContent.length >= 1) {
const value = (pathLen + operationLen + summaryLen) - splitedContent.length;
const rightParameterSpace = ' '.repeat(value);
parameterOutputDescription += `| _${splitedContent}${rightParameterSpace} _ |\r\n`;
}

if (matches.length >= 1) {
const lastContentInMatch = matches[matches.length-1];
let index = parametersContent.indexOf(lastContentInMatch) + lastContentInMatch.length;
const splitedContent = parametersContent.substring(index , parametersContent.length);
if (splitedContent.length >= 1) {
const value = (pathLen + operationLen + summaryLen) - splitedContent.length;
const rightParameterSpace = ' '.repeat(value);
parameterOutputDescription += `| _${splitedContent}${rightParameterSpace} _ |\r\n`;
}
}

}

output += parameterOutputDescription;
}
}
}
}
return output;
output += parameterOutputDescription;
}
}
}
}
return output;
}

function trimString(string, length) {
return string.length > length ? string.substring(0, length - 3) + "..." : string;
return string.length > length ? string.substring(0, length - 3) + "..." : string;
}


function parseAndWrite(showLog = false) {
const output = parseSwagger();
const output = parseSwagger();

let apiOutputReadme = `---\nsidebar_position: 6\n---\n\n# API Endpoint Documentation\nThe API has two ways of authentication using Cookie Authentication via the \`/api/account/login\` endpoint and Basic Authentication\n`;
apiOutputReadme += "\nThis document is auto generated";
apiOutputReadme += `\n\n${output}`;
if (showLog) {
console.log(apiOutputReadme);
}
let apiOutputReadme = `---\nsidebar_position: 6\n---\n\n# API Endpoint Documentation\nThe API has two ways of authentication using Cookie Authentication via the \`/api/account/login\` endpoint and Basic Authentication\n`;
apiOutputReadme += "\nThis document is auto generated";
apiOutputReadme += `\n\n${output}`;
if (showLog) {
console.log(apiOutputReadme);
}

fs.writeFileSync('docs/developer-guide/api/readme.md', apiOutputReadme, 'utf8');
fs.writeFileSync('docs/developer-guide/api/readme.md', apiOutputReadme, 'utf8');
}

if (require.main === module) {
parseAndWrite(true);
parseAndWrite(true);
}

module.exports = { parseAndWrite };
module.exports = {parseAndWrite};

97 changes: 50 additions & 47 deletions documentation/scripts/postbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require("fs");
const path = require("path");
const { spawnSync } = require('child_process');
const {spawnSync} = require('child_process');

const sourceHtaccess = path.join(__dirname, "..", ".htaccess");
const toHtaccess = path.join(__dirname, "..", "build", ".htaccess");
Expand All @@ -17,23 +17,23 @@ console.log(`${sourceFavicon} -> ${toFavicon}`);
fs.copyFileSync(sourceFavicon, toFavicon);

if (process.env.GOOGLE_VERIFICATION) {
const googleVerficationPath = path.join(__dirname, "..", "build", process.env.GOOGLE_VERIFICATION + ".html");
console.log("process.env.GOOGLE_VERIFICATION " + process.env.GOOGLE_VERIFICATION);
fs.writeFileSync(googleVerficationPath, 'google-site-verification: ' + process.env.GOOGLE_VERIFICATION + ".html");
const googleVerficationPath = path.join(__dirname, "..", "build", process.env.GOOGLE_VERIFICATION + ".html");
console.log("process.env.GOOGLE_VERIFICATION " + process.env.GOOGLE_VERIFICATION);
fs.writeFileSync(googleVerficationPath, 'google-site-verification: ' + process.env.GOOGLE_VERIFICATION + ".html");
}

function copyDir(src, dest) {
fs.mkdirSync(dest, { recursive: true });
let entries = fs.readdirSync(src, { withFileTypes: true });
fs.mkdirSync(dest, {recursive: true});
let entries = fs.readdirSync(src, {withFileTypes: true});

for (let entry of entries) {
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);
for (let entry of entries) {
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);

entry.isDirectory() ?
copyDir(srcPath, destPath) :
fs.copyFileSync(srcPath, destPath);
}
entry.isDirectory() ?
copyDir(srcPath, destPath) :
fs.copyFileSync(srcPath, destPath);
}
}

const sourceLegalFolder = path.join(__dirname, "..", "..", "starsky", "starsky", "wwwroot", "legal");
Expand All @@ -44,16 +44,16 @@ copyDir(sourceLegalFolder, toLegalFolder);


const gitHash = spawnSync("git", ["log", "-1", "--format=\"%H\""], {
cwd: __dirname,
env: process.env,
encoding: "utf-8"
cwd: __dirname,
env: process.env,
encoding: "utf-8"
});

if (!gitHash.stdout) {
return;
return;
}

const hash = gitHash.stdout.replace(/\"|\n/ig, "")
const hash = gitHash.stdout.replace(/"|\n/ig, "")

const versionTextPath = path.join(__dirname, "..", "build", "version.txt");

Expand All @@ -65,44 +65,47 @@ fs.writeFileSync(versionTextPath, hash)
const documentationDirectory = path.join(__dirname, ".."); // /git/starsky/documentation - without docs

function readFile(rootPath, from) {
const filename = path.join(rootPath, from);
if (fs.existsSync(filename) === false) {
return null;
}
return fs.readFileSync(filename, { encoding: "utf8" });
const filename = path.join(rootPath, from);
if (fs.existsSync(filename) === false) {
return null;
}
return fs.readFileSync(filename, {encoding: "utf8"});
}

function replaceRobotsTxt() {
let robots = readFile(documentationDirectory, path.join("static", "robots.template"));
console.log(robots.length >= 1 ? "robots.template contains content" : "robots.template is empty");

robots = robots.replace(/\{date\}/g, new Date().toLocaleDateString('en-UK', { year: 'numeric', month: 'long', day: 'numeric' }));

if (process.env.DOCS_URL) {
robots = robots.replace(/\{domain\}/g, process.env.DOCS_URL);
}
else {
console.error("env: DOCS_URL is not set, so skipping robots.txt");
return;
}

fs.mkdirSync(path.join(documentationDirectory, "build"), { recursive: true });
const filename = path.join(documentationDirectory, "build", "robots.txt");
console.log(filename);
fs.writeFileSync(filename, robots);
console.log(`${filename} generated`);
let robots = readFile(documentationDirectory, path.join("static", "robots.template"));
console.log(robots.length >= 1 ? "robots.template contains content" : "robots.template is empty");

robots = robots.replace(/\{date\}/g, new Date().toLocaleDateString('en-UK', {
year: 'numeric',
month: 'long',
day: 'numeric'
}));

if (process.env.DOCS_URL) {
robots = robots.replace(/\{domain\}/g, process.env.DOCS_URL);
} else {
console.error("env: DOCS_URL is not set, so skipping robots.txt");
return;
}

fs.mkdirSync(path.join(documentationDirectory, "build"), {recursive: true});
const filename = path.join(documentationDirectory, "build", "robots.txt");
console.log(filename);
fs.writeFileSync(filename, robots);
console.log(`${filename} generated`);
}

replaceRobotsTxt();


if (process.env.GTAG) {

fs.mkdirSync(path.join(documentationDirectory, "build"), { recursive: true });
const filePath = path.join(documentationDirectory, "build", "analytics.js")
fs.mkdirSync(path.join(documentationDirectory, "build"), {recursive: true});
const filePath = path.join(documentationDirectory, "build", "analytics.js")

let analyticsTemplate = readFile(documentationDirectory, path.join("static", "analytics.template"));
analyticsTemplate = analyticsTemplate.replace(/\G-999X9XX9XX/g, process.env.GTAG);
console.log(`GTAG generated ${filePath}`);
fs.writeFileSync(filePath, analyticsTemplate);
let analyticsTemplate = readFile(documentationDirectory, path.join("static", "analytics.template"));
analyticsTemplate = analyticsTemplate.replace(/G-999X9XX9XX/g, process.env.GTAG);
console.log(`GTAG generated ${filePath}`);
fs.writeFileSync(filePath, analyticsTemplate);
}
Loading

0 comments on commit e398c20

Please sign in to comment.