-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·295 lines (270 loc) · 9.88 KB
/
setup.sh
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
#credits
echo "==================================="
echo "Azle🚀 + React⚛️ Setup Script"
echo "Credits: ⭐️divin3circle⭐️"
echo "==================================="
# project name
echo "📝Enter project name: "
read -p "👉" project_name
echo "Setting up $project_name for you..."
echo ""
# main project directory
mkdir $project_name
cd $project_name
mkdir frontend backend declarations
npm install @types/node
echo "{
\"compilerOptions\": {
\"typeRoots\": [\"./node_modules/@types\"],
\"esModuleInterop\": true
}
}" > tsconfig.json
# backend setup
cd backend
echo ""
echo "👨💻 Setting up backend..."
npx azle new .
npm install
echo ""
echo "∞ Starting local internet computer replica..."
dfx start --clean --background
dfx deploy
CANISTER_ID=$(dfx canister id hello_world)
echo "1/3 => ✅Backend setup complete!"
cd ..
echo "📝Setting up declarations files"
cd declaration
echo "📝 Creating declarations file..."
echo "import fs from \"fs\";" > generateActor.js
echo "import path from \"path\";" >> generateActor.js
echo "" >> generateActor.js
echo "// Path to the .did file generated by Azle (replace with actual path)" >> generateActor.js
echo "const didFilePath = path.join(" >> generateActor.js
echo " __dirname," >> generateActor.js
echo " \"..\",">> generateActor.js
echo " \"backend\"," >> generateActor.js
echo " \".azle\"," >> generateActor.js
echo " \"hello_world\"," >> generateActor.js
echo " \"hello_world.did\"" >> generateActor.js
echo "); // Adjust path as necessary" >> generateActor.js
echo "" >> generateActor.js
echo "// Read the .did file" >> generateActor.js
echo "const didFile = fs.readFileSync(didFilePath, \"utf-8\");" >> generateActor.js
echo "" >> generateActor.js
echo "// Parse the .did file into JSON (assuming a format like 'service: () -> {...}')" >> generateActor.js
echo "const serviceDefinition = JSON.parse(\`{\${didFile}}\`).service;" >> generateActor.js
echo "" >> generateActor.js
echo "// Start generating the actor code" >> generateActor.js
echo "let actorCode = \`" >> generateActor.js
echo "import { Actor, HttpAgent } from '@dfinity/agent';" >> generateActor.js
echo "import { Principal } from '@dfinity/principal';" >> generateActor.js
echo "" >> generateActor.js
echo "const agent = new HttpAgent({ host: 'http://127.0.0.1:4943' });" >> generateActor.js
echo "" >> generateActor.js
echo "if (process.env.NODE_ENV === 'development') {" >> generateActor.js
echo " agent.fetchRootKey();" >> generateActor.js
echo "}" >> generateActor.js
echo "" >> generateActor.js
echo "const actor = Actor.createActor(" >> generateActor.js
echo " ({ IDL }) => {" >> generateActor.js
echo " return IDL.Service({" >> generateActor.js
echo "\`;" >> generateActor.js
echo "" >> generateActor.js
echo "// Loop through the service methods and generate their corresponding IDL" >> generateActor.js
echo "Object.keys(serviceDefinition).forEach((methodName) => {" >> generateActor.js
echo " const method = serviceDefinition[methodName];" >> generateActor.js
echo "" >> generateActor.js
echo " // If the method is a query" >> generateActor.js
echo " if (method.includes(\"query\")) {" >> generateActor.js
echo " const returnType = method.split(\" -> \")[1].split(\" \")[0];" >> generateActor.js
echo " actorCode += \`" >> generateActor.js
echo " \${methodName}: IDL.Func([], [IDL.\${returnType}], { query: true })," >> generateActor.js
echo " \`;" >> generateActor.js
echo " }" >> generateActor.js
echo "" >> generateActor.js
echo " // If the method is an update" >> generateActor.js
echo " else if (method.includes(\"->\")) {" >> generateActor.js
echo " const args = method.split(\"(\")[1].split(\")\")[0];" >> generateActor.js
echo " const returnType = method.split(\"->\")[1].trim();" >> generateActor.js
echo " actorCode += \`" >> generateActor.js
echo " \${methodName}: IDL.Func([IDL.\${args}], [\${returnType === \"()\" ? \"IDL.Void\" : \"IDL.\" + returnType}], [])," >> generateActor.js
echo " \`;" >> generateActor.js
echo " }" >> generateActor.js
echo "});" >> generateActor.js
echo "" >> generateActor.js
echo "actorCode += \`" >> generateActor.js
echo " });" >> generateActor.js
echo " }," >> generateActor.js
echo " { agent, canisterId: Principal.fromText('${CANISTER_ID}') }" >> generateActor.js
echo ");" >> generateActor.js
echo "" >> generateActor.js
echo "export default actor;" >> generateActor.js
echo "\`;" >> generateActor.js
echo "" >> generateActor.js
echo "// Write the generated actor code to a file in the frontend folder" >> generateActor.js
echo "fs.writeFileSync(" >> generateActor.js
echo " path.join(__dirname, \"frontend\", \"src\", \"actor.js\")," >> generateActor.js
echo " actorCode" >> generateActor.js
echo ");" >> generateActor.js
echo "" >> generateActor.js
echo "console.log(\"Generated actor.js with backend methods!\");" >> generateActor.js
node ./generateActor.js
#frontend setup
echo ""
echo "👨💻 Setting up frontend..."
cd frontend
npm create vite@latest . -- --template react -y
npm install @dfinity/agent @dfinity/principal @types/node process node-libs-browser global
# install taiwlindcss and postcss
npm install -D tailwindcss postcss autoprefixer
# create & modify tailwind.config.js configuration file
npx tailwindcss init -p
echo ""
echo "2/3 => ✅Frontend setup complete!"
#update app.tsx, tailwindcss config file & vite.config.js
echo ""
echo "📝 Updating files Azle backend logic..."
echo 'import { useState, useEffect } from "react";
import { Actor, HttpAgent } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
const canisterId = Principal.fromText('$CANISTER_ID');
const App = () => {
const [message, setMessageState] = useState("Nothing here yet");
const [isLoading, setIsLoading] = useState(false);
const [inputMessage, setInputMessage] = useState("");
// Initialize the HttpAgent and Actor
const agent = new HttpAgent({ host: "http://127.0.0.1:4943" });
if (process.env.NODE_ENV === "development") {
agent.fetchRootKey();
}
const actor = Actor.createActor(
({ IDL }) => {
return IDL.Service({
getMessage: IDL.Func([], [IDL.Text], []),
setMessage: IDL.Func([IDL.Text], [], []),
});
},
{ agent, canisterId }
);
const getMessage = async () => {
try {
setIsLoading(true);
const messageFromCanister = await actor.getMessage();
setMessageState(messageFromCanister);
setIsLoading(false);
} catch (error) {
setIsLoading(false);
console.error("Error fetching message:", error);
}
};
const setMessage = async () => {
try {
setIsLoading(true);
await actor.setMessage(inputMessage);
setInputMessage("");
getMessage();
setIsLoading(false);
} catch (error) {
setIsLoading(false);
console.error("Error setting message:", error);
}
};
useEffect(() => {
getMessage();
}, []);
return (
<div className="relative flex items-center justify-center h-screen w-full flex-col gap-4 px-4 font-mono">
{isLoading && (
<div className="absolute inset-0 bg-black bg-opacity-50 backdrop-blur-sm flex items-center justify-center z-10">
<div className="text-white">Setting message...</div>
</div>
)}
<div className="flex flex-col items-center justify-center gap-4 mb-12">
<div className="flex items-center gap-2">
<img
src="https://raw.githubusercontent.com/demergent-labs/azle/main/logo/logo.svg"
alt="Azle Logo"
className="w-24 h-24"
/>
<img src={viteLogo} alt="React Logo" className="w-24 h-24" />
</div>
<h1 className="text-3xl font-bold my-6">Azle + React Starter</h1>
<h1 className="text-xl leading-relaxed font-bold w-full text-center">
Message from the canister
</h1>
<h1 className="text-normal leading-relaxed w-full text-center">
{message}
</h1>
<input
type="text"
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
className="py-2 px-4 border border-gray-300 rounded-md"
/>
<button
onClick={setMessage}
className="py-2 px-8 bg-black text-white rounded-md"
>
Set Message
</button>
</div>
<h1 className="text-gray-500">
Start by editing{" "}
<span className="bg-gray-100 p-2 rounded-md font-semibold">
App.jsx
</span>{" "}
for frontend.
</h1>
<h1 className="text-gray-500">
And{" "}
<span className="bg-gray-100 p-2 rounded-md font-semibold">
index.ts
</span>{" "}
for backend(Azle).
</h1>
<a
href="https://github.com/divin3circle/azle-starter"
className="text-sm mt-8 text-blue-500 underline"
target="_blank"
>
Github(Documentation)
</a>
</div>
);
};
export default App; ' > src/App.jsx
echo "import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
define: {
global: 'globalThis',
},
plugins: [react()],
}); " > vite.config.js
echo "/** @type {import('tailwindcss').Config} */
export default {
content: [
'./index.html',
'./src/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
plugins: [],
}; " > tailwind.config.js
echo "@tailwind base;
@tailwind components;
@tailwind utilities; " > src/index.css
echo ""
echo "3/3 => ✅Files update complete""
echo ""
echo "⚠️Remeber to input your canister-id in App.tsx!!""
echo "ℹYour canister-id can be gotten after running dfx deploy in the backend directory"
cd ..
# additional instructions to the user
echo "==============================="
echo "🚀Setup complete!"
echo "Run 'dfx start' in the backend directory and 'npm run dev' in the frontend directory."
echo "Check readme for more details https://github.com/divin3circle/azle-starter"
echo "==============================="