-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlifLib.js
More file actions
474 lines (442 loc) · 21.1 KB
/
Copy pathAlifLib.js
File metadata and controls
474 lines (442 loc) · 21.1 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import fs from "fs";
import path from "path";
import { exec } from "child_process";
import readline from "readline";
import { createClient } from "@supabase/supabase-js";
import fetch from "node-fetch";
import { fileName } from "./alifweb";
const supabaseUrl = "https://gfyxjkwchsnczsmthpiw.supabase.co";
const supabaseKey =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImdmeXhqa3djaHNuY3pzbXRocGl3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDQyODUyODUsImV4cCI6MjA1OTg2MTI4NX0.b40Gi7Jsk_XkJSW-W5OU8oXyQZMHLUfqVw0R09Njyj4";
const supabase = createClient(supabaseUrl, supabaseKey);
const isPkg = typeof process.pkg !== "undefined";
const __filename = new URL(import.meta.url).pathname;
const __dirname = path.dirname(__filename);
let alifDir = isPkg ? path.dirname(process.execPath) : __dirname;
// دالة النشر
export function pubLib() {
checkGit();
if (!fileName) {
console.error("ادخل اسم المكتبة");
process.exit(1);
}
const pkgPath = path.join(process.cwd(), "مكتبة.الف");
if (!fs.existsSync(pkgPath)) {
console.error('ملف "مكتبة.الف" غير موجود في المجلد');
process.exit(1);
}
const data = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
const requiredFields = [
"الاسم",
"اسم_المستودع",
"الاصدار",
"الوصف",
"الصورة",
"المطور",
"حساب_المطور",
];
requiredFields.forEach((field) => {
if (!Object.keys(data).includes(field)) {
console.error(`الحقل "${field}" غير موجود في ملف مكتبة.الف`);
process.exit(1);
}
});
if (data["الاسم"].replaceAll(" ", "_") !== fileName) {
console.error(
`اسم المكتبة "${fileName}" لاكن بدل المسافة "_" يجب ان يكون مساوي لـ"الاسم" في ملف "مكتبة.الف"`
);
process.exit(1);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"هل تم إنشاء مستودع على GitHub ورفع الملفات؟ (نعم/لا): ",
(answer) => {
if (answer === "نعم" || answer === "y") {
// التحقق من وجود المستودع على GitHub
const repoUrl = `https://github.com/${data["حساب_المطور"]}/${data["اسم_المستودع"]}`;
exec(`git ls-remote ${repoUrl}`, (gitError, stdout, stderr) => {
if (gitError || stderr) {
console.error(
`تعذر الوصول إلى المستودع على GitHub: ${repoUrl}. تأكد من صحة الحساب واسم المستودع.`
);
process.exit(1);
} else {
console.log(
"تم التحقق من وجود المستودع على GitHub بنجاح."
);
// رفع الي قاعدة البيانات
const finalData = {
الاسم: data["الاسم"].replaceAll(" ", "_"),
اسم_المستودع: data["اسم_المستودع"],
الاصدار: data["الاصدار"],
الوصف: data["الوصف"],
الصورة: data["الصورة"],
المطور: data["المطور"],
حساب_المطور: data["حساب_المطور"],
المستودع: `https://github.com/${data["حساب_المطور"]}/${data["اسم_المستودع"]}/`,
التحميلات: 0,
};
supabase
.from("libraries")
.insert(finalData)
.then(({ error }) => {
if (error) {
if (
error.message.includes(
"duplicate key"
) &&
error.message.includes("الاسم")
) {
console.error("يوجد مكتبة بنفس الاسم");
} else if (
error.message.includes(
"duplicate key"
) &&
error.message.includes("المستودع")
) {
console.error(
"المستودع موجود في مكتبة اخري"
);
} else {
console.error(
"حدث خطا اثناء رفع المكتبة",
error
);
}
process.exit(1);
} else {
console.log("تم رفع المكتبة بنجاح!");
process.exit(0);
}
});
}
});
} else if (answer === "لا" || answer === "n") {
console.log(
"يرجى إنشاء مستودع على GitHub أولاً ثم إعادة المحاولة."
);
} else {
console.error("رد ب نعم او لا");
}
rl.close();
}
);
}
// دالة التحميل
export function getLib() {
async function install(fileName) {
try {
const libDir = path.join(alifDir, "libraries", fileName);
// console.log(`مكان المكتبات: ${alifDir}`);
await new Promise((resolve, reject) => {
supabase
.from("libraries")
.select("*")
.eq("الاسم", fileName)
.then(({ data, error }) => {
if (error || !data || data.length === 0) {
reject(
`حدث خطأ: لم يتم العثور على المكتبة "${fileName}"`
);
} else {
const developer = data[0].حساب_المطور;
const repoName = data[0].اسم_المستودع;
// تحميل المكتبة من الجيت هاب
exec(
`npx degit ${developer}/${repoName} ${libDir}`,
(err) => {
if (err) {
if (
String(err).includes(
"destination directory is not empty"
)
) {
reject(
`"المكتبة "${fileName}" موجودة بالفعل`
);
} else {
reject(`حدث خطأ: "${err}"`);
}
} else {
// زيادة عدد التحميلات
const downloads = data[0].التحميلات + 1;
supabase
.from("libraries")
.update({ التحميلات: downloads })
.eq("الاسم", fileName)
.then(({ error }) => {
if (error) {
console.error(
"حدث خطأ أثناء تحديث عدد التحميلات:",
error
);
}
});
console.log("تم تحميل المكتبة بنجاح!");
resolve();
}
}
);
}
});
});
} catch (error) {
console.error(error);
}
}
if (!fileName) {
console.error("اكتب اسم المكتبة صح");
} else {
install(fileName);
}
}
// دالة تحديث المكتبة
export function updateLib() {
async function reinstall(fileName) {
try {
const libDir = path.join(alifDir, "libraries", fileName);
if (!fs.existsSync(libDir)) {
console.error(
`المكتبة غير موجودة حملها بكتابة "alif install ${fileName}"`
);
process.exit(1);
}
const libFile = path.join(libDir, "مكتبة.الف");
let localVersion = null;
if (fs.existsSync(libFile)) {
const fileContent = JSON.parse(
fs.readFileSync(libFile, "utf8")
);
if (fileContent && fileContent["الاصدار"]) {
localVersion = fileContent["الاصدار"];
}
}
await new Promise((resolve, reject) => {
supabase
.from("libraries")
.select("*")
.eq("الاسم", fileName)
.then(({ data, error }) => {
if (error || !data || data.length === 0) {
reject(
`حدث خطأ: لم يتم العثور على المكتبة "${fileName}"`
);
} else {
const developer = data[0].حساب_المطور;
const repoName = data[0].اسم_المستودع;
const pubLibVer = data[0].الاصدار;
if (pubLibVer > localVersion) {
fs.rmSync(libDir, {
recursive: true,
force: true,
});
// تحميل المكتبة من الجيت هاب
exec(
`npx degit ${developer}/${repoName} ${libDir}`,
(err) => {
if (err) {
if (
String(err).includes(
"destination directory is not empty"
)
) {
reject(
`المكتبة "${fileName}" موجودة بالفعل`
);
} else {
reject(`حدث خطأ: "${err}"`);
}
} else {
console.log(
"تم تحديث المكتبة بنجاح!"
);
resolve();
}
}
);
} else {
console.error("لا يوجد تحديث متوفر");
}
}
});
});
} catch (error) {
console.error(error);
}
}
if (!fileName) {
console.error("اكتب اسم المكتبة صح");
} else {
reinstall(fileName);
}
}
// تحديث اللغة
export function alifUpdate(isLib = false) {
const نوع_التطبيق = isLib ? "AlifWeb" : "alif";
const اسم_التطبيق =
process.platform === "win32"
? `${نوع_التطبيق}.exe`
: process.platform === "darwin"
? `${نوع_التطبيق}-mac`
: `${نوع_التطبيق}-linux`;
const المستودع = isLib ? "iskepr/AlifWeb" : "alifcommunity/Alif";
const التطبيق_المحلي = path.join(alifDir, نوع_التطبيق);
fetch(`https://api.github.com/repos/${المستودع}/releases/latest`)
.then((response) => response.json())
.then((data) => {
if (!data || !data.tag_name) {
console.error(`حدث خطأ: لا يوجد إصدار متوفر`);
return;
}
const احدث_الإصدار = data.tag_name;
// جرب تشغيل الأمر للحصول على الإصدار الحالي
exec(`${التطبيق_المحلي} -v`, (err, stdout) => {
const الإصدار_الحالي = err
? "0.0.0"
: (stdout.match(/\d+\.\d+\.\d+/) || ["0.0.0"])[0];
if (الإصدار_الحالي === احدث_الإصدار) {
console.log(
`${نوع_التطبيق} بالفعل على أحدث إصدار (${احدث_الإصدار})`
);
return;
}
console.log(
`جارِ التحديث من ${الإصدار_الحالي} إلى ${احدث_الإصدار}...`
);
exec(
`curl -L -o ${path.join(
alifDir,
اسم_التطبيق
)} --create-dirs -z ${path.join(
alifDir,
اسم_التطبيق
)} https://github.com/${المستودع}/releases/download/${احدث_الإصدار}/${اسم_التطبيق}`,
(error) => {
if (error) {
console.error(`حدث خطأ أثناء التحديث:`, error);
return;
}
const الاسم_القديم = path.join(alifDir, اسم_التطبيق);
const الاسم_الجديد = path.join(alifDir, نوع_التطبيق);
// حذف الملف القديم إذا كان موجوداً
fs.unlink(الاسم_الجديد, (deleteError) => {
if (deleteError && deleteError.code !== "ENOENT") {
console.error(
`خطأ أثناء حذف الملف القديم:`,
deleteError
);
return;
}
// إعادة التسمية بعد الحذف
fs.rename(
الاسم_القديم,
الاسم_الجديد,
(renameError) => {
if (renameError) {
console.error(
`خطأ أثناء إعادة التسمية:`,
renameError
);
return;
}
fs.chmod(
الاسم_الجديد,
0o755,
(chmodError) => {
if (chmodError) {
console.error(
`خطأ في إضافة الصلاحيات:`,
chmodError
);
return;
}
console.log(
`تم تحديث ${نوع_التطبيق} إلى الإصدار ${احدث_الإصدار} بنجاح!`
);
}
);
}
);
});
}
);
});
})
.catch((error) => {
console.error(`فشل جلب الإصدار الأخير من ${المستودع}:`, error);
});
}
// التحقق من git
export function checkGit() {
exec("git --version", (error, stdout, stderr) => {
if (error || stderr) {
console.log(" Git غير مثبت، جاري تثبيته...");
let installCommand = "";
if (process.platform === "win32") {
installCommand =
"winget install --id Git.Git -e --source winget"; // ويندوز
} else if (process.platform === "darwin") {
installCommand = "brew install git"; // ماك
} else if (process.platform === "linux") {
exec("cat /etc/os-release", (osError, osStdout, osStderr) => {
if (osError || osStderr) {
console.error(
"لم يتم تحديد التوزيعة بشكل صحيح يرجي تثبيت git."
);
process.exit(1);
}
if (
osStdout.includes("ubuntu") ||
osStdout.includes("debian")
) {
installCommand = "sudo apt install git";
} else if (
osStdout.includes("fedora") ||
osStdout.includes("centos")
) {
installCommand = "sudo dnf install git";
}
if (installCommand) {
console.log(
`سيتم تثبيت git باستخدام: ${installCommand}`
);
exec(
installCommand,
(installError, installStdout, installStderr) => {
if (installError || installStderr) {
console.error(
"حدث خطأ أثناء تثبيت Git:",
installError || installStderr
);
process.exit(1);
}
console.log("تم تثبيت Git بنجاح!");
}
);
}
});
}
if (installCommand) {
console.log(`🚀 سيتم تثبيت git باستخدام: ${installCommand}`);
exec(
installCommand,
(installError, installStdout, installStderr) => {
if (installError || installStderr) {
console.error(
" حدث خطأ أثناء تثبيت Git:",
installError || installStderr
);
process.exit(1);
}
console.log(`${installStdout} تم تثبيت Git بنجاح!`);
}
);
}
} else {
// console.log(` Git اصدار: ${stdout}`);
}
});
}