Created
October 21, 2019 20:54
-
-
Save jaecktec/8f386d6525ee1c1125a6d4b1f90f060e to your computer and use it in GitHub Desktop.
file inliner to inline markdown code into swagger documents
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
#!/usr/bin/env node | |
const commandLineArgs = require('command-line-args'); | |
const fs = require('fs'); | |
String.prototype.replaceAll = function (search, replacement) { | |
const target = this; | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
const optionDefinitions = [ | |
{name: 'sourceFile', alias: 's', type: String}, | |
{name: 'outputFile', alias: 'o', type: String}, | |
{name: 'relativeResolvePath', alias: 'b', type: String}, | |
]; | |
const options = commandLineArgs(optionDefinitions); | |
if(options["sourceFile"] === undefined){ | |
console.log("parameter s (sourceFile) is needed") | |
process.exit(-1) | |
} | |
if(options["outputFile"] === undefined){ | |
console.log("parameter o (outputFile) is needed") | |
process.exit(-1) | |
} | |
if(options["relativeResolvePath"] === undefined){ | |
options["relativeResolvePath"] = __dirname; | |
process.exit(-1) | |
} | |
async function escapeFileIntoString(filePath) { | |
let replacement = await readFile(options['relativeResolvePath'] + "/" + filePath); | |
replacement = replacement.replaceAll(/\\/, '\\\\'); | |
replacement = replacement.replaceAll(/\n/, '\\n'); | |
replacement = replacement.replaceAll(/\r/, '\\r'); | |
replacement = replacement.replaceAll(/"/, '\\"'); | |
return replacement | |
} | |
function readFile(filePath) { | |
return new Promise((resolve, reject) => { | |
fs.readFile(filePath, 'utf8', function (err, contents) { | |
if (err) reject(err); | |
resolve(contents) | |
}); | |
}); | |
} | |
(async () => { | |
console.log(options); | |
let content = await readFile(options["sourceFile"]); | |
const placeholderPattern = /(\${{file:\/\/([\.\w\/]*)}})/; | |
const contentArr = content.split('\n'); | |
await new Promise(resolve => { | |
let countDown = contentArr.length; | |
contentArr.forEach(async (elem, index, arr) => { | |
const match = elem.match(placeholderPattern); | |
if (match && match.length > 1) { | |
const fileToBeResolved = match[2]; | |
const replacement = await escapeFileIntoString(fileToBeResolved); | |
arr[index] = elem.replace(match[1], replacement) | |
} | |
countDown--; | |
if (countDown < 1) { | |
resolve(); | |
} | |
}); | |
}); | |
fs.writeFile(options["outputFile"], contentArr.join('\n'), function (err) { | |
if (err) { | |
return console.log(err); | |
} | |
}); | |
})().catch(console.error); |
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
{ | |
"name": "file-inliner", | |
"version": "0.0.1", | |
"description": "tool to inline ${{file://file.txt}} into a file", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Constantin-Marius Jaeck", | |
"license": "MIT", | |
"bin": { | |
"file-inliner": "./index.js" | |
}, | |
"dependencies": { | |
"command-line-args": "^5.0.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment