Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions scripts/api-pages-script.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,23 @@ const sk_colors = ["alice_blue", "antique_white", "aqua", "aquamarine", "azure",
// ------------------------------------------------------------------------------
// Get a list of all the files in a directory and it's subdirectories
// ------------------------------------------------------------------------------
function getAllFiles(dir, allFilesList = []) {
function getAllFiles(dir, baseDir = dir, allFilesList = []) {
try {
const files = fs.readdirSync(dir);
files.map(file => {
const name = dir + '/' + file;
if (fs.statSync(name).isDirectory()) { // check if subdirectory is present
getAllFiles(name, allFilesList); // do recursive execution for subdirectory

files.forEach((file) => {
const fullPath = path.join(dir, file);

if (fs.statSync(fullPath).isDirectory()) {
getAllFiles(fullPath, baseDir, allFilesList);
} else {
allFilesList.push(file); // push filename into the array
allFilesList.push(path.relative(baseDir, fullPath).replaceAll("\\", "/"));
}
})
});
} catch (err) {
console.error(kleur.yellow(`Warning: Unable to access directory ${dir}`), err);
}

return allFilesList;
}

Expand Down Expand Up @@ -247,19 +250,19 @@ function getUsageExampleImports(categoryKey, functionKey) {
let categoryPath = '/usage-examples/' + categoryKey;
let categoryFilePath = './public/usage-examples/' + categoryKey;

const functionFiles = getAllFiles(categoryFilePath).filter(file => file.startsWith(functionKey));
const functionFiles = getAllFiles(categoryFilePath).filter(file => path.basename(file).startsWith(functionKey));
if (functionFiles.length > 0) {
const txtFiles = functionFiles.filter(file => file.endsWith('.txt'))
if (txtFiles.length > 0) {
txtFiles.forEach((exampleTxtKey) => {
let exampleKey = exampleTxtKey.replaceAll(".txt", "");
let exampleKey = path.basename(exampleTxtKey).replaceAll(".txt", "");

let importTitle = exampleKey.replaceAll("-", "_");

languageOrder.forEach((lang) => {
const languageFiles = functionFiles.filter(file => file.endsWith(languageFileExtensions[lang]));
let codeFilePath = categoryPath + "/" + exampleTxtKey.replaceAll(".txt", languageFileExtensions[lang]);

let codeFilePath = categoryPath + "/" + exampleTxtKey.replaceAll(".txt", languageFileExtensions[lang]);
// import code if available
if (languageFiles.length > 0) {
languageCodeAvailable[lang] = true;
Expand Down Expand Up @@ -335,7 +338,7 @@ function getUsageExampleContent(jsonData, categoryKey, groupName, functionKey) {
let categoryFilePath = './public/usage-examples/' + categoryKey;

let exampleKey = functionKey.replaceAll(".txt", "");
const functionFiles = getAllFiles(categoryFilePath).filter(file => file.startsWith(exampleKey));
const functionFiles = getAllFiles(categoryFilePath).filter(file => path.basename(file).startsWith(exampleKey));

if (functionFiles.length > 0) {

Expand All @@ -344,10 +347,10 @@ function getUsageExampleContent(jsonData, categoryKey, groupName, functionKey) {

// import code if available
if (functionFiles.length > 0) {
let importTitle = exampleKey.replaceAll("-", "_");
let importTitle = path.basename(exampleTxtKey).replaceAll(".txt", "").replaceAll("-", "_");

// Description
let exampleNum = exampleKey.replace(/\D/g, '');
let exampleNum = path.basename(exampleTxtKey).replace(/\D/g, '');
mdxData += `**Example ${exampleNum}**: `;
let exampleTxt = fs.readFileSync(categoryFilePath + "/" + exampleTxtKey);
mdxData += exampleTxt.toString();
Expand All @@ -368,7 +371,7 @@ function getUsageExampleContent(jsonData, categoryKey, groupName, functionKey) {
// Check if both top level and oop code has been found for current function
const csharpFiles = functionFiles.filter(file => file.endsWith("-top-level.cs") || file.endsWith("-oop.cs")).filter(file => file.includes(exampleKey));
const cppFiles = functionFiles.filter(file => file.endsWith("-sk.cpp") || file.endsWith("-beyond.cpp")).filter(file => file.includes(exampleKey));
functionTag = exampleKey.split("-")[0];
functionTag = path.basename(exampleTxtKey).split("-")[0];
if (lang == "cpp") {
functionTag = groupName;
}
Expand Down Expand Up @@ -417,14 +420,14 @@ function getUsageExampleContent(jsonData, categoryKey, groupName, functionKey) {
let outputFilePath = categoryPath + "/" + exampleTxtKey;


const imageFiles = functionFiles.filter(file => file.endsWith(exampleKey + '.png'));
const imageFiles = functionFiles.filter(file => path.basename(file) === path.basename(exampleTxtKey).replaceAll(".txt", ".png"));
// Check for .png files
if (imageFiles.length > 0) {
outputFilePath = outputFilePath.replaceAll(".txt", ".png");
mdxData += `![${exampleKey} example](${outputFilePath})\n`
}
else {
const gifFiles = functionFiles.filter(file => file.endsWith('.gif')).filter(file => file.startsWith(exampleKey));
const gifFiles = functionFiles.filter(file => file.endsWith('.gif')).filter(file => path.basename(file).startsWith(path.basename(exampleTxtKey).replaceAll(".txt", "")));
// Check for .gif files
if (gifFiles.length > 0) {
outputFilePath = outputFilePath.replaceAll(".txt", ".gif");
Expand Down