-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate staging from immediate IPFS integration #9
Open
AritraDey-Dev
wants to merge
1
commit into
SpiderNitt:main
Choose a base branch
from
AritraDey-Dev:feature/staging-changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still Staging is done by adding files to IPFS
Previous commit or existing snapshot is not checked before creating the new snapshot, same file with no changes may get committed twice
Suggestion: Now the commit message can be written as a blank space by not writing anything within the quotes so we can also add a check point to check whether a proper commit message is written.