main.ts
 1  
 2  
 3  import { $ } from "zx";
 4  
 5  //  echo 'package.json' | ipfs add 
 6  
 7  async function ipfs_add(content: string) {
 8    const out = await $`echo ${content} | ipfs add`;
 9    const cid = out.stdout.split(" ")[1]
10  
11    return cid
12  }
13  
14  async function ipfs_cat(cid: string) {
15      if (!cid) {
16          throw new Error("CID is required for ipfs cat");    
17      }
18  
19      let out = null;
20      try 
21      {   
22          out = await $`ipfs cat ${cid}`;
23  
24      }
25       catch
26      (error) {
27          console.error("Error during ipfs cat:", error);
28          throw error;
29      }
30      
31      if (out.exitCode !== 0) {
32          throw new Error(`ipfs cat failed with exit code ${out.exitCode}`);
33      }
34  
35      return out.stdout.trim();
36  }
37  
38  async function main() {
39      const now = new Date();
40  
41      const dateString = now.toISOString()
42  
43  
44      const out = await ipfs_add(dateString)
45  
46      const cid = out.stdout.split(' ')[1];
47  
48      console.log({cid, dateString});
49      const cat_out = await ipfs_cat(cid)
50  
51  
52      console.log({cid, cat_out, dateString})
53  
54      if (cat_out === dateString) {
55          console.log("IPFS add and cat are working correctly");
56      }
57  
58  
59      console.log({out});
60  
61      console.log("Package.json content displayed");
62  }
63  
64  // (async () => {
65  //     await main();
66  // })();
67  
68  export { ipfs_cat, ipfs_add }
69