hackmud-script-manager 0.19.1-5bceac8 → 0.19.1-7a27b63

Sign up to get free protection for your applications and to get access to all the features.
package/bin/hsm.js CHANGED
@@ -3,16 +3,16 @@ import { Cache } from "@samual/lib/Cache"
3
3
  import { assert } from "@samual/lib/assert"
4
4
  import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
5
5
  import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
6
- import { readFile, writeFile, mkdir, rmdir } from "fs/promises"
6
+ import { writeFile, readFile } from "fs/promises"
7
7
  import { homedir } from "os"
8
- import { resolve, extname, basename, dirname, relative } from "path"
8
+ import { extname, basename, resolve, dirname, relative } from "path"
9
9
  import { supportedExtensions } from "../constants.js"
10
10
  import { generateTypeDeclaration } from "../generateTypeDeclaration.js"
11
11
  import { pull } from "../pull.js"
12
12
  import { syncMacros } from "../syncMacros.js"
13
+ import "@samual/lib/readDirectoryWithStats"
13
14
  import "@samual/lib/copyFilePersistent"
14
- const configDirectoryPath = resolve(homedir(), ".config"),
15
- configFilePath = resolve(configDirectoryPath, "hsm.json"),
15
+ const version = "0.19.1-7a27b63",
16
16
  options = new Map(),
17
17
  commands = [],
18
18
  userColours = new Cache(user => {
@@ -20,119 +20,7 @@ const configDirectoryPath = resolve(homedir(), ".config"),
20
20
  for (const char of user) hash += (hash >> 1) + hash + "xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj".indexOf(char) + 1
21
21
  return [colourJ, colourK, colourM, colourW, colourL, colourB][hash % 6](user)
22
22
  }),
23
- logNeedHackmudPathMessage = () =>
24
- console.error(
25
- colourS(
26
- `${colourD("You need to set hackmudPath in config before you can use this command")}\n\n${colourA("To fix this:")}\nOpen hackmud and run "${colourC("#dir")}"\nThis will open a file browser and print your hackmud user's script directory\nGo up 2 directories and then copy the path\nThen in a terminal run "${colourC("hsm")} ${colourL("config set")} ${colourV("hackmudPath")} ${colourB("<the path you copied>")}"`
27
- )
28
- ),
29
- logHelp = () => {
30
- const pushCommandDescription = "Push scripts from a directory to hackmud user's scripts directories",
31
- mangleNamesOptionDescription =
32
- "Reduce character count further but lose function names in error call stacks",
33
- forceQuineCheatsOptionDescription = `Force quine cheats on. Use ${colourN("--force-quine-cheats")}=${colourV("false")} to force off.`
34
- console.log(colourN("Version") + colourS(": ") + colourV("0.19.1-5bceac8"))
35
- switch (commands[0]) {
36
- case "config":
37
- switch (commands[1]) {
38
- case "get":
39
- console.log(
40
- `\n${colourJ("Retrieve a value from the config file")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(`${commands[0]} ${commands[1]}`)} ${colourB("<key>")}`
41
- )
42
- break
43
- case "set":
44
- console.log(
45
- `\n${colourJ("Assign a value to the config file")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(`${commands[0]} ${commands[1]}`)} ${colourB("<key> <value>")}`
46
- )
47
- break
48
- case "delete":
49
- console.log(
50
- `\n${colourJ("Remove a key and value from the config file")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(`${commands[0]} ${commands[1]}`)} ${colourB("<key>")}`
51
- )
52
- break
53
- default:
54
- console.log(
55
- colourS(
56
- `${colourN("Config path")}: ${colourV(configFilePath)}\n\n${colourJ("Modify the config file")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0] + " get")} ${colourB("<key>")}\n Retrieve a value from the config file\n${colourC("hsm")} ${colourL(commands[0] + " set")} ${colourB("<key> <value>")}\n Assign a value to the config file\n${colourC("hsm")} ${colourL(commands[0] + " delete")} ${colourB("<key>")}\n Remove a key and value from the config file`
57
- )
58
- )
59
- }
60
- break
61
- case "push":
62
- console.log(
63
- colourS(
64
- `\n${colourJ(pushCommandDescription)}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<directory> [<script user>.<script name>...]")}\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n ${mangleNamesOptionDescription}\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}`
65
- )
66
- )
67
- break
68
- case "dev":
69
- case "watch":
70
- console.log(
71
- colourS(
72
- `\n${colourJ("Watch a directory and push a script when modified")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<directory> [<script user>.<script name>...]")}\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n ${mangleNamesOptionDescription}\n${colourN("--type-declaration-path")}=${colourB("<path>")}\n Path to generate a type declaration file for the scripts\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}`
73
- )
74
- )
75
- break
76
- case "pull":
77
- console.log(
78
- colourS(
79
- `\n${colourJ("Pull a script a from a hackmud user's script directory")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<script user>")}${colourV(".")}${colourB("<script name>")}`
80
- )
81
- )
82
- break
83
- case "minify":
84
- case "golf":
85
- console.log(
86
- colourS(
87
- `\n${colourJ("Minify a script file on the spot")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<target> [output path]")}\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n ${mangleNamesOptionDescription}\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}\n${colourN("--watch")}\n Watch for changes`
88
- )
89
- )
90
- break
91
- case "generate-type-declaration":
92
- case "gen-type-declaration":
93
- case "gen-dts":
94
- case "gen-types":
95
- console.log(
96
- colourS(
97
- `${colourJ("Generate a type declaration file for a directory of scripts")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<directory> [output path]")}`
98
- )
99
- )
100
- break
101
- case "sync-macros":
102
- console.log("\n" + colourJ("Sync macros across all hackmud users"))
103
- break
104
- default:
105
- console.log(
106
- colourS(
107
- `\n${colourJ("Hackmud Script Manager")}\n\n${colourA("Commands:")}\n${colourL("push")}\n ${pushCommandDescription}\n${colourL("dev")}\n Watch a directory and push a script when modified\n${colourL("golf")}\n Minify a script file on the spot\n${colourL("gen-dts")}\n Generate a type declaration file for a directory of scripts\n${colourL("sync-macros")}\n Sync macros across all hackmud users\n${colourL("config")}\n Modify and view the config file\n${colourL("pull")}\n Pull a script a from a hackmud user's script directory`
108
- )
109
- )
110
- }
111
- },
112
- exploreObject = (object, keys, createPath = !1) => {
113
- for (const key of keys)
114
- object =
115
- createPath ?
116
- "object" == typeof object[key] ?
117
- object[key]
118
- : (object[key] = {})
119
- : object?.[key]
120
- return object
121
- },
122
- logInfo = ({ file, users, minLength, error }, hackmudPath) => {
123
- error ?
124
- logError(`error "${chalk.bold(error.message)}" in ${chalk.bold(file)}`)
125
- : console.log(
126
- `pushed ${chalk.bold(file)} to ${users.map(user => chalk.bold(userColours.get(user))).join(", ")} | ${chalk.bold(minLength + "")} chars | ${chalk.bold(resolve(hackmudPath, users[0], "scripts", basename(file, extname(file))) + ".js")}`
127
- )
128
- },
129
- log = message => {
130
- console.log(colourS(message))
131
- },
132
- logError = message => {
133
- console.error(colourD(message))
134
- process.exitCode = 1
135
- }
23
+ log = message => console.log(colourS(message))
136
24
  for (const argument of process.argv.slice(2))
137
25
  if ("-" == argument[0]) {
138
26
  const [key, valueRaw] = argument.split("=")
@@ -149,35 +37,10 @@ for (const argument of process.argv.slice(2))
149
37
  else for (const option of key.slice(1)) options.set(option, value)
150
38
  } else commands.push(argument)
151
39
  if ("v" == commands[0] || "version" == commands[0] || options.get("version") || options.get("v")) {
152
- console.log("0.19.1-5bceac8")
40
+ console.log(version)
153
41
  process.exit()
154
42
  }
155
- let configDidNotExist = !1
156
- const configPromise = readFile(configFilePath, { encoding: "utf-8" }).then(
157
- configFile => {
158
- let temporaryConfig
159
- try {
160
- temporaryConfig = JSON.parse(configFile)
161
- } catch {
162
- log("Config file was corrupted, resetting")
163
- return {}
164
- }
165
- if (!temporaryConfig || "object" != typeof temporaryConfig) {
166
- log("Config file was corrupted, resetting")
167
- return {}
168
- }
169
- if ("hackmudPath" in temporaryConfig && "string" != typeof temporaryConfig.hackmudPath) {
170
- log('Property "hackmudPath" of config file was corrupted, removing')
171
- delete temporaryConfig.hackmudPath
172
- }
173
- return temporaryConfig
174
- },
175
- () => {
176
- configDidNotExist = !0
177
- return {}
178
- }
179
- ),
180
- pushModule = import("../push.js"),
43
+ const pushModule = import("../push.js"),
181
44
  processScriptModule = import("../processScript/index.js"),
182
45
  watchModule = import("../watch.js"),
183
46
  chokidarModule = import("chokidar"),
@@ -202,12 +65,8 @@ let autoExit = !0
202
65
  switch (commands[0]) {
203
66
  case "push":
204
67
  {
205
- const { hackmudPath } = await configPromise
206
- if (!hackmudPath) {
207
- logNeedHackmudPathMessage()
208
- break
209
- }
210
- const sourcePath = commands[1]
68
+ const hackmudPath = getHackmudPath(),
69
+ sourcePath = commands[1]
211
70
  if (!sourcePath) {
212
71
  logError("Must provide the directory to push from\n")
213
72
  logHelp()
@@ -275,12 +134,8 @@ switch (commands[0]) {
275
134
  case "dev":
276
135
  case "watch":
277
136
  {
278
- const { hackmudPath } = await configPromise
279
- if (!hackmudPath) {
280
- logNeedHackmudPathMessage()
281
- break
282
- }
283
- const sourcePath = commands[1]
137
+ const hackmudPath = getHackmudPath(),
138
+ sourcePath = commands[1]
284
139
  if (!sourcePath) {
285
140
  logError("Must provide the directory to watch\n")
286
141
  logHelp()
@@ -353,34 +208,24 @@ switch (commands[0]) {
353
208
  break
354
209
  case "pull":
355
210
  {
356
- const { hackmudPath } = await configPromise
357
- if (!hackmudPath) {
358
- logNeedHackmudPathMessage()
359
- break
360
- }
361
- const script = commands[1]
211
+ const hackmudPath = getHackmudPath(),
212
+ script = commands[1]
362
213
  if (!script) {
363
214
  logError("Must provide the script to pull\n")
364
215
  logHelp()
365
216
  break
366
217
  }
367
218
  const sourcePath = commands[2] || "."
368
- try {
369
- await pull(sourcePath, hackmudPath, script)
370
- } catch (error) {
219
+ await pull(sourcePath, hackmudPath, script).catch(error => {
371
220
  console.error(error)
372
221
  logError(`Something went wrong, did you forget to ${colourC("#down")} the script?`)
373
- }
222
+ })
374
223
  }
375
224
  break
376
225
  case "sync-macros":
377
226
  {
378
- const { hackmudPath } = await configPromise
379
- if (!hackmudPath) {
380
- logNeedHackmudPathMessage()
381
- break
382
- }
383
- const { macrosSynced, usersSynced } = await syncMacros(hackmudPath)
227
+ const hackmudPath = getHackmudPath(),
228
+ { macrosSynced, usersSynced } = await syncMacros(hackmudPath)
384
229
  log(`Synced ${macrosSynced} macros to ${usersSynced} users`)
385
230
  }
386
231
  break
@@ -397,100 +242,17 @@ switch (commands[0]) {
397
242
  }
398
243
  const sourcePath = resolve(target),
399
244
  outputPath = commands[2] || "./player.d.ts",
400
- typeDeclaration = await generateTypeDeclaration(sourcePath, (await configPromise).hackmudPath)
245
+ typeDeclaration = await generateTypeDeclaration(sourcePath, getHackmudPath())
401
246
  let typeDeclarationPath = resolve(outputPath)
402
- try {
403
- await writeFile(typeDeclarationPath, typeDeclaration)
404
- } catch (error) {
405
- assert(error instanceof Error)
247
+ await writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
248
+ assert(error instanceof Error, "src/bin/hsm.ts:327:35")
406
249
  if ("EISDIR" != error.code) throw error
407
250
  typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
408
- await writeFile(typeDeclarationPath, typeDeclaration)
409
- }
251
+ return writeFile(typeDeclarationPath, typeDeclaration)
252
+ })
410
253
  log("Wrote type declaration to " + chalk.bold(typeDeclarationPath))
411
254
  }
412
255
  break
413
- case "config":
414
- switch (commands[1]) {
415
- case "get":
416
- {
417
- const key = commands[2]
418
- key ? log(exploreObject(await configPromise, key.split("."))) : console.log(await configPromise)
419
- }
420
- break
421
- case "delete":
422
- {
423
- const key = commands[2]
424
- if (!key) {
425
- logError("Must provide a key to delete\n")
426
- logHelp()
427
- break
428
- }
429
- const keyParts = key.split("."),
430
- pathName = keyParts
431
- .map(name => (/^[a-z_$][\w$]*$/i.test(name) ? name : JSON.stringify(name)))
432
- .join("."),
433
- lastKey = keyParts.pop(),
434
- config = await configPromise
435
- delete exploreObject(config, keyParts)?.[lastKey]
436
- log(`Removed ${colourV(pathName)} from config file`)
437
- }
438
- break
439
- case "set":
440
- {
441
- const key = commands[2],
442
- value = commands[3]
443
- if (!key) {
444
- logError("Must provide a key and value\n")
445
- logHelp()
446
- break
447
- }
448
- const keys = key.split("."),
449
- pathName = keys
450
- .map(name => (/^[a-z_$][\w$]*$/i.test(name) ? name : JSON.stringify(name)))
451
- .join(".")
452
- if (!value) {
453
- logError(`Must provide a value for the key ${pathName}\n`)
454
- logHelp()
455
- break
456
- }
457
- const lastKey = keys.pop(),
458
- config = await configPromise
459
- if (keys.length || "hackmudPath" != lastKey) {
460
- let object = config
461
- for (const key of keys)
462
- if ("object" == typeof object[key]) object = object[key]
463
- else {
464
- object[key] = {}
465
- object = object[key]
466
- }
467
- object[lastKey] = value
468
- } else config.hackmudPath = resolve(value.startsWith("~/") ? homedir() + value.slice(1) : value)
469
- console.log(config)
470
- await (async config => {
471
- const json = JSON.stringify(config, void 0, "\t")
472
- configDidNotExist && log("Creating config file at " + configFilePath)
473
- await writeFile(configFilePath, json).catch(async error => {
474
- switch (error.code) {
475
- case "EISDIR":
476
- await rmdir(configFilePath)
477
- break
478
- case "ENOENT":
479
- await mkdir(configDirectoryPath)
480
- break
481
- default:
482
- throw error
483
- }
484
- await writeFile(configFilePath, json)
485
- })
486
- })(config)
487
- }
488
- break
489
- default:
490
- commands[1] && logError(`Unknown command: ${JSON.stringify(commands[1])}\n`)
491
- logHelp()
492
- }
493
- break
494
256
  case "help":
495
257
  case "h":
496
258
  logHelp()
@@ -554,36 +316,31 @@ switch (commands[0]) {
554
316
  : fileBaseName + ".js"
555
317
  )
556
318
  const golfFile = () =>
557
- readFile(target, { encoding: "utf-8" }).then(
558
- async source => {
559
- const timeStart = performance.now(),
560
- { script, warnings } = await processScript(source, {
561
- minify: !(options.get("no-minify") || options.get("skip-minify")),
562
- scriptUser,
563
- scriptName,
564
- filePath: target,
565
- mangleNames,
566
- forceQuineCheats
567
- }),
568
- timeTook = performance.now() - timeStart
569
- for (const { message, line } of warnings)
570
- log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(line + "")}`)
571
- await writeFilePersistent(outputPath, script)
572
- .catch(async error => {
573
- if (!commands[2] || "EISDIR" != error.code) throw error
574
- outputPath = resolve(outputPath, basename(target, fileExtension) + ".js")
575
- await writeFilePersistent(outputPath, script)
576
- })
577
- .then(
578
- () =>
579
- log(
580
- `Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${chalk.bold(relative(".", outputPath))} | took ${Math.round(100 * timeTook) / 100}ms`
581
- ),
582
- error => logError(error.message)
319
+ readFile(target, { encoding: "utf-8" }).then(async source => {
320
+ const timeStart = performance.now(),
321
+ { script, warnings } = await processScript(source, {
322
+ minify: !(options.get("no-minify") || options.get("skip-minify")),
323
+ scriptUser,
324
+ scriptName,
325
+ filePath: target,
326
+ mangleNames,
327
+ forceQuineCheats
328
+ }),
329
+ timeTook = performance.now() - timeStart
330
+ for (const { message, line } of warnings)
331
+ log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(line + "")}`)
332
+ await writeFilePersistent(outputPath, script)
333
+ .catch(error => {
334
+ if (!commands[2] || "EISDIR" != error.code) throw error
335
+ outputPath = resolve(outputPath, basename(target, fileExtension) + ".js")
336
+ return writeFilePersistent(outputPath, script)
337
+ })
338
+ .then(() =>
339
+ log(
340
+ `Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${chalk.bold(relative(".", outputPath))} | took ${Math.round(100 * timeTook) / 100}ms`
583
341
  )
584
- },
585
- error => logError(error.message)
586
- )
342
+ )
343
+ })
587
344
  if (options.get("watch")) {
588
345
  const { watch: watchFile } = await chokidarModule
589
346
  watchFile(target, { awaitWriteFinish: { stabilityThreshold: 100 } })
@@ -594,7 +351,86 @@ switch (commands[0]) {
594
351
  }
595
352
  break
596
353
  default:
597
- commands[0] && logError(`Unknown command: ${JSON.stringify(commands[0])}\n`)
354
+ commands[0] && logError(`Unknown command: ${colourL(commands[0])}\n`)
598
355
  logHelp()
599
356
  }
600
357
  autoExit && process.exit()
358
+ function logHelp() {
359
+ const pushCommandDescription = "Push scripts from a directory to hackmud user's scripts directories",
360
+ forceQuineCheatsOptionDescription = `Force quine cheats on. Use ${colourN("--force-quine-cheats")}=${colourV("false")} to force off`,
361
+ hackmudPathOption = `${colourN("--hackmud-path")}=${colourB("<path>")}\n Override hackmud path`
362
+ console.log(colourN("Version") + colourS(": ") + colourV(version))
363
+ switch (commands[0]) {
364
+ case "dev":
365
+ case "watch":
366
+ case "push":
367
+ console.log(
368
+ colourS(
369
+ `\n${colourJ("push" == commands[0] ? pushCommandDescription : "Watch a directory and push a script when modified")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB('<directory> ["<script user>.<script name>"...]')}\n\n${colourA("Arguments:")}\n${colourB("<directory>")}\n The source directory containing your scripts\n${colourB("<script user>")}\n A user to push script(s) to. Can be set to wild card (${colourV("*")}) which will try\n and discover users to push to\n${colourB("<script name>")}\n Name of a script to push. Can be set to wild card (${colourV("*")}) to find all scripts\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n Reduce character count further but lose function names in error call stacks\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}\n${hackmudPathOption}\n${"push" == commands[0] ? "" : `${colourN("--type-declaration-path")}=${colourB("<path>")}\n Path to generate a type declaration file for the scripts\n`}\n${colourA("Examples:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")}\n\tPushes all scripts found in ${colourV("src")} folder to all users\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("foo")}${colourV(".")}${colourL("bar")}\n Pushes a script named ${colourL("bar")} found in ${colourV("src")} folder to user ${userColours.get("foo")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("foo")}${colourV(".")}${colourL("bar")} ${colourC("baz")}${colourV(".")}${colourL("qux")}\n Multiple can be specified\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("foo")}${colourV(".")}${colourL("*")}\n\tPushes all scripts found in ${colourV("src")} folder to user ${userColours.get("foo")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("*")}${colourV(".")}${colourL("foo")}\n\tPushes all scripts named ${colourL("foo")} found in ${colourV("src")} folder to all user\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("*")}${colourV(".")}${colourL("*")}\n\tPushes all scripts found in ${colourV("src")} folder to all users`
370
+ )
371
+ )
372
+ break
373
+ case "pull":
374
+ console.log(
375
+ colourS(
376
+ `\n${colourJ("Pull a script a from a hackmud user's script directory")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<script user>")}${colourV(".")}${colourB("<script name>")}\n\n${colourA("Options:")}\n${hackmudPathOption}`
377
+ )
378
+ )
379
+ break
380
+ case "minify":
381
+ case "golf":
382
+ console.log(
383
+ colourS(
384
+ `\n${colourJ("Minify a script file on the spot")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<target> [output path]")}\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n Reduce character count further but lose function names in error call stacks\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}\n${colourN("--watch")}\n Watch for changes`
385
+ )
386
+ )
387
+ break
388
+ case "generate-type-declaration":
389
+ case "gen-type-declaration":
390
+ case "gen-dts":
391
+ case "gen-types":
392
+ console.log(
393
+ colourS(
394
+ `${colourJ("Generate a type declaration file for a directory of scripts")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB("<directory> [output path]")}\n\n${colourA("Options:")}\n${hackmudPathOption}`
395
+ )
396
+ )
397
+ break
398
+ case "sync-macros":
399
+ console.log(
400
+ colourS(
401
+ `\n${colourJ("Sync macros across all hackmud users")}\n\n${colourA("Options:")}\n${hackmudPathOption}`
402
+ )
403
+ )
404
+ break
405
+ default:
406
+ console.log(
407
+ colourS(
408
+ `\n${colourJ("Hackmud Script Manager")}\n\n${colourA("Commands:")}\n${colourL("push")}\n ${pushCommandDescription}\n${colourL("dev")}\n Watch a directory and push a script when modified\n${colourL("golf")}\n Minify a script file on the spot\n${colourL("gen-dts")}\n Generate a type declaration file for a directory of scripts\n${colourL("sync-macros")}\n Sync macros across all hackmud users\n${colourL("pull")}\n Pull a script a from a hackmud user's script directory`
409
+ )
410
+ )
411
+ }
412
+ }
413
+ function logInfo({ file, users, minLength, error }, hackmudPath) {
414
+ error ?
415
+ logError(`error "${chalk.bold(error.message)}" in ${chalk.bold(file)}`)
416
+ : console.log(
417
+ `pushed ${chalk.bold(file)} to ${users.map(user => chalk.bold(userColours.get(user))).join(", ")} | ${chalk.bold(minLength + "")} chars | ${chalk.bold(resolve(hackmudPath, users[0], "scripts", basename(file, extname(file))) + ".js")}`
418
+ )
419
+ }
420
+ function logError(message) {
421
+ console.error(colourD(message))
422
+ process.exitCode = 1
423
+ }
424
+ function getHackmudPath() {
425
+ const hackmudPathOption = options.get("hackmud-path")
426
+ if (null != hackmudPathOption && "string" != typeof hackmudPathOption) {
427
+ logError(`Option ${colourN("--hackmud-path")} must be a string, got ${colourV(hackmudPathOption)}\n`)
428
+ logHelp()
429
+ process.exit(1)
430
+ }
431
+ return (
432
+ hackmudPathOption ||
433
+ process.env.HSM_HACKMUD_PATH ||
434
+ ("win32" == process.platform ? resolve(process.env.APPDATA, "hackmud") : resolve(homedir(), ".config/hackmud"))
435
+ )
436
+ }
@@ -1,31 +1,31 @@
1
- import { readdir } from "fs/promises"
1
+ import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
2
2
  import { basename, resolve } from "path"
3
- const generateTypeDeclaration = async (sourceDirectory, hackmudPath) => {
3
+ async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
4
4
  const users = new Set()
5
5
  if (hackmudPath)
6
- for (const dirent of await readdir(hackmudPath, { withFileTypes: !0 }))
7
- dirent.isFile() && dirent.name.endsWith(".key") && users.add(basename(dirent.name, ".key"))
6
+ for (const { stats, name } of await readDirectoryWithStats(hackmudPath))
7
+ stats.isFile() && name.endsWith(".key") && users.add(basename(name, ".key"))
8
8
  const wildScripts = [],
9
9
  wildAnyScripts = [],
10
10
  allScripts = {},
11
11
  allAnyScripts = {}
12
12
  await Promise.all(
13
- (await readdir(sourceDirectory, { withFileTypes: !0 })).map(async dirent => {
14
- if (dirent.isFile())
15
- dirent.name.endsWith(".ts") ?
16
- dirent.name.endsWith(".d.ts") || wildScripts.push(basename(dirent.name, ".ts"))
17
- : dirent.name.endsWith(".js") && wildAnyScripts.push(basename(dirent.name, ".js"))
18
- else if (dirent.isDirectory()) {
13
+ (await readDirectoryWithStats(sourceDirectory)).map(async ({ stats, name }) => {
14
+ if (stats.isFile())
15
+ name.endsWith(".ts") ?
16
+ name.endsWith(".d.ts") || wildScripts.push(basename(name, ".ts"))
17
+ : name.endsWith(".js") && wildAnyScripts.push(basename(name, ".js"))
18
+ else if (stats.isDirectory()) {
19
19
  const scripts = [],
20
20
  anyScripts = []
21
- allScripts[dirent.name] = scripts
22
- allAnyScripts[dirent.name] = anyScripts
23
- users.add(dirent.name)
24
- for (const file of await readdir(resolve(sourceDirectory, dirent.name), { withFileTypes: !0 }))
25
- file.isFile() &&
26
- (file.name.endsWith(".ts") ?
27
- dirent.name.endsWith(".d.ts") || scripts.push(basename(file.name, ".ts"))
28
- : file.name.endsWith(".js") && anyScripts.push(basename(file.name, ".js")))
21
+ allScripts[name] = scripts
22
+ allAnyScripts[name] = anyScripts
23
+ users.add(name)
24
+ for (const child of await readDirectoryWithStats(resolve(sourceDirectory, name)))
25
+ child.stats.isFile() &&
26
+ (child.name.endsWith(".ts") ?
27
+ name.endsWith(".d.ts") || scripts.push(basename(child.name, ".ts"))
28
+ : child.name.endsWith(".js") && anyScripts.push(basename(child.name, ".js")))
29
29
  }
30
30
  })
31
31
  )
@@ -65,4 +65,4 @@ const generateTypeDeclaration = async (sourceDirectory, hackmudPath) => {
65
65
  o += "}\n"
66
66
  return o
67
67
  }
68
- export { generateTypeDeclaration as default, generateTypeDeclaration }
68
+ export { generateTypeDeclaration }
package/index.js CHANGED
@@ -5,7 +5,7 @@ export { pull } from "./pull.js"
5
5
  export { push } from "./push.js"
6
6
  export { syncMacros } from "./syncMacros.js"
7
7
  export { watch } from "./watch.js"
8
- import "fs/promises"
8
+ import "@samual/lib/readDirectoryWithStats"
9
9
  import "path"
10
10
  import "@babel/generator"
11
11
  import "@babel/parser"
@@ -47,4 +47,5 @@ import "@samual/lib/clearObject"
47
47
  import "@samual/lib/copyFilePersistent"
48
48
  import "@samual/lib/Cache"
49
49
  import "@samual/lib/writeFilePersistent"
50
+ import "fs/promises"
50
51
  import "chokidar"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hackmud-script-manager",
3
- "version": "0.19.1-5bceac8",
3
+ "version": "0.19.1-7a27b63",
4
4
  "description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
5
5
  "keywords": [
6
6
  "api",
@@ -59,7 +59,7 @@
59
59
  "@rollup/plugin-commonjs": "^25.0.7",
60
60
  "@rollup/plugin-json": "^6.1.0",
61
61
  "@rollup/plugin-node-resolve": "^15.2.3",
62
- "@samual/lib": "^0.10.1",
62
+ "@samual/lib": "0.10.2-e64c5bc",
63
63
  "acorn": "^8.11.3",
64
64
  "chalk": "^5.3.0",
65
65
  "chokidar": "^3.6.0",
@@ -69,17 +69,16 @@
69
69
  "rollup": "^4.14.2",
70
70
  "terser": "^5.30.3"
71
71
  },
72
- "engines": {
73
- "node": "^18 || >=20",
74
- "pnpm": "^9.0.1"
75
- },
76
72
  "type": "module",
77
73
  "exports": {
74
+ ".": "./index.js",
78
75
  "./*": "./*.js",
79
76
  "./*.js": "./*.js"
80
77
  },
81
78
  "bin": {
82
- "hsm.d": "bin/hsm.d.ts",
83
79
  "hsm": "bin/hsm.js"
80
+ },
81
+ "engines": {
82
+ "node": "^18 || >=20"
84
83
  }
85
84
  }