@soft-artel/ci 2.3.86 → 2.3.87
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/.env +21 -0
- package/.eslintcache +1 -0
- package/.eslintignore +4 -0
- package/.eslintrc +246 -0
- package/.gitlab-ci.yml +12 -0
- package/README.md +33 -0
- package/_publish.sh +11 -0
- package/package.json +2 -2
- package/src/Project.ts +286 -0
- package/src/commands/incrementBuild.ts +46 -0
- package/src/commands/k8s-build.ts +441 -0
- package/src/commands/k8s-deploy.ts +215 -0
- package/src/commands/xcode.ts +192 -0
- package/src/services/Git.ts +203 -0
- package/src/services/Gitlab.ts +129 -0
- package/src/services/Jira.ts +228 -0
- package/src/services/Reporter.ts +230 -0
- package/src/utils/Exception.ts +19 -0
- package/src/utils/Logger.ts +85 -0
- package/src/utils/Shell.ts +120 -0
- package/src/utils/helpers.ts +126 -0
- package/src/utils/prototype.ts +313 -0
- package/test.ts +0 -0
- package/tsconfig.json +25 -0
- package/Project.d.ts +0 -57
- package/Project.d.ts.map +0 -1
- package/Project.js +0 -173
- package/Project.js.map +0 -1
- package/commands/incrementBuild.d.ts +0 -3
- package/commands/incrementBuild.d.ts.map +0 -1
- package/commands/incrementBuild.js +0 -34
- package/commands/incrementBuild.js.map +0 -1
- package/commands/k8s-build.d.ts +0 -30
- package/commands/k8s-build.d.ts.map +0 -1
- package/commands/k8s-build.js +0 -291
- package/commands/k8s-build.js.map +0 -1
- package/commands/k8s-deploy.d.ts +0 -11
- package/commands/k8s-deploy.d.ts.map +0 -1
- package/commands/k8s-deploy.js +0 -145
- package/commands/k8s-deploy.js.map +0 -1
- package/commands/xcode.d.ts +0 -3
- package/commands/xcode.d.ts.map +0 -1
- package/commands/xcode.js +0 -128
- package/commands/xcode.js.map +0 -1
- package/services/Git.d.ts +0 -46
- package/services/Git.d.ts.map +0 -1
- package/services/Git.js +0 -138
- package/services/Git.js.map +0 -1
- package/services/Gitlab.d.ts +0 -14
- package/services/Gitlab.d.ts.map +0 -1
- package/services/Gitlab.js +0 -101
- package/services/Gitlab.js.map +0 -1
- package/services/Jira.d.ts +0 -32
- package/services/Jira.d.ts.map +0 -1
- package/services/Jira.js +0 -136
- package/services/Jira.js.map +0 -1
- package/services/Reporter.d.ts +0 -43
- package/services/Reporter.d.ts.map +0 -1
- package/services/Reporter.js +0 -134
- package/services/Reporter.js.map +0 -1
- package/utils/Exception.d.ts +0 -5
- package/utils/Exception.d.ts.map +0 -1
- package/utils/Exception.js +0 -14
- package/utils/Exception.js.map +0 -1
- package/utils/Logger.d.ts +0 -11
- package/utils/Logger.d.ts.map +0 -1
- package/utils/Logger.js +0 -62
- package/utils/Logger.js.map +0 -1
- package/utils/Shell.d.ts +0 -39
- package/utils/Shell.d.ts.map +0 -1
- package/utils/Shell.js +0 -89
- package/utils/Shell.js.map +0 -1
- package/utils/helpers.d.ts +0 -16
- package/utils/helpers.d.ts.map +0 -1
- package/utils/helpers.js +0 -109
- package/utils/helpers.js.map +0 -1
- package/utils/prototype.d.ts +0 -9
- package/utils/prototype.d.ts.map +0 -1
- package/utils/prototype.js +0 -186
- package/utils/prototype.js.map +0 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
import { log } from '../utils/Logger';
|
|
4
|
+
import { htmlEntities, stripTags } from '../utils/helpers';
|
|
5
|
+
import git from './Git';
|
|
6
|
+
import { Project } from '../Project';
|
|
7
|
+
|
|
8
|
+
const ENV = process.env;
|
|
9
|
+
|
|
10
|
+
const RELEASE_BO_URL = ENV.RELEASE_BO_URL || 'https://bo.soft-artel.com/ci/jira';
|
|
11
|
+
const RELEASE_JIRA_URL = ENV.RELEASE_JIRA_URL || 'https://jira.soft-artel.com';
|
|
12
|
+
|
|
13
|
+
export interface ReleaseInfo{
|
|
14
|
+
commits: Record<string, any>[];
|
|
15
|
+
components: string[];
|
|
16
|
+
bugs: Issue[];
|
|
17
|
+
tasks: Issue[];
|
|
18
|
+
changelog: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ===========================================
|
|
22
|
+
|
|
23
|
+
async function resolve(prj: Project, head = true): Promise<ReleaseInfo>{
|
|
24
|
+
|
|
25
|
+
const release: ReleaseInfo = {
|
|
26
|
+
commits:[], components: [], bugs:[], tasks:[], changelog: 'No issues found in git commits!',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// 1. Получаем информацию из гита - по тому какие коммиты были
|
|
30
|
+
|
|
31
|
+
const fromTag = prj.prevVersion ? await git.existTag(prj.prevVersion) : await git.lastTag();
|
|
32
|
+
const toTag = head ? undefined : `v${prj.version}`;
|
|
33
|
+
|
|
34
|
+
const gitLog = await git.getCommitsAndIssues({
|
|
35
|
+
fromTag: fromTag ?? '', toTag: toTag ?? '', onlyJira: true, limit: 1000,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
release.commits = Object.values(gitLog.commits)
|
|
39
|
+
|
|
40
|
+
const issuesKeys = Object.keys(gitLog.issues);
|
|
41
|
+
|
|
42
|
+
if(issuesKeys.length === 0){
|
|
43
|
+
// если никто не комитил с ключами - выходим!!!
|
|
44
|
+
log.info('No issues found in git log!');
|
|
45
|
+
return release;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 2. Резолвим задачи - получаем названия и компоненты
|
|
49
|
+
|
|
50
|
+
try{
|
|
51
|
+
|
|
52
|
+
const resp = await axios({
|
|
53
|
+
method:'POST',
|
|
54
|
+
url: `${ RELEASE_BO_URL }/resolve`,
|
|
55
|
+
headers:{ 'PRIVATE-TOKEN': ENV.RELEASE_BO_TOKEN || ENV.GITLAB_PASS || '' },
|
|
56
|
+
data: { issues: issuesKeys },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if(resp?.data?.data?.bugs && Array.isArray(resp.data.data.bugs)){
|
|
60
|
+
for (const fields of resp.data.data.bugs) {
|
|
61
|
+
const issue = new Issue(fields);
|
|
62
|
+
if(issue.key !== ''){
|
|
63
|
+
release.bugs.push(issue);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if(resp?.data?.data?.tasks && Array.isArray(resp.data.data.tasks)){
|
|
69
|
+
for (const fields of resp.data.data.tasks) {
|
|
70
|
+
const issue = new Issue(fields);
|
|
71
|
+
if(issue.key !== ''){
|
|
72
|
+
release.tasks.push(issue);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if(resp?.data?.data?.components){
|
|
78
|
+
release.components = Object.values<string>(resp.data.data.components).sort();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
} catch(err){
|
|
82
|
+
err.request = '...';
|
|
83
|
+
err.connection = '...';
|
|
84
|
+
err.message = 'JIRA RESOLVE ERR: ' + err.message;
|
|
85
|
+
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
// 3. Формируем ченж лог
|
|
91
|
+
|
|
92
|
+
log.info(`\n\n===============\nRELEASE: ${ prj.group }/${ prj.name }/v${ prj.version }`,'', `pev:${ fromTag }`);
|
|
93
|
+
|
|
94
|
+
// let changelog = `🎉 ${ prj.group.toUpperCase() }: <b>${ prj.name.toUpperCase() } v${ prj.version }</b> [ ${ stage.toUpperCase() } ] \n\n`
|
|
95
|
+
|
|
96
|
+
let msg = '';
|
|
97
|
+
|
|
98
|
+
if(release.components.length > 0){
|
|
99
|
+
msg += '<b>Components:</b>\n';
|
|
100
|
+
for (const comp of release.components) {
|
|
101
|
+
msg += `- ${ comp }\n`;
|
|
102
|
+
}
|
|
103
|
+
msg += '\n';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if(release.tasks.length > 0){
|
|
107
|
+
msg += '<b>Done:</b>\n';
|
|
108
|
+
for (const issue of release.tasks) {
|
|
109
|
+
msg += `- 🛠 <b><a href="${ RELEASE_JIRA_URL }/browse/${ issue.key }">${ issue.key }</a></b>: ${issue.summary}\n`;
|
|
110
|
+
}
|
|
111
|
+
msg += '\n';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if(release.bugs.length > 0){
|
|
115
|
+
msg += '<b>BugFixes:</b>\n';
|
|
116
|
+
for (const issue of release.bugs) {
|
|
117
|
+
msg += `- 🐞 <b><a href="${ RELEASE_JIRA_URL }/browse/${ issue.key }">${ issue.key }</a></b>: ${issue.summary}\n`;
|
|
118
|
+
}
|
|
119
|
+
msg += '\n';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
release.changelog = msg;
|
|
123
|
+
|
|
124
|
+
log.info('changelog', msg);
|
|
125
|
+
log.info('text', stripTags(msg));
|
|
126
|
+
|
|
127
|
+
return release;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ------------------------
|
|
131
|
+
|
|
132
|
+
export class Issue{
|
|
133
|
+
|
|
134
|
+
key = '';
|
|
135
|
+
updated = new Date();
|
|
136
|
+
|
|
137
|
+
type = '';
|
|
138
|
+
status = '';
|
|
139
|
+
summary = '';
|
|
140
|
+
assignee = { name: '', email: '', login: '' };
|
|
141
|
+
components: string[] = [];
|
|
142
|
+
|
|
143
|
+
constructor(fields: Record<string, any>){
|
|
144
|
+
this.key = fields.key;
|
|
145
|
+
this.updated = new Date(Date.parse(fields.updated));
|
|
146
|
+
this.type = fields.type;
|
|
147
|
+
this.status = fields.status;
|
|
148
|
+
this.summary = htmlEntities(fields.summary);
|
|
149
|
+
if(fields.assignee && typeof fields.assignee === 'object'){
|
|
150
|
+
this.assignee.name = fields.assignee.name;
|
|
151
|
+
this.assignee.email = fields.assignee.email;
|
|
152
|
+
this.assignee.login = fields.assignee.login;
|
|
153
|
+
}
|
|
154
|
+
this.components = Array.isArray(fields.components) ? fields.components : [];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// --------------------
|
|
159
|
+
|
|
160
|
+
async function moveBugsAsDeployed(bugs: { key: string }[]){
|
|
161
|
+
const issuesKeys = [];
|
|
162
|
+
|
|
163
|
+
for (const bug of bugs) {
|
|
164
|
+
issuesKeys.push(bug.key);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if(issuesKeys.length === 0){
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const resp = await axios({
|
|
172
|
+
method:'POST',
|
|
173
|
+
url: `${ RELEASE_BO_URL }/deployed`,
|
|
174
|
+
headers:{ 'PRIVATE-TOKEN': ENV.RELEASE_BO_TOKEN || ENV.GITLAB_PASS || '' },
|
|
175
|
+
data: { issues: issuesKeys },
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if(resp?.data?.data?.deployed && Array.isArray(resp.data.data.deployed)){
|
|
179
|
+
return resp.data.data.deployed;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
// ------------------------
|
|
187
|
+
|
|
188
|
+
export const Jira = {
|
|
189
|
+
resolve,
|
|
190
|
+
moveBugsAsDeployed,
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
// let msg = `## **${ prj.version }**
|
|
195
|
+
// ${ new Date().toLocaleString('ru-RU', {
|
|
196
|
+
// weekday: 'long',
|
|
197
|
+
// year: 'numeric',
|
|
198
|
+
// month: 'short',
|
|
199
|
+
// day: 'numeric',
|
|
200
|
+
// timeZone: 'Europe/Moscow',
|
|
201
|
+
// hour12: false,
|
|
202
|
+
// hour: 'numeric',
|
|
203
|
+
// minute: 'numeric',
|
|
204
|
+
// }) }`
|
|
205
|
+
|
|
206
|
+
// if( release.components.length > 0 ){
|
|
207
|
+
// msg += '**Components:**\n'
|
|
208
|
+
// for (const comp of release.components) {
|
|
209
|
+
// msg += `- **${ comp }**\n`
|
|
210
|
+
// }
|
|
211
|
+
// msg += '\n\n'
|
|
212
|
+
// }
|
|
213
|
+
|
|
214
|
+
// if( release.tasks.length > 0 ){
|
|
215
|
+
// msg += '**Done:**\n'
|
|
216
|
+
// for (const issue of release.tasks) {
|
|
217
|
+
// msg += `- 🛠 **[${ issue.key }](${ RELEASE_JIRA_URL }/browse/${ issue.key })**: ${issue.summary}, _by [${ issue.assignee.name }](${ RELEASE_JIRA_URL }/issues/?jql=assignee%20in%20(${issue.assignee.login}))_\n`
|
|
218
|
+
// }
|
|
219
|
+
// msg += '\n\n'
|
|
220
|
+
// }
|
|
221
|
+
|
|
222
|
+
// if( release.bugs.length > 0 ){
|
|
223
|
+
// msg += '**BugFixes:**\n'
|
|
224
|
+
// for (const issue of release.bugs) {
|
|
225
|
+
// msg += `- 🐞 **[${ issue.key }](${ RELEASE_JIRA_URL }/browse/${ issue.key })**: ${issue.summary}, _by [${ issue.assignee.name }](${ RELEASE_JIRA_URL }/issues/?jql=assignee%20in%20(${issue.assignee.login}))_\n`
|
|
226
|
+
// }
|
|
227
|
+
// msg += '\n'
|
|
228
|
+
// }
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { htmlEntities } from "../utils/helpers";
|
|
2
|
+
|
|
3
|
+
const ENV = process.env;
|
|
4
|
+
ENV.NTBA_FIX_319='true';
|
|
5
|
+
|
|
6
|
+
import '../utils/prototype';
|
|
7
|
+
|
|
8
|
+
import TelegramBot from 'node-telegram-bot-api';
|
|
9
|
+
|
|
10
|
+
import { log } from '../utils/Logger';
|
|
11
|
+
import { Project } from '../Project';
|
|
12
|
+
import { ReleaseInfo } from './Jira';
|
|
13
|
+
|
|
14
|
+
// ------------------------
|
|
15
|
+
|
|
16
|
+
export interface ReporterOptions {
|
|
17
|
+
chat_id: number;
|
|
18
|
+
chat_rel?: Record<string, number>;
|
|
19
|
+
|
|
20
|
+
token: string;
|
|
21
|
+
|
|
22
|
+
showGroup?: boolean;
|
|
23
|
+
shortVersion?: boolean;
|
|
24
|
+
showBuild?: boolean;
|
|
25
|
+
|
|
26
|
+
preTitle?: string;
|
|
27
|
+
postTitle?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ------------------------
|
|
31
|
+
|
|
32
|
+
export class Reporter {
|
|
33
|
+
|
|
34
|
+
$bot;
|
|
35
|
+
$prj: Project;
|
|
36
|
+
$opt: ReporterOptions;
|
|
37
|
+
|
|
38
|
+
// --------------
|
|
39
|
+
|
|
40
|
+
$chat_id = 0;
|
|
41
|
+
$chat_rel: Record<string, number> = {};
|
|
42
|
+
|
|
43
|
+
$message_id = 0;
|
|
44
|
+
|
|
45
|
+
$message_title_prefix = '▶️';
|
|
46
|
+
$message_title_icon = '';
|
|
47
|
+
$message_title_postfix = '';
|
|
48
|
+
|
|
49
|
+
$message_body = '';
|
|
50
|
+
|
|
51
|
+
$message_footer_hr = '---------------';
|
|
52
|
+
|
|
53
|
+
$message_footer_changes_from = '';
|
|
54
|
+
$message_footer_changes_to = '';
|
|
55
|
+
|
|
56
|
+
// --------------
|
|
57
|
+
|
|
58
|
+
constructor(prj: Project, opt: ReporterOptions){
|
|
59
|
+
this.$prj = prj;
|
|
60
|
+
this.$chat_id = opt.chat_id;
|
|
61
|
+
this.$opt = opt;
|
|
62
|
+
|
|
63
|
+
this.$bot = new TelegramBot(opt.token);
|
|
64
|
+
|
|
65
|
+
if(opt.chat_rel){
|
|
66
|
+
this.$chat_rel = opt.chat_rel;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
log.dbg('options', opt);
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// --------------
|
|
74
|
+
|
|
75
|
+
get messageParts(){
|
|
76
|
+
let title = `${ this.$message_title_prefix } ${ this.$message_title_icon} `;
|
|
77
|
+
title += `${this.$prj.group.toUpperCase()} / <b>${ this.$prj.name.toUpperCase() } `;
|
|
78
|
+
title += this.$opt.shortVersion ? `v${ this.$prj.shortVersion } #${ this.$prj.build }` : `v${ this.$prj.version }`;
|
|
79
|
+
title += `</b>${ this.$message_title_postfix }`;
|
|
80
|
+
|
|
81
|
+
const job = this.$prj.job;
|
|
82
|
+
|
|
83
|
+
let footer = this.$message_footer_hr + '\n';
|
|
84
|
+
footer += `job: <a href="${ job.url }">${ job.id }</a> `;
|
|
85
|
+
footer += `by: <a href="${ job.commit.url }">${ job.user.name }</a> `;
|
|
86
|
+
|
|
87
|
+
// const cmpUrl = `${ENV.CI_PROJECT_URL}/-/compare/${ this.$message_footer_changes_from }...${ this.$message_footer_changes_to }?from_project_id=${this.$prj.id}`
|
|
88
|
+
// footer += `diff: <a href="${ cmpUrl }">${ this.$message_footer_changes_from } - ${ this.$message_footer_changes_to }</a> `;
|
|
89
|
+
|
|
90
|
+
return { title, footer };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// --------------
|
|
94
|
+
|
|
95
|
+
async startBuild(){
|
|
96
|
+
this.$message_title_prefix = '▶️';
|
|
97
|
+
this.$message_title_icon = '📦';
|
|
98
|
+
|
|
99
|
+
await this.append(`${this.$prj.job.commit.title}\n${this.$prj.job.commit.author.name}\n${ this.$message_footer_hr }`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// --------------
|
|
103
|
+
|
|
104
|
+
async startDeploy(to: string){
|
|
105
|
+
this.$message_title_prefix = '▶️';
|
|
106
|
+
this.$message_title_icon = '🚀';
|
|
107
|
+
|
|
108
|
+
this.$message_title_postfix = `-> ${ to.toUpperCase() }`;
|
|
109
|
+
|
|
110
|
+
await this.append(`${ this.$message_footer_hr }`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// --------------
|
|
114
|
+
|
|
115
|
+
async appendDeploy(to: string){
|
|
116
|
+
this.$message_title_prefix = '▶️';
|
|
117
|
+
this.$message_title_icon = '🚀';
|
|
118
|
+
|
|
119
|
+
this.$message_title_postfix = `-> ${ to.toUpperCase() }`;
|
|
120
|
+
|
|
121
|
+
await this.append(`\n${ this.$message_footer_hr }\nDEPLOY:`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// --------------
|
|
125
|
+
|
|
126
|
+
async fail(err: Error){
|
|
127
|
+
this.$message_title_prefix = '❌';
|
|
128
|
+
|
|
129
|
+
let msg = `${ this.$message_footer_hr }\n`;
|
|
130
|
+
msg += `‼️ <a href="${ this.$prj.job.url }">ERROR</a>:\n`;
|
|
131
|
+
// msg += htmlEntities(err.message);
|
|
132
|
+
msg += htmlEntities(err.stack || err.message);
|
|
133
|
+
|
|
134
|
+
await this.append(msg, '');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// --------------
|
|
138
|
+
|
|
139
|
+
async succses(release: ReleaseInfo | undefined = undefined){
|
|
140
|
+
this.$message_title_prefix = '✅';
|
|
141
|
+
// this.$message_body = '';
|
|
142
|
+
|
|
143
|
+
if(release === undefined || release.changelog === 'No issues found in git commits!'){
|
|
144
|
+
await this.append(release ? `commits:${ release.commits.length } done:${ release.tasks.length }, bugs: ${ release.bugs.length }` : '', '');
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let msg = this.$message_footer_hr + '\n';
|
|
149
|
+
msg += `🎉 WHATS NEW in <b>${ this.$opt.shortVersion ? `v${ this.$prj.shortVersion } #${ this.$prj.build }` : `v${ this.$prj.version }` }</b>:\n\n`;
|
|
150
|
+
msg += release.changelog;
|
|
151
|
+
await this.append(msg, ' ');
|
|
152
|
+
|
|
153
|
+
const chat_rel = Number(this.$chat_rel[this.$prj.stage]);
|
|
154
|
+
if(chat_rel && chat_rel !== 0){
|
|
155
|
+
// Дублируем сообщение в другой чат если указан
|
|
156
|
+
msg = `${this.$prj.group.toUpperCase()} / <b>${ this.$prj.name.toUpperCase() }</b>\n` + msg;
|
|
157
|
+
await this.send(msg, chat_rel);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// --------------
|
|
163
|
+
|
|
164
|
+
async skip(msg: string = ''){
|
|
165
|
+
this.$message_title_prefix = '⏩';
|
|
166
|
+
this.$message_body = '';
|
|
167
|
+
msg = `${this.$prj.job.commit.title}\n${this.$prj.job.commit.author.name}\n${this.$message_footer_hr}\n${ msg }`;
|
|
168
|
+
await this.append(msg, '');
|
|
169
|
+
}
|
|
170
|
+
// --------------
|
|
171
|
+
|
|
172
|
+
async append(text: string, message_postfix = '...'){
|
|
173
|
+
|
|
174
|
+
// 1. build message:
|
|
175
|
+
const { title, footer} = this.messageParts;
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
let message = this.$message_body.length > 0 ? this.$message_body + '\n' : '';
|
|
179
|
+
message += text.length > 0 ? text : '';
|
|
180
|
+
this.$message_body = message;
|
|
181
|
+
|
|
182
|
+
if(message.length > 3000){
|
|
183
|
+
message = message.slice(0, 3000);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
message += message_postfix.length > 0 ? '\n' + message_postfix : '';
|
|
187
|
+
|
|
188
|
+
message = `${title}\n${message}\n${footer}`;
|
|
189
|
+
|
|
190
|
+
// log.dbg(message);
|
|
191
|
+
|
|
192
|
+
if(this.$message_id === 0){
|
|
193
|
+
this.$message_id = await this.send(message);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
await this.$bot.editMessageText(message, {
|
|
199
|
+
chat_id: this.$chat_id, message_id: this.$message_id, parse_mode:'HTML', disable_web_page_preview: true,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
} catch (err) {
|
|
203
|
+
log.fail('ERROR editMessageText:\n' + err.message);
|
|
204
|
+
log.fail(message);
|
|
205
|
+
return 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// --------------
|
|
211
|
+
|
|
212
|
+
async send(message: string, chat_id_opt = this.$chat_id){
|
|
213
|
+
|
|
214
|
+
const chat_id = chat_id_opt;
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
|
|
218
|
+
message = message.length < 4090 ? message : message.slice(0, 4090);
|
|
219
|
+
const res = await this.$bot.sendMessage(chat_id, message, { parse_mode: 'HTML', disable_web_page_preview: true });
|
|
220
|
+
return res.message_id;
|
|
221
|
+
|
|
222
|
+
} catch (err) {
|
|
223
|
+
log.fail('ERROR sendMessage\n' + err.message);
|
|
224
|
+
log.fail(message);
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class Exception extends Error {
|
|
2
|
+
params?: Record<string, any>;
|
|
3
|
+
|
|
4
|
+
// -------------
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
message: string,
|
|
8
|
+
params?: Record<string, any>) {
|
|
9
|
+
super();
|
|
10
|
+
|
|
11
|
+
this.message = message;
|
|
12
|
+
this.params = params;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @see https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
|
|
16
|
+
*/
|
|
17
|
+
Object.setPrototypeOf(this, Exception.prototype);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/member-ordering */
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
|
|
4
|
+
import '../utils/prototype';
|
|
5
|
+
import { Exception } from './Exception';
|
|
6
|
+
|
|
7
|
+
const ENV = process.env;
|
|
8
|
+
|
|
9
|
+
export const log = {
|
|
10
|
+
|
|
11
|
+
isDebug: true,
|
|
12
|
+
|
|
13
|
+
dbg: (title: string, params?: any, note?: string ) => {
|
|
14
|
+
console.log( logString( 'dim', { title, params, note }))
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
info: (title: string, params?: any, note?: string ) => {
|
|
18
|
+
console.log( logString( 'cyan', { title, params, note }))
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
fail: (title: string, params?: any, note?: string ) => {
|
|
22
|
+
console.log( logString( 'red', { title, params, note }))
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
sucses: (title: string, params?: any, note?: string ) => {
|
|
26
|
+
console.log( logString( 'green', { title, params, note }))
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
error: ( error: Exception | Error , params?: any ) => {
|
|
30
|
+
console.error( logString( 'red', {
|
|
31
|
+
title: error?.message || JSON.stringify( error ),
|
|
32
|
+
params: params || (error as Exception).params,
|
|
33
|
+
note: error.stack
|
|
34
|
+
}))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
// --------------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
function logString( color: 'dim' | 'cyan' | 'red' | 'green', logData: { title?: string, note?: string, params?: any } = {} ): string {
|
|
43
|
+
|
|
44
|
+
const { title, note, params } = logData;
|
|
45
|
+
|
|
46
|
+
let logStr = ''
|
|
47
|
+
|
|
48
|
+
if( title ){
|
|
49
|
+
logStr += title + '\n'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if( note ){
|
|
53
|
+
logStr += chalk.dim( note ) + '\n'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if( params ){
|
|
57
|
+
logStr += chalk.dim( getParams( params ) ) + '\n'
|
|
58
|
+
}
|
|
59
|
+
// return chalk.dim(``)
|
|
60
|
+
|
|
61
|
+
logStr = logStr.slice(0, -1)
|
|
62
|
+
|
|
63
|
+
return chalk[ color ]( logStr )
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// --------------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
function getParams(params: any): string {
|
|
70
|
+
|
|
71
|
+
if( typeof params === 'string' ){
|
|
72
|
+
return params
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if( typeof params === 'number' ){
|
|
76
|
+
return `${ params }`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
return JSON.stringify( params, undefined, 3 );
|
|
81
|
+
} catch (e) {
|
|
82
|
+
console.error( e );
|
|
83
|
+
return ''
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import shelljs from 'shelljs';
|
|
2
|
+
|
|
3
|
+
import { log } from '../utils/Logger';
|
|
4
|
+
import { resolvePath, asyncSleep } from '../utils/helpers';
|
|
5
|
+
|
|
6
|
+
// ===========================================
|
|
7
|
+
|
|
8
|
+
function exec(cmd: string, opt: ExecOptions = { silent: !log.isDebug, ignoreError: false }): Promise<string>{
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
|
|
11
|
+
if(opt.silent === undefined){
|
|
12
|
+
opt.silent = true;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if(opt.silent === false){
|
|
16
|
+
log.dbg(cmd);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
shelljs.exec(cmd, opt, (code, stdout, stderr) => {
|
|
20
|
+
if(code === 0){
|
|
21
|
+
resolve(stdout.trim());
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if(code !== 0 && opt.ignoreError){
|
|
26
|
+
resolve('');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const e = new Error();
|
|
31
|
+
e.message = cmd+'\n'+ (stderr.length > 0 ? stderr : stdout);
|
|
32
|
+
e.name = String(code);
|
|
33
|
+
reject(e);
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// -------------
|
|
41
|
+
|
|
42
|
+
async function execRepeat(cmd: string, repeats = 10, sleep = 10, opt = { silent: !log.isDebug, ignoreError: false }){
|
|
43
|
+
|
|
44
|
+
let tryCount = repeats;
|
|
45
|
+
let e = new Error();
|
|
46
|
+
|
|
47
|
+
while(tryCount > 0) {
|
|
48
|
+
tryCount -= 1;
|
|
49
|
+
try{
|
|
50
|
+
|
|
51
|
+
return await exec(cmd, { silent: opt.silent, ignoreError: false });
|
|
52
|
+
|
|
53
|
+
} catch(err){
|
|
54
|
+
e = err;
|
|
55
|
+
log.fail(`Exec Repeats [${tryCount + 1}] FAIL: ${ cmd }`);
|
|
56
|
+
log.error(err);
|
|
57
|
+
await asyncSleep(sleep);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if(opt.ignoreError){
|
|
62
|
+
return e.message;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw e;
|
|
66
|
+
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// -------------
|
|
70
|
+
|
|
71
|
+
async function mkdir(dir: string, opt?: ExecOptions){
|
|
72
|
+
return exec(`mkdir -p ${ resolvePath(dir) }`, opt);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function cat(file: string, opt = { silent: true, ignoreError: false }){
|
|
76
|
+
const filePath = resolvePath(file);
|
|
77
|
+
if(shelljs.test('-e', filePath) === false && opt && opt.ignoreError === true){
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
return exec('cat '+filePath, opt);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function find(where: string, filename = '', maxdepth = 3, ignore: string[] = [], pars = '', opt = { silent: true, ignoreError: false }){
|
|
84
|
+
|
|
85
|
+
let cmd = `find ${ where } ${pars}`;
|
|
86
|
+
|
|
87
|
+
for (const path of ignore) {
|
|
88
|
+
cmd += ` -not -path '${path}'`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
cmd += ` -maxdepth ${maxdepth}`;
|
|
92
|
+
cmd += ` -name ${filename}`;
|
|
93
|
+
|
|
94
|
+
return (await exec(cmd, opt)).split('\n').filter((f)=>f.length > 2);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function ls(where: string, pars = '', opt = { silent: true, ignoreError: false }){
|
|
98
|
+
return (await exec(`ls ${ where } ${ pars }`, opt)).split('\n').filter((f)=>f.length > 2);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ExecOptions {
|
|
102
|
+
silent?: boolean;
|
|
103
|
+
ignoreError?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default {
|
|
107
|
+
cd: shelljs.cd,
|
|
108
|
+
chmod: shelljs.chmod,
|
|
109
|
+
grep: shelljs.grep,
|
|
110
|
+
pwd: shelljs.pwd,
|
|
111
|
+
sed: shelljs.sed,
|
|
112
|
+
test: shelljs.test,
|
|
113
|
+
|
|
114
|
+
exec,
|
|
115
|
+
execRepeat,
|
|
116
|
+
mkdir,
|
|
117
|
+
cat,
|
|
118
|
+
find,
|
|
119
|
+
ls,
|
|
120
|
+
};
|