Skip to content

Commit

Permalink
Refactor walk function in index.js to improve clarity and maintainabi…
Browse files Browse the repository at this point in the history
…lity; add detailed comments and restructure code for better readability
  • Loading branch information
PieterDePauw committed Nov 13, 2024
1 parent b323b31 commit 9f642f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 53 deletions.
95 changes: 43 additions & 52 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66929,55 +66929,6 @@ function v4(options, buf, offset) {
}
/* harmony default export */ const esm_v4 = (v4);

// EXTERNAL MODULE: external "path"
var external_path_ = __nccwpck_require__(6928);
;// CONCATENATED MODULE: ./src/sources/util.ts
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
// Import modules


// Function to walk a directory and return all files
function walk(dir, parentPath) {
return __awaiter(this, void 0, void 0, function* () {
// > Read the contents of the directory
const immediateFiles = yield (0,promises_namespaceObject.readdir)(dir);
// > Recursively walk the directory and return all files in the directory and subdirectories
const recursiveFiles = yield Promise.all(
// >> For each file in the directory, ...
immediateFiles.map((file) => __awaiter(this, void 0, void 0, function* () {
// >>> Construct the full path to the file
const path = (0,external_path_.join)(dir, file);
// >>> Get the file stats
const stats = yield (0,promises_namespaceObject.stat)(path);
// >>> If the file is a directory, recursively walk the directory
if (stats.isDirectory()) {
// >>>> Construct the name of the corresponding .mdx file
const docPath = `${(0,external_path_.basename)(path)}.mdx`;
// >>>> Construct the parent path for the next iteration
const nextParentPath = immediateFiles.includes(docPath) ? (0,external_path_.join)((0,external_path_.dirname)(path), docPath) : parentPath;
// >>>> Recursively walk the directory with the next path and parent path
return walk(path, nextParentPath);
}
// >>> If the file is a file, return the file path
if (stats.isFile()) {
return [{ path, parentPath }];
}
// >>> If the file is not a file or directory, return an empty array
return [];
})));
// > Return the flattened array of files sorted by path name
return recursiveFiles.reduce((all, folderContents) => all.concat(folderContents), []).sort((a, b) => a.path.localeCompare(b.path));
});
}

;// CONCATENATED MODULE: ./node_modules/github-slugger/regex.js
// This module is generated by `script/`.
/* eslint-disable no-control-regex, no-misleading-character-class, no-useless-escape */
Expand Down Expand Up @@ -67062,6 +67013,8 @@ function slug (value, maintainCase) {
return value.replace(regex, '').replace(/ /g, '-')
}

// EXTERNAL MODULE: external "path"
var external_path_ = __nccwpck_require__(6928);
;// CONCATENATED MODULE: ./node_modules/mdast-util-to-string/lib/index.js
/**
* @typedef {import('mdast').Root|import('mdast').Content} Node
Expand Down Expand Up @@ -94342,7 +94295,7 @@ const filter =
/* eslint-disable import/no-unresolved */
/* eslint-disable no-shadow */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
var markdown_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
Expand All @@ -94361,6 +94314,45 @@ var markdown_awaiter = (undefined && undefined.__awaiter) || function (thisArg,




/**
* Walks a directory and returns all files in the directory and subdirectories.
* @param dir - The directory to walk.
* @param parentPath - The optional parent path for the next iteration.
* @returns Promise that resolves to an array of objects containing the path and optional parent path.
*/
function walk(dir, parentPath) {
return __awaiter(this, void 0, void 0, function* () {
// > Read the contents of the directory
const immediateFiles = yield (0,promises_namespaceObject.readdir)(dir);
// > Recursively walk the directory and return all files in the directory and subdirectories
const recursiveFiles = yield Promise.all(
// >> For each file in the directory, ...
immediateFiles.map((file) => __awaiter(this, void 0, void 0, function* () {
// >>> Construct the full path to the file
const path = (0,external_path_.join)(dir, file);
// >>> Get the file stats
const stats = yield (0,promises_namespaceObject.stat)(path);
// >>> If the file is a directory, recursively walk the directory
if (stats.isDirectory()) {
// >>>> Construct the name of the corresponding .mdx file
const docPath = `${(0,external_path_.basename)(path)}.mdx`;
// >>>> Construct the parent path for the next iteration
const nextParentPath = immediateFiles.includes(docPath) ? (0,external_path_.join)((0,external_path_.dirname)(path), docPath) : parentPath;
// >>>> Recursively walk the directory with the next path and parent path
return walk(path, nextParentPath);
}
// >>> If the file is a file, return the file path
if (stats.isFile()) {
return [{ path, parentPath }];
}
// >>> If the file is not a file or directory, return an empty array
return [];
})));
// > Return the flattened array of files sorted by path name
return recursiveFiles.reduce((all, folderContents) => all.concat(folderContents), []).sort((a, b) => a.path.localeCompare(b.path));
});
}
/**
* Extracts ES literals from an `estree` `ObjectExpression`
* into a plain JavaScript object.
Expand Down Expand Up @@ -94548,7 +94540,7 @@ class MarkdownSource extends BaseSource {
* @returns An object containing the checksum, meta, and sections of the markdown content.
*/
load() {
return markdown_awaiter(this, void 0, void 0, function* () {
return __awaiter(this, void 0, void 0, function* () {
const contents = yield (0,promises_namespaceObject.readFile)(this.filePath, "utf8");
const { checksum, meta, sections } = processMdxForSearch(contents);
this.checksum = checksum;
Expand Down Expand Up @@ -94611,7 +94603,6 @@ var main_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arg




// Define the generateSources function
function generateSources(_a) {
return main_awaiter(this, arguments, void 0, function* ({ docsRootPath, ignoredFiles = ["pages/404.mdx"] }) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

0 comments on commit 9f642f7

Please sign in to comment.