Skip to content
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

Feat/husky lint #431

Merged
merged 32 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ed814b3
add poc husky lint script
kyzooghost Dec 4, 2024
4d926b6
test commit change
kyzooghost Dec 4, 2024
5be0d98
undo test change
kyzooghost Dec 4, 2024
5aa932b
test change
kyzooghost Dec 4, 2024
e7be1df
undo test change
kyzooghost Dec 4, 2024
9446ceb
test
kyzooghost Dec 4, 2024
a513e13
test
kyzooghost Dec 4, 2024
91d45cb
test
kyzooghost Dec 4, 2024
7223d5d
test
kyzooghost Dec 4, 2024
e0c87d4
do
kyzooghost Dec 4, 2024
208a27f
test
kyzooghost Dec 4, 2024
24ec15c
test
kyzooghost Dec 4, 2024
9a8ad8b
test
kyzooghost Dec 4, 2024
5dbd778
test
kyzooghost Dec 4, 2024
8fbfb89
test
kyzooghost Dec 4, 2024
59c1d7d
test
kyzooghost Dec 4, 2024
5f7c8bf
test
kyzooghost Dec 4, 2024
7f18ac9
test
kyzooghost Dec 4, 2024
619435c
test
kyzooghost Dec 5, 2024
521aa8c
test
kyzooghost Dec 5, 2024
6b19c2e
refactor to pre-commit to NodeJS script
kyzooghost Dec 5, 2024
4277ef7
add logs for demo
kyzooghost Dec 5, 2024
6a6d404
small refactor of pre-commit.js
kyzooghost Dec 5, 2024
d9ad23a
add shebang to pre-commit
kyzooghost Dec 5, 2024
c9875d1
add other folders to husky script
kyzooghost Dec 11, 2024
96282dc
Merge branch 'main' into feat/husky-lint
kyzooghost Dec 11, 2024
0fd9ec5
added postman to pre-commit
kyzooghost Dec 11, 2024
9c0d551
remove test md change
kyzooghost Dec 11, 2024
4b93511
add comments for pre-commit.js
kyzooghost Dec 18, 2024
3b3dd33
adjust lint:fix scripts
kyzooghost Dec 18, 2024
919b326
Merge branch 'main' into feat/husky-lint
kyzooghost Dec 20, 2024
1426dbf
Update .husky/pre-commit.js
kyzooghost Dec 20, 2024
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
10 changes: 10 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Run `npx husky` in root directory to initialize this pre-commit hook for local machine

# Execute NodeJS script because
# i.) Husky requires NodeJS -> fair assumption that machine will have NodeJS
# ii.) Cleaner syntax and abstractions than shell scripting
node .husky/pre-commit.js

exit 0
217 changes: 217 additions & 0 deletions .husky/pre-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/**
* Runs as git pre-commit hook
* Filters the list of changed files on 'git commit'
* If *.ts files in specified projects are detected, runs the 'lint:ts:fix' package.json script for that project
* E.g. if a *.ts file is changed in /sdk, then this script will run 'pnpm run lint:ts:fix' in the /sdk project
*/

const fs = require('fs');
const { execSync } = require('child_process');

/**
* ENUMS
*/

// File extensions to filter for
const FILE_EXTENSION = {
TYPESCRIPT: "TYPESCRIPT",
SOLIDITY: "SOLIDITY",
}

// Projects to filter for
const FOLDER = {
BRIDGEUI: "BRIDGEUI",
CONTRACTS: "CONTRACTS",
E2E: "E2E",
OPERATIONS: "OPERATIONS",
POSTMAN: "POSTMAN",
SDK: "SDK",
}

// Project runtimes
const RUNTIME = {
NODEJS: "NODEJS"
}

/**
* MAPPINGS
*/

// File extension => regex
const FILE_EXTENSION_FILTERS = {
[FILE_EXTENSION.TYPESCRIPT]: "\.ts$",
[FILE_EXTENSION.SOLIDITY]: "\.sol$",
};

// File extension => script in package.json to run
const FILE_EXTENSION_LINTING_COMMAND = {
[FILE_EXTENSION.TYPESCRIPT]: "pnpm run lint:ts:fix",
[FILE_EXTENSION.SOLIDITY]: "pnpm run lint:sol:fix",
};

// Project => Path in monorepo
const FOLDER_PATH = {
[FOLDER.BRIDGEUI]: "bridge-ui/",
[FOLDER.CONTRACTS]: "contracts/",
[FOLDER.E2E]: "e2e/",
[FOLDER.OPERATIONS]: "operations/",
[FOLDER.POSTMAN]: "postman/",
[FOLDER.SDK]: "sdk/",
};

// Project => List of changed files
const FOLDER_CHANGED_FILES = {
[FOLDER.BRIDGEUI]: new Array(),
[FOLDER.CONTRACTS]: new Array(),
[FOLDER.E2E]: new Array(),
[FOLDER.OPERATIONS]: new Array(),
[FOLDER.POSTMAN]: new Array(),
[FOLDER.SDK]: new Array(),
};

// Project => Runtime
const FOLDER_RUNTIME = {
[FOLDER.BRIDGEUI]: RUNTIME.NODEJS,
[FOLDER.CONTRACTS]: RUNTIME.NODEJS,
[FOLDER.E2E]: RUNTIME.NODEJS,
[FOLDER.OPERATIONS]: RUNTIME.NODEJS,
[FOLDER.POSTMAN]: RUNTIME.NODEJS,
[FOLDER.SDK]: RUNTIME.NODEJS,
};

/**
* MAIN FUNCTION
*/

main();

function main() {
const changedFileList = getChangedFileList();
partitionChangedFileList(changedFileList);

for (const folder in FOLDER) {
if (!isDependenciesInstalled(folder)) {
console.error(`Dependencies not installed in ${FOLDER_PATH[folder]}, exiting...`)
process.exit(1);
}
const changedFileExtensions = getChangedFileExtensions(folder);
executeLinting(folder, changedFileExtensions);
}

updateGitIndex();
}

/**
* HELPER FUNCTIONS
*/

/**
* Gets a list of changed files in the git commit
* @returns {string[]}
*/
function getChangedFileList() {
try {
const cmd = 'git diff --name-only HEAD'
const stdout = execSync(cmd, { encoding: 'utf8' });
return stdout.split('\n').filter(file => file.trim() !== '');
} catch (error) {
console.error($`Error running ${cmd}:`, error.message);
process.exit(1)
}
}

/**
* Partitions list of changed files from getChangedFileList() by project
* Stores results in FOLDER_CHANGED_FILES
* @param {string[]}
*/
function partitionChangedFileList(_changedFileList) {
for (const file of _changedFileList) {
for (const path in FOLDER) {
if (file.match(new RegExp(`^${FOLDER_PATH[path]}`))) {
FOLDER_CHANGED_FILES[path].push(file);
}
}
}
}

/**
* Checks if runtime dependencies are installed for a project
* @param {FOLDER}
* @returns {boolean}
*/
function isDependenciesInstalled(_folder) {
const runtime = FOLDER_RUNTIME[_folder];
const path = FOLDER_PATH[_folder];

switch(runtime) {
case RUNTIME.NODEJS:
const dependencyFolder = `${path}node_modules`
return fs.existsSync(dependencyFolder)
default:
console.error(`${runtime} runtime not supported.`);
return false
}
}

/**
* Resolve list of changed file extensions for a project
* @param {FOLDER}
* @returns {FILE_EXTENSION[]}
*/
function getChangedFileExtensions(_folder) {
// Use sets to implement early exit from loop, once we have matched all configured file extensions
const remainingFileExtensionsSet = new Set(Object.values(FILE_EXTENSION));
const foundFileExtensionsSet = new Set();

for (const file of FOLDER_CHANGED_FILES[_folder]) {
for (const fileExtension of remainingFileExtensionsSet) {
if (file.match(new RegExp(FILE_EXTENSION_FILTERS[fileExtension]))) {
foundFileExtensionsSet.add(fileExtension);
remainingFileExtensionsSet.delete(fileExtension);
}
}

// No more remaining file extensions to look for
if (remainingFileExtensionsSet.size == 0) break;
}

return Array.from(foundFileExtensionsSet);
}

/**
* Execute linting command
* @param {FOLDER, FILE_EXTENSION[]}
*/
function executeLinting(_folder, _changedFileExtensions) {
for (const fileExtension of _changedFileExtensions) {
const path = FOLDER_PATH[_folder];
const cmd = FILE_EXTENSION_LINTING_COMMAND[fileExtension];
console.log(`${fileExtension} change found in ${path}, linting...`);
try {
// Execute command synchronously and stream output directly to the current stdout
execSync(`
cd ${path};
${cmd};
`, { stdio: 'inherit' });
} catch (error) {
console.error(`Error:`, error.message);
console.error(`Exiting...`);
process.exit(1);
}
}
}

/**
* Redo `git add` for files updated during executeLinting(), so that they are not left out of the commit
* The difference between 'git add .' and 'git update-index --again', is that the latter will not include untracked files
*/
function updateGitIndex() {
try {
const cmd = 'git update-index --again'
execSync(cmd, { stdio: 'inherit' });
} catch (error) {
console.error($`Error running ${cmd}:`, error.message);
process.exit(1);
}
}
3 changes: 2 additions & 1 deletion bridge-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"lint:fix": "next lint --fix",
"lint:fix": "pnpm run lint:ts:fix",
"lint:ts:fix": "next lint --fix",
kyzooghost marked this conversation as resolved.
Show resolved Hide resolved
"clean": "rimraf node_modules .next .next-env.d.ts",
"install:playwright": "playwright install --with-deps",
"build:cache": "synpress",
Expand Down
3 changes: 2 additions & 1 deletion operations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"prettier": "prettier -c '**/*.{js,ts}'",
"prettier:fix": "prettier -w '**/*.{js,ts}'",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"lint:fix": "pnpm run lint:ts:fix",
"lint:ts:fix": "eslint . --ext .ts --fix",
kyzooghost marked this conversation as resolved.
Show resolved Hide resolved
"test": "node --experimental-vm-modules node_modules/jest/bin/jest --bail --detectOpenHandles --forceExit",
"clean": "rimraf node_modules dist coverage",
"postpack": "shx rm -f oclif.manifest.json",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"lint:fix": "pnpm run -r --if-present lint:fix",
"clean": "pnpm run -r --if-present clean && rm -rf node_modules",
"test": "pnpm run -r --if-present test",
"build": "pnpm run -r --if-present build"
"build": "pnpm run -r --if-present build",
"prepare": "husky"
},
"devDependencies": {
"@types/node": "20.12.7",
Expand All @@ -23,6 +24,7 @@
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"husky": "9.1.7",
"prettier": "3.2.5",
"rimraf": "5.0.5",
"ts-node": "10.9.2",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading