custom-deploy 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/index.js +105 -0
- package/package.json +21 -0
package/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import 'dotenv/config';
|
|
3
|
+
import {execSync} from 'child_process';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import axios from 'axios';
|
|
7
|
+
import FormData from 'form-data';
|
|
8
|
+
import archiver from 'archiver';
|
|
9
|
+
|
|
10
|
+
const TELEGRAM_BOT_TOKEN = process.env.BOT_TOKEN;
|
|
11
|
+
const CHAT_ID = process.env.CHAT_ID;
|
|
12
|
+
const DIST_PATH = path.resolve('dist');
|
|
13
|
+
|
|
14
|
+
function getCurrentBranch() {
|
|
15
|
+
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getBuildCommand(branch) {
|
|
19
|
+
const upper = branch.toUpperCase();
|
|
20
|
+
|
|
21
|
+
if (upper === 'PROD') return 'npm run build-prod';
|
|
22
|
+
if (upper === 'DEV') return 'npm run build';
|
|
23
|
+
|
|
24
|
+
throw new Error(`Unsupported branch: ${branch}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getZipPath(branch) {
|
|
28
|
+
const upper = branch.toUpperCase();
|
|
29
|
+
|
|
30
|
+
if (upper === 'PROD') return path.resolve('build-prod.zip');
|
|
31
|
+
if (upper === 'DEV') return path.resolve('build-dev.zip');
|
|
32
|
+
|
|
33
|
+
throw new Error(`Unsupported branch for zip: ${branch}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function runBuild(command) {
|
|
37
|
+
console.log(`Running: ${command}`);
|
|
38
|
+
execSync(command, {stdio: 'inherit'});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function zipDist(zipPath) {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const output = fs.createWriteStream(zipPath);
|
|
44
|
+
const archive = archiver('zip', {zlib: {level: 9}});
|
|
45
|
+
|
|
46
|
+
output.on('close', resolve);
|
|
47
|
+
archive.on('error', reject);
|
|
48
|
+
|
|
49
|
+
archive.pipe(output);
|
|
50
|
+
archive.directory(DIST_PATH, false);
|
|
51
|
+
archive.finalize();
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function sendTelegramMessage(text) {
|
|
56
|
+
const url = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`;
|
|
57
|
+
await axios.post(url, {chat_id: CHAT_ID, text});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function sendTelegramFile(zipPath) {
|
|
61
|
+
const url = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument`;
|
|
62
|
+
|
|
63
|
+
const form = new FormData();
|
|
64
|
+
form.append('chat_id', CHAT_ID);
|
|
65
|
+
form.append('document', fs.createReadStream(zipPath));
|
|
66
|
+
|
|
67
|
+
await axios.post(url, form, {
|
|
68
|
+
headers: form.getHeaders()
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function main() {
|
|
73
|
+
try {
|
|
74
|
+
if (!TELEGRAM_BOT_TOKEN || !CHAT_ID) {
|
|
75
|
+
throw new Error('Missing TELEGRAM_BOT_TOKEN or CHAT_ID');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const branch = getCurrentBranch();
|
|
79
|
+
const buildCommand = getBuildCommand(branch);
|
|
80
|
+
const zipPath = getZipPath(branch);
|
|
81
|
+
|
|
82
|
+
console.log(`Branch: ${branch}`);
|
|
83
|
+
|
|
84
|
+
runBuild(buildCommand);
|
|
85
|
+
|
|
86
|
+
if (!fs.existsSync(DIST_PATH)) {
|
|
87
|
+
throw new Error('dist folder not found');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
await zipDist(zipPath);
|
|
91
|
+
|
|
92
|
+
await sendTelegramMessage(`✅ Build completed for branch: ${branch}`);
|
|
93
|
+
await sendTelegramFile(zipPath);
|
|
94
|
+
|
|
95
|
+
console.log(`Sent: ${zipPath}`);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.error('❌', err.message);
|
|
98
|
+
if (TELEGRAM_BOT_TOKEN && CHAT_ID) {
|
|
99
|
+
await sendTelegramMessage(`❌ Deployment failed: ${err.message}`);
|
|
100
|
+
}
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "custom-deploy",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI tool to build and send deployment artifacts via Telegram",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"deploy": "index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"start": "node index.js"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"archiver": "^7.0.1",
|
|
17
|
+
"axios": "^1.7.2",
|
|
18
|
+
"dotenv": "^16.4.5",
|
|
19
|
+
"form-data": "^4.0.0"
|
|
20
|
+
}
|
|
21
|
+
}
|