2017-12-04 23:33:08 +00:00
#!/usr/bin/env node
2017-12-13 21:19:58 +00:00
2018-06-13 08:10:41 +00:00
const meName = 'generate-json-tld-desc.json' ;
2017-12-13 21:19:58 +00:00
2017-12-14 04:49:59 +00:00
process . on ( 'unhandledRejection' , error => {
console . error ( meName + ": (FATAL)" , error ) ;
process . exit ( 1 ) ;
} ) ;
const countries = require ( 'country-data' ) . countries ;
const country = require ( 'countryjs' ) ;
const parse = require ( 'csv-parse' ) ;
const fs = require ( 'fs-extra' ) ;
const path = require ( 'path' ) ;
2018-06-14 20:14:43 +00:00
const md5File = require ( 'md5-file' ) ;
2017-12-14 04:49:59 +00:00
const pathinfo = require ( 'pathinfo' ) ;
const program = require ( 'commander' ) ;
const tmp = require ( 'tmp' ) ;
2018-06-13 05:51:12 +00:00
//tmp.setGracefulCleanup();
2017-12-14 04:49:59 +00:00
2018-06-22 01:06:57 +00:00
const fileTldDescJs = path . dirname ( require . main . filename ) + '/../../../formats/json/tld-desc.json' ;
const fileTldsCsv = path . dirname ( require . main . filename ) + '/../../../tlds.csv' ;
2017-12-14 04:49:59 +00:00
program
. option ( '-q, --quiet' , 'Quiet Mode' )
. parse ( process . argv ) ;
if ( ! program . quiet ) {
console . log ( meName ) ;
console . log ( " (c) 2017 Doug Bird, All Rights Reserved." ) ;
console . log ( " see README.md for licensing and other information" ) ;
console . log ( " https://github.com/katmore/tld-enum#readme" ) ;
console . log ( "" ) ;
2018-06-13 08:10:41 +00:00
console . log ( " Generates new JSON format file 'tld-desc.json' from the 'tlds.csv' file" ) ;
2017-12-14 04:49:59 +00:00
console . log ( "" ) ;
}
( async ( ) => {
const tmpDir = tmp . dirSync ( { unsafeCleanup : true } ) ;
2018-06-13 05:51:12 +00:00
const fileNewTldDescJson = tmpDir . name + '/tld-desc.json' ;
2017-12-14 04:49:59 +00:00
let existingMd5 = null ;
2018-06-13 05:51:12 +00:00
if ( fs . existsSync ( fileTldDescJs ) ) {
2018-06-14 20:14:43 +00:00
existingMd5 = md5File . sync ( fileTldDescJs ) ;
2018-06-13 05:51:12 +00:00
const pathinfoTlds = pathinfo ( fileTldDescJs ) ;
const fileBackupTlds = pathinfoTlds . dirname + pathinfoTlds . sep + pathinfoTlds . basename + '-' + existingMd5 + '-backup.js' ;
2017-12-14 04:49:59 +00:00
if ( ! fs . existsSync ( fileBackupTlds ) ) {
2018-06-13 05:51:12 +00:00
fs . copySync ( fileTldDescJs , fileBackupTlds ) ;
2017-12-14 04:49:59 +00:00
}
}
process . stdout . write ( "reading 'tlds.csv'..." ) ;
let parser = parse ( { delimiter : ',' } ) ;
2018-06-13 05:51:12 +00:00
let tldDesc = { } ;
2018-06-14 20:24:46 +00:00
let i = 0 ;
2017-12-14 04:49:59 +00:00
parser . on ( 'readable' , function ( ) {
2018-06-13 05:51:12 +00:00
let row , domain , desc ;
2017-12-14 04:49:59 +00:00
while ( row = parser . read ( ) ) {
if ( ! row . length ) {
2018-06-13 05:51:12 +00:00
console . error ( meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv + "'" ) ;
2017-12-14 04:49:59 +00:00
process . exit ( 1 ) ;
}
2018-06-13 05:51:12 +00:00
if ( typeof row [ 1 ] === 'undefined' ) {
console . error ( meName + ": (FATAL) invalid 'tlds.csv', missing column 2 on row #" + i + " in '" + fileTldsCsv + "'" ) ;
process . exit ( 1 ) ;
}
domain = row [ 0 ] ;
desc = row [ 1 ] ;
tldDesc [ domain ] = desc ;
2017-12-14 04:49:59 +00:00
i ++ ;
}
} ) ;
parser . write ( fs . readFileSync ( fileTldsCsv ) ) ;
2018-06-14 20:14:43 +00:00
parser . end ( function ( ) {
console . log ( "done" ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
process . stdout . write ( "generating new 'tld-desc.json' file..." ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
fs . appendFileSync ( fileNewTldDescJson , JSON . stringify ( tldDesc , null , 2 ) ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
console . log ( "done" ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
if ( existingMd5 ) {
const newMd5 = md5File . sync ( fileNewTldDescJson ) ;
if ( newMd5 == existingMd5 ) {
console . error ( meName + ": (NOTICE) ignoring newly generated 'tld-desc.json' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldDescJs + ")" ) ;
return ;
}
}
fs . copySync ( fileNewTldDescJson , fileTldDescJs ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
console . log ( "saved new 'tld-desc.json' file" ) ;
} ) ;
2017-12-14 04:49:59 +00:00
} ) ( ) ;