11import { confirm } from "@inquirer/prompts" ;
22import type { Command } from "commander" ;
3- import { deleteConfig , getConfigPath , loadConfig } from "../helpers/config" ;
3+ import {
4+ deleteConfig ,
5+ getConfigPath ,
6+ isLoggedIn ,
7+ loadConfig ,
8+ } from "../helpers/config" ;
9+ import {
10+ logAndQuit ,
11+ logLoginMessageAndQuit ,
12+ logSessionTokenExpiredAndQuit ,
13+ } from "../helpers/errors" ;
14+ import { getApiUrl } from "../helpers/urls" ;
415
516// development only commands
617export function registerDev ( program : Command ) {
718 if ( process . env . IS_DEVELOPMENT_CLI_ENV ) {
8- program . command ( "ping" ) . action ( async ( ) => {
9- console . log ( "pong" ) ;
19+ registerConfig ( program ) ;
20+
21+ program . command ( "me" ) . action ( async ( ) => {
22+ const accountId = await getLoggedInAccountId ( ) ;
23+ console . log ( accountId ) ;
24+
1025 process . exit ( 0 ) ;
1126 } ) ;
27+ program . command ( "epoch" ) . action ( async ( ) => {
28+ const MILLS_PER_EPOCH = 1000 * 60 * 60 ;
29+ console . log ( Math . floor ( Date . now ( ) / MILLS_PER_EPOCH ) ) ;
1230
13- registerConfig ( program ) ;
31+ process . exit ( 0 ) ;
32+ } ) ;
33+ program . command ( "ping" ) . action ( async ( ) => {
34+ const data = await pingServer ( ) ;
35+ console . log ( data ) ;
36+
37+ process . exit ( 0 ) ;
38+ } ) ;
1439 }
1540}
1641
42+ // --
43+
1744function registerConfig ( program : Command ) {
1845 const configCmd = program
1946 . command ( "config" )
@@ -44,8 +71,6 @@ function registerConfig(program: Command) {
4471 . action ( removeConfigAction ) ;
4572}
4673
47- // --
48-
4974async function showConfigAction ( ) {
5075 const config = await loadConfig ( ) ;
5176 console . log ( config ) ;
@@ -62,3 +87,59 @@ async function removeConfigAction() {
6287 }
6388 process . exit ( 0 ) ;
6489}
90+
91+ // --
92+
93+ async function getLoggedInAccountId ( ) {
94+ const loggedIn = await isLoggedIn ( ) ;
95+ if ( ! loggedIn ) {
96+ logLoginMessageAndQuit ( ) ;
97+ }
98+ const config = await loadConfig ( ) ;
99+
100+ const response = await fetch ( await getApiUrl ( "me" ) , {
101+ method : "GET" ,
102+ headers : {
103+ "Content-Type" : "application/json" ,
104+ Authorization : `Bearer ${ config . auth_token } ` ,
105+ } ,
106+ } ) ;
107+ if ( ! response . ok ) {
108+ if ( response . status === 401 ) {
109+ logSessionTokenExpiredAndQuit ( ) ;
110+ }
111+
112+ logAndQuit ( "Failed to fetch account info" ) ;
113+ }
114+
115+ const data = await response . json ( ) ;
116+
117+ return data . id ;
118+ }
119+
120+ async function pingServer ( ) {
121+ const loggedIn = await isLoggedIn ( ) ;
122+ if ( ! loggedIn ) {
123+ logLoginMessageAndQuit ( ) ;
124+ }
125+ const config = await loadConfig ( ) ;
126+
127+ const response = await fetch ( await getApiUrl ( "ping" ) , {
128+ method : "GET" ,
129+ headers : {
130+ "Content-Type" : "application/json" ,
131+ Authorization : `Bearer ${ config . auth_token } ` ,
132+ } ,
133+ } ) ;
134+ if ( ! response . ok ) {
135+ if ( response . status === 401 ) {
136+ logSessionTokenExpiredAndQuit ( ) ;
137+ }
138+
139+ logAndQuit ( "Failed to ping server" ) ;
140+ }
141+
142+ const data = await response . json ( ) ;
143+
144+ return data ;
145+ }
0 commit comments