ihub-cli 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +285 -114
  2. package/package.json +3 -1
package/index.js CHANGED
@@ -10,109 +10,14 @@ import dirtoFileArray from "./recursive_file_extractor.js"
10
10
  import { PinataSDK } from "pinata";
11
11
  import { provideClient } from "./dbconnection.js"
12
12
 
13
+ import pLimit from "p-limit";
14
+
13
15
 
14
16
  const IHUB_DIR = path.join(os.homedir(), ".ihub");
15
17
  const FILE_TO_STORE_LOGIN = path.join(IHUB_DIR, "login.txt");
16
18
 
17
19
 
18
20
 
19
- async function UpdateRepo(cid,pinata) {
20
-
21
-
22
- const result = await pinata.gateways.public.get(cid)
23
- const Data = result.data;
24
- console.log(Data)
25
- const arrayBuffer = await Data.arrayBuffer()
26
- const buffer = Buffer.from(arrayBuffer);
27
- console.log(buffer)
28
- deleteFilesWithExtension(".history.bundle",".")
29
- let dynamicstring=crypto.randomUUID()
30
- let shortID = dynamicstring.substring(0, 5)
31
- let bpath=`${shortID}.history.bundle`
32
-
33
- fs.writeFileSync(`${bpath}`,buffer);
34
-
35
- //const absolutePath = path.resolve(folder);
36
- const absoluteBundlePath = path.resolve(bpath);
37
-
38
- execSync(`git fetch ${absoluteBundlePath} refs/heads/*:refs/remotes/bundle/*`, {
39
- cwd: `.`,
40
- stdio: 'inherit'
41
- });
42
-
43
- const currentBranch = execSync(
44
- `git branch --show-current`,
45
- {
46
- cwd: '.',
47
- encoding: 'utf8',
48
- shell: true
49
- }
50
- ).trim();
51
-
52
- const bundleBranch = `bundle/${currentBranch}`;
53
-
54
- console.log(`šŸ”€ Merging ${bundleBranch} → ${currentBranch}`);
55
- execSync(
56
- `git merge ${bundleBranch} --allow-unrelated-histories --no-edit`,
57
- {
58
- cwd: '.',
59
- stdio: 'inherit',
60
- shell: true
61
- }
62
- );
63
-
64
- console.log("pulled successfully")
65
-
66
- }
67
-
68
-
69
-
70
- async function Pull(folder,pinata,client){
71
-
72
-
73
- const db = client.db("ihub_db");
74
- const coll = db.collection("ihub_col");
75
- const targetManifestId= fs.readFileSync(FILE_TO_STORE_LOGIN,'utf8');
76
- const doc = await coll.findOne({
77
- "owner": "system",
78
- "manifests.id": targetManifestId
79
- });
80
- if(doc) {
81
-
82
- let uploads=null
83
- let bundle=null
84
- let manifests=doc.manifests
85
-
86
-
87
- for(let obj of manifests){
88
-
89
- let dbfolder=String(obj.folder)
90
-
91
- if(obj.id==targetManifestId && dbfolder==folder){
92
- console.log(obj.folder)
93
- uploads=obj.uploads
94
- }
95
-
96
-
97
-
98
- }
99
- console.log(uploads)
100
-
101
- for(let obj of uploads){
102
-
103
- let name=obj.name
104
- const isIncluded = name.includes(".history.bundle");
105
- if(isIncluded) {
106
-
107
- bundle=obj.cid
108
-
109
- }}
110
-
111
- await UpdateRepo(bundle,pinata)
112
-
113
- }}
114
-
115
-
116
21
 
117
22
  async function getcreds() {
118
23
 
@@ -129,10 +34,14 @@ async function getcreds() {
129
34
  }
130
35
 
131
36
  async function getRuntime() {
37
+
132
38
  const { jwt, gateway } = await getcreds();
133
39
  const pinata = new PinataSDK({ pinataJwt: jwt, pinataGateway: gateway });
40
+
41
+
134
42
  const client = provideClient();
135
43
  return { pinata, client };
44
+
136
45
  }
137
46
 
138
47
 
@@ -182,11 +91,28 @@ function fileWithExtensionExists(extension,folder) {
182
91
 
183
92
 
184
93
 
185
- async function deleteManifest(targetManifestId, foldername,client) {
186
94
 
187
95
 
188
- const db = client.db("ihub_db");
189
- const coll = db.collection("ihub_col");
96
+ async function deleteManifest(targetManifestId, foldername,client,mpc,prompt) {
97
+
98
+
99
+
100
+
101
+ let db = client.db("ihub_db");
102
+ let coll = db.collection("ihub_col");
103
+
104
+ if (mcp ==true){
105
+
106
+ db = client.db("ihub_db");
107
+ coll = db.collection("mcp_registry");
108
+
109
+ }else if(prompt ==true){
110
+
111
+ db = client.db("ihub_db");
112
+ coll = db.collection("prompt_comp");
113
+ }
114
+
115
+
190
116
  await coll.updateOne(
191
117
  { owner: "system" },
192
118
  {
@@ -203,6 +129,60 @@ function fileWithExtensionExists(extension,folder) {
203
129
 
204
130
 
205
131
 
132
+
133
+ //--------------------------------------------------------------------
134
+
135
+ async function deactivateOlderVersions(targetId, targetFolder,mcp,prompt) {
136
+
137
+ let db = client.db("ihub_db");
138
+ let coll = db.collection("ihub_col");
139
+
140
+ if (mcp ==true){
141
+
142
+ db = client.db("ihub_db");
143
+ coll = db.collection("mcp_registry");
144
+
145
+ }else if(prompt ==true){
146
+ db = client.db("ihub_db");
147
+ coll = db.collection("prompt_comp");
148
+ }
149
+
150
+
151
+ try {
152
+ const filter = {
153
+ "manifests": {
154
+ $elemMatch: { id: targetId, folder: targetFolder }
155
+ }
156
+ };
157
+
158
+ const update = {
159
+ $set: { "manifests.$[elem].is_latest": false }
160
+ };
161
+ const options = {
162
+ arrayFilters: [
163
+ {
164
+ "elem.id": targetId,
165
+ "elem.folder": targetFolder
166
+ }
167
+ ]
168
+ };
169
+
170
+ const result = await coll.updateMany(filter, update, options);
171
+
172
+ console.log(`Matched ${result.matchedCount} docs and updated ${result.modifiedCount} manifests.`);
173
+ return result;
174
+ } catch (error) {
175
+ console.error("Global update failed:", error);
176
+ }
177
+ }
178
+
179
+
180
+
181
+
182
+ //-------------------------------------------------------------------------
183
+
184
+
185
+
206
186
  async function manifestExists(targetManifestId, foldername,client) {
207
187
 
208
188
  const db = client.db("ihub_db");
@@ -269,6 +249,7 @@ async function getFileNew(folder,obj,pinata) {
269
249
  const result = await pinata.gateways.public.get(obj.cid)
270
250
  const Data = result.data;
271
251
 
252
+ //fs.mkdirSync(folder, { recursive: true });
272
253
  const filePath = path.join(folder, obj.name);
273
254
  fs.writeFileSync(`${filePath}`,Data,"utf-8");
274
255
  console.log("successfully")
@@ -321,15 +302,29 @@ function gitBundler(FOLDER_TO_UPLOAD){
321
302
  }
322
303
 
323
304
 
324
- async function Clone(folder,pinata,client){
305
+ async function Clone(folder,pinata,client,mcp,prompt){
325
306
 
326
307
 
327
- const db = client.db("ihub_db");
328
- const coll = db.collection("ihub_col");
308
+
309
+ let db = client.db("ihub_db");
310
+ let coll = db.collection("ihub_col");
311
+
312
+ if (mcp ==true){
313
+
314
+ db = client.db("ihub_db");
315
+ coll = db.collection("mcp_registry");
316
+
317
+ }
318
+ else if(prompt ==true){
319
+ db = client.db("ihub_db");
320
+ coll = db.collection("prompt_comp");
321
+ }
322
+
323
+
324
+
329
325
  const targetManifestId= fs.readFileSync(FILE_TO_STORE_LOGIN,'utf8');
330
326
  const doc = await coll.findOne({
331
327
  "owner": "system",
332
- "manifests.id": targetManifestId
333
328
  });
334
329
  if(doc) {
335
330
 
@@ -412,37 +407,57 @@ async function CloneNew(folder,pinata,client){
412
407
 
413
408
 
414
409
 
410
+ async function Push(FOLDER_TO_UPLOAD,pinata,client,mcp,prompt) {
415
411
 
412
+ try {
416
413
 
417
- async function Push(FOLDER_TO_UPLOAD,pinata,client) {
414
+ let db = client.db("ihub_db");
415
+ let coll = db.collection("ihub_col");
418
416
 
419
- try {
417
+ if (mcp ==true){
420
418
 
419
+ db = client.db("ihub_db");
420
+ coll = db.collection("mcp_registry");
421
+
422
+ }
423
+ else if(prompt ==true){
424
+ db = client.db("ihub_db");
425
+ coll = db.collection("prompt_comp");
426
+ }
421
427
 
422
- const db = client.db("ihub_db");
423
- const coll = db.collection("ihub_col");
428
+
424
429
 
425
- let uploads=[]
430
+ //let uploads=[]
426
431
  const absolutePath = path.resolve(FOLDER_TO_UPLOAD);
427
432
  let files=dirtoFileArray(absolutePath)
428
433
 
434
+ /*
429
435
  for (let file of files) {
430
436
 
431
437
  const upload=await pinata.upload.public.file(file)
432
438
  uploads.push(upload)
433
439
  }
440
+ */
441
+
442
+
434
443
 
444
+ const limit = pLimit(5); // 5 concurrent uploads
445
+ const uploadPromises = files.map(file =>
446
+ limit(() => pinata.upload.public.file(file))
447
+ );
435
448
 
449
+ const uploads = await Promise.all(uploadPromises);
436
450
  console.log(uploads)
437
451
  const data = fs.readFileSync(FILE_TO_STORE_LOGIN, 'utf8');
438
452
  const lastpath = path.basename(absolutePath);
439
-
453
+
440
454
  let meta={"id":data,"folder":lastpath,"uploads":uploads,"is_latest":true}
441
455
 
442
456
 
443
457
  let docalreadyexists=await manifestExists(data,lastpath,client)
444
458
  if (docalreadyexists){
445
- await deleteManifest(data,lastpath,client)
459
+ //await deleteManifest(data,lastpath,client,mcp,prompt)
460
+ await deactivateOlderVersions(data,lastpath,mcp,prompt)
446
461
  }
447
462
 
448
463
  await coll.findOneAndUpdate(
@@ -464,6 +479,116 @@ async function Push(FOLDER_TO_UPLOAD,pinata,client) {
464
479
 
465
480
 
466
481
 
482
+ async function UpdateRepo(cid,pinata) {
483
+
484
+
485
+ const result = await pinata.gateways.public.get(cid)
486
+ const Data = result.data;
487
+ console.log(Data)
488
+ const arrayBuffer = await Data.arrayBuffer()
489
+ const buffer = Buffer.from(arrayBuffer);
490
+ console.log(buffer)
491
+ deleteFilesWithExtension(".history.bundle",".")
492
+ let dynamicstring=crypto.randomUUID()
493
+ let shortID = dynamicstring.substring(0, 5)
494
+ let bpath=`${shortID}.history.bundle`
495
+
496
+ fs.writeFileSync(`${bpath}`,buffer);
497
+
498
+ //const absolutePath = path.resolve(folder);
499
+ const absoluteBundlePath = path.resolve(bpath);
500
+
501
+ execSync(`git fetch ${absoluteBundlePath} refs/heads/*:refs/remotes/bundle/*`, {
502
+ cwd: `.`,
503
+ stdio: 'inherit'
504
+ });
505
+
506
+ const currentBranch = execSync(
507
+ `git branch --show-current`,
508
+ {
509
+ cwd: '.',
510
+ encoding: 'utf8',
511
+ shell: true
512
+ }
513
+ ).trim();
514
+
515
+ const bundleBranch = `bundle/${currentBranch}`;
516
+
517
+ console.log(`šŸ”€ Merging ${bundleBranch} → ${currentBranch}`);
518
+ execSync(
519
+ `git merge ${bundleBranch} --allow-unrelated-histories --no-edit`,
520
+ {
521
+ cwd: '.',
522
+ stdio: 'inherit',
523
+ shell: true
524
+ }
525
+ );
526
+
527
+ console.log("pulled successfully")
528
+
529
+ }
530
+
531
+
532
+
533
+ async function Pull(folder,pinata,client,mcp,prompt){
534
+
535
+
536
+ let db = client.db("ihub_db");
537
+ let coll = db.collection("ihub_col");
538
+
539
+ if (mcp == true){
540
+
541
+ db = client.db("ihub_db");
542
+ coll = db.collection("mcp_registry");
543
+
544
+ }
545
+ else if(prompt == true){
546
+ db = client.db("ihub_db");
547
+ coll = db.collection("prompt_comp");
548
+ }
549
+
550
+
551
+ const targetManifestId= fs.readFileSync(FILE_TO_STORE_LOGIN,'utf8');
552
+ const doc = await coll.findOne({
553
+ "owner": "system",
554
+ });
555
+ if(doc) {
556
+
557
+ let uploads=null
558
+ let bundle=null
559
+ let manifests=doc.manifests
560
+
561
+
562
+ for(let obj of manifests){
563
+
564
+ let dbfolder=String(obj.folder)
565
+
566
+ if(obj.id==targetManifestId && dbfolder==folder){
567
+ console.log(obj.folder)
568
+ uploads=obj.uploads
569
+ }
570
+
571
+
572
+
573
+ }
574
+ console.log(uploads)
575
+
576
+ for(let obj of uploads){
577
+
578
+ let name=obj.name
579
+ const isIncluded = name.includes(".history.bundle");
580
+ if(isIncluded) {
581
+
582
+ bundle=obj.cid
583
+
584
+ }}
585
+
586
+ await UpdateRepo(bundle,pinata)
587
+
588
+ }}
589
+
590
+
591
+
467
592
 
468
593
  yargs(hideBin(process.argv))
469
594
  .command(
@@ -492,7 +617,23 @@ yargs(hideBin(process.argv))
492
617
  return yargs.positional('folderpath', {
493
618
  describe: 'The local folder to push',
494
619
  type: 'string'
620
+ })
621
+ .option('mcp', {
622
+
623
+ alias: 'm',
624
+ type: 'boolean',
625
+ description: 'mcp server repo or not',
626
+ default: false
627
+ })
628
+ .option('prompt', {
629
+
630
+ alias: 'p',
631
+ type: 'boolean',
632
+ description: 'mcp server repo or not',
633
+ default: false
495
634
  });
635
+
636
+
496
637
  },
497
638
  async (argv) => {
498
639
 
@@ -501,7 +642,7 @@ yargs(hideBin(process.argv))
501
642
 
502
643
  console.log(`\nā¬†ļø Starting PUSH operation on folder: ${argv.folderpath}`);
503
644
  gitBundler(argv.folderpath);
504
- await Push(argv.folderpath,pinata,client);
645
+ await Push(argv.folderpath,pinata,client,argv.mcp,argv.prompt);
505
646
  console.log('Push operation finished.');
506
647
 
507
648
  }catch(e){
@@ -520,6 +661,20 @@ yargs(hideBin(process.argv))
520
661
  return yargs.positional('foldername', {
521
662
  describe: 'the repo/folder to pull',
522
663
  type: 'string'
664
+ })
665
+ .option('mcp', {
666
+
667
+ alias: 'm',
668
+ type: 'boolean',
669
+ description: 'mcp server repo or not',
670
+ default: false
671
+ })
672
+ .option('prompt', {
673
+
674
+ alias: 'p',
675
+ type: 'boolean',
676
+ description: 'mcp server repo or not',
677
+ default: false
523
678
  });
524
679
  },
525
680
  async (argv) => {
@@ -549,7 +704,22 @@ yargs(hideBin(process.argv))
549
704
  describe: 'The folder to clone',
550
705
  type: 'string'
551
706
  })
707
+ .option('mcp', {
708
+
709
+ alias: 'm',
710
+ type: 'boolean',
711
+ description: 'mcp server repo or not',
712
+ default: false
713
+ })
714
+ .option('prompt', {
715
+
716
+ alias: 'p',
717
+ type: 'boolean',
718
+ description: 'mcp server repo or not',
719
+ default: false
720
+ })
552
721
  .option('new', {
722
+
553
723
  alias: 'n',
554
724
  type: 'boolean',
555
725
  description: 'new repo or existing repo',
@@ -566,7 +736,8 @@ yargs(hideBin(process.argv))
566
736
 
567
737
  }
568
738
  else {
569
- await Clone(argv.foldername,pinata,client);
739
+
740
+ await Clone(argv.foldername,pinata,client,argv.mcp,argv.prompt);
570
741
  console.log('Clone operation finished.');
571
742
 
572
743
  }
@@ -590,7 +761,7 @@ yargs(hideBin(process.argv))
590
761
  .example('ihub op push ./repo', 'Push a local repository')
591
762
  .example('ihub op clone <reponame> --new true', 'Clone a new repository | name will be the reponame in UI')
592
763
  .example('ihub op clone <reponame>', 'Clone an existing repository | name will be the reponame in UI')
593
- .example('ihub op clone <reponame>', 'Pull updates in an existing repository | name will be the reponame in UI')
764
+ .example('ihub op pull <reponame>', 'Pull updates in an existing repository | name will be the reponame in UI')
594
765
  .epilog('ImmutableHub CLI • Built with ā¤ļø')
595
766
  .help()
596
767
  .argv;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ihub-cli",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "immutablehub cli to push and clone repos",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -13,8 +13,10 @@
13
13
  "author": "immutablehub",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
+ "@huggingface/hub": "^2.7.1",
16
17
  "jest": "^30.2.0",
17
18
  "mongodb": "^7.0.0",
19
+ "p-limit": "^7.2.0",
18
20
  "pinata": "^2.5.1",
19
21
  "yargs": "^18.0.0"
20
22
  }