|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const mkdirp = require('mkdirp').sync |
| 5 | +const path = require('path') |
| 6 | +const program = require('commander') |
| 7 | +const allVersions = require('../lib/all-versions') |
| 8 | + |
| 9 | +// [start-readme] |
| 10 | +// |
| 11 | +// This script creates new static webhook payload files for a new version. |
| 12 | +// |
| 13 | +// [end-readme] |
| 14 | + |
| 15 | +program |
| 16 | + .description('Create new payload files in lib/webhooks/static/<new_version> based on an existing version.') |
| 17 | + .option('-n, --newVersion <version>', 'The version to copy the payloads to. Must be in <plan@release> format.') |
| 18 | + .option('-o, --oldVersion <version>', 'The version to copy the payloads from. Must be in <plan@release> format.') |
| 19 | + .parse(process.argv) |
| 20 | + |
| 21 | +if (!(program.newVersion && program.oldVersion)) { |
| 22 | + console.log('Error! You must provide --newVersion and --oldVersion.') |
| 23 | + process.exit(1) |
| 24 | +} |
| 25 | + |
| 26 | +if (!(Object.keys(allVersions).includes(program.newVersion) && Object.keys(allVersions).includes(program.oldVersion))) { |
| 27 | + console.log('Error! You must provide the full name of a supported version, e.g., [email protected].') |
| 28 | + process.exit(1) |
| 29 | +} |
| 30 | + |
| 31 | +const newVersionDirName = allVersions[program.newVersion].miscVersionName |
| 32 | +const oldVersionDirName = allVersions[program.oldVersion].miscVersionName |
| 33 | + |
| 34 | +const payloadsDir = 'lib/webhooks/static' |
| 35 | +const srcDir = path.join(payloadsDir, oldVersionDirName) |
| 36 | +const destDir = path.join(payloadsDir, newVersionDirName) |
| 37 | + |
| 38 | +// create the new directory |
| 39 | +mkdirp(destDir) |
| 40 | + |
| 41 | +// copy the files |
| 42 | +fs.readdirSync(srcDir).forEach(file => { |
| 43 | + const srcFile = path.join(srcDir, file) |
| 44 | + const destFile = path.join(destDir, file) |
| 45 | + fs.copyFileSync(srcFile, destFile) |
| 46 | +}) |
| 47 | + |
| 48 | +// check that it worked |
| 49 | +if (!fs.existsSync(destDir)) { |
| 50 | + console.log(`Error! A new directory was not successfully created at ${destDir}.`) |
| 51 | + process.exit(1) |
| 52 | +} |
| 53 | + |
| 54 | +if (!fs.readdirSync(destDir).length) { |
| 55 | + console.log(`Error! The directory created at ${destDir} is empty.`) |
| 56 | + process.exit(1) |
| 57 | +} |
| 58 | + |
| 59 | +// print success message |
| 60 | +console.log(`Done! Copied ${srcDir} to ${destDir}.`) |
0 commit comments