Skip to content

Commit

Permalink
Add: try catch block
Browse files Browse the repository at this point in the history
Handle no path case in the add command
  • Loading branch information
SANTHOSH17-DOT committed Oct 14, 2023
1 parent 9ca42ab commit 91b133b
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 134 deletions.
35 changes: 0 additions & 35 deletions .github/workflows/npm-publish-github-packages.yml

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "statikvc",
"version": "1.0.3-alpha",
"version": "1.0.4-alpha",
"description": "An IPFS based version control system with static file hosting features. Basic functions like repo initiation, staging, committing, and logging have been implemented. (Under active development)",
"exports": "./dist/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ console.log(figlet.textSync("Statik"));
const program = new Command();
program
.name("statik")
.version("1.0.0")
.version("1.0.4-alpha")
.description("An IPFS based version control system with static file hosting features")
program.command("init <ipfs_node_url>").description("Initialize a new Statik repository")
program.command("add [file_path]").description("Add a file to the Statik repository")
Expand Down
44 changes: 25 additions & 19 deletions src/vc/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import { IsStatik } from "../utils/checkStatik.js";
import fs from 'fs'
import { FetchConfig } from "../utils/fetchConfig.js";
export async function Commit(cwd: string,message: string){
IsStatik(cwd)
const snapshot = fs.readFileSync(cwd+"/.statik/SNAPSHOT").toString()
if(!snapshot.length){
console.error("No changes to commit")
try{
IsStatik(cwd)
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 prevCommit = fs.readFileSync(cwd+"/.statik/HEAD").toString()
const commit = {
prevCommit: prevCommit,
snapshot: snapshot,
message: message,
timestamp: Date.now()
}
const result = await client.add(JSON.stringify(commit))
fs.writeFileSync(cwd+"/.statik/HEAD",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)
}
const client = create({url: FetchConfig(cwd).ipfs_node_url})
const prevCommit = fs.readFileSync(cwd+"/.statik/HEAD").toString()
const commit = {
prevCommit: prevCommit,
snapshot: snapshot,
message: message,
timestamp: Date.now()
}
const result = await client.add(JSON.stringify(commit))
fs.writeFileSync(cwd+"/.statik/HEAD",result.path)
fs.writeFileSync(cwd+"/.statik/SNAPSHOT","")
console.log(
"Committed to IPFS with hash: "+result.path
)
process.exit(0)

}
29 changes: 17 additions & 12 deletions src/vc/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import fs from 'fs'
import { FetchConfig } from "../utils/fetchConfig.js";

export async function Log(cwd: string){
IsStatik(cwd)
const client = create({url: FetchConfig(cwd).ipfs_node_url})
let head = fs.readFileSync(cwd+"/.statik/HEAD").toString()
let asyncitr = client.cat(head)
console.log("Commit history: ")
while(head.length){
for await(const itr of asyncitr){
const data = Buffer.from(itr).toString()
console.log("<"+head+">"+" "+JSON.parse(data).message)
head = JSON.parse(data).prevCommit
asyncitr = client.cat(head)
try{
IsStatik(cwd)
const client = create({url: FetchConfig(cwd).ipfs_node_url})
let head = fs.readFileSync(cwd+"/.statik/HEAD").toString()
let asyncitr = client.cat(head)
console.log("Commit history: ")
while(head.length){
for await(const itr of asyncitr){
const data = Buffer.from(itr).toString()
console.log("<"+head+">"+" "+JSON.parse(data).message)
head = JSON.parse(data).prevCommit
asyncitr = client.cat(head)
}
}
process.exit(0)
}catch(e){
console.error(e)
process.exit(1)
}
process.exit(0)
}
43 changes: 24 additions & 19 deletions src/vc/init.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import fs from "fs";
export async function Init(cwd: string,ipfs_node_url:string){
let reinitialize = false;
if(fs.existsSync(cwd+"/.statik")){
reinitialize = true;
fs.rmSync(cwd+"/.statik", {recursive: true})
try{
let reinitialize = false;
if(fs.existsSync(cwd+"/.statik")){
reinitialize = true;
fs.rmSync(cwd+"/.statik", {recursive: true})
}
fs.mkdirSync(cwd+"/.statik")
fs.mkdirSync(cwd+"/.statik/refs/heads",{recursive: true})

fs.writeFileSync(cwd+"/.statik/HEAD", "")
fs.writeFileSync(cwd+"/.statik/SNAPSHOT", "")
fs.writeFileSync(cwd+"/.statik/CONFIG", JSON.stringify({
ipfs_node_url: ipfs_node_url
}))

if(reinitialize){
console.error("Reinitialized Statik repository in "+cwd+"/.statik")
}else{
console.log("Initialized Statik repository in "+cwd+"/.statik")
}
process.exit(0)
}catch(e){
console.error(e)
process.exit(1)
}
fs.mkdirSync(cwd+"/.statik")
fs.mkdirSync(cwd+"/.statik/refs/heads",{recursive: true})

fs.writeFileSync(cwd+"/.statik/HEAD", "")
fs.writeFileSync(cwd+"/.statik/SNAPSHOT", "")
fs.writeFileSync(cwd+"/.statik/CONFIG", JSON.stringify({
ipfs_node_url: ipfs_node_url
}))

if(reinitialize){
console.error("Reinitialized Statik repository in "+cwd+"/.statik")
}else{
console.log("Initialized Statik repository in "+cwd+"/.statik")
}
process.exit(0)
}
99 changes: 54 additions & 45 deletions src/vc/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,65 @@ import { create, globSource } from "ipfs-http-client";
import { IsStatik } from "../utils/checkStatik.js";
import fs from 'fs'
import { FetchConfig } from "../utils/fetchConfig.js";
// If order of elements changes, the hash changes
// Fix the logic (Sort maybe?)
// Accept file path '.'
export async function Add(cwd:string,paths:string[]){
IsStatik(cwd)
const client = create({url: FetchConfig(cwd).ipfs_node_url})
const prevCommit = fs.readFileSync(cwd+"/.statik/HEAD").toString()
if(!prevCommit.length){
let snapshot=[];
for (const path of paths){
for await (const result of client.addAll(globSource(path,{recursive:true}))) {
snapshot.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
try{
IsStatik(cwd)
if(!paths.length){
console.log("No file path specified!")
console.log("Hint: statik help")
return
}
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}))) {
for(const prev of prevContent){
if(prev.path==result.path){
prevContent.splice(prevContent.indexOf(prev),1)
break;
const client = create({url: FetchConfig(cwd).ipfs_node_url})
const prevCommit = fs.readFileSync(cwd+"/.statik/HEAD").toString()
if(!prevCommit.length){
let snapshot=[];
for (const path of paths){
for await (const result of client.addAll(globSource(path,{recursive:true}))) {
snapshot.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}))) {
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)
}
prevContent.push(result)
}
const result = await client.add(JSON.stringify(prevContent))
fs.writeFileSync(cwd+"/.statik/SNAPSHOT",result.path)
console.log(
"Files staged to IPFS with cid: "+result.path
)
}
const result = await client.add(JSON.stringify(prevContent))
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)
}
process.exit(0)
}

0 comments on commit 91b133b

Please sign in to comment.