diff --git a/bin/downloadData.js b/bin/downloadData.js index da7e8656..0bbbfda4 100644 --- a/bin/downloadData.js +++ b/bin/downloadData.js @@ -1,11 +1,13 @@ -'use strict'; - -const config = require('pelias-config').generate(); -const validateISOCode = require('../lib/validateISOCode'); - -const countryCode = validateISOCode(config.imports.geonames.countryCode); - -const filename = countryCode === 'ALL' ? 'allCountries' : countryCode; - -const task = require('../lib/tasks/download'); -task(filename); +'use strict'; + +const config = require('pelias-config').generate(); +const countryCodeArrayCreator = require('../lib/countryCodeArrayCreator'); + + +const countrycode = countryCodeArrayCreator(config.imports.geonames.countryCode); + +const task = require('../lib/tasks/download'); +for (var i in countrycode) { + //No ISO code validation required, already done in countryCodeArrayCreator + task(countrycode[i]); +} diff --git a/import.js b/import.js index 71155407..820959a1 100644 --- a/import.js +++ b/import.js @@ -1,18 +1,17 @@ const config = require('pelias-config').generate(); const _ = require('lodash'); const logger = require('pelias-logger').get('geonames'); +const countryCodeArrayCreator = require('./lib/countryCodeArrayCreator'); if (_.has(config, 'imports.geonames.adminLookup')) { logger.info('imports.geonames.adminLookup has been deprecated, ' + - 'enable adminLookup using imports.adminLookup.enabled = true'); + 'enable adminLookup using imports.adminLookup.enabled = true'); } - -const resolvers = require( './lib/tasks/resolvers' ); +const countrycode = countryCodeArrayCreator(config.imports.geonames.countryCode); +const resolvers = require('./lib/tasks/resolvers'); const task = require('./lib/tasks/import'); -const validateISOCode = require('./lib/validateISOCode'); - -const isocode = validateISOCode( config.imports.geonames.countryCode ); -const filename = isocode === 'ALL' ? 'allCountries' : isocode; -const source = resolvers.selectSource( filename ); - -task( source ); +for (var i in countrycode) { + //No ISO code validation required, already done in countryCodeArrayCreator + const source = resolvers.selectSource(countrycode[i]); + task(source); +} diff --git a/lib/countryCodeArrayCreator.js b/lib/countryCodeArrayCreator.js new file mode 100644 index 00000000..b5be4e84 --- /dev/null +++ b/lib/countryCodeArrayCreator.js @@ -0,0 +1,22 @@ +const validateISOCode = require('./validateISOCode'); + +//Just wraps the string into an array and returns an array if all countryCodes are valid +function countryCodeArrayCreator(countrycode) { + if (typeof countrycode === 'string') { + const isocode = validateISOCode(countrycode); + const filename = isocode === 'ALL' ? 'allCountries' : isocode; + let countrycodeArray = []; + countrycodeArray.push(filename); + return countrycodeArray; + } else if (Array.isArray(countrycode)) { + for (var i in countrycode) { + validateISOCode(countrycode[i]); + } + return countrycode; + } else { + throw new Error('imports.geonames.countryCode must be either a string ' + + 'or an array of strings.'); + } +} + +module.exports = countryCodeArrayCreator;