-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-app.ts
79 lines (64 loc) · 1.89 KB
/
get-app.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
import Sealious from 'sealious';
import S from './lib/s';
import path from 'path';
import fs from 'fs';
import uuidv4 from 'uuid/v4';
import util from 'util';
import Settings from './lib/settings';
const writeFile = util.promisify(fs.writeFile);
const exists = util.promisify(fs.access);
const mkdir = util.promisify(fs.mkdir);
const fieldTypes = {
slug: require('./field-types/slug'),
arrayOfObject: require('./field-types/array-of-objects'),
};
const manifest = {
name: 'What Seal Do?',
logo: './image.svg',
version: '1.0',
default_language: 'pl',
base_url: 'http://localhost:8080',
admin_email: '[email protected]',
};
async function renderPreview(uuid, elements) {
let html = '';
let temporary_path = `/tmp/sealpage_bundle/${uuid}`;
try {
await exists('/tmp/sealpage_bundle');
} catch (error) {
await mkdir('/tmp/sealpage_bundle');
}
try {
await exists(temporary_path);
} catch (error) {
await mkdir(temporary_path);
}
let output_dir = path.resolve(temporary_path);
const path_prefix = `/previews/${uuid}`;
const s = new S({ output_dir, path_prefix, preview_mode: true });
// render preview using component instances initialized by s object
for (const [componentName, componentProps] of elements) {
html += await s.components[componentName].render(componentProps);
}
await writeFile(`${output_dir}/index.html`, html);
return `${path_prefix}/index.html?${uuidv4()}`;
}
export default function({ config, settings_source = {} }) {
const app = new Sealious.App(config, manifest);
S.Settings = new Settings(settings_source);
for (const type in fieldTypes) {
fieldTypes[type].default(app);
}
app.WwwServer.static_route(
'/tmp/sealpage_bundle', // system path
'/previews' // url
);
app.WwwServer.custom_route(
'POST',
'/api/v1/render',
async (app, context, { uuid, body }) => {
return renderPreview(uuid.replace(/(\.|\/)/g, '-'), body);
}
);
return app;
}