Skip to content

Instantly share code, notes, and snippets.

@PatrickHeneise
Created October 12, 2021 12:39
Show Gist options
  • Save PatrickHeneise/bbca1a8c4816f92aa3796db41a4a6203 to your computer and use it in GitHub Desktop.
Save PatrickHeneise/bbca1a8c4816f92aa3796db41a4a6203 to your computer and use it in GitHub Desktop.
Read a file, skip every second line, write file (when copying code with line numbers)
const fs = require('fs')
const { join } = require('path')
function parse(file) {
const path = join(process.env.PWD, 'test', 'fixtures', file)
const content = fs.readFileSync(path, 'utf8')
const lines = content.split('\n')
let data = ''
let stringified = 'data = '
lines.forEach((line, idx) => {
if (!(idx % 2)) {
stringified += line
}
})
eval(stringified)
let template = 'module.exports = exports = '
template += JSON.stringify(data, null, 2)
fs.writeFileSync(`${path}.js`, template, 'utf8')
}
const file = process.argv[2]
parse(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment