Skip to content

Instantly share code, notes, and snippets.

@rjhilgefort
Created July 25, 2019 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjhilgefort/0118d3086874319399a7b2450ea97eef to your computer and use it in GitHub Desktop.
Save rjhilgefort/0118d3086874319399a7b2450ea97eef to your computer and use it in GitHub Desktop.
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const parser = require('csv-parse/lib/sync')
const JsonStringify = x => JSON.stringify(x, null, 2)
const SRC_DIR = path.join(__dirname, '..', 'src')
_.forEach(
[
{
workingDir: path.join(SRC_DIR, 'foo'),
source: 'foo-file.csv',
dest: 'foo-file.csv.json',
},
],
({ workingDir, source, dest }) => {
const sourceCsv = fs.readFileSync(path.join(workingDir, source))
const sourceJson = parser(sourceCsv, { delimiter: ',', columns: true })
const outputFile = path.join(workingDir, dest)
console.log(`Writing ${outputFile.replace(SRC_DIR, '')}`)
fs.writeFileSync(outputFile, JsonStringify(sourceJson))
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment