11import { readFile , mkdir as mkDir , writeFile , rmdir as rmDir } from "fs/promises"
22import { resolve as resolvePath } from "path"
33import { homedir as homeDir } from "os"
4- import { build , clear , push , sync , watch } from "hackmud_env-tools"
5- import c , { redBright , yellowBright , greenBright , blueBright , cyanBright , magentaBright , bold , dim } from "ansi-colors"
4+ import { build , clear , pull , push , pushBuilt , watch } from "hackmud_env-tools"
5+ import { redBright , yellowBright , greenBright , blueBright , cyanBright , magentaBright , bold , dim } from "ansi-colors"
6+
7+ interface LooseObject < T = any > {
8+ [ key : string ] : T | undefined
9+ }
610
711// let o = ""
812
@@ -33,10 +37,12 @@ const commands: string[] = []
3337// }
3438// }
3539
36- let config : Partial < {
40+ let config : LooseObject & Partial < {
3741 hackmudPath : string
3842 defaultUser : string
39- [ key : string ] : any
43+ users : LooseObject < {
44+ colour : string
45+ } >
4046} > | undefined
4147
4248for ( let arg of process . argv . slice ( 2 ) ) {
@@ -99,15 +105,15 @@ for (let arg of process.argv.slice(2)) {
99105 break
100106 }
101107
102- case "push" : {
108+ case "push-built " : {
103109 const config = await getConfig ( )
104110
105111 if ( config . hackmudPath ) {
106112 const target = commands [ 1 ] || "dist"
107113 const user = commands [ 2 ] || config . defaultUser
108114
109115 if ( user ) {
110- const { pushedCount } = await push ( target , config . hackmudPath , user )
116+ const { pushedCount } = await pushBuilt ( target , config . hackmudPath , user )
111117
112118 console . log ( `pushed ${ pushedCount } file(s) to ${ user } ` )
113119 } else
@@ -118,52 +124,97 @@ for (let arg of process.argv.slice(2)) {
118124 break
119125 }
120126
121- case "sync " : {
127+ case "push " : {
122128 const config = await getConfig ( )
123129
124130 if ( config . hackmudPath ) {
125131 const srcPath = commands [ 1 ] || "."
126132 const hackmudPath = config . hackmudPath
127133 const users = options . get ( "users" ) ?. toString ( ) . split ( "," ) || [ ]
128134 const scripts = options . get ( "scripts" ) ?. toString ( ) . split ( "," ) || [ ]
129-
130135 const colours = [ redBright , greenBright , yellowBright , blueBright , magentaBright , cyanBright ]
131- const userColours = new Map < string , string > ( )
136+ const configUsers = config . users = config . users || { }
137+
138+ await push (
139+ srcPath ,
140+ hackmudPath ,
141+ users ,
142+ scripts ,
143+ ( { minLength, srcLength, users, script } ) =>
144+ users . length && console . log (
145+ `wrote ${
146+ bold ( minLength . toString ( ) )
147+ } chars from ${
148+ dim ( script )
149+ } to ${
150+ users . map ( user =>
151+ ( configUsers [ user ] = configUsers [ user ] || { colour : colours [ Math . floor ( Math . random ( ) * colours . length ) ] ( user ) } ) . colour
152+ ) . join ( ", " )
153+ } and saved ${
154+ bold ( ( srcLength - minLength ) . toString ( ) )
155+ } chars`
156+ )
157+ )
158+ } else
159+ console . log ( "you need to set hackmudPath in config before you can use this command" )
132160
133- sync ( srcPath , hackmudPath , users , scripts , ( file , { minLength, srcLength, users } ) => {
134- if ( users . length )
135- console . log ( `synced ${ dim ( file ) } to ${ users . map ( user => {
136- let colour = userColours . get ( user )
161+ break
162+ }
137163
138- if ( ! colour )
139- userColours . set ( user , colour = colours [ Math . floor ( Math . random ( ) * colours . length ) ] ( user ) )
164+ case "watch" : {
165+ const config = await getConfig ( )
140166
141- return colour
142- } ) . join ( ", " ) } [saved ${ bold ( ( srcLength - minLength ) . toString ( ) ) } chars]`)
143- } )
167+ if ( config . hackmudPath ) {
168+ const srcPath = commands [ 1 ] || "."
169+ const hackmudPath = config . hackmudPath
170+ const users = options . get ( "users" ) ?. toString ( ) . split ( "," ) || [ ]
171+ const scripts = options . get ( "scripts" ) ?. toString ( ) . split ( "," ) || [ ]
172+ const colours = [ redBright , greenBright , yellowBright , blueBright , magentaBright , cyanBright ]
173+ const configUsers = config . users = config . users || { }
174+
175+ watch (
176+ srcPath ,
177+ hackmudPath ,
178+ users ,
179+ scripts ,
180+ ( { minLength, srcLength, users, script } ) => {
181+ users . length && console . log (
182+ `wrote ${
183+ bold ( minLength . toString ( ) )
184+ } chars from ${
185+ dim ( script )
186+ } to ${
187+ users . map ( user =>
188+ ( configUsers [ user ] = configUsers [ user ] || { colour : colours [ Math . floor ( Math . random ( ) * colours . length ) ] ( user ) } ) . colour
189+ ) . join ( ", " )
190+ } and saved ${
191+ bold ( ( srcLength - minLength ) . toString ( ) )
192+ } chars`
193+ )
194+
195+ updateConfig ( )
196+ }
197+ )
144198 } else
145199 console . log ( "you need to set hackmudPath in config before you can use this command" )
146200
147201 break
148202 }
149203
150- case "watch " : {
204+ case "pull " : {
151205 const config = await getConfig ( )
152206
153207 if ( config . hackmudPath ) {
154- const srcPath = commands [ 1 ] || "src"
155- const user = commands [ 2 ] || config . defaultUser
156-
157- if ( user ) {
158- const { } = await watch ( srcPath , config . hackmudPath , user , ( { minLength, oldLength, name } ) => {
159- console . log ( `built and pushed ${ name } to ${ user } [saved ${ oldLength - minLength } chars]` )
160- } )
208+ const script = commands [ 1 ]
161209
162- // console.log(`pushed ${pushedCount} file(s) to ${user}`)
210+ if ( script ) {
211+ const srcPath = commands [ 2 ] || "."
212+ const hackmudPath = config . hackmudPath
213+ pull ( srcPath , hackmudPath , script )
163214 } else
164- console . log ( "set defaultUser in config first" )
215+ help ( )
165216 } else
166- console . log ( "set hackmudPath in config first " )
217+ console . log ( "you need to set hackmudPath in config before you can use this command " )
167218
168219 break
169220 }
@@ -233,24 +284,7 @@ for (let arg of process.argv.slice(2)) {
233284 help ( )
234285 }
235286
236- if ( config ) {
237- const json = JSON . stringify ( config )
238-
239- writeFile ( configFile , json ) . catch ( async error => {
240- switch ( error . code ) {
241- case "EISDIR" :
242- await rmDir ( configFile )
243- break
244- case "ENOENT" :
245- await mkDir ( configDir )
246- break
247- default :
248- throw error
249- }
250-
251- writeFile ( configFile , json )
252- } )
253- }
287+ updateConfig ( )
254288} ) ( )
255289
256290function help ( ) {
@@ -297,3 +331,24 @@ function exploreObject(object: any, keys: string[], createPath = false) {
297331
298332 return object
299333}
334+
335+ function updateConfig ( ) {
336+ if ( config ) {
337+ const json = JSON . stringify ( config )
338+
339+ writeFile ( configFile , json ) . catch ( async error => {
340+ switch ( error . code ) {
341+ case "EISDIR" :
342+ await rmDir ( configFile )
343+ break
344+ case "ENOENT" :
345+ await mkDir ( configDir )
346+ break
347+ default :
348+ throw error
349+ }
350+
351+ writeFile ( configFile , json )
352+ } )
353+ }
354+ }
0 commit comments