-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconverter.ts
83 lines (68 loc) · 2.87 KB
/
converter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { ConsolePlatform } from "../runners/console/consoleInterfaces";
import { ConsoleUtils } from "../runners/console/consoleUtils";
import * as child_process from "child_process"
import * as path from "path"
import * as fs from "fs"
import { pathToFileURL } from "url";
import { fstat } from "fs";
import { FileContains } from "../assertions/fileContains";
export class Converter {
private readonly TMP_DIR = "tmp_convert";
private commandsAvailable: boolean;
constructor() {
// Create temporary directory in which files can be created & modified
this.createTempDir();
this.commandsAvailable = this.commandAvailable("asciidoctor -v") && this.commandAvailable("pandoc");
}
createTempDir(): void {
// Create tempDir if not already existing
let dir = this.getTempDir();
if(!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
createTempFile(filename: string, content: string) {
fs.writeFileSync(path.join(this.getTempDir(),filename), content);
}
readTempFile(filename: string): string {
let file_path = path.join(this.getTempDir(), filename);
let fileContent = fs.readFileSync(file_path, "utf8");
return fileContent;
}
getTempDir(): string {
// Return tempDir
return path.join(__dirname, this.TMP_DIR);
}
convertAsciidocToMarkdown(asciidocString: string): string {
if(asciidocString !== undefined) {
/**
* 1. Write asciidocString to temp.adoc file
* 2. Use AsciiDoctor to convert AsciiDoc String to DocBook XML file
* 3. Use Pandoc to convert DocBook XML file to Markdown File
* 4. Read Markdown file (eventually combine with step 2?)
*/
this.createTempFile("temp.adoc", asciidocString);
let markdownString = "";
if(this.commandsAvailable) {
this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir());
this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir());
markdownString = this.readTempFile("temp.md");
}else {
markdownString = asciidocString;
}
return markdownString;
}
}
commandAvailable(command: string): boolean {
let isAvailable = false;
let process = child_process.spawnSync(command, {shell: true, cwd: this.getTempDir()});
return process.status == 0;
}
executeCommand(command: string, directory: string) {
let process = child_process.spawnSync(command, {shell: true, cwd: directory});
if(process.status != 0) {
console.log("Error executing command: " + command + " (exit code: )" + process.status + ")");
console.log(process.stderr.toString(), process.toString());
}
}
}