-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
executable file
·273 lines (252 loc) · 10.2 KB
/
init.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const fs = require("fs");
const readline = require("readline");
const { spawn, execSync } = require("child_process");
const path = require("path");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const writeEnv = (key, value) => {
const envFile = ".env";
let content = "";
if (fs.existsSync(envFile)) {
content = fs.readFileSync(envFile, "utf8");
const regex = new RegExp(`^${key}=.*`, "m");
if (regex.test(content)) {
content = content.replace(regex, `${key}=${value}`);
} else {
content += `\n${key}=${value}`;
}
} else {
content = `${key}=${value}`;
}
fs.writeFileSync(envFile, content, "utf8");
};
const getProjectName = () => {
return new Promise((resolve) => {
const askProjectName = () => {
rl.question("Choose a name for your project: ", (projectName) => {
if (projectName.trim() === "") {
console.log(
"Project name cannot be blank. Please enter a valid name.",
);
askProjectName();
} else {
console.log(`You have chosen the project name: ${projectName}`);
resolve(projectName);
}
});
};
askProjectName();
});
};
const displayOptions = (options) => {
options.forEach((option, index) => {
console.log(`${index + 1}) ${option}`);
});
};
const promptOptions = (message, options, defaultOption) => {
return new Promise((resolve) => {
const askOption = () => {
displayOptions(options);
rl.question(message, (reply) => {
if (reply.trim() === "") {
console.log(`No option selected. Defaulting to ${defaultOption}.`);
resolve(defaultOption);
} else if (!isNaN(reply) && reply >= 1 && reply <= options.length) {
resolve(options[reply - 1]);
} else {
console.log(`Invalid option ${reply}. Please select a valid number.`);
askOption();
}
});
};
askOption();
});
};
const printIntro = () => {
const intro = `
╔───────────────────────────────────────────────────────────────────────────────────────────────────────────────╗
│ │
│ .... -+++++++. .... │
│ -++++++++- .++++++++. _____ _ _ _ _ ____ _ _ │
│ .++- .. .++- .++- | ____| |_| |__ ___ _ __ _ __ (_) |_ _ _ / ___| | ___ _ _ __| | │
│ --++---- .++- ... | _| | __| '_ \\ / _ \\ '__| '_ \\| | __| | | | | | | |/ _ \\| | | |/ _\` | │
│ --++---- .++-. ... | |___| |_| | | | __/ | | | | | | |_| |_| | | |___| | (_) | |_| | (_| | │
│ .++- .+++. . .--. |_____|\\__|_| |_|\\___|_| |_| |_|_|\\__|\\__, | \\____|_|\\___/ \\__,_|\\__,_| │
│ -++++++++. .---------. |___/ │
│ .... .-------. .... │
│ │
╚───────────────────────────────────────────────────────────────────────────────────────────────────────────────╝
Welcome to the Ethernity Cloud SDK
The Ethernity Cloud SDK is a comprehensive toolkit designed to facilitate the development and management of
decentralized applications (dApps) and serverless binaries on the Ethernity Cloud ecosystem. Geared towards
developers proficient in Python or Node.js, this toolkit aims to help you effectively harness the key features
of the ecosystem, such as data security, decentralized processing, and blockchain-driven transparency and
trustless model for real-time data processing.
`;
console.log(intro);
};
const main = async () => {
printIntro();
const projectName = await getProjectName();
console.log();
const serviceTypeOptions = ["Nodenithy", "Pynithy", "Custom"];
const serviceType = await promptOptions(
"Select the type of code to be ran during the compute layer (default is Nodenithy): ",
serviceTypeOptions,
"Nodenithy",
);
let dockerRepoUrl, dockerLogin, dockerPassword, baseImageTag;
if (serviceType === "Custom") {
dockerRepoUrl = await new Promise((resolve) =>
rl.question("Enter Docker repository URL: ", resolve),
);
dockerLogin = await new Promise((resolve) =>
rl.question("Enter Docker Login (username): ", resolve),
);
dockerPassword = await new Promise((resolve) =>
rl.question("Enter Password: ", resolve),
);
baseImageTag = await new Promise((resolve) =>
rl.question("Enter the image tag: ", resolve),
);
}
console.log();
const blockchainNetworkOptions = [
"Bloxberg Mainnet",
"Bloxberg Testnet",
"Polygon Mainnet",
"Polygon Amoy Testnet",
];
const blockchainNetwork = await promptOptions(
"On which Blockchain network do you want to have the app set up, as a starting point? (default is Bloxberg Testnet): ",
blockchainNetworkOptions,
"Bloxberg Testnet",
);
console.log();
console.log(
`Checking if the project name (image name) is available on the ${blockchainNetwork.replace(/ /g, "_")} network and ownership...`,
);
// const { execSync } = require('child_process');
// execSync(`python $(pwd)/node_modules/ethernity-cloud-sdk-js/nodenithy/run/image_registry.py "${blockchainNetwork.replace(/ /g, "_")}" "${projectName.replace(/ /g, "-")}" v3`);
const scriptPath = path.resolve(__dirname, "nodenithy/run/image_registry.js");
// Spawn a new process to run the image_registry.js script
const runChildProcess = () =>
new Promise((resolve, reject) => {
const child = spawn("node", [
scriptPath,
blockchainNetwork.replace(/ /g, "_"),
projectName.replace(/ /g, "-"),
"v3",
]);
// Handle stdout data
child.stdout.on("data", (data) => {
console.log(
`${data}`.replace("duplicate definition - constructor", ""),
);
});
child.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
await runChildProcess();
// // Handle stderr data
// child.stderr.on('data', (data) => {
// console.error(`stderr: ${data}`);
// });
// Handle process exit
// child.on('close', (code) => {
// console.log(`child process exited with code ${code}`);
// });
console.log();
const ipfsServiceOptions = ["Ethernity (best effort)", "Custom IPFS"];
const ipfsService = await promptOptions(
"Select the IPFS pinning service you want to use (default is Ethernity): ",
ipfsServiceOptions,
"Ethernity (best effort)",
);
let customUrl, ipfsToken;
if (ipfsService === "Custom IPFS") {
customUrl = await new Promise((resolve) =>
rl.question(
"Enter the endpoint URL for the IPFS pinning service you want to use: ",
resolve,
),
);
ipfsToken = await new Promise((resolve) =>
rl.question(
"Enter the access token to be used when calling the IPFS pinning service: ",
resolve,
),
);
} else {
customUrl = "http://ipfs.ethernity.cloud:5001";
}
fs.mkdirSync("src/serverless", { recursive: true });
console.log();
const appTemplateOptions = ["yes", "no"];
const useAppTemplate = await promptOptions(
"Do you want a 'Hello World' app template as a starting point? (default is yes): ",
appTemplateOptions,
"yes",
);
if (useAppTemplate === "yes") {
console.log("Bringing Frontend/Backend templates...");
console.log(" src/serverless/backend.js (Hello World function)");
console.log(
" src/ec_helloworld_example.js (Hello World function call - Frontend)",
);
// Simulate copying files
fs.cpSync("node_modules/ethernity-cloud-sdk-js/nodenithy/src/", "src/", {
recursive: true,
});
fs.cpSync(
"node_modules/ethernity-cloud-sdk-js/nodenithy/public/",
"public/",
{ recursive: true },
);
console.log("Installing required packages...");
// Simulate npm install
execSync(
"npm install @ethernity-cloud/[email protected] @testing-library/[email protected] @testing-library/[email protected] @testing-library/[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]",
{ stdio: "inherit" },
);
} else {
console.log(
"Define backend functions in src/ectasks to be available for Frontend interaction.",
);
}
writeEnv("PROJECT_NAME", projectName.replace(/ /g, "_"));
writeEnv("SERVICE_TYPE", serviceType);
if (serviceType === "Custom") {
writeEnv("BASE_IMAGE_TAG", baseImageTag || "");
writeEnv("DOCKER_REPO_URL", dockerRepoUrl);
writeEnv("DOCKER_LOGIN", dockerLogin);
writeEnv("DOCKER_PASSWORD", dockerPassword);
} else if (serviceType === "Nodenithy") {
writeEnv("BASE_IMAGE_TAG", "");
writeEnv("DOCKER_REPO_URL", "registry.scontain.com:5050");
writeEnv("DOCKER_LOGIN", "");
writeEnv("DOCKER_PASSWORD", "");
} else if (serviceType === "Pynithy") {
writeEnv("BASE_IMAGE_TAG", "");
writeEnv("DOCKER_REPO_URL", "registry.scontain.com:5050");
writeEnv("DOCKER_LOGIN", "");
writeEnv("DOCKER_PASSWORD", "");
}
writeEnv("BLOCKCHAIN_NETWORK", blockchainNetwork.replace(/ /g, "_"));
writeEnv("IPFS_ENDPOINT", customUrl);
writeEnv("IPFS_TOKEN", ipfsToken || "");
writeEnv("VERSION", "v1");
console.log();
console.log(
"To start the application, run the appropriate start command based on your setup.",
);
rl.close();
};
main();