@@ -30,8 +30,7 @@ const pGitconfig = promisify(gitconfig);
30
30
31
31
export async function command ( opts : OptionValues ) : Promise < void > {
32
32
const config = await pGitconfig ( process . cwd ( ) ) ;
33
- const gitconfigremoteoriginurl =
34
- config . remote && config . remote . origin && config . remote . origin . url ;
33
+ const gitconfigremoteoriginurl = config ?. remote ?. origin ?. url ;
35
34
updatePackageMetadata ( opts , gitconfigremoteoriginurl ) ;
36
35
}
37
36
@@ -41,7 +40,7 @@ interface Repository {
41
40
directory : string ;
42
41
}
43
42
44
- interface packageJSON {
43
+ interface PackageJSON {
45
44
name : string ;
46
45
version : string ;
47
46
main : string ;
@@ -78,8 +77,8 @@ const path = {
78
77
* @param {String } DotDotPath relative path
79
78
* @returns {String } resolved absolutePath
80
79
*/
81
- resolveRelativeFromAbsolute ( DotDotPath : String ) {
82
- const pathsArray = DotDotPath . replaceAll ( / [ \ /| \\ ] / g, '/' ) . split ( '/' ) ;
80
+ resolveRelativeFromAbsolute ( DotDotPath : string ) {
81
+ const pathsArray = DotDotPath . replaceAll ( / [ / | \\ ] / g, '/' ) . split ( '/' ) ;
83
82
const map = pathsArray . reduce (
84
83
( acc , e ) => acc . set ( e , ( acc . get ( e ) || 0 ) + 1 ) ,
85
84
new Map ( ) ,
@@ -92,17 +91,14 @@ const path = {
92
91
} ,
93
92
} ;
94
93
95
- function findCodeowners ( element : String ) {
94
+ function findCodeowners ( element : string ) {
96
95
if ( fs . existsSync ( `${ element } /.github/CODEOWNERS` ) ) {
97
96
return element ; // found the root dir
98
97
}
99
98
return findCodeowners ( `${ element } /..` ) ;
100
99
}
101
100
102
- export function updatePackageMetadata (
103
- opts : OptionValues ,
104
- gitconfigremoteoriginurl : string ,
105
- ) {
101
+ function loadPackageJSON ( opts : OptionValues ) {
106
102
// load the package.json from the specified (or current) folder
107
103
const workingDir = `${ process . cwd ( ) } /${ opts . dir } ` ;
108
104
console . log ( `Updating ${ workingDir } / package.json` ) ;
@@ -116,9 +112,63 @@ export function updatePackageMetadata(
116
112
const packageJSONPath = join ( workingDir , 'package.json' ) ;
117
113
const packageJSON = JSON . parse (
118
114
readFileSync ( packageJSONPath , 'utf8' ) ,
119
- ) as packageJSON ;
115
+ ) as PackageJSON ;
116
+
117
+ return [ packageJSON , packageJSONPath , rootdir , relative_path ] ;
118
+ }
119
+
120
+ function replaceKeywordsInPackageJSON (
121
+ packageJSON : PackageJSON ,
122
+ newKeywords : string [ ] ,
123
+ i : any ,
124
+ ) {
125
+ for ( let j = 0 ; j < newKeywords . length ; j ++ ) {
126
+ if (
127
+ newKeywords [ j ] . startsWith ( 'lifecycle:' ) ||
128
+ newKeywords [ j ] . startsWith ( 'support:' )
129
+ ) {
130
+ // replace existing; remove from array
131
+ packageJSON . keywords [ i ] = newKeywords [ j ] ;
132
+ newKeywords . splice ( j , 1 ) ;
133
+ }
134
+ }
135
+ return packageJSON ;
136
+ }
120
137
121
- /* now let's change some values */
138
+ function addNewKeywords ( packageJSON : PackageJSON , opts : OptionValues ) {
139
+ // initialize empty string array if not already present
140
+ if ( ! packageJSON . keywords ) {
141
+ packageJSON . keywords = [ ] ;
142
+ }
143
+
144
+ // eslint-disable-next-line prefer-const
145
+ let newKeywords = opts . keywords . split ( ',' ) ;
146
+ // if already have keywords, replace lifecycle and support with new values (if defined)
147
+ if ( packageJSON . keywords . length > 0 ) {
148
+ for ( let i = 0 ; i < packageJSON . keywords . length ; i ++ ) {
149
+ // can only have ONE lifecycle and one support keyword, so remove replace any existing values
150
+ if (
151
+ packageJSON . keywords [ i ] . startsWith ( 'lifecycle:' ) ||
152
+ packageJSON . keywords [ i ] . startsWith ( 'support:' )
153
+ ) {
154
+ packageJSON = replaceKeywordsInPackageJSON ( packageJSON , newKeywords , i ) ;
155
+ }
156
+ }
157
+ }
158
+ // add in the remaining keywords + dedupe
159
+ for ( const element of newKeywords ) {
160
+ packageJSON . keywords . push ( element ) ;
161
+ }
162
+ packageJSON . keywords = _ . uniq ( packageJSON . keywords ) ;
163
+ return [ packageJSON , opts ] ;
164
+ }
165
+
166
+ export function updatePackageMetadata (
167
+ opts : OptionValues ,
168
+ gitconfigremoteoriginurl : string ,
169
+ ) {
170
+ const [ packageJSON , packageJSONPath , rootdir , relative_path ] =
171
+ loadPackageJSON ( opts ) ;
122
172
123
173
// 1. add backstage version matching the current value of backstage.json in this repo
124
174
if ( fs . existsSync ( join ( rootdir , '/backstage.json' ) ) ) {
@@ -158,39 +208,8 @@ export function updatePackageMetadata(
158
208
packageJSON . homepage = new URL ( opts . homepage ) ;
159
209
packageJSON . bugs = new URL ( opts . bugs ) ;
160
210
161
- // initialize empty string array if not already present
162
- if ( ! packageJSON . keywords ) {
163
- packageJSON . keywords = [ ] ;
164
- }
165
-
166
- // eslint-disable-next-line prefer-const
167
- let newKeywords = opts . keywords . split ( ',' ) ;
168
- // if already have keywords, replace lifecycle and support with new values (if defined)
169
- if ( packageJSON . keywords . length > 0 ) {
170
- for ( let i = 0 ; i < packageJSON . keywords . length ; i ++ ) {
171
- // can only have ONE lifecycle and one support keyword, so remove replace any existing values
172
- if (
173
- packageJSON . keywords [ i ] . startsWith ( 'lifecycle:' ) ||
174
- packageJSON . keywords [ i ] . startsWith ( 'support:' )
175
- ) {
176
- for ( let j = 0 ; j < newKeywords . length ; j ++ ) {
177
- if (
178
- newKeywords [ j ] . startsWith ( 'lifecycle:' ) ||
179
- newKeywords [ j ] . startsWith ( 'support:' )
180
- ) {
181
- // replace existing; remove from array
182
- packageJSON . keywords [ i ] = newKeywords [ j ] ;
183
- newKeywords . splice ( j , 1 ) ;
184
- }
185
- }
186
- }
187
- }
188
- }
189
- // add in the remaining keywords + dedupe
190
- for ( let j = 0 ; j < newKeywords . length ; j ++ ) {
191
- packageJSON . keywords . push ( newKeywords [ j ] ) ;
192
- }
193
- packageJSON . keywords = _ . uniq ( packageJSON . keywords ) ;
211
+ // add keywords (replace some), then uniquify
212
+ addNewKeywords ( packageJSON , opts ) ;
194
213
195
214
/* all done! */
196
215
0 commit comments