-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
35 lines (30 loc) · 1.12 KB
/
compile.js
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
import { compileTwine2HTML, parseTwee, parseStoryFormat } from 'extwee';
import { readFile, writeFile, mkdir } from 'node:fs/promises';
/**
* Creates a directory if it does not exist.
* @param {string} path The path to the directory to create.
*/
async function ensureDir(path) {
try {
await mkdir(path);
}
catch (err) {
if (err.code != 'EEXIST')
throw err;
}
}
// Read in the story source file and parse it
const inputFile = await readFile('story.twee', 'utf-8');
const story = parseTwee(inputFile);
// Download the story format file (chapbook hosts them publically, nice!)
// Other formats may require other methods for fetching the format.js file
const storyFormatResponse = await fetch('https://klembot.github.io/chapbook/use/2.1.0/format.js');
const storyFormatText = await storyFormatResponse.text();
const storyFormat = parseStoryFormat(storyFormatText); // Now parse it
// Actually do the compilation
const compiledStory = compileTwine2HTML(story, storyFormat);
// Write out the result!
await ensureDir('dist');
await writeFile('dist/index.html', compiledStory, {
encoding: 'utf-8'
});