-
Notifications
You must be signed in to change notification settings - Fork 108
/
build-indexes.js
33 lines (26 loc) · 1.26 KB
/
build-indexes.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
const { writeFile, readdir, lstatSync } = require('fs-promise');
const { join } = require('path');
const { logError, logSuccess } = require('./helper');
const path = name => join(__dirname, name ? `banks/${name}` : 'banks');
const generateRequiresFile = (filePath, requires) => {
const content = `module.exports = [${requires}\n];\n`;
writeFile(path(filePath), content).then(() => {
const dirPath = new RegExp(`${__dirname}/`);
logSuccess(filePath.replace(dirPath, ''));
}).catch((err) => { logError(`Can't write ${filePath} \n${err}`); });
};
const createRequires = (files) => {
const countries = files.filter(file => lstatSync(path(file)).isDirectory());
const indexFilesRequires = countries.map(country => `\n require('./${country}/index')`);
countries.forEach((country) => {
readdir(path(country))
.then((items) => {
const banksRequires = items
.filter(file => /\.json$/.test(file))
.map(bank => `\n require('./${bank.replace(/\.json$/, '')}')`);
generateRequiresFile(`${country}/index.js`, banksRequires);
}).catch((err) => { logError(`Can't read ${country} directory \n${err}`); });
});
generateRequiresFile('index.js', indexFilesRequires);
};
readdir(path()).then(createRequires).catch(logError);