|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const { execSync } = require('child_process') |
| 6 | +const semver = require('semver') |
| 7 | + |
| 8 | +/* |
| 9 | + * This script performs two checks to prevent shipping development mode OpenAPI schemas: |
| 10 | +* - Ensures the `info.version` property is a semantic version. |
| 11 | +* In development mode, the `info.version` property is a string |
| 12 | +* containing the `github/github` branch name. |
| 13 | +* - Ensures the decorated schema matches the dereferenced schema. |
| 14 | +* The workflow that calls this script runs `script/rest/update-files.js` |
| 15 | +* with the `--decorate-only` switch then checks to see if files changed. |
| 16 | +* |
| 17 | +*/ |
| 18 | + |
| 19 | +// Check that the `info.version` property is a semantic version |
| 20 | +const dereferencedDir = path.join(process.cwd(), 'lib/rest/static/dereferenced') |
| 21 | +const schemas = fs.readdirSync(dereferencedDir) |
| 22 | +schemas.forEach(filename => { |
| 23 | + const schema = require(path.join(dereferencedDir, filename)) |
| 24 | + if (!semver.valid(schema.info.version)) { |
| 25 | + console.log(`🚧⚠️ Your branch contains a development mode OpenAPI schema: ${schema.info.version}. This check is a reminder to not 🚢 OpenAPI files in development mode. 🛑`) |
| 26 | + process.exit(1) |
| 27 | + } |
| 28 | +}) |
| 29 | + |
| 30 | +// Check that the decorated schema matches the dereferenced schema |
| 31 | +const changedFiles = execSync('git diff --name-only HEAD').toString() |
| 32 | + |
| 33 | +if(changedFiles !== '') { |
| 34 | + console.log(`These files were changed:\n${changedFiles}`) |
| 35 | + console.log(`🚧⚠️ Your decorated and dereferenced schema files don't match. Ensure you're using decorated and dereferenced schemas from the automatically created pull requests by the 'github-openapi-bot' user. For more information, see 'script/rest/README.md'. 🛑`) |
| 36 | + process.exit(1) |
| 37 | +} |
| 38 | + |
| 39 | +// All checks pass, ready to ship |
| 40 | +console.log('All good 👍') |
| 41 | +process.exit(0) |
0 commit comments