@sujithx1/optimizeguard 1.0.1 โ 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -9
- package/bun.lock +1 -0
- package/package.json +1 -1
- package/sdk.md +4 -4
- package/src/cli.ts +5 -5
- package/src/engine.ts +14 -14
- package/src/rules/index.ts +16 -16
- package/src/rules/performance.ts +12 -12
- package/src/rules/reporter.ts +9 -9
- package/src/rules/security.ts +13 -13
- package/src/rules/type.ts +23 -23
- package/src/scanner.ts +18 -5
package/README.md
CHANGED
|
@@ -1,11 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
# ๐ OptimizeGuard
|
|
2
|
+
|
|
3
|
+
OptimizeGuard is a lightweight **code optimization guard** that helps developers check whether their code is **optimized, safe, and type-correct before committing or pushing**.
|
|
4
|
+
|
|
5
|
+
It runs with **one simple command** and gives **clear, human-readable feedback** โ no complex setup needed.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ๐ Why OptimizeGuard?
|
|
10
|
+
|
|
11
|
+
Many code issues are found **after pushing** or during **code review**.
|
|
12
|
+
OptimizeGuard catches them **early**, right on your machine.
|
|
13
|
+
|
|
14
|
+
โ Faster feedback
|
|
15
|
+
โ Cleaner commits
|
|
16
|
+
โ Better code quality
|
|
17
|
+
โ Developer-friendly output
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## ๐ What does it check?
|
|
22
|
+
|
|
23
|
+
### โก Performance
|
|
24
|
+
- `await` inside loops
|
|
25
|
+
- Blocking or inefficient patterns
|
|
26
|
+
|
|
27
|
+
### ๐ง Type Safety
|
|
28
|
+
- Usage of `any`
|
|
29
|
+
- Unsafe or missing TypeScript types
|
|
30
|
+
|
|
31
|
+
### ๐ Security
|
|
32
|
+
- Hardcoded secrets
|
|
33
|
+
- `eval()` and unsafe executions
|
|
34
|
+
|
|
35
|
+
### ๐งน Code Quality
|
|
36
|
+
- Common anti-patterns
|
|
37
|
+
- Unoptimized logic hints
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## ๐ฆ Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install -g @sujithx1/optimizeguard
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
โถ๏ธ Usage
|
|
48
|
+
optimizeguard
|
|
49
|
+
|
|
50
|
+
Example output
|
|
51
|
+
โ OptimizeGuard failed
|
|
52
|
+
|
|
53
|
+
โข Performance issue: await inside loop (src/api/user.ts)
|
|
54
|
+
โข Type issue: usage of 'any' (src/services/auth.ts)
|
|
55
|
+
|
|
56
|
+
Fix the issues and try again.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
If everything is good:
|
|
60
|
+
โ
OptimizeGuard passed
|
|
61
|
+
Your code is optimized and safe to commit ๐
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
๐ Use as Pre-Commit Hook (Recommended)
|
|
65
|
+
|
|
66
|
+
Automatically block bad commits.
|
|
67
|
+
npx husky add .husky/pre-commit "optimizeguard"
|
|
5
68
|
|
|
6
|
-
To run:
|
|
7
|
-
```sh
|
|
8
|
-
bun run dev
|
|
9
|
-
```
|
|
10
69
|
|
|
11
|
-
open http://localhost:3000
|
package/bun.lock
CHANGED
package/package.json
CHANGED
package/sdk.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
84f314b3ed646e5b431a36dd5aae66d0e10c67e5397c25c4b6ca3b58b6626d0f
|
|
2
|
-
9130c7d5e62160aaa72e33696b4282ed4b9f73f2a8e13925caaaf167fd0de31f
|
|
3
|
-
d2456011201d030099de954eda56a8be9ab94d32e87241b19cbce98ad7a1d410
|
|
4
|
-
c2b041f9d8a7f333350d8233c4357eaa3398ccc51aa3dd2d20dc46c552450947
|
|
1
|
+
84f314b3ed646e5b431a36dd5aae66d0e10c67e5397c25c4b6ca3b58b6626d0f
|
|
2
|
+
9130c7d5e62160aaa72e33696b4282ed4b9f73f2a8e13925caaaf167fd0de31f
|
|
3
|
+
d2456011201d030099de954eda56a8be9ab94d32e87241b19cbce98ad7a1d410
|
|
4
|
+
c2b041f9d8a7f333350d8233c4357eaa3398ccc51aa3dd2d20dc46c552450947
|
|
5
5
|
56bea1d7da7273c042fef8764f82e7e6e89e36e32abdf40f9f320bf5be9e8ab7
|
package/src/cli.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
import { runOptimizeGuard } from "./engine.js";
|
|
3
|
-
|
|
4
|
-
const result = await runOptimizeGuard();
|
|
5
|
-
|
|
6
|
-
if (!result.ok) process.exit(1);
|
|
2
|
+
import { runOptimizeGuard } from "./engine.js";
|
|
3
|
+
|
|
4
|
+
const result = await runOptimizeGuard();
|
|
5
|
+
|
|
6
|
+
if (!result.ok) process.exit(1);
|
package/src/engine.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { scanProject } from "./scanner.js";
|
|
2
|
-
import { runRules } from "./rules/index.js";
|
|
3
|
-
import { report } from "./rules/reporter.js";
|
|
4
|
-
|
|
5
|
-
export async function runOptimizeGuard() {
|
|
6
|
-
console.log("โก OptimizeGuard v1 running...\n");
|
|
7
|
-
|
|
8
|
-
const files = await scanProject();
|
|
9
|
-
const issues = await runRules(files);
|
|
10
|
-
|
|
11
|
-
report(issues);
|
|
12
|
-
|
|
13
|
-
return { ok: issues.length === 0 };
|
|
14
|
-
}
|
|
1
|
+
import { scanProject } from "./scanner.js";
|
|
2
|
+
import { runRules } from "./rules/index.js";
|
|
3
|
+
import { report } from "./rules/reporter.js";
|
|
4
|
+
|
|
5
|
+
export async function runOptimizeGuard() {
|
|
6
|
+
console.log("โก OptimizeGuard v1 running...\n");
|
|
7
|
+
|
|
8
|
+
const files = await scanProject();
|
|
9
|
+
const issues = await runRules(files);
|
|
10
|
+
|
|
11
|
+
report(issues);
|
|
12
|
+
|
|
13
|
+
return { ok: issues.length === 0 };
|
|
14
|
+
}
|
package/src/rules/index.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { perfRules } from "./performance";
|
|
2
|
-
import { securityRules } from "./security";
|
|
3
|
-
import { typeRules } from "./type";
|
|
4
|
-
|
|
5
|
-
export async function runRules(files: string[]) {
|
|
6
|
-
const all = [...perfRules, ...securityRules, ...typeRules];
|
|
7
|
-
const issues: any[] = [];
|
|
8
|
-
|
|
9
|
-
for (const file of files) {
|
|
10
|
-
for (const rule of all) {
|
|
11
|
-
issues.push(...(await rule.check(file)));
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return issues;
|
|
16
|
-
}
|
|
1
|
+
import { perfRules } from "./performance";
|
|
2
|
+
import { securityRules } from "./security";
|
|
3
|
+
import { typeRules } from "./type";
|
|
4
|
+
|
|
5
|
+
export async function runRules(files: string[]) {
|
|
6
|
+
const all = [...perfRules, ...securityRules, ...typeRules];
|
|
7
|
+
const issues: any[] = [];
|
|
8
|
+
|
|
9
|
+
for (const file of files) {
|
|
10
|
+
for (const rule of all) {
|
|
11
|
+
issues.push(...(await rule.check(file)));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return issues;
|
|
16
|
+
}
|
package/src/rules/performance.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
export const perfRules = [
|
|
3
|
-
{
|
|
4
|
-
name: "await-in-loop",
|
|
5
|
-
async check(file: string) {
|
|
6
|
-
const content = fs.readFileSync(file, "utf8");
|
|
7
|
-
return /for\s*\(.*\)\s*{[\s\S]*await/.test(content)
|
|
8
|
-
? [{ type: "PERF", file, msg: "await inside loop" }]
|
|
9
|
-
: [];
|
|
10
|
-
},
|
|
11
|
-
},
|
|
12
|
-
];
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
export const perfRules = [
|
|
3
|
+
{
|
|
4
|
+
name: "await-in-loop",
|
|
5
|
+
async check(file: string) {
|
|
6
|
+
const content = fs.readFileSync(file, "utf8");
|
|
7
|
+
return /for\s*\(.*\)\s*{[\s\S]*await/.test(content)
|
|
8
|
+
? [{ type: "PERF", file, msg: "await inside loop" }]
|
|
9
|
+
: [];
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
];
|
package/src/rules/reporter.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export function report(issues: any[]) {
|
|
2
|
-
if (!issues.length) return console.log("โ
Code is optimized and safe!\n");
|
|
3
|
-
|
|
4
|
-
for (const i of issues) {
|
|
5
|
-
console.log(`โ [${i.type}] ${i.msg} โ ${i.file}`);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
console.log(`\nScore: ${Math.max(0, 100 - issues.length * 5)} / 100`);
|
|
9
|
-
}
|
|
1
|
+
export function report(issues: any[]) {
|
|
2
|
+
if (!issues.length) return console.log("โ
Code is optimized and safe!\n");
|
|
3
|
+
|
|
4
|
+
for (const i of issues) {
|
|
5
|
+
console.log(`โ [${i.type}] ${i.msg} โ ${i.file}`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
console.log(`\nScore: ${Math.max(0, 100 - issues.length * 5)} / 100`);
|
|
9
|
+
}
|
package/src/rules/security.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
|
|
3
|
-
export const securityRules = [
|
|
4
|
-
{
|
|
5
|
-
name: "hardcoded-secret",
|
|
6
|
-
async check(file: string) {
|
|
7
|
-
const content = fs.readFileSync(file, "utf8");
|
|
8
|
-
return /(API_KEY|SECRET|TOKEN)\s*=\s*['"]/i.test(content)
|
|
9
|
-
? [{ type: "SEC", file, msg: "Hardcoded secret detected" }]
|
|
10
|
-
: [];
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
];
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
|
|
3
|
+
export const securityRules = [
|
|
4
|
+
{
|
|
5
|
+
name: "hardcoded-secret",
|
|
6
|
+
async check(file: string) {
|
|
7
|
+
const content = fs.readFileSync(file, "utf8");
|
|
8
|
+
return /(API_KEY|SECRET|TOKEN)\s*=\s*['"]/i.test(content)
|
|
9
|
+
? [{ type: "SEC", file, msg: "Hardcoded secret detected" }]
|
|
10
|
+
: [];
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
];
|
package/src/rules/type.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
// src/rules/types.ts
|
|
2
|
-
import { execSync } from "child_process";
|
|
3
|
-
|
|
4
|
-
export const typeRules = [
|
|
5
|
-
{
|
|
6
|
-
name: "typescript-check",
|
|
7
|
-
async check(_file: string) {
|
|
8
|
-
try {
|
|
9
|
-
// Run tsc in noEmit mode
|
|
10
|
-
execSync("tsc --noEmit", { stdio: "pipe" });
|
|
11
|
-
return [];
|
|
12
|
-
} catch (e: any) {
|
|
13
|
-
// Extract errors
|
|
14
|
-
const output = e.stdout?.toString() || e.message || "Type check failed";
|
|
15
|
-
const errors = output
|
|
16
|
-
.split("\n")
|
|
17
|
-
.filter((line: string) => line.trim() !== "")
|
|
18
|
-
.map((line: string) => ({ type: "TYPE", file: "ts", msg: line }));
|
|
19
|
-
return errors;
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
];
|
|
1
|
+
// src/rules/types.ts
|
|
2
|
+
import { execSync } from "child_process";
|
|
3
|
+
|
|
4
|
+
export const typeRules = [
|
|
5
|
+
{
|
|
6
|
+
name: "typescript-check",
|
|
7
|
+
async check(_file: string) {
|
|
8
|
+
try {
|
|
9
|
+
// Run tsc in noEmit mode
|
|
10
|
+
execSync("tsc --noEmit", { stdio: "pipe" });
|
|
11
|
+
return [];
|
|
12
|
+
} catch (e: any) {
|
|
13
|
+
// Extract errors
|
|
14
|
+
const output = e.stdout?.toString() || e.message || "Type check failed";
|
|
15
|
+
const errors = output
|
|
16
|
+
.split("\n")
|
|
17
|
+
.filter((line: string) => line.trim() !== "")
|
|
18
|
+
.map((line: string) => ({ type: "TYPE", file: "ts", msg: line }));
|
|
19
|
+
return errors;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
];
|
package/src/scanner.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
|
|
2
|
+
import { glob } from "glob";
|
|
3
|
+
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
|
|
6
|
+
export async function scanProject() {
|
|
7
|
+
|
|
8
|
+
if(!fs.existsSync('src'))
|
|
9
|
+
{
|
|
10
|
+
console.log('๐ซ No src folder found. Please run this command from the root of your project or Skiping Scan.')
|
|
11
|
+
return []
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
return glob("src/**/*.{ts,tsx,js,jsx}", {
|
|
16
|
+
nodir: true,
|
|
17
|
+
});
|
|
18
|
+
}
|