Skip to content

Commit

Permalink
bump add.page to npx version
Browse files Browse the repository at this point in the history
doesnt fully fix, still needs memory fix for npx usage
  • Loading branch information
BankkRoll authored Oct 20, 2023
1 parent 1ea8d17 commit d1e3083
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "repo2pdf",
"version": "2.1.6",
"version": "2.1.61",
"description": "A Node.js utility for generating a PDF document from a GitHub repository",
"main": "dist/clone.js",
"bin": {
Expand Down
130 changes: 66 additions & 64 deletions src/clone.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
#!/usr/bin/env node
import fs from 'fs';
import fs from "fs";
const fsPromises = fs.promises;
import path from 'path';
import path from "path";

import git from 'simple-git';
import PDFDocument from 'pdfkit';
import { default as hljs } from 'highlight.js';
import { isBinaryFileSync } from 'isbinaryfile';
import strip from 'strip-comments';
import git from "simple-git";
import PDFDocument from "pdfkit";
import { default as hljs } from "highlight.js";
import { isBinaryFileSync } from "isbinaryfile";
import strip from "strip-comments";

import { htmlToJson } from './syntax';
import loadIgnoreConfig, { IgnoreConfig } from './loadIgnoreConfig';
import { htmlToJson } from "./syntax";
import loadIgnoreConfig, { IgnoreConfig } from "./loadIgnoreConfig";
import {
universalExcludedExtensions,
universalExcludedNames,
} from './universalExcludes';
} from "./universalExcludes";

import { configQuestions } from './configHandler';
import { configQuestions } from "./configHandler";

//@ts-ignore
import type chalkType from 'chalk';
import type chalkType from "chalk";
//@ts-ignore
import type inquirerType from 'inquirer';
import type inquirerType from "inquirer";
//@ts-ignore
import type oraType from 'ora';
import type oraType from "ora";

let chalk: typeof chalkType;
let inquirer: typeof inquirerType;
let ora: typeof oraType;

const spinnerPromise = import('ora').then((oraModule) => {
const spinnerPromise = import("ora").then((oraModule) => {
ora = oraModule.default;
return ora('Setting everything up...').start();
return ora("Setting everything up...").start();
});

Promise.all([
import('chalk').then((chalkModule) => chalkModule.default),
import('inquirer').then((inquirerModule) => inquirerModule.default),
import("chalk").then((chalkModule) => chalkModule.default),
import("inquirer").then((inquirerModule) => inquirerModule.default),
spinnerPromise,
])
.then(([chalkModule, inquirerModule, spinner]) => {
chalk = chalkModule;
inquirer = inquirerModule;
spinner.succeed('Setup complete');
spinner.succeed("Setup complete");
configQuestions(main, chalk, inquirer);
})
.catch((err) => {
spinnerPromise.then((spinner) => {
spinner.fail('An error occurred during setup');
spinner.fail("An error occurred during setup");
});
console.error(err);
});
Expand All @@ -63,39 +63,40 @@ async function main(
onePdfPerFile: any,
outputFileName: fs.PathLike,
outputFolderName: any,
keepRepo: any,
keepRepo: any
) {
const gitP = git();
let tempDir = './tempRepo';
let tempDir = "./tempRepo";

let doc: typeof PDFDocument | null = null;
if (!onePdfPerFile) {
doc = new PDFDocument({
bufferPages: true,
autoFirstPage: false,
});
doc.pipe(fs.createWriteStream(outputFileName));
doc.addPage();
}
if (!onePdfPerFile) {
doc = new PDFDocument({
bufferPages: true,
autoFirstPage: false,
});
doc.pipe(fs.createWriteStream(outputFileName));
doc.addPage();
}

let fileCount = 0;

let ignoreConfig: IgnoreConfig | null = null;

const spinner = ora(chalk.blueBright('Setting everything up...')).start();
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const spinner = ora(chalk.blueBright("Setting everything up...")).start();
const delay = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

try {
if (useLocalRepo) {
tempDir = repoPath;
} else {
spinner.start(chalk.blueBright('Cloning repository...'));
spinner.start(chalk.blueBright("Cloning repository..."));

await gitP.clone(repoPath, tempDir);
spinner.succeed(chalk.greenBright('Repository cloned successfully'));
spinner.succeed(chalk.greenBright("Repository cloned successfully"));
}

spinner.start(chalk.blueBright('Processing files...'));
spinner.start(chalk.blueBright("Processing files..."));

ignoreConfig = await loadIgnoreConfig(tempDir);

Expand All @@ -114,7 +115,7 @@ if (!onePdfPerFile) {
`Page: ${i + 1} of ${pages.count}`,
0,
doc.page.height - oldBottomMargin / 2,
{ align: 'center' },
{ align: "center" }
);
doc.page.margins.bottom = oldBottomMargin;
}
Expand All @@ -126,20 +127,20 @@ if (!onePdfPerFile) {
spinner.succeed(
chalk.greenBright(
`${
onePdfPerFile ? 'PDFs' : 'PDF'
} created with ${fileCount} files processed.`,
),
onePdfPerFile ? "PDFs" : "PDF"
} created with ${fileCount} files processed.`
)
);

if (!keepRepo && !useLocalRepo) {
await delay(3000);
fs.rmSync(tempDir, { recursive: true, force: true });
spinner.succeed(
chalk.greenBright('Temporary repository has been deleted.'),
chalk.greenBright("Temporary repository has been deleted.")
);
}
} catch (err) {
spinner.fail(chalk.redBright('An error occurred'));
spinner.fail(chalk.redBright("An error occurred"));
console.error(err);
}

Expand Down Expand Up @@ -169,7 +170,7 @@ if (!onePdfPerFile) {
if (stat.isFile()) {
fileCount++;
spinner.text = chalk.blueBright(
`Processing files... (${fileCount} processed)`,
`Processing files... (${fileCount} processed)`
);
let fileName = path.relative(tempDir, filePath);

Expand All @@ -180,33 +181,34 @@ if (!onePdfPerFile) {
});

const pdfFileName = path
.join(outputFolderName, fileName.replace(path.sep, '_'))
.concat('.pdf');
.join(outputFolderName, fileName.replace(path.sep, "_"))
.concat(".pdf");

await fsPromises.mkdir(path.dirname(pdfFileName), {
recursive: true,
});
doc.pipe(fs.createWriteStream(pdfFileName));
doc.addPage();
}

if (doc) {
if (isBinaryFileSync(filePath)) {
const data = fs.readFileSync(filePath).toString('base64');
const data = fs.readFileSync(filePath).toString("base64");
if (fileCount > 1) doc.addPage();
doc
.font('Courier')
.font("Courier")
.fontSize(10)
.text(`${fileName}\n\nBASE64:\n\n${data}`, { lineGap: 4 });
} else {
let data = await fsPromises.readFile(filePath, 'utf8');
data = data.replace(/Ð/g, '\n');
data = data.replace(/\r\n/g, '\n');
data = data.replace(/\r/g, '\n');
data = data.replace(/uþs/g, '');
let data = await fsPromises.readFile(filePath, "utf8");
data = data.replace(/Ð/g, "\n");
data = data.replace(/\r\n/g, "\n");
data = data.replace(/\r/g, "\n");
data = data.replace(/uþs/g, "");

doc.addPage();
doc
.font('Courier')
.font("Courier")
.fontSize(10)
.text(`${fileName}\n\n`, { lineGap: 4 });

Expand All @@ -216,10 +218,10 @@ if (!onePdfPerFile) {

// Remove empty whitespace lines
if (removeEmptyLines) {
data = data.replace(/^\s*[\r\n]/gm, '');
data = data.replace(/^\s*[\r\n]/gm, "");
}

const extension = path.extname(filePath).replace('.', '');
const extension = path.extname(filePath).replace(".", "");
let highlightedCode;
try {
// Check if language is supported before attempting to highlight
Expand All @@ -230,36 +232,36 @@ if (!onePdfPerFile) {
} else {
// Use plaintext highlighting if language is not supported
highlightedCode = hljs.highlight(data, {
language: 'plaintext',
language: "plaintext",
}).value;
}
} catch (error) {
// Use plaintext highlighting if an error occurs
highlightedCode = hljs.highlight(data, {
language: 'plaintext',
language: "plaintext",
}).value;
}
const hlData = htmlToJson(highlightedCode);
let lineNum = 1;
const lineNumWidth = hlData
.filter((d) => d.text === '\n')
.filter((d) => d.text === "\n")
.length.toString().length;
for (let i = 0; i < hlData.length; i++) {
const { text, color } = hlData[i];
if (i == 0 || hlData[i - 1]?.text === '\n')
if (i == 0 || hlData[i - 1]?.text === "\n")
if (addLineNumbers) {
doc.text(
String(lineNum++).padStart(lineNumWidth, ' ') + ' ',
String(lineNum++).padStart(lineNumWidth, " ") + " ",
{
continued: true,
textIndent: 0,
},
}
);
}
if (color) doc.fillColor(color);
else doc.fillColor('black');
else doc.fillColor("black");

if (text !== '\n') doc.text(text, { continued: true });
if (text !== "\n") doc.text(text, { continued: true });
else doc.text(text);
}
}
Expand All @@ -275,9 +277,9 @@ if (!onePdfPerFile) {
}

if (!onePdfPerFile) {
doc?.on('finish', () => {
doc?.on("finish", () => {
spinner.succeed(
chalk.greenBright(`PDF created with ${fileCount} files processed.`),
chalk.greenBright(`PDF created with ${fileCount} files processed.`)
);
});
}
Expand Down

0 comments on commit d1e3083

Please sign in to comment.