-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
SentryCli.ts
72 lines (63 loc) · 2.23 KB
/
SentryCli.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
import { Answers } from 'inquirer';
import * as _ from 'lodash';
import * as path from 'path';
import { Args } from '../Constants';
export interface SentryCliProps {
[s: string]: string;
}
type SentryCliConfig = Record<string, SentryCliProps>;
export class SentryCli {
// eslint-disable-next-line @typescript-eslint/typedef
private _resolve = require.resolve;
constructor(protected _argv: Args) {}
public setResolveFunction(resolve: (path: string) => string): void {
this._resolve = resolve as any;
}
public convertAnswersToProperties(answers: Answers): SentryCliProps {
const props: SentryCliProps = {};
props['defaults/url'] = this._argv.url;
props['defaults/org'] = _.get(answers, 'config.organization.slug', null);
props['defaults/project'] = _.get(answers, 'config.project.slug', null);
props['auth/token'] = _.get(answers, 'config.auth.token', null);
try {
const cliPath = this._resolve('@sentry/cli/bin/sentry-cli');
props['cli/executable'] = path
.relative(process.cwd(), cliPath)
.replace(/\\/g, '\\\\');
} catch (e) {
// we do nothing and leave everyting as it is
}
return props;
}
/** Create the contents of a `sentry.properties` file */
public dumpProperties(props: SentryCliProps): string {
const rv = [];
for (let key in props) {
// eslint-disable-next-line no-prototype-builtins
if (props.hasOwnProperty(key)) {
const value = props[key];
key = key.replace(/\//g, '.');
if (value === undefined || value === null) {
// comment that property out since it has no value
rv.push(`#${key}=`);
} else {
rv.push(`${key}=${value}`);
}
}
}
// eslint-disable-next-line prefer-template
return rv.join('\n') + '\n';
}
public dumpConfig(config: SentryCliConfig): string {
const dumpedSections: string[] = [];
for (const sectionName in config) {
// eslint-disable-next-line no-prototype-builtins
if (config.hasOwnProperty(sectionName)) {
const props = this.dumpProperties(config[sectionName]);
const section = `[${sectionName}]\n${props}`;
dumpedSections.push(section);
}
}
return dumpedSections.join('\n');
}
}