|
1 | | -import { exec } from "child_process"; |
| 1 | +import { create } from "kubo-rpc-client"; |
| 2 | +import { globSource } from "kubo-rpc-client"; |
2 | 3 | import { dirname, join } from "path"; |
3 | 4 | import { fileURLToPath } from "url"; |
4 | | -import { promisify } from "util"; |
5 | 5 |
|
6 | | -const execAsync = promisify(exec); |
7 | 6 | const __filename = fileURLToPath(import.meta.url); |
8 | 7 | const __dirname = dirname(__filename); |
9 | 8 |
|
10 | | -async function checkIpfsDaemon() { |
| 9 | +const ipfsConfig = { |
| 10 | + host: "ipfs.nifty.ink", |
| 11 | + port: 3001, |
| 12 | + protocol: "https", |
| 13 | + timeout: 250000, |
| 14 | +}; |
| 15 | + |
| 16 | +async function addDirectoryToIpfs(path) { |
| 17 | + console.log("📦 Adding directory to IPFS via Nifty Ink..."); |
| 18 | + |
11 | 19 | try { |
12 | | - await execAsync("ipfs --version"); |
13 | | - // Check if daemon is running and has peers |
14 | | - const { stdout } = await execAsync("ipfs swarm peers"); |
15 | | - const peerCount = stdout.split("\n").filter(Boolean).length; |
| 20 | + const ipfs = create(ipfsConfig); |
| 21 | + |
| 22 | + // Track the root directory CID |
| 23 | + let rootCid = null; |
16 | 24 |
|
17 | | - if (peerCount < 1) { |
18 | | - console.log("⚠️ Warning: Your IPFS node has no peers. Content might not be accessible immediately."); |
19 | | - console.log("Waiting for peers to connect..."); |
20 | | - // Wait for peers to connect |
21 | | - await new Promise(resolve => setTimeout(resolve, 10000)); |
22 | | - const { stdout: newStdout } = await execAsync("ipfs swarm peers"); |
23 | | - const newPeerCount = newStdout.split("\n").filter(Boolean).length; |
24 | | - if (newPeerCount < 1) { |
25 | | - console.log("Still no peers connected. You might want to:"); |
26 | | - console.log("1. Check your internet connection"); |
27 | | - console.log("2. Ensure your IPFS daemon is not behind a firewall"); |
28 | | - console.log("3. Try running 'ipfs daemon --enable-pubsub-experiment' for better connectivity"); |
| 25 | + // Add the entire directory to IPFS |
| 26 | + for await (const result of ipfs.addAll(globSource(path, "**/*"), { |
| 27 | + pin: true, |
| 28 | + wrapWithDirectory: true, // This is key - it wraps all files in a directory |
| 29 | + })) { |
| 30 | + if (result.path === "") { |
| 31 | + // This is the root directory entry |
| 32 | + rootCid = result.cid; |
29 | 33 | } else { |
30 | | - console.log(`✓ Connected to ${newPeerCount} peers`); |
| 34 | + console.log(`Added ${result.path} - CID: ${result.cid}`); |
31 | 35 | } |
32 | | - } else { |
33 | | - console.log(`✓ Connected to ${peerCount} peers`); |
34 | 36 | } |
35 | | - } catch (error) { |
36 | | - console.log(error); |
37 | | - console.error("❌ IPFS is not installed or daemon is not running."); |
38 | | - console.log("Please install IPFS and start the daemon:"); |
39 | | - console.log("1. Install IPFS: https://docs.ipfs.tech/install/"); |
40 | | - console.log("2. Start the daemon: ipfs daemon"); |
41 | | - process.exit(1); |
42 | | - } |
43 | | -} |
44 | 37 |
|
45 | | -async function addDirectoryToIpfs(path) { |
46 | | - console.log("📦 Adding directory to IPFS..."); |
47 | | - |
48 | | - try { |
49 | | - // Add the entire directory to IPFS and get the hash |
50 | | - const { stdout } = await execAsync(`ipfs add -r -Q "${path}"`); |
51 | | - const cid = stdout.trim(); |
52 | | - |
53 | | - // Announce the content to the network |
54 | | - try { |
55 | | - await execAsync(`ipfs dht provide ${cid}`); |
56 | | - console.log("✓ Announced content to the IPFS network"); |
57 | | - } catch (error) { |
58 | | - console.log(error); |
59 | | - console.log("⚠️ Warning: Could not announce content to the network. Content might take longer to be available."); |
| 38 | + if (!rootCid) { |
| 39 | + throw new Error("Failed to get root directory CID"); |
60 | 40 | } |
61 | 41 |
|
62 | | - return cid; |
| 42 | + return rootCid.toString(); |
63 | 43 | } catch (error) { |
64 | 44 | console.error("Error adding directory to IPFS:", error); |
65 | 45 | throw error; |
66 | 46 | } |
67 | 47 | } |
68 | 48 |
|
69 | 49 | async function main() { |
70 | | - // First check if IPFS is installed and running |
71 | | - await checkIpfsDaemon(); |
72 | | - |
73 | 50 | // Get the path to the out directory |
74 | 51 | const outDir = join(__dirname, "..", "out"); |
75 | 52 |
|
76 | | - console.log("🚀 Uploading to IPFS..."); |
| 53 | + console.log("🚀 Uploading to Nifty Ink IPFS..."); |
77 | 54 | const cid = await addDirectoryToIpfs(outDir); |
78 | 55 |
|
79 | | - // Give the network some time to propagate the content |
80 | | - console.log("\n⏳ Waiting for network propagation..."); |
81 | | - await new Promise(resolve => setTimeout(resolve, 5000)); |
82 | | - |
83 | 56 | console.log("\n✨ Upload complete! Your site is now available at:"); |
84 | | - console.log(`🔗 IPFS Gateway: https://ipfs.io/ipfs/${cid}`); |
85 | | - console.log("\n💡 To ensure your site stays available:"); |
86 | | - console.log("1. Keep your IPFS daemon running"); |
87 | | - console.log("2. Pin the CID on other nodes: ipfs pin add " + cid); |
88 | | - console.log("\n💡 If the gateway times out, you can:"); |
89 | | - console.log("1. Wait a few minutes and try again"); |
90 | | - console.log("2. Install the IPFS Companion browser extension"); |
| 57 | + console.log(`🔗 Nifty Ink Gateway: https://gateway.nifty.ink:42069/ipfs/${cid}`); |
| 58 | + console.log("\n💡 Note: Your content is being served through the Nifty Ink IPFS gateway"); |
91 | 59 | } |
92 | 60 |
|
93 | 61 | main().catch(err => { |
|
0 commit comments