Refactors out some of the helpers

This commit is contained in:
Akshay Nair
2020-10-17 21:21:59 +05:30
parent 08c7e5c65a
commit 7155beb00c
6 changed files with 24 additions and 33 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
const R = require('ramda');
const { VALID_RECORD_TYPES, TTL, ENV } = require('../utils/constants');
const { domainService: dc } = require('../utils/domain-service');
const { getDomains: gd } = require('../utils/domain');
const { getDomains: gd } = require('../utils/get-domain');
const getRecords = R.compose(R.toPairs, R.pick(VALID_RECORD_TYPES));
+1 -1
View File
@@ -1,4 +1,4 @@
const { getDomains } = require('../utils/domain');
const { getDomains } = require('../utils/get-domain');
describe('getDomains', () => {
it('should resolve with the list of domains', async () => {
+1 -1
View File
@@ -1,6 +1,6 @@
const R = require('ramda');
const fs = require('fs');
const { getDomains } = require('../utils/domain');
const { getDomains } = require('../utils/get-domain');
const { validateDomainData } = require('../utils/validations');
const { DOMAINS_PATH } = require('../utils/constants');
+5 -28
View File
@@ -1,8 +1,9 @@
const R = require('ramda');
const { cpanel } = require('./lib/cpanel');
const { DOMAIN_DOMAIN, IS_TEST } = require('./constants');
const { log, print, lazyTask, batchLazyTasks } = require('./helpers');
const log = IS_TEST ? () => {} : console.log;
const BATCH_SIZE = 1;
const recordToRedirection = ({ name, address }) => ({
domain: `${name}.${DOMAIN_DOMAIN}`,
@@ -46,20 +47,6 @@ const diffRecords = (oldRecords, newRecords) => {
return { add, remove };
};
const print = fn => x => log(fn(x)) || x;
const lazyTask = fn => data => () => fn(data);
const batchLazyTasks = count => tasks => tasks.reduce((batches, task) => {
if (batches.length === 0) return [[task]];
const full = R.init(batches);
const last = R.last(batches);
if (last.length >= count) return [...batches, [task]];
return [...full, [...last, task]];
}, []);
const executeBatch = (batches) => batches.reduce((promise, batch, index) => {
return promise.then(async () => {
log('>>> Running batch number:', index + 1, `(size: ${batch.length})`);
@@ -77,8 +64,6 @@ const executeBatch = (batches) => batches.reduce((promise, batch, index) => {
}, Promise.resolve());
const getDomainService = ({ cpanel }) => {
let hostList = [];
const fetchZoneRecords = () => cpanel.zone.fetch().then(R.map(zoneToRecord));
const fetchRedirections = () => cpanel.redirection.fetch().then(R.map(redirectionToRecord));
@@ -105,19 +90,11 @@ const getDomainService = ({ cpanel }) => {
print(({ name }) => `Deleting redirection for ${name}`),
));
const getHosts = async () => {
if (hostList.length) return hostList;
const list = await Promise.all([fetchZoneRecords(), fetchRedirections()]).then(R.flatten);
hostList = list;
return list;
};
const BATCH_SIZE = 1;
const getHosts = () =>
Promise.all([fetchZoneRecords(), fetchRedirections()]).then(R.flatten);
const addRecords = R.compose(batchLazyTasks(BATCH_SIZE), R.filter(Boolean), R.map(R.cond([
[ R.propEq('name', 'www'), R.always(null) ],
[ R.propEq('name', 'www'), R.always(null) ], // Ignore www
[ R.propEq('type', 'URL'), addRedirection ],
[ R.T, addZoneRecord ],
])));
+16 -2
View File
@@ -1,6 +1,8 @@
const R = require('ramda');
const { IS_TEST } = require('./constants');
const log = m => x => console.log(m, x) || x;
const log = IS_TEST ? () => {} : console.log;
const print = fn => x => log(fn(x)) || x;
const between = (min, max) => num => num >= min && num <= max;
const testRegex = regex => str => !!(str && str.match(regex));
@@ -16,5 +18,17 @@ const and = R.allPass;
const then = fn => p => p.then(fn);
module.exports = { or, and, validate, between, testRegex, log, then };
const lazyTask = fn => data => () => fn(data);
const batchLazyTasks = count => tasks => tasks.reduce((batches, task) => {
if (batches.length === 0) return [[task]];
const full = R.init(batches);
const last = R.last(batches);
if (last.length >= count) return [...batches, [task]];
return [...full, [...last, task]];
}, []);
module.exports = { or, and, validate, between, testRegex, log, print, then, lazyTask, batchLazyTasks };