-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
121 lines (107 loc) · 3.91 KB
/
index.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import express from 'express';
import path from "path";
import ejs from 'ejs';
import assert from "assert";
import dotenv from 'dotenv';
import * as fs from "fs";
import docx from 'docx';
import * as http from "http";
import {Server} from "socket.io";
import chokidar from 'chokidar';
import nocache from 'nocache';
// import debounce from 'lodash.debounce';
const debounce = (x) => x;
// loading environment variables
dotenv.config();
// constants
const PORT = 3000;
const GENERATED_FILENAME = process.env.GENERATED_FILENAME || "output.docx";
const PUBLIC_URL = process.env.PUBLIC_URL;
const DEBOUNCE_TIME = process.env.DEBOUNCE_TIME || 250;
const NODE_ENV = process.env.NODE_ENV || "development";
const VIEWER = process.env.VIEWER || "GOOGLE";
const STRATEGY = process.env.STRATEGY || "static";
const __dirname = path.resolve();
const BUILD_DIR = path.join(__dirname, 'docx-build');
const SRC_DIR = path.join(__dirname, 'docx-src');
if (NODE_ENV === "production") {
console.log = () => {
};
}
assert(PUBLIC_URL, "PUBLIC_URL is not defined, please configure the .env file");
// create build folder if not exists
if (!fs.existsSync(BUILD_DIR)) {
fs.mkdirSync(BUILD_DIR);
console.log("Created build folder /docx-build");
}
// create server and socket.io
const app = express()
const server = http.createServer(app);
const io = new Server(server);
const {Packer} = docx;
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
// watching source.js file and generating docx file
const watcher_src = chokidar.watch(`${SRC_DIR}`);
watcher_src.on('change', debounce(async (_path) => {
try {
console.log(`File source.js has been changed`);
const doc_sample = await import(`./docx-src/source.js?version=${new Date()}`);
// write to file
const buffer = await Packer.toBuffer(doc_sample.default);
fs.writeFileSync(path.join(BUILD_DIR, GENERATED_FILENAME), buffer);
console.log(GENERATED_FILENAME, "has been generated");
} catch (err) {
console.error("Error while generating file", GENERATED_FILENAME, err.message);
}
}, DEBOUNCE_TIME, {
leading: false,
trailing: true,
}));
// listening to docx file changes and emit reload event
const watcher_build = chokidar.watch(`${BUILD_DIR}/${GENERATED_FILENAME}`);
watcher_build.on('change', debounce((_path) => {
console.log(`File ${GENERATED_FILENAME} has been changed`);
io.sockets.emit('reload'); // emit reload event to clients
console.log(`reload event sent at`, new Date());
}, DEBOUNCE_TIME, {
leading: false,
trailing: true,
}));
// serving index.html to view the generated docx file
app.engine('html', ejs.renderFile);
app.use(nocache());
app.get('/', (req, res) => {
res.render(path.join(__dirname, 'public', 'index.html'), {
PUBLIC_URL: PUBLIC_URL,
GENERATED_FILENAME: GENERATED_FILENAME,
NODE_ENV: NODE_ENV,
DEBOUNCE_TIME: DEBOUNCE_TIME,
VIEWER: VIEWER,
STRATEGY: STRATEGY,
});
})
app.get("/generate/:id", async (req, res) => {
try {
const doc_sample = await import(`./docx-src/source.js?version=${new Date()}`);
const b64string = await Packer.toBase64String(doc_sample.default);
res.setHeader('Content-Disposition', `attachment; filename=${GENERATED_FILENAME}`);
res.send(Buffer.from(b64string, 'base64'));
} catch (err) {
console.error("Error while generating file", GENERATED_FILENAME, err.message);
}
});
// serving docx files
app.use('/static', express.static(BUILD_DIR));
// serving assets
app.use('/assets', express.static(path.join(__dirname, 'public', 'assets')));
// serving socket.io client
app.use('/socket.io.local', express.static(path.join(__dirname, 'node_modules', 'socket.io', 'client-dist')));
// starting server
server.listen(PORT, () => {
console.log(`listening at http://localhost:${PORT}`)
})