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
25 changes: 17 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ async function runWizard() {
// Ask for transcript/text file
// TODO: if they give a different primary source than already exists, wipe all subsequent defaults

const fileName = await input({
const sourcePath = await input({
message:
"What's your primary source? \n Drag a text file (or youtube link, experimental) in here, or leave empty/whitespace to open an editor: ",
default: wizardState.primarySourceFilename || undefined,
"What's your primary source? \n Drag a text file, a directory path (use --dir or --path flag), or a youtube link (experimental) in here, or leave empty/whitespace to open an editor: ",
default: wizardState.primarySourcePath || undefined,
validate: async (filename) => {
if(filename?.trim()) {
if((filename === wizardState.primarySourceFilename || filename === parsePlatformIndependentPath(filename)) && wizardState.loadedPrimarySource) return true;
Expand All @@ -130,13 +130,22 @@ async function runWizard() {
} catch(err) {
return `Looked like a youtube video - Couldn't fetch transcript from ${filename}: ${err}`;
}
} else if(!fs.existsSync(parsePlatformIndependentPath(filename))) {
} else if(!fs.existsSync(parsePlatformIndependentPath(sourcePath))) {
return `File not found - tried to load ${filename}. Try again.`;
} else {
try {
const dataFromFile = fs.readFileSync(parsePlatformIndependentPath(filename), "utf-8");
wizardState.loadedPrimarySource = dataFromFile;
wizardState.primarySourceFilename = parsePlatformIndependentPath(filename);
const parsedPath = parsePlatformIndependentPath(sourcePath);
if (sourcePath.startsWith("--dir") || sourcePath.startsWith("--path")) {
const directoryPath = sourcePath.split(" ")[1];
if (!fs.existsSync(directoryPath) || !fs.lstatSync(directoryPath).isDirectory()) {
return `The provided path does not exist or is not a directory: ${directoryPath}. Try again.`;
}
await processTextFilesInDirectory(directoryPath, wizardState);
} else if (fs.lstatSync(parsedPath).isFile()) {
const dataFromFile = fs.readFileSync(parsePlatformIndependentPath(sourcePath), "utf-8");
wizardState.loadedPrimarySource = dataFromFile;
wizardState.primarySourcePath = parsePlatformIndependentPath(sourcePath);
}
} catch(err) {
return `Couldn't read file - tried to load ${filename}. Try again.`;
}
Expand Down Expand Up @@ -965,4 +974,4 @@ async function runWizard() {
);
}

runWizard();
runWizard();
50 changes: 50 additions & 0 deletions src/fileProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fs from "node:fs";
import path from "node:path";
import { generatePages } from "./page-generator";
import { parsePlatformIndependentPath } from "./utils";
import { WizardState, ReadyToGeneratePage } from "./types";

async function processFile(filePath: string, wizardState: WizardState): Promise<void> {
const content = fs.readFileSync(filePath, "utf-8");
const pages: ReadyToGeneratePage[] = [{
section: {
title: path.basename(filePath, path.extname(filePath)),
permalink: parsePlatformIndependentPath(filePath),
singleSentenceDescription: "",
subsections: []
},
levels: filePath.split(path.sep).slice(0, -1),
messages: [{ role: "system", content: "Generate documentation for this content." }, { role: "user", content }]
}];
await generatePages(true, pages, path.dirname(filePath), wizardState);
}

async function processDirectory(directoryPath: string, wizardState: WizardState): Promise<void> {
const entries = fs.readdirSync(directoryPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(directoryPath, entry.name);
if (entry.isDirectory()) {
await processDirectory(fullPath, wizardState);
} else if (entry.isFile() && path.extname(entry.name) === ".txt") {
await processFile(fullPath, wizardState);
}
}
}

export { processFile, processDirectory, processTextFilesInDirectory };

async function processTextFilesInDirectory(directoryPath: string, wizardState: WizardState): Promise<void> {
const entries = fs.readdirSync(directoryPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(directoryPath, entry.name);
if (entry.isDirectory()) {
await processTextFilesInDirectory(fullPath, wizardState);
} else if (entry.isFile() && path.extname(entry.name) === ".txt") {
try {
await processFile(fullPath, wizardState);
} catch (error) {
console.error(`Error processing file ${fullPath}:`, error);
}
}
}
}