codechronicle-cli 1.0.0
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/commands/add.js +376 -0
- package/commands/clone.js +75 -0
- package/commands/commit.js +178 -0
- package/commands/init.js +106 -0
- package/commands/login.js +56 -0
- package/commands/logout.js +12 -0
- package/commands/pull.js +94 -0
- package/commands/push.js +70 -0
- package/commands/revert.js +121 -0
- package/commands/whoami.js +11 -0
- package/config/config.js +3 -0
- package/config/supabase.js +24 -0
- package/index.js +112 -0
- package/package.json +26 -0
- package/services/api.js +11 -0
- package/services/authService.js +18 -0
- package/services/repositoryApi.js +86 -0
- package/utils/auth.js +23 -0
- package/utils/chronConfig.js +30 -0
- package/utils/unzipDirectory.js +17 -0
- package/utils/zipDirectory.js +32 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const FormData = require("form-data");
|
|
3
|
+
const api = require("./api");
|
|
4
|
+
|
|
5
|
+
class RepositoryApi {
|
|
6
|
+
//this function is used to create new repo
|
|
7
|
+
async createRepository(token, repositoryData) {
|
|
8
|
+
const response = await api.post(`/api/repositories`, repositoryData, {
|
|
9
|
+
headers: {
|
|
10
|
+
Authorization: `Bearer ${token}`,
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return response.data;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//this function is used to push repo
|
|
18
|
+
async pushRepository(repositoryId, zipPath, token) {
|
|
19
|
+
try {
|
|
20
|
+
const form = new FormData();
|
|
21
|
+
|
|
22
|
+
// "snapshot" must match upload.single("snapshot")
|
|
23
|
+
form.append("snapshot", fs.createReadStream(zipPath));
|
|
24
|
+
|
|
25
|
+
const response = await api.post(
|
|
26
|
+
`/api/repositories/${repositoryId}/push`,
|
|
27
|
+
form,
|
|
28
|
+
{
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: `Bearer ${token}`,
|
|
31
|
+
...form.getHeaders(),
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return response.data;
|
|
37
|
+
} finally {
|
|
38
|
+
if (fs.existsSync(zipPath)) {
|
|
39
|
+
fs.unlinkSync(zipPath);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async pullRepository(repositoryId, token) {
|
|
45
|
+
const response = await api.get(`/api/repositories/${repositoryId}/pull`, {
|
|
46
|
+
headers: {
|
|
47
|
+
Authorization: `Bearer ${token}`,
|
|
48
|
+
},
|
|
49
|
+
responseType: "stream",
|
|
50
|
+
});
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async getLatestCommit(repoId, token) {
|
|
55
|
+
const response = await api.get(
|
|
56
|
+
`/api/repositories/${repoId}/latest-commit`,
|
|
57
|
+
{
|
|
58
|
+
headers: {
|
|
59
|
+
Authorization: `Bearer ${token}`,
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
return response;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async getRepoDetail(repoId, token) {
|
|
67
|
+
const response = await api.get(`/api/repositories/${repoId}`, {
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: `Bearer ${token}`,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
return response;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async cloneRepository(repoId, token) {
|
|
76
|
+
const response = await api.get(`/api/repositories/${repoId}/clone`, {
|
|
77
|
+
headers: {
|
|
78
|
+
Authorization: `Bearer ${token}`,
|
|
79
|
+
},
|
|
80
|
+
responseType: "stream",
|
|
81
|
+
});
|
|
82
|
+
return response;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = new RepositoryApi();
|
package/utils/auth.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const { getConfig } = require("./chronConfig");
|
|
2
|
+
|
|
3
|
+
function requireAuth() {
|
|
4
|
+
const config = getConfig();
|
|
5
|
+
|
|
6
|
+
if (!config) {
|
|
7
|
+
console.log("✖ You are not logged in.");
|
|
8
|
+
console.log("Run 'chron login' to login.");
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return config;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getToken() {
|
|
16
|
+
return requireAuth().token;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getCurrentUser() {
|
|
20
|
+
return requireAuth().user;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = { requireAuth, getToken, getCurrentUser };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const fs = require("fs"); //file system
|
|
2
|
+
const os = require("os"); //Node.js’s operating system module for system info and utilities
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const CONFIG_PATH = path.join(os.homedir(), ".chronconfig"); //os.homedir(): Path to the user’s home directory
|
|
6
|
+
|
|
7
|
+
function saveConfig(data) {
|
|
8
|
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(data, null, 4), "utf-8");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getConfig() {
|
|
12
|
+
if (!fs.existsSync(CONFIG_PATH)) {
|
|
13
|
+
//Check existence
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const data = fs.readFileSync(CONFIG_PATH, "utf-8");
|
|
18
|
+
return JSON.parse(data);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function clearConfig() {
|
|
22
|
+
if (!fs.existsSync(CONFIG_PATH)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fs.unlinkSync(CONFIG_PATH);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = { saveConfig, getConfig, clearConfig };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const unzipper = require("unzipper");
|
|
3
|
+
|
|
4
|
+
async function unzipDirectory(zipPath, destination) {
|
|
5
|
+
// Create destination folder if it doesn't exist
|
|
6
|
+
if (!fs.existsSync(destination)) {
|
|
7
|
+
fs.mkdirSync(destination, { recursive: true });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const zip = await unzipper.Open.file(zipPath);
|
|
11
|
+
|
|
12
|
+
await zip.extract({
|
|
13
|
+
path: destination,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { unzipDirectory };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const fs = require("fs"); //file system
|
|
2
|
+
const archiver = require("archiver"); //used to compress file to ZIP file
|
|
3
|
+
|
|
4
|
+
async function zipDirectory(sourceDir, outputZip) {
|
|
5
|
+
return new Promise(async (resolve, reject) => {
|
|
6
|
+
const output = fs.createWriteStream(outputZip);
|
|
7
|
+
const archive = archiver("zip", {
|
|
8
|
+
zlib: { level: 9 },
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
output.on("close", () => {
|
|
12
|
+
resolve();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
output.on("error", (err) => {
|
|
16
|
+
reject(err);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
archive.on("error", (err) => {
|
|
20
|
+
reject(err);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
archive.pipe(output);
|
|
24
|
+
|
|
25
|
+
// Add the entire directory while preserving its structure.
|
|
26
|
+
archive.directory(sourceDir, false);
|
|
27
|
+
|
|
28
|
+
await archive.finalize();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = { zipDirectory };
|