Skip to content

Commit 18712a9

Browse files
committed
Move createMain to utils file
1 parent 14d5e6f commit 18712a9

File tree

2 files changed

+69
-59
lines changed

2 files changed

+69
-59
lines changed

javascript/commands/utils.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import fs from "node:fs";
2+
import fse from "fs-extra";
3+
import path from "node:path";
4+
import { ending, frontmatter, preamble } from "../latexContent.js";
5+
6+
export const createMain = (
7+
inputDir: string,
8+
outputDir: string,
9+
parseType: string
10+
) => {
11+
// TODO: Use fs-extra to auto create subfolders
12+
if (!fs.existsSync(outputDir)) {
13+
fs.mkdirSync(outputDir);
14+
}
15+
16+
if (parseType == "js" || parseType == "json") {
17+
return;
18+
}
19+
20+
if (parseType == "web") {
21+
if (!fs.existsSync(path.join(outputDir, "/chapters"))) {
22+
fs.mkdirSync(path.join(outputDir, "/chapters"));
23+
}
24+
fse.copy(path.join(__dirname, "/../static"), outputDir, err => {
25+
if (err) return console.error(err);
26+
});
27+
return;
28+
}
29+
30+
// for latex version only
31+
// create sicpjs.tex file
32+
// FIXME: Remove any
33+
const chaptersFound: any[] = [];
34+
const files = fs.readdirSync(inputDir);
35+
files.forEach(file => {
36+
if (file.match(/chapter/)) {
37+
chaptersFound.push(file);
38+
}
39+
});
40+
const stream = fs.createWriteStream(path.join(outputDir, "sicpjs.tex"));
41+
stream.once("open", fd => {
42+
stream.write(preamble);
43+
stream.write(frontmatter);
44+
chaptersFound.forEach(chapter => {
45+
const pathStr = "./" + chapter + "/" + chapter + ".tex";
46+
stream.write("\\input{" + pathStr + "}\n");
47+
});
48+
stream.write(ending);
49+
stream.end();
50+
});
51+
// makes the .latexmkrc file
52+
const latexmkrcStream = fs.createWriteStream(
53+
path.join(outputDir, ".latexmkrc")
54+
);
55+
latexmkrcStream.once("open", fd => {
56+
latexmkrcStream.write(latexmkrcContent);
57+
latexmkrcStream.end();
58+
});
59+
};
60+
61+
const latexmkrcContent = `$pdflatex = "xelatex %O %S";
62+
$pdf_mode = 1;
63+
$dvi_mode = 0;
64+
$postscript_mode = 0;`;

javascript/index.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from "node:fs";
2-
import fse from "fs-extra";
32
import util from "util";
43
import path from "path";
54

@@ -15,11 +14,6 @@ import {
1514
recursiveProcessTextLatex
1615
} from "./parseXmlLatex.js";
1716
import { setupSnippetsPdf } from "./processingFunctions/processSnippetPdf.js";
18-
import { preamble, frontmatter, ending } from "./latexContent.js";
19-
const latexmkrcContent = `$pdflatex = "xelatex %O %S";
20-
$pdf_mode = 1;
21-
$dvi_mode = 0;
22-
$postscript_mode = 0;`;
2317

2418
// html (comparison version)
2519
import { switchTitle } from "./htmlContent.js";
@@ -42,6 +36,7 @@ import { writeRewritedSearchData } from "./searchRewrite.js";
4236
import { setupSnippetsJson } from "./processingFunctions/processSnippetJson.js";
4337
import { createTocJson } from "./generateTocJson.js";
4438
import { setupReferencesJson } from "./processingFunctions/processReferenceJson.js";
39+
import { createMain } from "./commands/utils.js";
4540

4641
export let parseType;
4742
let version;
@@ -243,62 +238,13 @@ const createIndexHtml = version => {
243238
});
244239
};
245240

246-
const createMain = () => {
247-
if (!fs.existsSync(outputDir)) {
248-
fs.mkdirSync(outputDir);
249-
}
250-
251-
if (parseType == "js" || parseType == "json") {
252-
return;
253-
}
254-
255-
if (parseType == "web") {
256-
if (!fs.existsSync(path.join(outputDir, "/chapters"))) {
257-
fs.mkdirSync(path.join(outputDir, "/chapters"));
258-
}
259-
fse.copy(path.join(__dirname, "/../static"), outputDir, err => {
260-
if (err) return console.error(err);
261-
});
262-
return;
263-
}
264-
265-
// for latex version only
266-
// create sicpjs.tex file
267-
const chaptersFound = [];
268-
const files = fs.readdirSync(inputDir);
269-
files.forEach(file => {
270-
if (file.match(/chapter/)) {
271-
chaptersFound.push(file);
272-
}
273-
});
274-
const stream = fs.createWriteStream(path.join(outputDir, "sicpjs.tex"));
275-
stream.once("open", fd => {
276-
stream.write(preamble);
277-
stream.write(frontmatter);
278-
chaptersFound.forEach(chapter => {
279-
const pathStr = "./" + chapter + "/" + chapter + ".tex";
280-
stream.write("\\input{" + pathStr + "}\n");
281-
});
282-
stream.write(ending);
283-
stream.end();
284-
});
285-
// makes the .latexmkrc file
286-
const latexmkrcStream = fs.createWriteStream(
287-
path.join(outputDir, ".latexmkrc")
288-
);
289-
latexmkrcStream.once("open", fd => {
290-
latexmkrcStream.write(latexmkrcContent);
291-
latexmkrcStream.end();
292-
});
293-
};
294-
295241
async function main() {
296242
parseType = process.argv[2];
297243
if (parseType == "pdf") {
298244
outputDir = path.join(__dirname, "../latex_pdf");
299245

300246
switchParseFunctionsLatex(parseType);
301-
createMain();
247+
createMain(inputDir, outputDir, parseType);
302248

303249
console.log("setup snippets\n");
304250
await recursiveTranslateXml("", "setupSnippet");
@@ -326,7 +272,7 @@ async function main() {
326272

327273
switchParseFunctionsHtml(version);
328274
switchTitle(version);
329-
createMain();
275+
createMain(inputDir, outputDir, parseType);
330276

331277
console.log("\ngenerate table of content\n");
332278
await recursiveTranslateXml("", "generateTOC");
@@ -345,15 +291,15 @@ async function main() {
345291
} else if (parseType == "js") {
346292
outputDir = path.join(__dirname, "../js_programs");
347293

348-
createMain();
294+
createMain(inputDir, outputDir, parseType);
349295
console.log("setup snippets\n");
350296
await recursiveTranslateXml("", "setupSnippet");
351297
console.log("setup snippets done\n");
352298
recursiveTranslateXml("", "parseXml");
353299
} else if (parseType == "json") {
354300
outputDir = path.join(__dirname, "../json");
355301

356-
createMain();
302+
createMain(inputDir, outputDir, parseType);
357303

358304
console.log("\ngenerate table of content\n");
359305
await recursiveTranslateXml("", "generateTOC");

0 commit comments

Comments
 (0)