-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (174 loc) · 7.36 KB
/
index.js
File metadata and controls
188 lines (174 loc) · 7.36 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const express = require('express');
const serveStatic = require('serve-static');
const bodyParser = require('body-parser');
const path = require("path");
const fs = require("fs-extra");
const app = express();
const yaml = require('js-yaml');
const clone = require('clone');
const klaw = require('klaw');
const DEFAULT_SETTINGS = path.resolve(path.join(__dirname,"settings.ini"));
const CATALOG_DIR = path.join(__dirname,'catalogs');
var defaultConfig = yaml.safeLoad(fs.readFileSync(DEFAULT_SETTINGS,'utf8'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use("/",serveStatic(path.join(__dirname,'public')));
app.get("/catalogs", function(req,res) {
getCatalogs().then(result => {
res.json(result);
}).catch(err => {
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
})
app.get("/catalog/:repo/:branch/*",(req,res) => {
getCatalog2(req.params.repo,req.params.branch,req.params[0]).then(result => {
res.json(result);
}).catch(err => {
console.log(err);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
})
// app.get("/catalog/*", function(req,res) {
// getCatalog(req.params[0]).then(result => {
// res.json(result);
// }).catch(err => {
// res.status(400).json({error:"unexpected_error", message:err.toString()});
// })
// })
app.listen(process.env.PORT||2880);
console.log("Listening on http://localhost:"+(process.env.PORT||2880));
async function getCatalog2(repo,branch,file) {
const parts = /^(.*locales)\/([^/]+)\/(.*)/.exec(file);
if (!parts) {
throw new Error("Not a valid catalog path");
}
const filePath = path.join(CATALOG_DIR,repo,branch,file);
const content = await fs.readFile(filePath,'utf8');
return JSON.parse(content);
}
async function getLanguages(catalogPath,filter) {
const files = await fs.readdir(catalogPath);
const langs = [];
for (let i=0; i<files.length; i++) {
if (files[i][0] !== ".") {
let stat = await fs.stat(path.join(catalogPath,files[i]));
if (stat.isDirectory()) {
if (!filter || files[i] === filter || filter === '*' || filter.indexOf(files[i]) !== -1) {
langs.push(files[i]);
}
}
}
}
return langs;
}
// function getCatalog(catalog) {
// console.log(catalog);
// var basename = path.basename(catalog);
// var dirname = path.dirname(catalog);
// return getFiles(path.join(CATALOG_DIR,dirname), dirname).then(result => {
// var catalog = {};
// for (locale in result.dirs) {
// if (result.dirs.hasOwnProperty(locale)) {
// if (result.dirs[locale].files.indexOf(basename) > -1) {
// try {
// catalog[locale] = JSON.parse(fs.readFileSync(path.join(CATALOG_DIR,dirname,locale,basename)));
// } catch(err) {
//
// }
// }
// }
// }
// return catalog
// })
//
// }
async function getCatalogs() {
const result = clone(defaultConfig);
for (let repo in defaultConfig) {
if (defaultConfig.hasOwnProperty(repo)) {
for (let branch in defaultConfig[repo].branches) {
if (defaultConfig[repo].branches.hasOwnProperty(branch)) {
var paths = defaultConfig[repo].branches[branch].paths;
delete result[repo].branches[branch].paths;
result[repo].branches[branch].catalogs = [];
for (let i=0;i<paths.length;i++) {
let catalogBasePath = path.join(CATALOG_DIR,repo,branch,paths[i]);
try {
var files = await walkFiles(path.join(catalogBasePath,'*'));
files.forEach(f=> {
f = path.join(paths[i],'*',f);
result[repo].branches[branch].catalogs.push(f)
})
} catch(err) {
console.log(err);
}
}
}
}
}
}
return result;
}
async function walkFiles(dir) {
return new Promise((resolve,reject) => {
const items = [] // files, directories, symlinks, etc
klaw(dir)
.on('data', item => !item.stats.isDirectory()?items.push(item.path.substring(dir.length+1)):null)
.on('end', () => { resolve(items )})
.on('error', err => { console.log(err); reject(err)})
});
}
// function getFiles(dir,trimmedDir) {
// trimmedDir = trimmedDir || "";
// var result = {name:trimmedDir,catalogs:{}};
// return new Promise((resolve,reject) => {
// var promises = [];
// fs.readdir(dir, (err, files) => {
// if (err) {
// return reject(err);
// }
// files.forEach(file => {
// if (file.charAt(0) !== '.') {
// var stat = fs.statSync(path.join(dir, file));
// if (stat.isFile()) {
// result.files = result.files||[];
// result.files.push(file);
// } else if (stat.isDirectory()) {
// promises.push(getFiles(path.join(dir,file),path.join(trimmedDir,file)).then(subdirResult => {
// if (file === 'locales') {
// let localeFiles = {};
// locales = Object.keys(subdirResult.dirs||{});
// locales.forEach(locale => {
// subdirResult.dirs[locale].files.forEach(messageFile => {
// if ((messageFile.substr(-5) === ".json") && (subdirResult.name.indexOf("node_modules") === -1)) {
// var fullPath = path.join(subdirResult.name,messageFile);
// localeFiles[fullPath] = localeFiles[fullPath] || [];
// localeFiles[fullPath].push(locale);
// }
// })
// });
// result.catalogs = localeFiles;
// // } else if (subdirResult.hasOwnProperty('catalogs')) {
// // result = {
// // catalogs: subdirResult.catalogs,
// // name: subdirResult.trimmedDir
// // }
// } else {
// result.catalogs = Object.assign({},result.catalogs,subdirResult.catalogs);
// result.dirs = result.dirs ||{};
// result.dirs[file] = subdirResult;
// }
// }));
// }
// }
// });
// resolve(Promise.all(promises).then(() => {
// if (trimmedDir !== "") {
// return result
// } else {
// return result.catalogs
// }
// }))
// })
// });
// }