This repository has been archived by the owner on May 27, 2020. It is now read-only.
/
hoaxwagen.js
executable file
·127 lines (105 loc) · 4.29 KB
/
hoaxwagen.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
'use strict';
/********************************************
* *
* DISCLAIMER: in the code below you may *
* encounter some amount of ugly code, *
* which might not be cleaned by the *
* Clean-Code Group in near future, and *
* which might include code repetitions, *
* ridiculously awful code style, *
* spaghetti-code and/or a feeling, which *
* possibly might have appeared *
* previously, which may lead to the *
* conclusion that the author of the code *
* is a dumbass. Please do not act *
* aggressively and/or recklessly, and *
* *
* Hold on to your butts *
* *
********************************************/
const fs = require('fs');
const path = require('path');
function vwExists() { // does it?
try {
require.resolve('volkswagen');
return true // well... I've got bad news for you!
} catch (e) {
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
// that's odd
return false
}
}
}
function getPackages() {
const packages = []; // a board of shame
const folders = fs.readdirSync(path.resolve('./node_modules'));
folders.forEach((folder) => {
if (fs.existsSync(path.join('./node_modules', folder, 'package.json'))) { // that's the case in f(x)=x/(x+2) % under natural conditions
const obj = JSON.parse(fs.readFileSync(path.join('./node_modules', folder, "package.json"), 'utf8')); // God bless it!
if (isValid(obj) && obj['name'] !== 'hoaxwagen') { // 1. is it the bad guy? 2. preventing the suicide
const p = parseObj(obj, path.resolve(path.join('./node_modules', folder)));
packages.push(p);
}
}
});
return packages;
}
function isValid(obj) { // bad guys validation
const isNull = (dep1, dep2) => (dep2 == null)
? typeof obj[dep1] === "undefined"
: typeof obj[dep1][dep2] === "undefined";
const isEmpty = (dep) => Object.keys(obj[dep]).length === 0;
// not-TODO: optimize this mess someday
if (isNull('dependencies') || isEmpty('dependencies')) {
if (isNull('devDependencies') || isEmpty('devDependencies')) {
return false
} else if (isNull('devDependencies', 'volkswagen')) {
return false
}
} else if (isNull('dependencies', 'volkswagen')) {
return false
}
return true
}
function parseObj(obj, path) {
const isNull = (dep1) => obj[dep1] === "undefined";
return {
name: isNull('name') ? null : obj["name"],
version: isNull('version') ? null : obj["version"],
yarn: isNull('name') ? null : "yarn.pm/" + obj["name"], // yarn or npm?
path: path
}
}
function monitor() { // call it in Webpack or <insert whatever you want>
if (!vwExists()) {
console.log('✅ Clean from Volkswagen!'); // much clean, very Volkswagen
return
}
const p = getPackages();
// This is an example of something aimed at clarifying, that makes even more mess than before
const warn = (text) => console.log('\x1b[31m%s\x1b[0m', text);
const warn2 = (text1, text2) => console.log('\x1b[31m%s\x1b[0m%s', text1, text2);
const dflt = (text) => console.log(text);
switch (p.length) {
case 0: // no package uses it... well, does it?
warn('🚨 WARNING 🚨 Volkswagen 🛴 is detected, but no package uses it.\n\n' +
'...wait a second! Is that you? Are you the person who uses it!? Shame on you!');
return;
case 1: // It could have been worse, John. A lot worse.
warn2(`🚨 WARNING 🚨 Volkswagen 🛴 is detected in the following package: `, `${p[0].name} @ ${p[0].version} (${p[0].yarn})`);
break;
default: // damn it!
warn(`🚨 WARNING 🚨 Volkswagen 🛴 is detected in the following packages:`);
p.forEach((p, i) => dflt(`${i+1}. ${p.name} @ ${p.version} (${p.yarn})`));
}
warn('🚧🕳 BE AWARE OF POSSIBLE FUTURE CONSEQUENCES!'); // and thankful to Code-Clean Group for finding them
}
// finally, I export something other than my stupidity!
module.exports = {
monitor: monitor,
isFraud: vwExists, // very unlikely
getPackages: getPackages,
_isValid: isValid, // for testing purposes only
_parseObj: parseObj // only for testing purposes
};
// good bye