-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
build-format.js
38 lines (30 loc) · 1.46 KB
/
build-format.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
36
37
38
const fs = require('fs');
const ejs = require('ejs');
// Read and parse story.json file.
const storyFile = fs.readFileSync('story.json', { encoding: 'utf8' });
const story = JSON.parse(storyFile);
// Read story format HTML template.
const srcIndex = fs.readFileSync('src/story.ejs', { encoding: 'utf8' });
// Read bundled format.
const formatSource = fs.readFileSync('build/format.bundle.js', { encoding: 'utf8' });
// Read bundled CSS.
const storyCSS = fs.readFileSync('build/format.css', { encoding: 'utf8' });
// Replace the bundles in the format HTML template.
const indexSource = ejs.render(srcIndex, {
format: `<script>${formatSource}</script>`,
story_css: `<style>${storyCSS}</style>`
});
// Write the resulting file.
// fs.writeFileSync('build/index.html', indexSource);
// Read the bundled editor code.
// const editorSource = fs.readFileSync("build/editor.bundle.js", {'encoding': 'utf8'});
// Add the HTML template code to the story object.
story.source = indexSource;
// Generate format.js.
const format = 'window.storyFormat(' + JSON.stringify(story) + ');';
fs.writeFileSync('dist/format.js', format);
// Re-read format.js, replacing the editor code to create a malformed JSON.
// let compiledFormat = fs.readFileSync(`dist/format.js`, {'encoding': 'utf8'});
// compiledFormat = compiledFormat.replace("\"setup\":\"\"", `\"setup\": function(){${editorSource}}`);
// Re-write the format.js with editor code additions.
// fs.writeFileSync(`dist/format.js`, compiledFormat);