Created
September 13, 2020 22:14
-
-
Save officialhopsof/faafdde667e10cc4f9de24df92c30b0b to your computer and use it in GitHub Desktop.
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
// In order to automatically deploy our ApiStage from Cloudformation, our | |
// Deployment resource name needs to be unique. In order to achieve this, | |
// we are to create a preprocessor that will find the string "$TIMESTAMP$" | |
// and replace it with the system time. This will allow us to create a unique | |
// resource name. | |
const fs = require("fs") | |
const path = require("path") | |
const replacements = [ | |
{ input: /\$TIMESTAMP\$/g, output: Date.now().toString() } | |
] | |
const getAllFiles = function (dirPath, arrayOfFiles) { | |
files = fs.readdirSync(dirPath) | |
arrayOfFiles = arrayOfFiles || [] | |
files.forEach(file => { | |
if (fs.statSync(dirPath + "/" + file).isDirectory()) { | |
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles) | |
} else { | |
arrayOfFiles.push(path.join(dirPath, "/", file)) | |
} | |
}) | |
return arrayOfFiles.filter(s => s.endsWith('/template.yml') || s.endsWith('/template.yaml')); | |
} | |
const projectPath = path.join(__dirname, '../'); | |
const inputPath = path.join(projectPath, 'api/'); | |
console.log(`Preprocessing Templates from: ${inputPath}`) | |
const outputPath = path.join(__dirname, '../dist/api/') | |
const templates = getAllFiles(inputPath); | |
templates.forEach(template => { | |
fs.readFile(template, 'utf8', function (err, data) { | |
if (err) throw err; | |
replacements.forEach(replacement => { | |
if (template.startsWith(inputPath)) { | |
let outPath = path.join(outputPath, template.slice(inputPath.length)); | |
// Create any needed output directories | |
fs.mkdirSync(path.dirname(outPath), { recursive: true }); | |
console.log(`PreProcessing: ./${template.slice(projectPath.length)} => ./${outPath.slice(projectPath.length)}`) | |
templateOutput = data.replace(replacement.input, replacement.output); | |
fs.writeFileSync(outPath, templateOutput) | |
} else { | |
throw `Template: ${template} is not in the directory ${inputPath}`; | |
} | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment