-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchData.js
More file actions
72 lines (62 loc) · 2 KB
/
Copy pathfetchData.js
File metadata and controls
72 lines (62 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
require("dotenv").config({ path: ".env" });
// Replace with your GitHub repo URL
const repoUrl = process.env.DATA_REPO_URL;
const localRepoName = process.env.DATA_REPO_NAME; // The name of the repo folder created after cloning
const currentFolder = __dirname;
const itemsToMove = ["public", "seed", "data.sqlite"];
function cloneRepo(repoUrl, localRepoPath) {
return new Promise((resolve, reject) => {
exec(`git clone ${repoUrl} ${localRepoPath}`, (error, stdout, stderr) => {
if (error) {
reject(`Error cloning repo: ${stderr}`);
} else {
resolve(`Repo cloned to ${localRepoPath}`);
}
});
});
}
function moveItem(src, dest) {
return new Promise((resolve, reject) => {
fs.rename(src, dest, (error) => {
if (error) {
reject(`Error moving ${src} to ${dest}: ${error}`);
} else {
resolve(`Moved ${src} to ${dest}`);
}
});
});
}
function deleteFolder(folderPath) {
return new Promise((resolve, reject) => {
fs.rm(folderPath, { recursive: true, force: true }, (error) => {
if (error) {
reject(`Error deleting folder ${folderPath}: ${error}`);
} else {
resolve(`Deleted folder ${folderPath}`);
}
});
});
}
async function main() {
const localRepoPath = path.join(currentFolder, localRepoName);
try {
console.log(await cloneRepo(repoUrl, localRepoPath));
for (const item of itemsToMove) {
const srcPath = path.join(localRepoPath, item);
const destPath = path.join(currentFolder, item);
console.log(await moveItem(srcPath, destPath));
}
console.log(await deleteFolder(localRepoPath));
console.log("All tasks completed successfully!");
} catch (error) {
console.error(error);
}
}
if (!fs.existsSync(path.join(__dirname, "public"))) {
main()
.then(() => console.log("Data fetched successfully"))
.catch((err) => console.log("error fetching data"));
}