From 91b133b54f76769e335ee3713643d4fcfe57bf17 Mon Sep 17 00:00:00 2001 From: SANTHOSH17-DOT Date: Sat, 14 Oct 2023 12:59:10 +0530 Subject: [PATCH] Add: try catch block Handle no path case in the add command --- .../workflows/npm-publish-github-packages.yml | 35 ------- package-lock.json | 4 +- package.json | 2 +- src/index.ts | 2 +- src/vc/commit.ts | 44 +++++---- src/vc/history.ts | 29 +++--- src/vc/init.ts | 43 ++++---- src/vc/stage.ts | 99 ++++++++++--------- 8 files changed, 124 insertions(+), 134 deletions(-) delete mode 100644 .github/workflows/npm-publish-github-packages.yml diff --git a/.github/workflows/npm-publish-github-packages.yml b/.github/workflows/npm-publish-github-packages.yml deleted file mode 100644 index c2d775b..0000000 --- a/.github/workflows/npm-publish-github-packages.yml +++ /dev/null @@ -1,35 +0,0 @@ -# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created -# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages - -name: Node.js Package - -on: - release: - types: [published] - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - - run: npm ci - - publish-gpr: - needs: build - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - registry-url: https://npm.pkg.github.com/ - - run: npm ci - - run: npm publish --registry=https://npm.pkg.github.com/ - env: - NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}} diff --git a/package-lock.json b/package-lock.json index 541ce23..6f1268b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "statikvc", - "version": "1.0.3-alpha", + "version": "1.0.4-alpha", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "statikvc", - "version": "1.0.3-alpha", + "version": "1.0.4-alpha", "license": "ISC", "dependencies": { "commander": "^11.0.0", diff --git a/package.json b/package.json index b9c6cd7..bb04411 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index fbb0769..20f7b5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ").description("Initialize a new Statik repository") program.command("add [file_path]").description("Add a file to the Statik repository") diff --git a/src/vc/commit.ts b/src/vc/commit.ts index 461b3a7..5131e92 100644 --- a/src/vc/commit.ts +++ b/src/vc/commit.ts @@ -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) + } \ No newline at end of file diff --git a/src/vc/history.ts b/src/vc/history.ts index db3817b..23ed08d 100644 --- a/src/vc/history.ts +++ b/src/vc/history.ts @@ -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) } \ No newline at end of file diff --git a/src/vc/init.ts b/src/vc/init.ts index 36bc13c..417a721 100644 --- a/src/vc/init.ts +++ b/src/vc/init.ts @@ -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) } \ No newline at end of file diff --git a/src/vc/stage.ts b/src/vc/stage.ts index e8fc321..2be3e7d 100644 --- a/src/vc/stage.ts +++ b/src/vc/stage.ts @@ -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) } \ No newline at end of file