-
Notifications
You must be signed in to change notification settings - Fork 27
/
submit.ts
220 lines (180 loc) · 7.23 KB
/
submit.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Command, flags } from '@oclif/command';
import chalk from 'chalk';
import cli from 'cli-ux';
import * as FormData from 'form-data';
import * as fs from 'fs';
import * as fuzzy from 'fuzzy';
import * as inquirer from 'inquirer';
import fetch from 'node-fetch';
import { Response } from 'node-fetch';
import { AppCompiler, AppPackager, FolderDetails, VariousUtils } from '../misc';
import { CloudAuth } from '../misc/cloudAuth';
export default class Submit extends Command {
public static description = 'submits an App to the Marketplace for review';
public static flags = {
help: flags.help({ char: 'h' }),
update: flags.boolean({ description: 'submits an update instead of creating one' }),
categories: flags.string({
char: 'c',
description: 'a comma separated list of the categories for the App',
}),
};
public async run() {
inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt'));
const { flags } = this.parse(Submit);
//#region app packaging
cli.action.start(`${chalk.green('packaging')} your app`);
const fd = new FolderDetails(this);
try {
await fd.readInfoFile();
} catch (e) {
this.error(e && e.message ? e.message : e);
}
const compiler = new AppCompiler(fd);
const report = await compiler.compile();
if (!report.diagnostics.length) {
this.error('TypeScript compiler error(s) occurred');
}
const packager = new AppPackager(this, fd);
const zipName = await packager.zipItUp();
cli.action.stop('packaged!');
//#endregion
//#region fetching categories
cli.action.start(`${chalk.green('fetching')} the available categories`);
const categories = await VariousUtils.fetchCategories();
cli.action.stop('fetched!');
//#endregion
//#region asking for information
const cloudAuth = new CloudAuth();
const hasToken = await cloudAuth.hasToken();
if (!hasToken) {
const cloudAccount: any = await inquirer.prompt([{
type: 'confirm',
name: 'hasAccount',
message: 'Have you logged into our Publisher Portal?',
default: true,
}]);
if (cloudAccount.hasAccount) {
try {
cli.action.start(chalk.green('*') + ' ' + chalk.gray('waiting for authorization...'));
await cloudAuth.executeAuthFlow();
cli.action.stop(chalk.green('success!'));
} catch (e) {
cli.action.stop(chalk.red('failed to authenticate.'));
return;
}
} else {
this.error('A Rocket.Chat Cloud account and a Marketplace Publisher account '
+ 'is required to submit an App to the Marketplace. (rc-apps login)');
}
}
if (typeof flags.update === 'undefined') {
const isNewApp = await inquirer.prompt([{
type: 'confirm',
name: 'isNew',
message: 'Is this a new App?',
default: true,
}]);
flags.update = !(isNewApp as any).isNew;
}
let changes = '';
if (flags.update) {
const result: any = await inquirer.prompt([{
type: 'input',
name: 'changes',
message: 'What changes were made in this version?',
}]);
changes = result.changes;
} else {
const isFreeQuestion: any = await inquirer.prompt([{
type: 'confirm',
name: 'isFree',
message: 'Is this App free or will it require payment?',
default: true,
}]);
if (!isFreeQuestion.isFree) {
this.error('Paid Apps must be submitted via our Publisher Portal: '
+ 'https://marketplace.rocket.chat/publisher/new/app');
}
}
let selectedCategories = new Array<string>();
if (flags.categories) {
selectedCategories = flags.categories.split(',');
} else {
const result = await inquirer.prompt([{
type: 'checkbox-plus',
name: 'categories',
message: 'Please select the categories which apply to this App?',
pageSize: 10,
highlight: true,
searchable: true,
validate: (answer: Array<string>) => {
if (answer.length === 0) {
return 'You must choose at least one color.';
}
return true;
},
// tslint:disable:promise-function-async
source: (_answersSoFar: object, input: string) => {
input = input || '';
return new Promise((resolve) => {
const fuzzyResult = fuzzy.filter(input, categories, {
extract: (item) => item.name,
});
const data = fuzzyResult.map((element) => {
return element.original;
});
resolve(data);
});
},
}] as any);
selectedCategories = (result as any).categories;
}
const confirmSubmitting = await inquirer.prompt([{
type: 'confirm',
name: 'submit',
message: 'Are you ready to submit?',
default: false,
}]);
if (!(confirmSubmitting as any).submit) {
return;
}
//#endregion
cli.action.start(`${chalk.green('submitting')} your app`);
const data = new FormData();
data.append('app', fs.createReadStream(fd.mergeWithFolder(zipName)));
data.append('categories', JSON.stringify(selectedCategories));
if (changes) {
data.append('changes', changes);
}
const token = await cloudAuth.getToken();
await this.asyncSubmitData(data, flags, fd, token);
cli.action.stop('submitted!');
}
// tslint:disable:promise-function-async
// tslint:disable-next-line:max-line-length
private async asyncSubmitData(data: FormData, flags: { [key: string]: any }, fd: FolderDetails, token: string): Promise<any> {
let url = 'https://marketplace.rocket.chat/v1/apps';
if (flags.update) {
url += `/${fd.info.id}`;
}
const headers: { [key: string]: string } = {};
if (token) {
headers.Authorization = 'Bearer ' + token;
}
const res: Response = await fetch(url, {
method: 'POST',
body: data,
headers,
});
if (res.status !== 200) {
const result = await res.json();
if (result.code === 467 || result.code === 466) {
throw new Error(result.error);
}
throw new Error(`Failed to submit the App. Error code ${result.code}: ${result.error}`);
} else {
return res.json();
}
}
}