-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddToken.js
140 lines (118 loc) · 4.08 KB
/
addToken.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
import { execSync } from "child_process";
import { existsSync } from "fs";
import Menu from "./menu.js";
import { getJsonList, closeReadline } from "./addToJsonList.js";
import { getChainIdFromName } from "./getChainIdFromName.js";
import { getChainNameFromId } from "./getChainNameFromId.js";
async function addToken() {
process.stdin.setRawMode(true);
process.stdin.resume();
try {
console.clear();
// Select token list repository
const menu = new Menu();
const choices = ["default", "extended", "unsupported"];
const selectedChoice = await menu.getChoice({
query: "Select List",
items: choices,
initialIndex: 0,
});
// Set repository based on selection
let repository;
let folderName;
switch (selectedChoice) {
case "default":
repository = "[email protected]:Uniswap/default-token-list.git";
break;
case "extended":
repository = "[email protected]:Uniswap/extended-token-list.git";
break;
case "unsupported":
repository = "[email protected]:Uniswap/unsupported-token-list.git";
break;
}
// Extract folder name from repository URL
folderName = repository.split("Uniswap/")[1].replace(".git", "");
// Install dependencies if needed
if (!existsSync("node_modules")) {
console.log("installing dependencies");
execSync("npm install", { stdio: "inherit" });
}
// Clone repository if needed
if (!existsSync(folderName)) {
console.log("cloning");
execSync(`git clone ${repository}`, { stdio: "inherit" });
}
// Change directory and update repo
process.chdir(`./${folderName}/src/tokens`);
execSync("git reset --hard && git checkout main && git pull", {
stdio: "inherit",
});
// Get list of chain files
const files = execSync("ls -1")
.toString()
.split("\n")
.filter(Boolean)
.map((file) => file.replace(/\.json$/, ""));
// Process chains
let addAnotherChain = "Yes";
const allNames = [];
let names = [];
while (addAnotherChain === "Yes") {
const chainMenu = new Menu();
const selectedChain = await chainMenu.getChoice({
query: "Select Chain",
items: files,
initialIndex: 0,
});
const chainName = getChainIdFromName(selectedChain);
const chainId = getChainNameFromId(chainName);
const { jsonList, names: newNames } = await getJsonList(chainId);
// Update JSON file
const currentFile = `./${selectedChain}.json`;
const currentContent = JSON.parse(
execSync(`cat ${currentFile}`).toString()
);
const updatedContent = [...currentContent, ...jsonList];
execSync(
`echo '${JSON.stringify(updatedContent, null, 2)}' > ${currentFile}`
);
allNames.push(...newNames);
names = newNames;
const continueChoices = ["Yes", "No"];
addAnotherChain = await new Menu().getChoice({
query: "Modify Another Chain?",
items: continueChoices,
initialIndex: 0,
});
}
// Create branch and commit changes
const random = Math.floor(Math.random() * 100) + 1;
const date = new Date().toISOString().split("T")[0];
const branchName = `add-${allNames.join("-")}-${date}-${random}`.replace(
/\s+/g,
"_"
);
execSync(`git checkout -b ${branchName}`, { stdio: "inherit" });
execSync("git add -u", { stdio: "inherit" });
const commitMsg = `add ${names.join(" ")}`;
execSync(`git commit -m "${commitMsg}"`, { stdio: "inherit" });
execSync("npm version minor", { stdio: "inherit" });
execSync("git push -u -f origin HEAD", { stdio: "inherit" });
execSync("gh pr create", { stdio: "inherit" });
execSync("git checkout main", { stdio: "inherit" });
} catch (error) {
process.stdin.setRawMode(false);
process.stdin.pause();
console.error(error);
} finally {
process.stdin.setRawMode(false);
process.stdin.pause();
closeReadline();
}
}
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
addToken().catch(console.error);
}
export { addToken };