-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreleaseFile.js
More file actions
134 lines (117 loc) · 4.68 KB
/
releaseFile.js
File metadata and controls
134 lines (117 loc) · 4.68 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
// Find the project root directory by searching for package.json
function findProjectRoot(startPath) {
let currentPath = startPath;
while (currentPath !== path.parse(currentPath).root) {
if (fs.existsSync(path.join(currentPath, 'package.json'))) {
return currentPath;
}
currentPath = path.dirname(currentPath);
}
return null;
}
const newVersion = process.env.npm_config_version_tag;
if (!newVersion) {
console.error('Error: You must provide a new version number using the --version_tag flag.');
process.exit(1);
}
const __filename = fileURLToPath(import.meta.url);
const scriptDir = dirname(__filename);
const projectRoot = findProjectRoot(scriptDir);
if (!projectRoot) {
console.error('Error: Could not find project root containing package.json.');
process.exit(1);
}
// --- Update configuration files ---
const filesToUpdate = [
{
path: path.join(projectRoot, 'package.json'),
update: (content) => {
const json = JSON.parse(content);
json.version = newVersion;
return JSON.stringify(json, null, 2);
},
message: 'package.json'
},
{
path: path.join(projectRoot, 'src-tauri', 'tauri.conf.json'),
update: (content) => {
const json = JSON.parse(content);
json.version = newVersion;
return JSON.stringify(json, null, 2);
},
message: 'src-tauri/tauri.conf.json'
},
{
path: path.join(projectRoot, 'src-tauri', 'Cargo.toml'),
update: (content) => {
return content.replace(/version = ".*?"/, `version = "${newVersion}"`);
},
message: 'src-tauri/Cargo.toml'
}
];
filesToUpdate.forEach(file => {
try {
let content = fs.readFileSync(file.path, 'utf8');
content = file.update(content);
fs.writeFileSync(file.path, content);
console.log(`✅ Updated version in ${file.message} to ${newVersion}`);
} catch (error) {
console.error(`❌ Failed to update ${file.message}: ${error.message}`);
process.exit(1);
}
});
// --- Generate package-lock.json ---
try {
console.log(`\n⏳ Generating package-lock.json...`);
execSync('npm install --no-audit --no-fund', { cwd: projectRoot, stdio: 'inherit' });
console.log(`✅ package-lock.json generated successfully.`);
} catch (error) {
console.error(`❌ Failed to generate package-lock.json: ${error.message}`);
process.exit(1);
}
// --- Git operations on file change or after timeout ---
const cargoLockPath = path.join(projectRoot, 'src-tauri', 'Cargo.lock');
let isDone = false;
function doGitOperations() {
if (isDone) return;
isDone = true;
try {
console.log(`\n⏳ Running Git commands...`);
// Add the updated files to the staging area
const filesToAdd = filesToUpdate.map(file => path.relative(projectRoot, file.path)).join(' ');
execSync(`git add ${filesToAdd} ${path.relative(projectRoot, cargoLockPath)} package-lock.json`, { cwd: projectRoot, stdio: 'inherit' });
// Commit the changes
execSync(`git commit -m "Release v${newVersion}"`, { cwd: projectRoot, stdio: 'inherit' });
console.log(`✅ Committed changes with message "Release v${newVersion}"`);
// Create a new Git tag
execSync(`git tag v${newVersion}`, { cwd: projectRoot, stdio: 'inherit' });
console.log(`✅ Created Git tag v${newVersion}`);
// Push the commit and the tag to the remote repository
console.log(`\n⏳ Pushing commit and tag to remote...`);
execSync(`git push && git push origin v${newVersion}`, { cwd: projectRoot, stdio: 'inherit' });
console.log(`✅ Pushed tag v${newVersion} successfully.`);
} catch (error) {
console.error(`❌ An error occurred during Git operations: ${error.message}`);
process.exit(1);
}
// Un-watch the file and exit the process
fs.unwatchFile(cargoLockPath);
}
// Set a timeout to run the git commands after 10 seconds
setTimeout(() => {
console.log(`\n✅ Timeout reached (10 seconds). Running Git operations.`);
doGitOperations();
}, 10000); // 10000 milliseconds = 10 seconds
// Watch the Cargo.lock file for changes
fs.watchFile(cargoLockPath, (curr, prev) => {
// Only proceed if the file has actually been modified and we haven't already acted
if (curr.mtime.getTime() !== prev.mtime.getTime()) {
console.log(`\n✅ ${path.basename(cargoLockPath)} updated. Running Git commands...`);
doGitOperations();
}
});