2017-12-13 21:36:06 +00:00
#!/usr/bin/env node
2018-06-13 08:10:41 +00:00
const meName = 'generate-js-tld-list.js' ;
2017-12-13 21:36:06 +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
const fileTldListJs = path . dirname ( require . main . filename ) + '/../../formats/js/tld-enum/list.js' ;
2017-12-14 04:49:59 +00:00
const fileTldsCsv = path . dirname ( require . main . filename ) + '/../../tlds.csv' ;
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" ) ;
2018-06-13 05:51:12 +00:00
console . log ( " https://github.com/katmore/tld-list#readme" ) ;
2017-12-14 04:49:59 +00:00
console . log ( "" ) ;
2018-06-13 08:10:41 +00:00
console . log ( " Generates new JavaScript format file 'list.js' from the 'tlds.csv' file" ) ;
2017-12-14 04:49:59 +00:00
console . log ( "" ) ;
}
( async ( ) => {
2018-06-13 05:51:12 +00:00
const tldEnumStartTldList = 'module.exports = ' ;
2017-12-14 04:49:59 +00:00
const tldEnumEndTldList = ';' ;
2018-06-13 05:51:12 +00:00
const tmpDir = tmp . dirSync ( { unsafeCleanup : true } ) ;
2017-12-14 04:49:59 +00:00
2018-06-13 05:51:12 +00:00
const fileNewTldListJs = tmpDir . name + '/list.js' ;
2017-12-14 04:49:59 +00:00
let existingMd5 = null ;
if ( fs . existsSync ( fileTldListJs ) ) {
2018-06-14 20:14:43 +00:00
existingMd5 = md5File . sync ( fileTldListJs ) ;
2017-12-14 04:49:59 +00:00
const pathinfoTlds = pathinfo ( fileTldListJs ) ;
const fileBackupTlds = pathinfoTlds . dirname + pathinfoTlds . sep + pathinfoTlds . basename + '-' + existingMd5 + '-backup.js' ;
if ( ! fs . existsSync ( fileBackupTlds ) ) {
fs . copySync ( fileTldListJs , fileBackupTlds ) ;
}
}
process . stdout . write ( "reading 'tlds.csv'..." ) ;
let parser = parse ( { delimiter : ',' } ) ;
let tldEnum = [ ] ;
parser . on ( 'readable' , function ( ) {
let i = 0 ;
let row ;
while ( row = parser . read ( ) ) {
if ( ! row . length ) {
console . error ( meName + ": (FATAL) invalid 'tlds.csv' row #" + i + ": " + fileTldsCsv ) ;
process . exit ( 1 ) ;
}
tldEnum . push ( row [ 0 ] ) ;
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 'list.js' file..." ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
fs . writeFileSync ( fileNewTldListJs , tldEnumStartTldList ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
fs . appendFileSync ( fileNewTldListJs , JSON . stringify ( tldEnum , null , 2 ) ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
fs . appendFileSync ( fileNewTldListJs , tldEnumEndTldList ) ;
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 ( fileNewTldListJs ) ;
if ( newMd5 == existingMd5 ) {
console . error ( meName + ": (NOTICE) ignoring newly generated 'list.js' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldListJs + ")" ) ;
return ;
}
}
fs . copySync ( fileNewTldListJs , fileTldListJs ) ;
2017-12-14 04:49:59 +00:00
2018-06-14 20:14:43 +00:00
console . log ( "saved new 'list.js' file" ) ;
} ) ;
2017-12-14 04:49:59 +00:00
} ) ( ) ;