diff --git a/script/create-webhooks-for-new-version.js b/script/create-webhooks-for-new-version.js new file mode 100755 index 0000000000..bcf0fa8b11 --- /dev/null +++ b/script/create-webhooks-for-new-version.js @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +const fs = require('fs') +const mkdirp = require('mkdirp').sync +const path = require('path') +const program = require('commander') +const allVersions = require('../lib/all-versions') + +// [start-readme] +// +// This script creates new static webhook payload files for a new version. +// +// [end-readme] + +program + .description('Create new payload files in lib/webhooks/static/ based on an existing version.') + .option('-n, --newVersion ', 'The version to copy the payloads to. Must be in format.') + .option('-o, --oldVersion ', 'The version to copy the payloads from. Must be in format.') + .parse(process.argv) + +if (!(program.newVersion && program.oldVersion)) { + console.log('Error! You must provide --newVersion and --oldVersion.') + process.exit(1) +} + +if (!(Object.keys(allVersions).includes(program.newVersion) && Object.keys(allVersions).includes(program.oldVersion))) { + console.log('Error! You must provide the full name of a supported version, e.g., enterprise-server@2.22.') + process.exit(1) +} + +const newVersionDirName = allVersions[program.newVersion].miscVersionName +const oldVersionDirName = allVersions[program.oldVersion].miscVersionName + +const payloadsDir = 'lib/webhooks/static' +const srcDir = path.join(payloadsDir, oldVersionDirName) +const destDir = path.join(payloadsDir, newVersionDirName) + +// create the new directory +mkdirp(destDir) + +// copy the files +fs.readdirSync(srcDir).forEach(file => { + const srcFile = path.join(srcDir, file) + const destFile = path.join(destDir, file) + fs.copyFileSync(srcFile, destFile) +}) + +// check that it worked +if (!fs.existsSync(destDir)) { + console.log(`Error! A new directory was not successfully created at ${destDir}.`) + process.exit(1) +} + +if (!fs.readdirSync(destDir).length) { + console.log(`Error! The directory created at ${destDir} is empty.`) + process.exit(1) +} + +// print success message +console.log(`Done! Copied ${srcDir} to ${destDir}.`)