aboutsummaryrefslogtreecommitdiff
path: root/index.js
blob: 5697cb25a5108df9e669dc6d2b1af8dc0499b329 (plain)
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
/*
 * MIT License
 *
 * Copyright (c) 2022- Minteck
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

const fs = require('fs');
const os = require('os');

if (process.argv.includes("--remove")) {
    console.log("Removing the Twilight Package Manager and all installed packages in 5 seconds.");
    console.log("Press Ctrl+C to abort.");
    global.to = 6;
    global.toi = setInterval(() => {
        to--;
        if (to <= 0) {
            clearInterval(toi);
            console.log("");
            console.log("Preparing...");

            if (fs.existsSync(os.homedir() + "/.twilight")) {
                try {
                    fs.mkdirSync(os.homedir() + "/.twilight/.uninstalltest");
                    fs.rmdirSync(os.homedir() + "/.twilight/.uninstalltest");
                } catch (e) {
                    console.log("Error -2: Cannot write to the Twilight install directory.");
                    process.exit(2);
                }
                try {
                    fs.mkdirSync(os.homedir() + "/.uninstalltest");
                    fs.rmdirSync(os.homedir() + "/.uninstalltest");
                } catch (e) {
                    console.log("Error -3: Cannot write to the Twilight install directory's parent directory.");
                    process.exit(3);
                }
            } else {
                console.log("Error -1: Cannot find an existing Twilight install.");
                process.exit(1);
            }

            console.log("Reading installed packages...");
            try {
                global.installed = require(os.homedir() + "/.twilight/installed.json");
            } catch (e) {
                console.log("Error -4: Unable to read installed packages.");
                process.exit(4);
            }
            try {
                global.repository = require(os.homedir() + "/.twilight/repository/list.json").map((i) => {return require(os.homedir() + "/.twilight/repository/" + i.substring(0, 1) + "/" + i + ".json");});
                global.ids = repository.map((i) => { return i.id; });
            } catch (e) {
                console.log("Error -5: Unable to read packages lists.");
                process.exit(4);
            }

            try {
                fs.mkdirSync(os.homedir() + "/Old Twilight Packages");
            } catch (e) {
                console.log("Error -6: Unable to create backup directory.");
                process.exit(6);
            }

            console.log("Backing up installed packages...");
            for (let pkg of installed) {
                if (!ids.includes(pkg.id)) {
                    console.log("Error -7: Unable to find reference to package: " + pkg.id);
                    process.exit(7);
                }
                let select = repository.filter((i) => i.id === pkg.id)[0];

                if (!fs.existsSync(os.homedir() + "/.twilight/packages/" + pkg.id)) {
                    console.log("Error -8: Unable to find package on disk: " + pkg.id);
                    process.exit(8);
                }

                try {
                    fs.renameSync(os.homedir() + "/.twilight/packages/" + pkg.id, os.homedir() + "/Old Twilight Packages/" + select.name.replaceAll("/", ""));
                } catch (e) {
                    console.log("Error -9: Unable to move package to backup directory: " + pkg.id);
                    process.exit(9);
                }

                if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + (select.execname ?? pkg.id) + (os.platform() === "win32" ? ".bat" : ""))) {
                    try {
                        fs.renameSync(os.homedir() + "/.twilight/binaries/" + (select.execname ?? pkg.id) + (os.platform() === "win32" ? ".bat" : ""), os.homedir() + "/Old Twilight Packages/" + select.name.replaceAll("/", "") + "/" + (select.execname ?? pkg.id) + (os.platform() === "win32" ? ".bat" : ""));
                    } catch (e) {
                        console.log("Error -10: Unable to reproduce package executable: " + pkg.id);
                        process.exit(9);
                    }
                }
            }

            console.log("-------------------------------------------------------------------------------");
            console.log("The Twilight Package Manager has now been uninstalled from your system.");
            console.log("You may need to update the $PATH variable and restart your system to complete");
            console.log("the uninstall process.");
            console.log("");
            console.log("To get more details about this change and how it affects the way you use");
            console.log("Minteck's software and services, visit the following the page:");
            console.log("  * https://gitlab.minteck.org/minteck/twilight/-/blob/trunk/README.md");
            console.log("");
            console.log("All packages you had installed, including the command you could have used to");
            console.log("run them, have been moved to the following directory:");
            console.log("  * " + (os.homedir() + "/Old Twilight Packages").replaceAll("/", (os.platform() === "win32" ? "\\" : "/")));
            console.log("");
            console.log("Thanks to everyone who used this package manager!")
            console.log("-------------------------------------------------------------------------------");
        } else {
            process.stdout.write(" ..." + to);
        }
    }, 1000);
} else {
    console.log("-------------------------------------------------------------------------------");
    console.log("The Twilight Package Manager is now out of support and cannot be working again.");
    console.log("Your installed packages are still usable, although it is recommended that you");
    console.log("use the standalone packages instead.");
    console.log("");
    console.log("To get more details about this change and how it affects the way you use");
    console.log("Minteck's software and services, visit the following the page:");
    console.log("  * https://gitlab.minteck.org/minteck/twilight/-/blob/trunk/README.md");
    console.log("");
    console.log("To uninstall the Twilight Package Manager (and your installed packages), run");
    console.log("this command:");
    console.log("  * twi --remove");
    console.log("");
    console.log("Thanks to everyone who used this package manager!")
    console.log("-------------------------------------------------------------------------------");
}