diff --git a/scripts/register-domains.js b/scripts/register-domains.js index e69de29bb..0d53b5e67 100644 --- a/scripts/register-domains.js +++ b/scripts/register-domains.js @@ -0,0 +1,37 @@ +const R = require('ramda'); +const { VALID_RECORD_TYPES, NC_DOMAIN } = require('../utils/constants'); +const { domainService: domain } = require('../utils/domain-service'); +const { getDomains } = require('../utils/domain'); + +const getRecords = R.compose(R.toPairs, R.pick(VALID_RECORD_TYPES)); + +const toHostList = R.chain(data => { + const rs = getRecords(data.record); + + const records = R.chain(([recordType, urls]) => + urls.map(url => ({ + HostName: data.name, + RecordType: recordType, + Address: url, + })) + , rs); + + return !data.forceHttps ? records : records.concat([ + { HostName: data.name, RecordType: 'URL', Address: `https://${data.name}.${NC_DOMAIN}` }, + ]); +}); + +const registerDomains = async ({ domainService, getDomains }) => { + const domains = await getDomains().then(toHostList); +}; + +const main = () => { + console.log('Running cli'); +}; + +if (require.main === module) { + main(); +} else { + module.exports = { toHostList, registerDomains }; +} + diff --git a/utils/constants.js b/utils/constants.js new file mode 100644 index 000000000..bd2de0d05 --- /dev/null +++ b/utils/constants.js @@ -0,0 +1,4 @@ +module.exports = { + VALID_RECORD_TYPES: ['CNAME', 'A', 'ALIAS', 'URL'], + NC_DOMAIN: 'booboo.xyz', +}; diff --git a/utils/domain-service.js b/utils/domain-service.js index 4f768ebf1..a3d997a8d 100644 --- a/utils/domain-service.js +++ b/utils/domain-service.js @@ -34,9 +34,7 @@ const getDomainService = ({ Namecheap }) => { return list; }; - const setHosts = hosts => { - return nc.dns.setHosts(NC_DOMAIN, hosts); - }; + const setHosts = hosts => nc.dns.setHosts(NC_DOMAIN, hosts); const getHostKey = host => `${host.HostName}--${host.RecordType}`; const toHostMap = hosts => hosts.reduce((acc, host) => { diff --git a/utils/register.test.js b/utils/register.test.js new file mode 100644 index 000000000..da83e55aa --- /dev/null +++ b/utils/register.test.js @@ -0,0 +1,38 @@ +const { toHostList } = require('../scripts/register-domains'); +const { NC_DOMAIN } = require('../utils/constants'); + +describe('toHostList', () => { + it('should flatten domain data to list of hosts (without https)', () => { + const res = toHostList([ + { name: 'akshay', forceHttps: false, record: { CNAME: ['phenax.github.io'] } }, + { name: 'foobar', forceHttps: false, record: { CNAME: ['v.io'] } }, + { name: 'xx', forceHttps: false, record: { A: ['1.2.3.4', '5.6.3.2', '1.2.31.1'] } }, + ]); + + expect(res).toEqual([ + { HostName: 'akshay', RecordType: 'CNAME', Address: 'phenax.github.io' }, + { HostName: 'foobar', RecordType: 'CNAME', Address: 'v.io' }, + { HostName: 'xx', RecordType: 'A', Address: '1.2.3.4' }, + { HostName: 'xx', RecordType: 'A', Address: '5.6.3.2' }, + { HostName: 'xx', RecordType: 'A', Address: '1.2.31.1' } + ]); + }); + + it('should flatten domain data to list of hosts (with https)', () => { + const res = toHostList([ + { name: 'akshay', forceHttps: true, record: { CNAME: ['phenax.github.io'] } }, + { name: 'foobar', forceHttps: false, record: { CNAME: ['v.io'] } }, + { name: 'xx', forceHttps: true, record: { A: ['1.2.3.4', '5.6.3.2', '1.2.31.1'] } }, + ]); + + expect(res).toEqual([ + { HostName: 'akshay', RecordType: 'CNAME', Address: 'phenax.github.io' }, + { HostName: 'akshay', RecordType: 'URL', Address: `https://akshay.${NC_DOMAIN}` }, + { HostName: 'foobar', RecordType: 'CNAME', Address: 'v.io' }, + { HostName: 'xx', RecordType: 'A', Address: '1.2.3.4' }, + { HostName: 'xx', RecordType: 'A', Address: '5.6.3.2' }, + { HostName: 'xx', RecordType: 'A', Address: '1.2.31.1' }, + { HostName: 'xx', RecordType: 'URL', Address: `https://xx.${NC_DOMAIN}` }, + ]); + }); +});