1
+ #!/usr/bin/env node
2
+ // Initialize the tracer and initiate auto-instrumentation of all supported libraries
3
+ // and frameworks. In this client example, we instrumented the http module used by the
4
+ // snowman client module.
5
+ const tracer = require ( './snowman/tracer' )
6
+
7
+ // Note that importing other modules should occur after init() to ensure their supported
8
+ // dependencies have been auto-instrumented.
9
+ const yargs = require ( 'yargs' )
10
+ const fs = require ( 'fs' )
11
+ const client = require ( './snowman/client' )
12
+
13
+ function writeId ( id ) {
14
+ // Write a new game's id to local .snowman file
15
+ return new Promise ( ( resolve , reject ) => {
16
+ fs . writeFile ( '.snowman' , id , ( err ) => {
17
+ if ( err ) {
18
+ reject ( err )
19
+ } else {
20
+ resolve ( )
21
+ }
22
+ } )
23
+ } )
24
+ }
25
+
26
+ function getId ( ) {
27
+ // Obtain an existing game's id from local .snowman
28
+ return new Promise ( ( resolve , reject ) => {
29
+ fs . readFile ( '.snowman' , ( err , data ) => {
30
+ if ( err ) {
31
+ reject ( `Error obtaining game id: ${ err } . Be sure to run "new" to create a game.` )
32
+ } else {
33
+ const id = data . toString ( )
34
+ resolve ( id )
35
+ }
36
+ } )
37
+ } )
38
+ }
39
+
40
+ function printResponse ( response ) {
41
+ const properties = [ 'message' , 'guesses' , 'progress' , 'remainingMisses' ]
42
+ for ( let i = 0 ; i < properties . length ; i ++ ) {
43
+ const prop = properties [ i ]
44
+ if ( response [ prop ] ) {
45
+ console . log ( `${ prop } : ${ response [ prop ] } ` )
46
+ }
47
+ }
48
+ }
49
+
50
+ yargs
51
+ . scriptName ( 'snowman' )
52
+ . command ( 'new' , 'Make a new game.' , { } , ( argv ) => {
53
+ client . newGame ( ) . then ( ( response ) => {
54
+ return writeId ( response . id ) . then ( ( ) => {
55
+ printResponse ( response )
56
+ } ) . catch ( ( e ) => console . error ( e ) )
57
+ } ) . catch ( ( e ) => console . error ( e ) )
58
+ } )
59
+ . command ( 'guess [letter]' , 'Make a guess.' , { } , ( argv ) => {
60
+ getId ( ) . then ( ( id ) => {
61
+ client . makeGuess ( argv . letter , id ) . then ( ( response ) => {
62
+ printResponse ( response )
63
+ if ( response . remainingMisses !== undefined ) {
64
+ // render the snowman
65
+ for ( let i = response . remainingMisses ; i < 8 ; i ++ ) {
66
+ console . log ( client . snowman [ i ] )
67
+ }
68
+ }
69
+ } ) . catch ( ( e ) => console . error ( e ) )
70
+ } ) . catch ( ( e ) => console . error ( e ) )
71
+ } )
72
+ . command ( 'answer' , 'Report the answer' , { } , ( argv ) => {
73
+ getId ( ) . then ( ( id ) => {
74
+ client . getAnswer ( id ) . then ( response => {
75
+ printResponse ( response )
76
+ } ) . catch ( ( e ) => console . error ( e ) )
77
+ } ) . catch ( ( e ) => console . error ( e ) )
78
+ } )
79
+ . command ( 'delete' , 'Delete your current game' , { } , ( argv ) => {
80
+ getId ( ) . then ( ( id ) => {
81
+ client . deleteGame ( id ) . then ( response => {
82
+ printResponse ( response )
83
+ } ) . catch ( ( e ) => console . error ( e ) )
84
+ } ) . catch ( ( e ) => console . error ( e ) )
85
+ } )
86
+ . help ( ) . argv
0 commit comments