codehub-ghx-cli 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/codehub-ghx-1.0.2.tgz +0 -0
- package/commands/add.js +19 -0
- package/commands/commit.js +30 -0
- package/commands/init.js +22 -0
- package/commands/pull.js +42 -0
- package/commands/push.js +34 -0
- package/commands/revert.js +30 -0
- package/index.js +99 -0
- package/package.json +23 -0
|
Binary file
|
package/commands/add.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
async function addRepo(filepath) {
|
|
6
|
+
const repoPath = path.resolve(process.cwd(),".codehub");
|
|
7
|
+
const stagingPath = path.join(repoPath, "staging");
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
await fs.mkdir(stagingPath, {recursive: true});
|
|
11
|
+
const fileName = path.basename(filepath);
|
|
12
|
+
await fs.copyFile(filepath, path.join(stagingPath, fileName));
|
|
13
|
+
console.log(`File ${fileName} added to staging area`);
|
|
14
|
+
} catch (err) {
|
|
15
|
+
console.log("error adding file:", err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = {addRepo};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const {v4: uuidv4} = require("uuid");
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
async function commitRepo(message) {
|
|
7
|
+
const repopath = path.resolve(process.cwd(), ".codehub");
|
|
8
|
+
const stagedPath = path.join(repopath, "staging");
|
|
9
|
+
const commitPath = path.join(repopath, "commits");
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const commitID = uuidv4();
|
|
13
|
+
const commitDir = path.join(commitPath, commitID);
|
|
14
|
+
await fs.mkdir(commitDir, {recursive: true});
|
|
15
|
+
|
|
16
|
+
const files = await fs.readdir(stagedPath);
|
|
17
|
+
for(const file of files){
|
|
18
|
+
await fs.copyFile(
|
|
19
|
+
path.join(stagedPath, file),
|
|
20
|
+
path.join(commitDir, file)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
await fs.writeFile(path.join(commitDir, "commit.json"),JSON.stringify({message, date:new Date().toISOString()}));
|
|
24
|
+
console.log(`Commit ${commitID} created with message: ${message}`);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.error("Error committing files: ",err);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = {commitRepo};
|
package/commands/init.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
async function initRepo(){
|
|
6
|
+
const repoPath = path.resolve(process.cwd(), ".codehub");
|
|
7
|
+
const commitsPath = path.join(repoPath, "commits");
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
await fs.mkdir(repoPath, {recursive: true});
|
|
11
|
+
await fs.mkdir(commitsPath, {recursive: true});
|
|
12
|
+
await fs.writeFile(
|
|
13
|
+
path.join(repoPath, "config.json"),
|
|
14
|
+
JSON.stringify({ bucket: process.env.S3_BUCKET})
|
|
15
|
+
);
|
|
16
|
+
console.log("Repository initialized!");
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error("Error initialising repository", error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { initRepo};
|
package/commands/pull.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { s3, s3_BUCKET } = require("../config/aws-config");
|
|
4
|
+
|
|
5
|
+
async function pullRepo() {
|
|
6
|
+
const repopath = path.resolve(process.cwd(), ".codehub");
|
|
7
|
+
const commitsPath = path.join(repopath, "commits");
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const data = await s3.listObjectsV2({
|
|
11
|
+
Bucket: s3_BUCKET,
|
|
12
|
+
Prefix: "commits/",
|
|
13
|
+
}).promise();
|
|
14
|
+
|
|
15
|
+
for (const obj of data.Contents) {
|
|
16
|
+
const parts = obj.Key.split("/");
|
|
17
|
+
if (parts.length < 3) continue;
|
|
18
|
+
|
|
19
|
+
const commitId = parts[1];
|
|
20
|
+
const filename = parts[2];
|
|
21
|
+
|
|
22
|
+
const commitDirPath = path.join(commitsPath, commitId);
|
|
23
|
+
await fs.mkdir(commitDirPath, { recursive: true });
|
|
24
|
+
|
|
25
|
+
const fileData = await s3.getObject({
|
|
26
|
+
Bucket: s3_BUCKET,
|
|
27
|
+
Key: obj.Key,
|
|
28
|
+
}).promise();
|
|
29
|
+
|
|
30
|
+
await fs.writeFile(
|
|
31
|
+
path.join(commitDirPath, filename),
|
|
32
|
+
fileData.Body
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
console.log("All commits pulled from S3!");
|
|
37
|
+
} catch (err) {
|
|
38
|
+
console.error("Unable to pull:", err);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { pullRepo };
|
package/commands/push.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const path = require('path');
|
|
3
|
+
// const {s3, s3_BUCKET} = require('../config/aws-config');
|
|
4
|
+
const {s3, s3_BUCKET} = require('../config/aws-config');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
async function pushRepo() {
|
|
8
|
+
const repopath = path.resolve(process.cwd(), ".codehub");
|
|
9
|
+
const commitsPath = path.join(repopath, "commits");
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const commitDirs = await fs.readdir(commitsPath);
|
|
13
|
+
for(const commitDir of commitDirs){
|
|
14
|
+
const commitPath = path.join(commitsPath, commitDir);
|
|
15
|
+
const files = await fs.readdir(commitPath);
|
|
16
|
+
|
|
17
|
+
for(const file of files){
|
|
18
|
+
const filePath = path.join(commitPath, file);
|
|
19
|
+
const fileContent = await fs.readFile(filePath);
|
|
20
|
+
const params = {
|
|
21
|
+
Bucket: s3_BUCKET,
|
|
22
|
+
Key: `commits/${commitDir}/${file}`,
|
|
23
|
+
Body: fileContent,
|
|
24
|
+
};
|
|
25
|
+
await s3.upload(params).promise();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
console.log("All commits pushed to S3.")
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error("Error pushing to s3:", error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = {pushRepo};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { promisify } = require("util");
|
|
4
|
+
// const {s3, s3_BUCKET} = require('../config/aws-config');
|
|
5
|
+
const {s3, s3_BUCKET} = require('../config/aws-config');
|
|
6
|
+
|
|
7
|
+
const readdir = promisify(fs.readdir);
|
|
8
|
+
const copyFile = promisify(fs.copyFile);
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async function revertRepo(commitID) {
|
|
12
|
+
const repopath = path.resolve(process.cwd(), ".codehub");
|
|
13
|
+
const commitsPath = path.join(repopath, "commits");
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const commitDir = path.join(commitsPath, commitID);
|
|
18
|
+
const files = await readdir(commitDir);
|
|
19
|
+
const parentDir = path.resolve(repopath, "..");
|
|
20
|
+
|
|
21
|
+
for(const file of files){
|
|
22
|
+
await copyFile(path.join(commitDir, file), path.join(parentDir, file));
|
|
23
|
+
}
|
|
24
|
+
console.log(`Commit ${commitID} reverted successfully!`);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.error("Unable to revert:", err);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = {revertRepo};
|
package/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// require("dotenv").config();
|
|
4
|
+
const path = require("path");
|
|
5
|
+
require("dotenv").config({
|
|
6
|
+
path: path.join(__dirname, ".env")
|
|
7
|
+
});
|
|
8
|
+
const yargs = require("yargs");
|
|
9
|
+
const { hideBin } = require("yargs/helpers");
|
|
10
|
+
|
|
11
|
+
// Import your command functions
|
|
12
|
+
const { initRepo } = require("./commands/init");
|
|
13
|
+
const { addRepo } = require("./commands/add");
|
|
14
|
+
const { commitRepo } = require("./commands/commit");
|
|
15
|
+
const { pushRepo } = require("./commands/push");
|
|
16
|
+
const { pullRepo } = require("./commands/pull");
|
|
17
|
+
const { revertRepo } = require("./commands/revert");
|
|
18
|
+
|
|
19
|
+
yargs(hideBin(process.argv))
|
|
20
|
+
|
|
21
|
+
// init
|
|
22
|
+
.command(
|
|
23
|
+
"init",
|
|
24
|
+
"Initialize a repository in the current folder",
|
|
25
|
+
() => {},
|
|
26
|
+
() => {
|
|
27
|
+
initRepo();
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
// add
|
|
32
|
+
.command(
|
|
33
|
+
"add <file>",
|
|
34
|
+
"Add a file to staging",
|
|
35
|
+
(y) => {
|
|
36
|
+
y.positional("file", {
|
|
37
|
+
describe: "File to stage",
|
|
38
|
+
type: "string",
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
(args) => {
|
|
42
|
+
addRepo(args.file);
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
// commit
|
|
47
|
+
.command(
|
|
48
|
+
"commit <message>",
|
|
49
|
+
"Commit staged files",
|
|
50
|
+
(y) => {
|
|
51
|
+
y.positional("message", {
|
|
52
|
+
describe: "Commit message",
|
|
53
|
+
type: "string",
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
(args) => {
|
|
57
|
+
commitRepo(args.message);
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
// push
|
|
62
|
+
.command(
|
|
63
|
+
"push",
|
|
64
|
+
"Push commits to the remote backend/S3",
|
|
65
|
+
() => {},
|
|
66
|
+
() => {
|
|
67
|
+
pushRepo();
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
// pull
|
|
72
|
+
.command(
|
|
73
|
+
"pull",
|
|
74
|
+
"Pull commits from the remote backend/S3",
|
|
75
|
+
() => {},
|
|
76
|
+
() => {
|
|
77
|
+
pullRepo();
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
// revert
|
|
82
|
+
.command(
|
|
83
|
+
"revert <commitID>",
|
|
84
|
+
"Revert to a specific commit",
|
|
85
|
+
(y) => {
|
|
86
|
+
y.positional("commitID", {
|
|
87
|
+
describe: "Commit ID to revert to",
|
|
88
|
+
type: "string",
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
(args) => {
|
|
92
|
+
revertRepo(args.commitID);
|
|
93
|
+
}
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
// default behavior
|
|
97
|
+
.demandCommand(1, "You must supply a valid command")
|
|
98
|
+
.help()
|
|
99
|
+
.parse();
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "codehub-ghx-cli",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ghx": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"aws-sdk": "^2.1692.0",
|
|
17
|
+
"axios": "^1.13.2",
|
|
18
|
+
"dotenv": "^17.2.3",
|
|
19
|
+
"path": "^0.12.7",
|
|
20
|
+
"uuid": "^13.0.0",
|
|
21
|
+
"yargs": "^18.0.0"
|
|
22
|
+
}
|
|
23
|
+
}
|