@@ -5,33 +5,42 @@ import * as path from 'node:path';
55import type { Args } from '../Constants' ;
66import { addToGitignore } from './Git' ;
77import { green , l , nl , red } from './Logging' ;
8+ import { Config } from '../Types' ;
89
910const SENTRYCLIRC_FILENAME = '.sentryclirc' ;
1011const GITIGNORE_FILENAME = '.gitignore' ;
1112const PROPERTIES_FILENAME = 'sentry.properties' ;
1213
1314export interface SentryCliProps {
14- [ s : string ] : string ;
15+ 'defaults/url' : string ;
16+ 'defaults/org' : string | null ;
17+ 'defaults/project' : string | null ;
18+ 'auth/token' : string | null ;
19+ 'cli/executable' ?: string ;
1520}
1621
17- type SentryCliConfig = Record < string , SentryCliProps > ;
22+ type SentryCliConfig = Record < string , Partial < SentryCliProps > > ;
23+ type RequireResolve = typeof require . resolve ;
1824
1925export class SentryCli {
20- // eslint-disable-next-line @typescript-eslint/typedef
21- private _resolve = require . resolve ;
26+ private _resolve : RequireResolve = require . resolve ;
2227
2328 public constructor ( protected _argv : Args ) { }
2429
25- public setResolveFunction ( resolve : ( path : string ) => string ) : void {
26- this . _resolve = resolve as any ;
30+ public setResolveFunction ( resolve : RequireResolve ) : void {
31+ this . _resolve = resolve ;
2732 }
2833
29- public convertAnswersToProperties ( answers : Answers ) : SentryCliProps {
30- const props : SentryCliProps = { } ;
31- props [ 'defaults/url' ] = this . _argv . url ;
32- props [ 'defaults/org' ] = answers . config ?. organization ?. slug ?? null ;
33- props [ 'defaults/project' ] = answers . config ?. project ?. slug ?? null ;
34- props [ 'auth/token' ] = answers . config ?. auth ?. token ?? null ;
34+ public convertAnswersToProperties (
35+ answers : Answers & { config ?: Config } ,
36+ ) : SentryCliProps {
37+ const props : SentryCliProps = {
38+ 'defaults/url' : this . _argv . url ,
39+ 'defaults/org' : answers . config ?. organization ?. slug ?? null ,
40+ 'defaults/project' : answers . config ?. project ?. slug ?? null ,
41+ 'auth/token' : answers . config ?. auth ?. token ?? null ,
42+ } ;
43+
3544 try {
3645 const cliPath = this . _resolve ( '@sentry/cli/bin/sentry-cli' , {
3746 paths : [ process . cwd ( ) ] ,
@@ -45,11 +54,26 @@ export class SentryCli {
4554 return props ;
4655 }
4756
48- /** Create the contents of a `sentry.properties` file */
49- public dumpProperties ( props : SentryCliProps ) : string {
50- const rv = [ ] ;
51- for ( const [ key , value ] of Object . entries ( props ) ) {
52- const normalizedKey = key . replace ( / \/ / g, '.' ) ;
57+ /**
58+ * Create the contents of a `sentry.properties` file
59+ * @param props the properties to write to the file
60+ * @param format the format of the file, either `rc`
61+ * (.sentryclirc) or `properties` (sentry.properties)
62+ */
63+ public dumpProperties (
64+ props : Partial < SentryCliProps > ,
65+ format : 'rc' | 'properties' = 'properties' ,
66+ ) : string {
67+ const propEntries = Object . entries ( props ) as [
68+ keyof SentryCliProps ,
69+ SentryCliProps [ keyof SentryCliProps ] ,
70+ ] [ ] ;
71+ const rv : string [ ] = [ ] ;
72+ for ( const [ key , value ] of propEntries ) {
73+ const normalizedKey =
74+ format === 'properties'
75+ ? key . replace ( / \/ / g, '.' )
76+ : key . split ( '/' ) . at ( 1 ) ?? '' ;
5377 if ( value === undefined || value === null ) {
5478 // comment that property out since it has no value
5579 rv . push ( `#${ normalizedKey } =` ) ;
@@ -61,10 +85,10 @@ export class SentryCli {
6185 return rv . join ( '\n' ) + '\n' ;
6286 }
6387
64- public dumpConfig ( config : SentryCliConfig ) : string {
88+ public dumpConfig ( config : Partial < SentryCliConfig > ) : string {
6589 const dumpedSections : string [ ] = [ ] ;
66- for ( const [ sectionName , val ] of Object . entries ( config ) ) {
67- const props = this . dumpProperties ( val ) ;
90+ for ( const [ sectionName , values ] of Object . entries ( config ) ) {
91+ const props = values ? this . dumpProperties ( values , 'rc' ) : '' ;
6892 const section = `[${ sectionName } ]\n${ props } ` ;
6993 dumpedSections . push ( section ) ;
7094 }
@@ -94,7 +118,7 @@ export class SentryCli {
94118 try {
95119 await fs . promises . appendFile (
96120 SENTRYCLIRC_FILENAME ,
97- this . dumpConfig ( { auth : { token : authToken } } ) ,
121+ this . dumpConfig ( { auth : { 'auth/ token' : authToken } } ) ,
98122 ) ;
99123 green ( `✓ Successfully added the auth token to ${ SENTRYCLIRC_FILENAME } ` ) ;
100124 } catch {
0 commit comments