diff --git a/fetch.js b/fetch.js index 99ce157..d716c77 100644 --- a/fetch.js +++ b/fetch.js @@ -4,6 +4,7 @@ const fs = require('fs'); const path = require('path'); const jsdom = require('jsdom'); const { JSDOM } = jsdom; +const csvstringify = require('csv-stringify/lib/sync') const WIKI_URL = 'https://en.wikipedia.org/wiki/Mobile_country_code'; const WIKI_URL_REGIONS = [ @@ -15,7 +16,8 @@ const WIKI_URL_REGIONS = [ 'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_7xx_(South_America)' ]; -const MCC_MNC_OUTPUT_FILE = path.join( __dirname, 'mcc-mnc-list.json'); +const MCC_MNC_OUTPUT_FILE_JSON = path.join( __dirname, 'mcc-mnc-list.json'); +const MCC_MNC_OUTPUT_FILE_CSV = path.join( __dirname, 'mcc-mnc-list.csv'); const STATUS_CODES_OUTPUT_FILE = path.join( __dirname, 'status-codes.json'); @@ -174,11 +176,19 @@ function collect (resolve, from, records, statusCodes, globals) { } function writeData(records, statusCodes) { - fs.writeFile( MCC_MNC_OUTPUT_FILE, JSON.stringify( records, null, 2 ), err => { + fs.writeFile( MCC_MNC_OUTPUT_FILE_JSON, JSON.stringify( records, null, 2 ), err => { if ( err ) { throw err; } - console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE ); + console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE_JSON ); + console.log( 'Total ' + records.length + ' records' ); + }); + + fs.writeFile( MCC_MNC_OUTPUT_FILE_CSV, csvstringify( records , {header: true} ), err => { + if ( err ) { + throw err; + } + console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE_CSV ); console.log( 'Total ' + records.length + ' records' ); }); diff --git a/package-lock.json b/package-lock.json index ef0d46d..0ac58a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1357,6 +1357,12 @@ } } }, + "csv-stringify": { + "version": "5.3.6", + "resolved": "https://registry.npmjs.org/csv-stringify/-/csv-stringify-5.3.6.tgz", + "integrity": "sha512-kPcRbMvo5NLLD71TAqW5K+g9kbM2HpIZJLAzm73Du8U+5TXmDp9YtXKCBLyxEh0q3Jbg8QhNFBz3b5VJzjZ/jw==", + "dev": true + }, "currently-unhandled": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz", diff --git a/package.json b/package.json index 6079353..497db7c 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "license": "MIT", "devDependencies": { "ava": "^2.4.0", + "csv-stringify": "^5.3.6", "eslint": "^6.8.0", "jsdom": "^16.2.2" },