-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
85 lines (78 loc) · 2.1 KB
/
index.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
84
85
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { exec, ExecException } from "child_process";
import { lstatSync, readFileSync } from "fs";
import { exit, env } from "process";
import { basename, join } from "path";
const argv = yargs(hideBin(process.argv))
.options({
presentation: {
type: "string",
demandOption: true,
alias: "p",
describe: "presentation to compile",
},
theme: {
type: "string",
demandOption: true,
alias: "t",
describe: "theme to use",
},
})
.usage("Usage: [ts-node] ./$0 -p <presentation> -t <theme>")
.check((argv) => {
return (
lstatSync(argv.theme).isDirectory() &&
lstatSync(argv.presentation).isFile()
);
})
.alias("h", "help")
.alias("V", "version")
.parseSync();
const { presentation, theme } = argv;
// https://stackoverflow.com/a/42037274
const encode_json_shell_escape = (content: string) => {
return JSON.stringify({ content })
.replace(/^"|"$/g, "") //remove JSON-string double quotes
.replace(/'/g, "'\"'\"'"); //escape single quotes the ugly bash way
};
const exec_handler = (
err: ExecException | null,
stdout: string,
stderr: string
) => {
if (err) {
console.error(stderr);
exit(1);
}
console.log(stdout);
};
const presentation_content: string = readFileSync(presentation, {
encoding: "utf-8",
});
const presentation_name: string = basename(presentation, ".md");
const output_dir: string = join(__dirname, "dest", presentation_name);
// build the theme by running make
const theme_build_command: string = "make";
console.log(`Compile ${theme} with command ${theme_build_command}`);
exec(
theme_build_command,
{
cwd: theme,
env: {
...env,
MARKDOWN_CONTENT: encode_json_shell_escape(presentation_content),
OUTPUT_LOCATION: output_dir,
},
},
exec_handler
);
// send our content to the same destination
console.log("compiling scss");
exec(`sass --no-source-map src:${output_dir}`, exec_handler);
console.log("compiling ts");
exec(
`tsc --outDir ${output_dir}`,
{ cwd: join(__dirname, "src") },
exec_handler
);