From 7155beb00c1e82a61b9c85cd1ee70b41b4262ab7 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 17 Oct 2020 21:21:59 +0530 Subject: [PATCH] Refactors out some of the helpers --- scripts/register-domains.js | 2 +- tests/domain-utils.test.js | 2 +- tests/domains.test.js | 2 +- utils/domain-service.js | 33 +++++------------------------- utils/{domain.js => get-domain.js} | 0 utils/helpers.js | 18 ++++++++++++++-- 6 files changed, 24 insertions(+), 33 deletions(-) rename utils/{domain.js => get-domain.js} (100%) diff --git a/scripts/register-domains.js b/scripts/register-domains.js index 00626092f..69435063c 100644 --- a/scripts/register-domains.js +++ b/scripts/register-domains.js @@ -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)); diff --git a/tests/domain-utils.test.js b/tests/domain-utils.test.js index 2d6868b8d..077896726 100644 --- a/tests/domain-utils.test.js +++ b/tests/domain-utils.test.js @@ -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 () => { diff --git a/tests/domains.test.js b/tests/domains.test.js index fd80c3f43..6c58d7037 100644 --- a/tests/domains.test.js +++ b/tests/domains.test.js @@ -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'); diff --git a/utils/domain-service.js b/utils/domain-service.js index 560e77853..76d511a0f 100644 --- a/utils/domain-service.js +++ b/utils/domain-service.js @@ -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 ], ]))); diff --git a/utils/domain.js b/utils/get-domain.js similarity index 100% rename from utils/domain.js rename to utils/get-domain.js diff --git a/utils/helpers.js b/utils/helpers.js index 713a06797..2c1f61351 100644 --- a/utils/helpers.js +++ b/utils/helpers.js @@ -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 };