Skip to content

feat(scripts): git hook to format generators #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

./scripts/ci/husky/pre-commit.js
./scripts/husky/pre-commit.js
./scripts/husky/format-generators.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the JSON one too? You can have a common format

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good idea

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"docker:build": "./scripts/docker/build.sh",
"docker:clean": "docker stop dev; docker rm -f dev; docker image rm -f api-clients-automation",
"docker:mount": "./scripts/docker/mount.sh",
"docker:no-tty": "docker exec -t dev yarn cli $*",
"docker:setup": "yarn docker:clean && yarn docker:build && yarn docker:mount",
"fix:json": "eslint --ext=json . --fix",
"github-actions:lint": "eslint --ext=yml .github/",
Expand Down
32 changes: 32 additions & 0 deletions scripts/husky/format-generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-commonjs */
const { exit } = require('process');

const ora = require('ora-classic');

const { run } = require('./utils');

async function formatGenerators() {
const diff = (await run('git diff --name-only --cached -- generators')).split(
'\n'
);
if (diff.length === 0 || diff[0].trim() === '') {
return;
}
const spinner = ora('Linting generators').start();
try {
await run('yarn docker:no-tty format java generators');
await run(`git add ${diff.join(' ')}`);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
spinner.fail('Failed to format generators');
exit(1);
}
spinner.succeed();
}

if (require.main === module && process.env.CI !== 'true') {
formatGenerators();
}
11 changes: 3 additions & 8 deletions scripts/ci/husky/pre-commit.js → scripts/husky/pre-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
/* eslint-disable import/no-commonjs */
/* eslint-disable @typescript-eslint/no-var-requires */
const chalk = require('chalk');
const execa = require('execa');
const micromatch = require('micromatch');

const clientConfig = require('../../../config/clients.config.json');
const clientConfig = require('../../config/clients.config.json');
const GENERATED_FILE_PATTERNS =
require('../../../config/generation.config').patterns;
require('../../config/generation.config').patterns;

const run = async (command, { cwd } = {}) => {
return (
(await execa.command(command, { shell: 'bash', all: true, cwd })).all ?? ''
);
};
const { run } = require('./utils');

function getPatterns() {
const patterns = GENERATED_FILE_PATTERNS;
Expand Down
16 changes: 16 additions & 0 deletions scripts/husky/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable import/no-commonjs */
/* eslint-disable @typescript-eslint/no-var-requires */
const execa = require('execa');

async function run(command) {
return (
(
await execa.command(command, {
shell: 'bash',
all: true,
})
).all ?? ''
);
}

module.exports = { run };
2 changes: 1 addition & 1 deletion tests/output/javascript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3663,7 +3663,7 @@ __metadata:

"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 4.7.4
resolution: "typescript@patch:typescript@npm%3A4.7.4#~builtin<compat/typescript>::version=4.7.4&hash=7ad353"
resolution: "typescript@patch:typescript@npm%3A4.7.4#~builtin<compat/typescript>::version=4.7.4&hash=f456af"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
Expand Down