Created
October 12, 2021 12:39
-
-
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)
This file contains 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 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