Last active
September 10, 2018 17:23
-
-
Save beaulac/1ecae22f9ddd545f464c876a79504eb8 to your computer and use it in GitHub Desktop.
Make IIS rewrite map from csv file.
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
'use strict'; | |
const fs = require('fs'); | |
const readline = require('readline'); | |
const [, , filename] = process.argv; | |
if (!filename) { | |
throw Error('NO FILENAME SPECIFIED'); | |
} | |
const rl = readline.createInterface({ | |
input: fs.createReadStream(filename, 'utf8') | |
}); | |
const baseName = filename.split('/').pop().replace('.csv', ''); | |
let output = `<rewriteMap name="${baseName}">`; | |
const makeRedirectLine = (original, redirect) => `<add key="${original}" value="${redirect}" />`; | |
let lineNumber = 1; | |
const processedOriginals = new Set(); | |
rl.on('line', line => { | |
if (lineNumber++ < 2) { | |
return; | |
} | |
const [original, redirect] = line.split(','); | |
if (original && redirect) { | |
if (processedOriginals.has(original)) { | |
console.warn(`Duplicate entry: ${original} (line ${lineNumber})`); | |
} else { | |
processedOriginals.add(original); | |
output += '\n\t'; | |
output += makeRedirectLine(original, redirect); | |
} | |
} | |
}); | |
rl.on('close', () => { | |
output += '\n</rewriteMap>\n'; | |
fs.writeFileSync(`${__dirname}/rewritemap_${baseName}.xml`, output); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment