@@ -5,27 +5,33 @@ import os from 'node:os'
55import { Writable } from 'node:stream'
66import { printError , printLog , runMain } from '../lib/executionUtils.ts'
77import { findPackageJsonFiles } from '../lib/filesUtils.ts'
8+ import { command } from '../lib/command.ts'
89
910const TOKEN_NAME = 'browser-sdk-granular'
1011
1112runMain ( async ( ) => {
13+ if ( getLoggedInUser ( ) !== 'datadog' ) {
14+ printError ( 'You must be logged in as datadog. Run `npm login` and try again.' )
15+ process . exit ( 1 )
16+ }
17+
1218 const accessToken = findAccessTokenFromNpmrc ( )
1319 if ( ! accessToken ) {
1420 printError ( 'Could not find NPM token in ~/.npmrc. Make sure you have logged in with `npm login`' )
1521 process . exit ( 1 )
1622 }
1723
18- const password = await prompt ( { question : 'Password : ' , showOutput : false } )
19- const otp = await prompt ( { question : 'OTP: ' } )
24+ const password = await prompt ( { question : 'npmjs.com password : ' , showOutput : false } )
25+ const otp = await prompt ( { question : 'npmjs.com OTP: ' } )
2026
21- const { objects : tokens } = ( await callNpmApi ( {
27+ const { objects : tokens } = await callNpmApi < {
28+ objects : Array < { name : string ; key : string } >
29+ } > ( {
2230 route : 'tokens' ,
2331 method : 'GET' ,
2432 otp,
2533 accessToken,
26- } ) ) as {
27- objects : Array < { name : string ; key : string } >
28- }
34+ } )
2935
3036 const existingToken = tokens . find ( ( token ) => token . name === TOKEN_NAME )
3137 if ( existingToken ) {
@@ -38,7 +44,7 @@ runMain(async () => {
3844 } )
3945 }
4046
41- await callNpmApi ( {
47+ const result = await callNpmApi < { token : string } > ( {
4248 route : 'tokens' ,
4349 method : 'POST' ,
4450 body : {
@@ -55,9 +61,26 @@ runMain(async () => {
5561 } )
5662
5763 printLog ( 'Token created' )
64+
65+ printLog ( 'Updating AWS SSM parameter...' )
66+ command `
67+ aws-vault exec sso-build-stable-developer -- aws ssm put-parameter --region=us-east-1 --name=ci.browser-sdk.npm_token --value=${ result . token } --type SecureString --overwrite
68+ `
69+ . withLogs ( )
70+ . run ( )
71+
72+ printLog ( 'All done!' )
5873} )
5974
60- async function callNpmApi ( {
75+ function getLoggedInUser ( ) {
76+ try {
77+ return command `npm whoami` . run ( ) . trim ( )
78+ } catch {
79+ return undefined
80+ }
81+ }
82+
83+ async function callNpmApi < T > ( {
6184 route,
6285 method,
6386 body,
@@ -69,7 +92,7 @@ async function callNpmApi({
6992 body ?: any
7093 otp : string
7194 accessToken : string
72- } ) : Promise < any > {
95+ } ) : Promise < T > {
7396 // https://api-docs.npmjs.com/#tag/Tokens/operation/createToken
7497 const response = await fetch ( `https://registry.npmjs.org/-/npm/v1/${ route } ` , {
7598 method,
@@ -97,7 +120,7 @@ async function callNpmApi({
97120 process . exit ( 1 )
98121 }
99122
100- return responseBody
123+ return responseBody as T
101124}
102125
103126function findPublisheablePackages ( ) {
0 commit comments