-
Notifications
You must be signed in to change notification settings - Fork 2
/
release.js
177 lines (146 loc) · 5.65 KB
/
release.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import dotenv from 'dotenv';
import fs from 'fs';
import readline from 'readline';
import archiver from 'archiver';
import axios from 'axios';
import { exec } from 'child_process';
dotenv.config();
// Read package.json and manifest.json
const package_json = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const manifest_json = JSON.parse(fs.readFileSync('./manifest.json', 'utf8'));
const version = package_json.version;
const manifest_id = manifest_json.id;
// Function to update manifest and push changes
async function update_manifest_and_push() {
// Update manifest.json with new version
manifest_json.version = version;
fs.writeFileSync('./manifest.json', JSON.stringify(manifest_json, null, 2));
// Commit and push to main
try {
await exec_command('git add .');
await exec_command(`git commit -m "Update manifest.json to version ${version}"`);
await exec_command('git push origin main');
console.log('Successfully pushed to main.');
} catch (error) {
console.error('Error in git operations:', error);
process.exit(1);
}
}
// Function to execute shell commands
function exec_command(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
});
});
}
// Function to create a release
async function create_release(confirmed_version, release_description) {
const release_name = confirmed_version;
console.log(`Creating release for version ${confirmed_version}`);
// Prepare release data
const release_data = {
tag_name: `${confirmed_version}`,
name: release_name,
body: release_description,
draft: false,
prerelease: false
};
// Environment variables
const github_token = process.env.GH_TOKEN;
const github_repo = process.env.GH_REPO;
if (!github_token || !github_repo) {
console.error('Error: GitHub token or repository not set in .env file.');
process.exit(1);
}
try {
// Create GitHub release
const release_response = await axios.post(`https://api.github.com/repos/${github_repo}/releases`, release_data, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${github_token}`
}
});
const release_info = release_response.data;
console.log('Release created:', release_info);
await upload_assets(release_info, github_token);
} catch (error) {
console.error('Error in release process:', error);
process.exit(1);
}
}
// Function to upload assets
async function upload_assets(release_info, github_token) {
const upload_asset = async (asset_path, asset_name) => {
const upload_url = `${release_info.upload_url.split('{')[0]}?name=${encodeURIComponent(asset_name)}`;
console.log(`Uploading ${asset_name} to ${upload_url}`);
try {
const stats = fs.statSync(asset_path);
const content_length = stats.size;
console.log(`Content length: ${content_length}`);
const response = await axios.post(upload_url, fs.createReadStream(asset_path), {
headers: {
'Authorization': `Bearer ${github_token}`,
'Content-Type': 'application/octet-stream',
'Content-Length': content_length
}
});
console.log(`File upload response for ${asset_name}:`, response.data);
} catch (error) {
console.error(`Error uploading file ${asset_name}:`, error);
}
};
// Create a zip file of dist folder
const zip_name = `${manifest_id}-${version}.zip`;
const output = fs.createWriteStream(`./${zip_name}`);
const archive = archiver('zip', { zlib: { level: 0 } });
archive.on('error', function(err) {
throw err;
});
archive.on('end', async function() {
console.log('Archive wrote %d bytes', archive.pointer());
// Upload manifest.json and styles.css
await upload_asset('./manifest.json', 'manifest.json');
await upload_asset('./styles.css', 'styles.css');
console.log('Uploaded files: manifest.json, styles.css');
await upload_asset('./dist/main.js', 'main.js');
console.log('Uploaded file: main.js');
// Upload zip file (last to prevent issues with uploading others)
await upload_asset(`./${zip_name}`, zip_name);
console.log('Zip file uploaded.');
// Remove zip file
fs.unlinkSync(`./${zip_name}`);
console.log('All files uploaded.');
});
archive.pipe(output);
archive.directory('dist/', false);
archive.file('manifest.json', { name: 'manifest.json' });
archive.file('styles.css', { name: 'styles.css' });
await archive.finalize();
}
// Main execution
async function main() {
await update_manifest_and_push();
const rl_interface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const confirmed_version = await new Promise(resolve => {
rl_interface.question(`Confirm release version (${version}): `, answer => {
resolve(answer || version);
});
});
const release_description = await new Promise(resolve => {
rl_interface.question('Enter release description: ', resolve);
});
rl_interface.close();
await create_release(confirmed_version, release_description);
}
main().catch(error => {
console.error('An error occurred:', error);
process.exit(1);
});