forked from mattdiamond/fuckitjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (66 loc) · 2.24 KB
/
index.js
File metadata and controls
83 lines (66 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
FuckItJS
Copyright 2012, Matt Diamond
Note: This is ALPHA software and may result in irreversible brain damage. All vibe coded by Andrew and chatgpt, use at your own risk.
*/
const fs = require('fs');
const path = require('path');
const vm = require('vm');
global._OriginalFuckIt = global.FuckIt;
let currentScriptPath = process.argv[1];
let currentScript = '';
let retryCount = 0;
// Load and run synchronously so the script is live before errors can occur
try {
const absolutePath = path.resolve(currentScriptPath);
currentScript = fs.readFileSync(absolutePath, 'utf8');
const script = new vm.Script(currentScript, { filename: currentScriptPath });
script.runInContext(vm.createContext(global));
} catch (err) {
setImmediate(() => { throw err });
}
process.on('uncaughtException', (err) => {
retryCount++;
console.error(`\n🧨 Script blew up (#${retryCount})`);
if (!currentScript) {
console.error("🤷♂️ No script is currently loaded.");
process.exit(1);
}
const lines = currentScript.split('\n');
const match = err.stack && err.stack.match(/:(\d+):\d+/);
let lineNumber = match ? parseInt(match[1], 10) : null;
if (!lineNumber || isNaN(lineNumber)) {
console.warn(`📉 Couldn't determine the line number. Rage quitting.`);
console.error(err.stack || err);
process.exit(1);
}
// Clamp the line number to within bounds
const clampedLine = Math.max(1, Math.min(lineNumber, lines.length));
console.warn(`🔪 Removing line ${clampedLine}: "${lines[clampedLine - 1].trim()}"`);
lines.splice(clampedLine - 1, 1);
currentScript = lines.join('\n');
if (!currentScript.trim()) {
console.error("💀 Script has been entirely nuked. There's nothing left to run.");
process.exit(1);
}
try {
const script = new vm.Script(currentScript, { filename: currentScriptPath });
script.runInContext(vm.createContext(global));
} catch (newErr) {
setImmediate(() => { throw newErr });
}
});
const FuckIt = {
noConflict: () => {
global.FuckIt = global._OriginalFuckIt;
return FuckIt;
},
moreConflict: () => {
for (const prop in global) {
if (prop === "process") continue;
global[prop] = FuckIt;
}
}
};
global.FuckIt = FuckIt;
module.exports = FuckIt;