-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (44 loc) · 1.8 KB
/
script.js
File metadata and controls
55 lines (44 loc) · 1.8 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
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
function initializeGit(directory, remoteUrl) {
try {
// Ensure the directory exists
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
// Change to the specified directory
process.chdir(directory);
// Check if the directory is already a Git repository
if (!fs.existsSync(path.join(directory, '.git'))) {
// Initialize Git repository
execSync('git init');
// Add a remote origin
execSync(`git remote add origin ${remoteUrl}`);
console.log(`Git initialized in '${directory}' with remote origin set to '${remoteUrl}'.`);
} else {
console.error(`Directory '${directory}' is already a Git repository.`);
}
} catch (error) {
console.error('Error initializing Git repository:', error.message);
}
}
function commitAndPush(directory, commitMessage) {
try {
// Change to the specified directory
process.chdir(directory);
// Add all files and create a commit
execSync('git add .');
execSync(`git commit -m "${commitMessage}"`);
// Push to the remote origin
execSync('git push origin master');
console.log(`Committed and pushed to the remote origin in '${directory}'.`);
} catch (error) {
console.error('Error committing and pushing:', error.message);
}
}
// Example usage
const remoteOriginUrl = 'https://github.com/gabrieldocs/code-town-xiii.git'; // Replace with your repository URL
const directoryToInitialize = 'C:/Users/lucgb/Developer/tempest/code-town-xiii'; // Replace with the desired local path
initializeGit(directoryToInitialize, remoteOriginUrl);
commitAndPush(directoryToInitialize, "Initial commit " + new Date().toLocaleDateString());