Skip to content

Instantly share code, notes, and snippets.

@prumand
Created February 28, 2020 11:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prumand/73ae1a01d22029d7969ce8a5dcaa453d to your computer and use it in GitHub Desktop.
Save prumand/73ae1a01d22029d7969ce8a5dcaa453d to your computer and use it in GitHub Desktop.
Prepend empty lines to existing source-map
#!/usr/bin/env node
const sourceMap = require('source-map')
const fs = require('fs')
const path = require('path')
/**
* Prepend six lines to source-map
*/
const runSourceMapPrepend = async (sourceMapFile) => {
const sourceMapFileContent = fs.readFileSync(sourceMapFile, 'utf8')
const originalConsumer = await new sourceMap.SourceMapConsumer(
sourceMapFileContent,
)
const node = sourceMap.SourceNode.fromStringWithSourceMap(
sourceMapFileContent,
await new sourceMap.SourceMapConsumer(sourceMapFileContent),
)
// add six empty lines
node.prepend('\n\n\n\n\n')
const newConsumer = sourceMap.SourceNode.fromStringWithSourceMap(
sourceMapFileContent,
originalConsumer,
)
const generator = sourceMap.SourceMapGenerator.fromSourceMap(originalConsumer)
const newSourceMapFileName = 'prepended-' + path.basename(sourceMapFile)
fs.writeFileSync(newSourceMapFileName, generator.toString())
console.log(
'Prepended 6 empty lines to source-map. Written to: ' +
newSourceMapFileName,
)
}
const fileSource = process.argv[2]
if (!fileSource) {
throw Error('No source for source-map-file given!')
}
runSourceMapPrepend(fileSource)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment