-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
74 lines (60 loc) · 1.65 KB
/
Copy pathsetup.js
File metadata and controls
74 lines (60 loc) · 1.65 KB
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
const Promise = require('bluebird');
const R = require('ramda');
const fs = require('fs');
const csvParser = Promise.promisify(require('csv-parse'));
const exec = Promise.promisify(require('child_process').exec);
const index = R.findIndex(R.equals('-f'), process.argv);
const file = process.argv[index + 1];
console.log("Using file: ", file);
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8081 });
wss.on('connection', (ws) => {
console.log('client connected');
ws.on('message', (message) => {
console.log('received: %s', message);
});
fs.watchFile(file, {interval: 200}, (curr, prev) => {
console.log(file);
console.log('file changed!');
ws.send('fileChanged');
});
ws.send('something');
});
/*
* Probably should use something like this
* to be more secure
const execFile = require('child_process').execFile;
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
console.error('stderr', stderr);
throw error;
}
console.log('stdout', stdout);
});
*/
const init = router => {
router.get('/api', (req, res) => {
res.json({message: 'hi! again!'});
});
router.post('/api', (req, res) => {
const {cmd} = req.body;
const command = `hledger -f ${file} ${cmd} -O csv`;
return exec(command).then( result => {
return csvParser(result);
}).then( data => {
/*
const header = R.head(data);
const json = R.compose(
R.map(R.zipObj(header)),
R.tail
)(data);
*/
res.json(data);
}).catch( e => {
res.json({message: e.message});
});
});
};
module.exports = {
init
};