Skip to content

Commit

Permalink
chore: automate openapi schema list (#6463)
Browse files Browse the repository at this point in the history
## About the changes
This PR automates the generation of exported open api schemas on
pre-commit, removing some manual steps and also standardizing the
process. The schema list definition now looks way simpler:

https://github.com/Unleash/unleash/blob/b6f3877296389fcecd344d1c9295027ac6c6d52a/src/lib/openapi/index.ts#L37-L49

Also added
https://github.com/Unleash/unleash/blob/2817e66b29ecf5bc908e340931cbdc0281192dea/src/lib/openapi/spec/index.ts#L1-L4
for devs
  • Loading branch information
gastonfournier committed Mar 8, 2024
1 parent 381af78 commit da41d3d
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 559 deletions.
7 changes: 7 additions & 0 deletions .husky/pre-commit
Expand Up @@ -2,3 +2,10 @@
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged

node .husky/update-openapi-spec-list.js

# Check if the src/lib/openapi/spec/index.ts file was modified and if so, add it to the commit
if git ls-files -m | grep -q "src/lib/openapi/spec/index.ts"; then
git add src/lib/openapi/spec/index.ts
fi
34 changes: 34 additions & 0 deletions .husky/update-openapi-spec-list.js
@@ -0,0 +1,34 @@
const fs = require('fs');
const path = require('path');

const directoryPath = path.join(`${__dirname}/..`, 'src/lib/openapi/spec');
const indexPath = path.join(directoryPath, 'index.ts');

// Read files from the directory
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('Could not list the directory.', err);
process.exit(1);
}

const exports = files
.filter((file) => file.includes('schema.ts')) // Filter files by 'schema.ts'
.map((file) => `export * from './${file.replace('.ts', '')}';`) // Create export statements
.join('\n');

// Append export statements to index.ts
const script = path.basename(__filename);
const message = `/**
* Auto-generated file by ${script}. Do not edit.
* To run it manually execute \`node ${script}\` from ${path.basename(
__dirname,
)}
*/\n`;
fs.writeFileSync(indexPath, `${message}${exports}\n${message}`, (err) => {
if (err) {
console.error('Could not append to file.', err);
process.exit(1);
}
console.log('Export statements added to index.ts successfully.');
});
});

0 comments on commit da41d3d

Please sign in to comment.