-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsetup.js
107 lines (99 loc) · 3.05 KB
/
setup.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
import fs from "fs";
import path from "path";
async function fileExists(filePath) {
try {
await fs.promises.access(filePath);
return true;
} catch {
return false;
}
}
async function copyFile(src, dest) {
try {
if (await fileExists(dest)) {
console.log(`File already exists at ${dest}, skipping copy.`);
} else {
await fs.promises.copyFile(src, dest);
console.log(`Copied ${src} to ${dest}`);
}
} catch (err) {
console.error(`Error copying file from ${src} to ${dest}:`, err);
}
}
async function createFolder(folderPath) {
try {
const folderExists = await fs.promises
.access(folderPath)
.then(() => true)
.catch(() => false);
if (!folderExists) {
await fs.promises.mkdir(folderPath, { recursive: true });
console.log(`Created folder: ${folderPath}`);
}
} catch (err) {
console.error(`Error creating folder ${folderPath}:`, err);
}
}
async function fixMgoImports(dir) {
try {
const files = await fs.promises.readdir(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
await fixMgoImports(fullPath);
} else if (file.isFile() && file.name.endsWith(".js")) {
const content = await fs.promises.readFile(fullPath, "utf-8");
if (content.includes('from "../utils/mgo-types"')) {
const updatedContent = content.replace(
/from "\.\.\/utils\/mgo-types"/g,
'from "../utils/mgo-types.js"'
);
await fs.promises.writeFile(fullPath, updatedContent, "utf-8");
console.log(`Fixed imports in: ${fullPath}`);
}
if (content.includes('from "./mgo-system-state"')) {
const updatedContent = content.replace(
/from "\.\/mgo-system-state"/g,
'from "./mgo-system-state.js"'
);
await fs.promises.writeFile(fullPath, updatedContent, "utf-8");
console.log(`Fixed imports in: ${fullPath}`);
}
}
}
} catch (err) {
console.error(`Error fixing imports in directory ${dir}:`, err);
}
}
const copyOperations = [
{
src: path.join("config", "proxy_list_tmp.js"),
dest: path.join("config", "proxy_list.js"),
},
{
src: path.join("config", "config_tmp.js"),
dest: path.join("config", "config.js"),
},
{
src: path.join("accounts", "accounts_tmp.js"),
dest: path.join("accounts", "accounts.js"),
},
];
(async () => {
console.log(`Copying Template File`);
await createFolder("accounts");
for (let { src, dest } of copyOperations) {
await copyFile(src, dest);
}
console.log(`\nFixing @mgonetwork/mango.js imports...`);
const packageDir = path.join("node_modules", "@mgonetwork", "mango.js");
if (await fileExists(packageDir)) {
await fixMgoImports(packageDir);
} else {
console.error(`Directory ${packageDir} not found. Skipping import fixes.`);
}
console.log(`\nSetup Complete`);
console.log(
`Open and configure\n- accounts/accounts.js\n- config/config.js\n `
);
})();