forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-webhook-files.js
executable file
·76 lines (62 loc) · 2.03 KB
/
create-webhook-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
// [start-readme]
//
// This script creates new static webhook payload files for a new version.
//
// [end-readme]
import fs from 'fs'
import mkdirp from 'mkdirp'
import path from 'path'
import { program } from 'commander'
import { allVersions } from '../../lib/all-versions.js'
const payloadsDir = 'lib/webhooks/static'
program
.description(
'Create new payload files in lib/webhooks/static/<new_version> based on an existing version.'
)
.option(
'-n, --newVersion <version>',
'The version to copy the payloads to. Must be in <plan@release> format.'
)
.option(
'-o, --oldVersion <version>',
'The version to copy the payloads from. Must be in <plan@release> format.'
)
.parse(process.argv)
const newVersion = program.opts().newVersion
const oldVersion = program.opts().oldVersion
if (!(newVersion && oldVersion)) {
console.log('Error! You must provide --newVersion and --oldVersion.')
process.exit(1)
}
if (
!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).includes(oldVersion))
) {
console.log(
'Error! You must provide the full name of a currently supported version, e.g., [email protected].'
)
process.exit(1)
}
const newVersionDirName = allVersions[newVersion].miscVersionName
const oldVersionDirName = allVersions[oldVersion].miscVersionName
const srcDir = path.join(payloadsDir, oldVersionDirName)
const destDir = path.join(payloadsDir, newVersionDirName)
// create the new directory
await 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}.`)