purecommit 1.0.0

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.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { askSetup } from '../src/setup.js';
4
+
5
+ // Start the app
6
+ askSetup();
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "purecommit",
3
+ "version": "1.0.0",
4
+ "description": "Purify your commits by automatically removing console.logs and debug comments.",
5
+ "main": "src/purifier.js",
6
+ "bin": {
7
+ "purecommit": "bin/purecommit.js"
8
+ },
9
+ "type": "module",
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1",
12
+ "prepare": "husky install"
13
+ },
14
+ "keywords": [
15
+ "git",
16
+ "hooks",
17
+ "husky",
18
+ "clean",
19
+ "console.log",
20
+ "debug",
21
+ "pre-commit"
22
+ ],
23
+ "author": "Aman Raj",
24
+ "license": "MIT",
25
+ "dependencies": {
26
+ "chalk": "^5.3.0"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/Prof2807/CleanCommit.git"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/Prof2807/CleanCommit/issues"
34
+ },
35
+ "homepage": "https://github.com/Prof2807/CleanCommit#readme"
36
+ }
@@ -0,0 +1,42 @@
1
+ import { execSync } from 'child_process';
2
+ import fs from 'fs';
3
+ import chalk from 'chalk';
4
+
5
+ export function runMainLogic() {
6
+ try {
7
+ const output = execSync('git diff --staged --name-only --diff-filter=d', { encoding: 'utf-8' });
8
+ let files = output.split('\n').filter(file => file.trim() !== '');
9
+
10
+ if (files.length === 0) {
11
+ console.log(chalk.yellow("No staged files to purify."));
12
+ return;
13
+ }
14
+
15
+ let purified = 0;
16
+ for (const file of files) {
17
+ if (fs.existsSync(file)) {
18
+ const originalContent = fs.readFileSync(file, 'utf-8');
19
+ const deletePattern = /^[ \t]*(?:console\.log\(.*?\);?|.*\/\/\s*debug.*)(?![ \t]*\/\/\s*keep).*$/gmi;
20
+ const stripKeepPattern = /[ \t]*\/\/\s*keep/gi;
21
+ const cleanedContent = originalContent
22
+ .replace(deletePattern, '')
23
+ .replace(stripKeepPattern, '');
24
+ if (originalContent !== cleanedContent) {
25
+ const blobHash = execSync('git hash-object -w --stdin', {
26
+ input: cleanedContent,
27
+ encoding: 'utf-8'
28
+ }).trim();
29
+ execSync(`git update-index --add --cacheinfo 100644 ${blobHash} "${file}"`);
30
+ purified++;
31
+ };
32
+ };
33
+ };
34
+ if (purified > 0) {
35
+ console.log(chalk.green.bold(`Purified ${purified} file(s) from your files`))
36
+ };
37
+ } catch (err) {
38
+ console.error(chalk.red("PureCommit Error: "), err);
39
+ if (err.stderr) console.error(err.stderr.toString());
40
+ process.exit(1);
41
+ };
42
+ };
package/src/setup.js ADDED
@@ -0,0 +1,38 @@
1
+ import readline from 'readline';
2
+ import fs from 'fs';
3
+ import { execSync } from 'child_process';
4
+ import { runMainLogic } from './purifier.js';
5
+
6
+ const IGNORE_FILE = '.purecommit_ignore';
7
+
8
+ export function askSetup() {
9
+ const rl = readline.createInterface({
10
+ input: process.stdin,
11
+ output: process.stdout
12
+ });
13
+
14
+ if (!fs.existsSync('.husky') && !fs.existsSync(IGNORE_FILE)) {
15
+ rl.question("Husky hook not found. Do you want to set PureCommit as a pre-commit hook? (y/n): ", (answer) => {
16
+ if (answer.toLowerCase() === 'y') {
17
+ try {
18
+ console.log("Setting up Husky...");
19
+ execSync("npx husky-init && npm install", {stdio: 'inherit'});
20
+
21
+ fs.writeFileSync('.husky/pre-commit', '#!/bin/sh\n. "$(dirname "$0")/_/husky.sh"\n\nnpx purecommit\n');
22
+ console.log('Setup complete! Your commits will now be purified automatically.');
23
+ } catch (err) {
24
+ console.error("Setup failed but you can still use PureCommit manually.");
25
+ process.exit(1);
26
+ }
27
+ } else {
28
+ fs.writeFileSync(IGNORE_FILE, 'User declined setup');
29
+ console.log("No Problem. Continuing with manual run...");
30
+ }
31
+ rl.close();
32
+ runMainLogic();
33
+ });
34
+ } else {
35
+ rl.close();
36
+ runMainLogic();
37
+ }
38
+ }