Created
June 21, 2018 13:29
-
-
Save anmolgkv/8ed41515b69bb7bd7e9603dc22f6bf2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { promisify } = require('util'); | |
const xml2js = require('xml2js'); | |
const parser = new xml2js.Parser(); | |
const parseString = promisify(parser.parseString); | |
var jsonexport = promisify(require('jsonexport')); | |
const fs = require('fs'); | |
const {sep} = require('path'); | |
const readdir = promisify(fs.readdir); | |
const readFile = promisify(fs.readFile); | |
const writeFile = promisify(fs.writeFile); | |
// Reading and Parsing the file | |
var sourcePath = process.argv[2] || 'Objects'; | |
var destinationPath = process.argv[3] || 'configuration'; | |
const directoryPath = __dirname + sep + sourcePath; | |
const destinationDirectoryPath = __dirname + sep + destinationPath; | |
readdir(directoryPath) | |
.then(processFiles) | |
.catch(console.error); | |
function processFiles(files) { | |
files = files.filter(file => {return file.indexOf('__mdt') < 0 && file.indexOf('object') > 0}) | |
console.log(files); | |
files.forEach(file => { | |
let filePath = directoryPath + sep + file | |
readFile(filePath) | |
.then(parseString, console.error) | |
.then(objectJSON => jsonexport(objectJSON.CustomObject.fields), console.error) | |
.then(csv => { | |
let csvFileName = file.replace('.object', '.csv'); | |
console.log(csvFileName); | |
let destinationFilePath = destinationDirectoryPath + sep + csvFileName; | |
writeFile(destinationFilePath, csv, 'utf8'); | |
}, console.error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment