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

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
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
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

# For this to run on pre-commit hook, should run `npx husky` in root after `git clone` repo onto dev 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
169 changes: 169 additions & 0 deletions .husky/pre-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
const fs = require('fs');
const { execSync } = require('child_process');

// ENUMS

const FILE_EXTENSION = {
TYPESCRIPT: "TYPESCRIPT",
SOLIDITY: "SOLIDITY",
}

const FOLDER = {
BRIDGEUI: "BRIDGEUI",
CONTRACTS: "CONTRACTS",
E2E: "E2E",
OPERATIONS: "OPERATIONS",
POSTMAN: "POSTMAN",
SDK: "SDK",
}

const RUNTIME = {
NODEJS: "NODEJS"
}

// MAPS

const FILE_EXTENSION_FILTERS = {
[FILE_EXTENSION.TYPESCRIPT]: "\.ts$",
[FILE_EXTENSION.SOLIDITY]: "\.sol$",
};

const FILE_EXTENSION_LINTING_COMMAND = {
[FILE_EXTENSION.TYPESCRIPT]: "pnpm run lint:ts:fix",
[FILE_EXTENSION.SOLIDITY]: "pnpm run lint:sol",
};

const FOLDER_PATH = {

[FOLDER.BRIDGEUI]: "bridge-ui/",
[FOLDER.CONTRACTS]: "contracts/",
[FOLDER.E2E]: "e2e/",
[FOLDER.OPERATIONS]: "operations/",
[FOLDER.POSTMAN]: "postman/",
[FOLDER.SDK]: "sdk/",
};

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(),
};

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

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)
}
}

function partitionChangedFileList(_changedFileList) {
// Populate lists of filter matches
for (const file of _changedFileList) {
for (const path in FOLDER) {
if (file.match(new RegExp(`^${FOLDER_PATH[path]}`))) {
FOLDER_CHANGED_FILES[path].push(file);
}
}
}
}

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
}
}

function getChangedFileExtensions(_folder) {
// Use sets to implement early exit from iteration of all changed files, once we have matched all file extensions of interest.
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);
}

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 route 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);
}
}
}

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);
}
}
1 change: 1 addition & 0 deletions bridge-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "next start",
"lint": "next lint",
"lint:fix": "next lint --fix",
"lint:ts:fix": "next lint --fix",
"clean": "rimraf node_modules .next .next-env.d.ts",
"install:playwright": "playwright install --with-deps",
"build:cache": "synpress",
Expand Down
1 change: 1 addition & 0 deletions operations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"prettier:fix": "prettier -w '**/*.{js,ts}'",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"lint:ts:fix": "eslint . --ext .ts --fix",
"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