diff --git a/src/vc/commit.ts b/src/vc/commit.ts index dc94e49..1cd8c95 100644 --- a/src/vc/commit.ts +++ b/src/vc/commit.ts @@ -2,33 +2,50 @@ import { create, globSource } from "ipfs-http-client"; import { IsStatik } from "../utils/checkStatik.js"; import fs from 'fs' import { FetchConfig } from "../utils/fetchConfig.js"; -export async function Commit(cwd: string,message: string){ - try{ - IsStatik(cwd) - const snapshot = fs.readFileSync(cwd+"/.statik/SNAPSHOT").toString() - if(!snapshot.length){ - console.error("No changes to commit") - process.exit(1) + +export async function Commit(cwd: string, message: string) { + try { + IsStatik(cwd); + + // Read the staged changes CID from SNAPSHOT + const snapshot = fs.readFileSync(cwd + "/.statik/SNAPSHOT").toString(); + + if (!snapshot.length) { + console.error("No changes to commit"); + process.exit(1); } - const client = create({url: FetchConfig(cwd).ipfs_node_url}) - const branch = fs.readFileSync(cwd+"/.statik/HEAD").toString() - const prevCommit = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() + + // Create an IPFS client + const client = create({ url: FetchConfig(cwd).ipfs_node_url }); + + // Read the current branch + const branch = fs.readFileSync(cwd + "/.statik/HEAD").toString(); + + // Read the previous commit CID from the current branch + const prevCommit = fs.readFileSync(cwd + "/.statik/heads/" + branch).toString(); + + // Create the commit object const commit = { prevCommit: prevCommit, snapshot: snapshot, message: message, timestamp: Date.now() - } - const result = await client.add(JSON.stringify(commit)) - fs.writeFileSync(cwd+"/.statik/heads/"+branch,result.path) - fs.writeFileSync(cwd+"/.statik/SNAPSHOT","") - console.log( - "Committed to IPFS with hash: "+result.path - ) - process.exit(0) - }catch(e){ - console.error(e) - process.exit(1) + }; + + // Add the commit object to IPFS + const result = await client.add(JSON.stringify(commit)); + + // Update the HEAD of the current branch to the new commit CID + fs.writeFileSync(cwd + "/.statik/heads/" + branch, result.path); + + // Clear the SNAPSHOT file + fs.writeFileSync(cwd + "/.statik/SNAPSHOT", ""); + + console.log("Committed to IPFS with hash: " + result.path); + process.exit(0); + } catch (e) { + console.error(e); + process.exit(1); } - -} \ No newline at end of file +} + diff --git a/src/vc/stage.ts b/src/vc/stage.ts index 90f5822..0e09cd6 100644 --- a/src/vc/stage.ts +++ b/src/vc/stage.ts @@ -1,77 +1,48 @@ import { create, globSource } from "ipfs-http-client"; import { IsStatik } from "../utils/checkStatik.js"; -import fs from 'fs' +import fs from 'fs'; import { FetchConfig } from "../utils/fetchConfig.js"; -import Path from 'path' -export async function Add(cwd:string,paths:string[]){ - try{ - IsStatik(cwd) - if(!paths.length){ - console.log("No file path specified!") - console.log("Hint: statik help") - return +import Path from 'path'; + +export async function Add(cwd: string, paths: string[]) { + try { + IsStatik(cwd); + + if (!paths.length) { + console.log("No file path specified!"); + console.log("Hint: statik help"); + return; } - const client = create({url: FetchConfig(cwd).ipfs_node_url}) - const branch = fs.readFileSync(cwd+"/.statik/HEAD").toString() - const prevCommit = fs.readFileSync(cwd+"/.statik/heads/"+branch).toString() - if(!prevCommit.length){ - let snapshot=[]; - for (const path of paths){ - for await (const result of client.addAll(globSource(path,{recursive:true}))) { - if(fs.statSync(cwd+"/"+path).isDirectory()) continue; - snapshot.push(result) - } + + // Assume the IPFS client is created only once for efficiency + const client = create({ url: FetchConfig(cwd).ipfs_node_url }); + + // Create an array to store the files to be committed + let stagedFiles = []; + + for (const path of paths) { + for await (const result of client.addAll(globSource(path, { recursive: true }))) { + if (fs.statSync(cwd + "/" + path).isDirectory()) continue; + stagedFiles.push(result); } - // console.log(snapshot) - const result = await client.add(JSON.stringify(snapshot)) - fs.writeFileSync(cwd+"/.statik/SNAPSHOT",result.path) - console.log( - "Files staged to IPFS with cid: "+result.path - ) - }else{ - let asyncitr = client.cat(prevCommit) - let prevSnapshot = ""; - for await(const itr of asyncitr){ - const data = Buffer.from(itr).toString() - prevSnapshot = JSON.parse(data).snapshot - } - let prevContent = []; - asyncitr = client.cat(prevSnapshot) - for await(const itr of asyncitr){ - const data = Buffer.from(itr).toString() - prevContent = JSON.parse(data) - } - // Not optimized - for (const path of paths){ - for await (const result of client.addAll(globSource(path,{recursive:true}))) { - // Check if the path is a directory - const path = result.path - if(fs.statSync(cwd+"/"+path).isDirectory()) continue; - let flag = true - for(const prev of prevContent){ - if(prev.path==result.path){ - prevContent.splice(prevContent.indexOf(prev),1,result) - flag = false - break; - } - } - if(flag) prevContent.push(result) - } - } - const result = await client.add(JSON.stringify(prevContent)) - // console.log(result.path,prevSnapshot) - if(result.path==prevSnapshot){ - console.log("There are no changes to add") - return - } - fs.writeFileSync(cwd+"/.statik/SNAPSHOT",result.path) - console.log( - "Files staged to IPFS with cid: "+result.path - ) } - process.exit(0) - }catch(e){ - console.error(e) - process.exit(1) + + // Check if there are any files to stage + if (stagedFiles.length === 0) { + console.log("There are no changes to add"); + return; + } + + // Save the staged files in a temporary location (e.g., .statik/staging/) + const stagingPath = cwd + "/.statik/staging/"; + const stagingCID = await client.add(stagedFiles, { pin: false }); + + // Create or update the SNAPSHOT file with the CID of the staged files + fs.writeFileSync(cwd + "/.statik/SNAPSHOT", stagingCID.path); + + console.log("Files staged for commit. CID: " + stagingCID.path); + } catch (e) { + console.error(e); + process.exit(1); } -} \ No newline at end of file +}