-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·202 lines (177 loc) · 5.76 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env node
const asar = require("asar");
const commandLineArgs = require("command-line-args");
const del = require("del");
const fs = require("fs");
const os = require("os");
const path = require("path");
const semverSort = require("semver-sort");
const { execSync } = require("child_process");
const optionDefinitions = [
{ name: "BEEEEEG", alias: "b", type: Boolean },
{ name: "smol", alias: "s", type: Boolean },
{ name: "uninstall", alias: "u", type: Boolean },
];
const commandLineOptions = commandLineArgs(optionDefinitions);
const getSlackDirectoryOnWindows = () => {
const slackRootDir = path.resolve(process.env.APPDATA, "..\\Local\\slack");
const appDirNamePrefix = "app-";
const getSlackVersions = (source) =>
fs
.readdirSync(source, { withFileTypes: true })
.filter((dir) => dir.isDirectory())
.map((dir) => dir.name)
.filter((dir) => dir.startsWith(appDirNamePrefix))
.map((dir) => dir.substring(appDirNamePrefix.length));
const slackVersions = getSlackVersions(slackRootDir);
const getLatestVersionNumber = (versions) => semverSort.desc(versions)[0];
const latestVersionNumber = getLatestVersionNumber(slackVersions);
return path.resolve(
slackRootDir,
`${appDirNamePrefix}${latestVersionNumber}`,
"resources"
);
};
const getSlackDirectory = () => {
console.info("Figuring out where Slack is installed");
switch (os.type()) {
case "Linux":
return path.resolve("/usr/lib/slack/resources/");
case "Darwin":
return path.resolve("/Applications/Slack.app/Contents/Resources/");
case "Windows_NT":
return getSlackDirectoryOnWindows();
default:
console.error(
`Sorry, this operating system (${os.type()}) is not supported yet, feel free to raise a ticket`
);
process.exit(2);
}
};
const getStylesFileLocation = (BEEEEEG, smol) => {
const resolveStylesFile = (filename) => {
const cssFileExtension = ".css";
return path.resolve(SCRIPT_DIR, "common", `${filename}${cssFileExtension}`);
};
if (BEEEEEG && !smol) {
return resolveStylesFile("slack-beeegmojis");
} else if (smol && !BEEEEEG) {
return resolveStylesFile("slack-smolbois");
} else {
return resolveStylesFile("slack-beeeg-E-smolz");
}
};
const SCRIPT_DIR = __dirname;
const slackDirectory = getSlackDirectory();
const stylesFile = getStylesFileLocation(
commandLineOptions.BEEEEEG,
commandLineOptions.smol
);
const appDirectory = path.resolve(slackDirectory, "app");
const appendTarget = path.resolve(appDirectory, "dist/preload.bundle.js");
const backupAsarPath = path.resolve(slackDirectory, "app.asar.bak");
const injectFilesDir = path.resolve(SCRIPT_DIR, "electron-hack");
const injectOpenFile = path.resolve(injectFilesDir, "1.txt");
const injectCloseFile = path.resolve(injectFilesDir, "2.txt");
const originalAsarPath = path.resolve(slackDirectory, "app.asar");
const closeSlack = () => {
console.info("Closing Slack");
try {
if (os.type() === "Windows_NT") {
execSync("taskkill /F /IM slack.exe");
} else if (os.type() === "Darwin" || os.type() === "Linux") {
execSync("pkill -9 slack");
}
} catch {
console.warn("Couldn't close Slack, maybe it wasn't running?");
}
};
const displayMemeMessage = () => {
if (commandLineOptions.BEEEEEG && !commandLineOptions.smol) {
console.info("I like 'em BEEEEEG\nhttps://youtu.be/WJ1I-z0pBU0");
} else if (commandLineOptions.BEEEEEG && commandLineOptions.smol) {
console.info(
"Some of the best memes are d̶e̶e̶p̶-̶f̶r̶i̶e̶d̶ are served medium-rare"
);
}
};
const removeModifiedFiles = () => {
if (fs.existsSync(appDirectory)) {
console.info("Cleaning up after last installation");
del.sync(appDirectory, { force: true });
}
};
const originalPackagedFileExists = () => {
return fs.existsSync(originalAsarPath);
};
const backupPackagedFile = () => {
if (fs.existsSync(backupAsarPath)) {
console.info("Removing the old Slack backup");
del.sync(backupAsarPath, { force: true });
}
console.info("Backing up Slack");
fs.copyFileSync(originalAsarPath, backupAsarPath);
};
const restorePackageFile = () => {
console.info("Restoring Slack from backup");
fs.copyFileSync(backupAsarPath, originalAsarPath);
};
const unpackSlackFiles = () => {
console.info("Unpacking Slack app");
asar.extractAll(originalAsarPath, appDirectory);
};
const packageSlackFiles = () => {
console.info("Re-packaging Slack app");
asar.createPackage(appDirectory, originalAsarPath);
};
const injectStyles = () => {
console.info("Chungus-ify-ing");
const injectOpenData = fs.readFileSync(injectOpenFile);
const stylesData = fs.readFileSync(stylesFile);
const injectCloseData = fs.readFileSync(injectCloseFile);
const appendStream = fs.createWriteStream(appendTarget, { flags: "a" });
appendStream.write(injectOpenData);
appendStream.write(stylesData);
appendStream.write(injectCloseData);
appendStream.end();
};
const removePackagedSlackFile = () => {
console.info("Removing packaged version of Slack");
del.sync(originalAsarPath, { force: true });
};
const displayDoneMessage = () => {
if (commandLineOptions.uninstall) {
console.info("Done!");
} else {
console.info("Done, have fun!");
}
};
const install = () => {
closeSlack();
displayMemeMessage();
removeModifiedFiles();
if (originalPackagedFileExists()) {
backupPackagedFile();
} else {
restorePackageFile();
}
unpackSlackFiles();
injectStyles();
removePackagedSlackFile();
packageSlackFiles();
displayDoneMessage();
};
const uninstall = () => {
closeSlack();
removeModifiedFiles();
removePackagedSlackFile();
if (!originalPackagedFileExists()) {
restorePackageFile();
}
displayDoneMessage();
};
if (!commandLineOptions.uninstall) {
install();
} else {
uninstall();
}