-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·198 lines (186 loc) · 7.39 KB
/
index.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env ts-node
"use strict";
/**
* Thanks to https://gist.github.com/jgcmarins/3bfd75b7978acb7d7b1c97a8564d2e64
*/
const fs = require("fs").promises;
const path = require("path");
const os = require('os');
const _ = require("lodash");
//var argv = require('minimist')(process.argv.slice(2));
const term = require('terminal-kit').terminal;
const semver = require('semver');
const changelogParser = require("changelog-parser");
//import changelog from 'generate-changelog';
const TOML = require("@iarna/toml");
const { Octokit } = require("@octokit/rest");
//import { exec as execCb } from 'child_process';
//import util from 'util';
//const exec = util.promisify(execCb);
const simpleGit = require('simple-git');
const git = simpleGit();
// npm --no-git-tag-version version from-git
const gitTokenFileName = ".gittoken";
const changelogFileName = "CHANGELOG.md";
const packageJson = async (octo, repo, { path = 'package.json', ref = 'master' } = {}) => {
const response = await octo.repos.getContent({ ref, path, ...repo });
const version = JSON.parse(Buffer.from(response.data.content, "base64")).version;
return {version, ref};
}
const cargoToml = async (octo, repo, { path = 'Cargo.toml', ref = 'master' } = {}) => {
const response = await octo.repos.getContent({ ref, path, ...repo });
const version = TOML.parse(Buffer.from(response.data.content, "base64")).package.version;
return {version, ref};
}
const more = (text) => {
return text;
}
//TODO: class
const reposFn = {
"tonlabs/ton-q-server": packageJson,
"tonlabs/ever-sdk": async (octo, repo) => await cargoToml(octo, repo, { path: "ton_client/Cargo.toml" }),
"tonlabs/ever-sdk-js": async (octo, repo) => await packageJson(octo, repo, { path: "packages/core/package.json" }),
"tonlabs/evernode-se": async (octo, repo) => await cargoToml(octo, repo, { path: "ton-node-se/ton_node_startup/Cargo.toml" }),
"tonlabs/everdev": async (octo, repo) => await packageJson(octo, repo, { ref: "main" }),
"tonlabs/everdev-vscode": async (octo, repo) => await packageJson(octo, repo, { ref: "main" }),
"tonlabs/ever-appkit-js": async (octo, repo) => await packageJson(octo, repo, { ref: "main" }),
/*
"tonlabs/ton-client-rs": cargoToml,
"tonlabs/ton-client-web-js": packageJson,
"tonlabs/ton-client-node-js": packageJson,
"tonlabs/ton-client-react-native-js": packageJson,
*/
};
const repoNames = Object.keys(reposFn);
const singleColumnMenuAsync = model => {
return new Promise((res, rej) => {
term.singleColumnMenu(
model,
(err, input) => {
if (err) { rej(err) }
res(input.selectedText)
}
)
})
}
const yesOrNoAsync = model => {
return new Promise((res, rej) => {
term.yesOrNo(
model,
(err, answer) => {
if (err) { rej(err) }
res(answer)
}
)
})
}
const inputFieldAsync = () => {
return new Promise((res, rej) => {
term.inputField(
(err, input) => {
if (err) { rej(err) }
res(input)
}
)
})
}
(async function main() {
const changelog = await changelogParser(changelogFileName);
if (!(changelog && changelog.versions && changelog.versions.length > 0)) {
throw Error(`FAILED: Not found versions at ${changelogFileName}`);
}
const {version, title, body} = changelog.versions[0];
const bodyRelease = '## ' + title + '\n' + body;
let auth;
try {
auth = await fs.readFile(path.resolve(os.homedir(), gitTokenFileName));
auth = auth.toString().trim();
} catch(err) {
auth = process.env.GITHUB_TOKEN;
};
if (auth === undefined) {
throw Error(`FAILED: Not found auth token at env.GITHUB_TOKEN or ${gitTokenFileName}`);
}
const octokit = new Octokit({auth}); //
const repoList = repoNames.map(name => {
const [owner, repo] = name.split('/');
return {owner, repo};
});
term.on( 'key' , key => {
if ( key === 'CTRL_C' ) {
term('\n');
term.grabInput( false ) ;
process.exit() ;
}
});
term.fullscreen({ noAlternate: false });
while(true) {
term.moveTo(0, 3).eraseDisplayBelow();
//term.bold("TARGET\n").styleReset("branch: ").bold.yellow(target_commitish + '\n\n');//TODO
term.bold("CURRENT\n").styleReset("Version: ").bold.green(version + '\n').gray(more(bodyRelease) + '\n\n');
const reposKit = await Promise.all(repoList.map(repo => octokit.repos.listReleases({ per_page: 1, ...repo }).catch(err => console.error('fail', err))));
const reposAct = await Promise.all(repoList.map(repo => reposFn[repo.owner+'/'+repo.repo](octokit, repo)));
//console.log('DEBUG:', _.get(reposKit, '[0].data[0]'));
const reposLastRelease = _.map(reposKit, _.property('data[0].tag_name'));
const repos = _.zip(repoNames, reposLastRelease, reposAct).map(triple => {
const [name, version, actual] = triple;
return {name, version, actual};
});
//term.on('submit');
//term.saveCursor();
//term.moveTo(0, term.height-1, "Submit: %s" , data.selectedText);
//term.restoreCursor();
term.bold("LATEST\n")
repos.forEach(repo => {
term.white(repo.name).white(": ");
if (repo.version) {
term.green(repo.version);
} else {
term.gray('unknown');
}
term(' ')
if (repo.version === repo.actual.version) {
term.cyan(repo.actual.version);
} else if (semver.gt(repo.version, repo.actual.version)) {
term.brightRed(repo.actual.version);
} else {
term.brightYellow(repo.actual.version);
}
term('\n');
});
term('\n');
const repoName = await singleColumnMenuAsync(repoNames); //, {continueOnSubmit: true}
const idx = repoNames.indexOf(repoName);
const target_commitish = repos[idx].actual.ref //TODO actual ref should be equal to current git branch
term.gray(`Release ${repoName} [target_commitish:${target_commitish}]? [y/N]`);
const release = await yesOrNoAsync({yes: ['y'], no: ['n', 'ENTER']});
if (release) {
term.yellow('\nStarting release...\n');
const repo = repoList[idx];
if (repos[idx].version == version) {
term.bold.yellow('WARNING!').white(' version already exists\n');
} else if (semver.gt(repos[idx].version, version)) {
term.bold.yellow('WARNING!').white(' attempt to downgrade\n');
} else {
//TODO createRelease build light tag without annotation, so lerna doesn't know about it
const res = await octokit.repos.createRelease({
draft: false,
prerelease: false,
body: bodyRelease,
target_commitish,
name: `Version: ${version}`,
tag_name: version,
...repo
});
if (res.data) {
term.cyan(_.get(res, 'data.html_url') + '\n');
} else {
term.error.bold.red('FAILED\n');
console.error(res);
}
}
term.white("Press ENTER to continue.");
await inputFieldAsync();
}
}
})().catch(err => {term.error.bold.red(err + '\n');});