generate.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import minimist from 'minimist'
  2. import path from 'path'
  3. import fs from 'fs'
  4. import { version, generate, compress } from 'libphonenumber-metadata-generator'
  5. // https://ru.stackoverflow.com/questions/1281148/referenceerror-dirname-is-not-defined
  6. import { fileURLToPath } from 'url'
  7. import { dirname } from 'path'
  8. const __filename = fileURLToPath(import.meta.url)
  9. const __dirname = dirname(__filename)
  10. // const REGION_CODE_FOR_NON_GEO_ENTITY = '001'
  11. const input = fs.readFileSync(path.join(__dirname, process.argv[2]), 'utf8')
  12. const output_file = process.argv[3]
  13. const command_line_arguments = minimist(process.argv.slice(4))
  14. // Included countries
  15. let included_countries
  16. if (command_line_arguments.countries) {
  17. included_countries = command_line_arguments.countries.split(',')
  18. console.log('Included countries:', included_countries)
  19. included_countries = new Set(included_countries)
  20. }
  21. // Include all regular expressions
  22. let extended = false
  23. if (command_line_arguments.extended) {
  24. console.log('Include extra validation regular expressions')
  25. extended = true
  26. }
  27. // Included phone number types
  28. let included_phone_number_types
  29. if (command_line_arguments.types) {
  30. included_phone_number_types = command_line_arguments.types.split(',')
  31. console.log('Included phone number types:', included_phone_number_types)
  32. included_phone_number_types = new Set(included_phone_number_types)
  33. }
  34. // Generate and compress metadata
  35. generate(input, version, included_countries, extended, included_phone_number_types).then((output) => {
  36. // Write uncompressed metadata into a file for easier debugging
  37. if (command_line_arguments.debug) {
  38. console.log('Output uncompressed JSON for debugging')
  39. fs.writeFileSync(path.join(__dirname, '../metadata.json'), JSON.stringify(output, undefined, 3))
  40. }
  41. // Compress the generated metadata
  42. fs.writeFileSync(path.join(__dirname, output_file), JSON.stringify(compress(output)))
  43. // Output mobile phone number type examples
  44. if (command_line_arguments.examples === 'mobile') {
  45. var examples = Object.keys(output.countries).reduce(function(out, country_code) {
  46. // if (country_code === REGION_CODE_FOR_NON_GEO_ENTITY) {
  47. // return out
  48. // }
  49. var mobile = output.countries[country_code].examples.mobile
  50. var fixed_line = output.countries[country_code].examples.fixed_line
  51. if (mobile) {
  52. out[country_code] = mobile
  53. }
  54. // "TA" country doesn't have any mobile phone number example
  55. else if (fixed_line) {
  56. console.warn(`Country ${country_code} doesn't have a mobile phone number example. Substituting with a fixed line phone number example.`)
  57. out[country_code] = fixed_line
  58. } else {
  59. console.error(`Country ${country_code} doesn't have neither a mobile phone number example nor a fixed line phone number example.`)
  60. // `async` errors aren't being caught at the top level in Node.js
  61. process.exit(1)
  62. }
  63. return out
  64. }, {})
  65. fs.writeFileSync(
  66. path.join(__dirname, '../examples.mobile.json'),
  67. JSON.stringify(examples)
  68. )
  69. }
  70. })