-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (135 loc) · 4.72 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Require from package
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
const helmet = require('helmet');
const morgan = require('morgan');
const fs = require('fs');
const dirTree = require('directory-tree');
// Require from local files
const pkg = require('./package.json');
// Declare constants
const PORT = pkg.port || 3011;
const CONFIG = pkg.config || './client/src/.config.json';
// Inititalize Express
const app = express();
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(compression());
app.use(helmet());
/* Application routes */
app.put('/config', (req, res) => {
let json;
try {
const contents = fs.readFileSync(CONFIG).toString();
try {
json = JSON.parse(contents);
} catch(e) {
json = {};
}
} catch (e) {
console.error('Could not read and parse config file', e);
json = {};
}
Object.keys(req.body).forEach(key => json[key] = req.body[key]);
try {
fs.writeFileSync(CONFIG, JSON.stringify(json, undefined, 2));
res.send({ok: true});
} catch (e) {
res.send({ok: false, message: e});
}
});
app.get('/config', (req, res) => {
try {
const contents = fs.readFileSync(CONFIG).toString();
try {
res.send(JSON.parse(contents));
} catch(e) {
console.error('Could not parse config', e);
res.send(null);
}
} catch(e) {
console.error('Could not read config file', e);
res.send(null);
}
});
app.get('/fetchFiles', async (req, res) => {
try {
const contents = fs.readFileSync(CONFIG).toString();
try {
const json = JSON.parse(contents);
try {
try {
const filteredTree = dirTree(json.translationsDir, {extensions:/\.json/, normalizePaths: true});
const addData = arr => {
arr.forEach(item => {
if (item.children) return addData(item.children);
let data;
try {
data = fs.readFileSync(item.path).toString();
} catch (e) {
console.error('Could not read translation file', item.path, e);
data = '';
}
item.content = data;
});
};
addData([filteredTree]);
// let files = await recursive(json.translationsDir, ['.DS_Store']);
// files = files.map(file => {
// let data;
// try {
// data = fs.readFileSync(file).toString();
// } catch (e) {
// console.warning('Could not read translation file', file, e);
// data = '';
// }
// return { name: file.replace(new RegExp(`^(${json.translationsDir}/)`), ''), data };s
// });
res.send(filteredTree);
} catch (e) {
console.error('Could not read translations directory', e);
res.send(null);
}
} catch(e) {
console.error('Could not read directory');
res.send(null);
}
} catch(e) {
console.error('Could not parse JSON file');
res.send(null);
}
} catch(e) {
console.error('No config file', e);
res.send(null);
}
});
app.patch('/updateTranslation', (req, res) => {
const { path, key, value } = req.body;
try {
const content = fs.readFileSync(path).toString();
try {
const json = JSON.parse(content);
try {
json[key] = value;
fs.writeFileSync(path, JSON.stringify(json, undefined, 2));
res.send(json);
} catch(e) {
console.error('Could not write', path, e);
res.send(null);
}
} catch (e) {
console.error('Could not parse', path, e);
res.send(null);
}
} catch(e) {
console.error('Could not read file', e);
res.send(null);
}
});
// Start server
app.listen(PORT, () => {
console.log(`react-intl-server started at ${PORT}`); // eslint-disable-line no-console
console.log('Access web ui at http://localhost:3012 or at supplied port number'); // eslint-disable-line no-console
});