hackmud-script-manager-ng 0.0.0-stage → 0.26.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.envrc +1 -0
  2. package/.github/FUNDING.yml +14 -0
  3. package/.github/workflows/publish.yml +27 -0
  4. package/.github/workflows/test.yml +18 -0
  5. package/.vscode/settings.json +55 -0
  6. package/CONTRIBUTING.md +68 -0
  7. package/LICENSE +21 -0
  8. package/README.md +86 -2
  9. package/env.d.ts +1305 -0
  10. package/eslint.config.js +81 -0
  11. package/game-scripts-tests/array-rest-works.ts +6 -0
  12. package/game-scripts-tests/this_array_in_object.ts +16 -0
  13. package/game-scripts-tests/this_function_array.ts +9 -0
  14. package/game-scripts-tests/this_in_default_argument.ts +35 -0
  15. package/game-scripts-tests/this_in_lambda_in_object.ts +26 -0
  16. package/package.json +113 -3
  17. package/pnpm-workspace.yaml +2 -0
  18. package/rollup.config.js +69 -0
  19. package/scripts/dev.sh +3 -0
  20. package/scripts/emit-declarations.sh +3 -0
  21. package/scripts/emit-package-json.js +9 -0
  22. package/scripts/lint.sh +5 -0
  23. package/scripts/package.sh +7 -0
  24. package/scripts/update.sh +4 -0
  25. package/scripts/version-dev.js +14 -0
  26. package/src/bin/hsm.ts +754 -0
  27. package/src/constants.ts +2 -0
  28. package/src/env.d.ts +13 -0
  29. package/src/generateTypeDeclaration.ts +119 -0
  30. package/src/index.ts +13 -0
  31. package/src/processScript/__snapshots__/index.ts.snap +8 -0
  32. package/src/processScript/index.ts +474 -0
  33. package/src/processScript/minify.ts +594 -0
  34. package/src/processScript/postprocess.ts +19 -0
  35. package/src/processScript/preprocess.ts +130 -0
  36. package/src/processScript/shared.ts +33 -0
  37. package/src/processScript/transform.ts +978 -0
  38. package/src/pull.ts +20 -0
  39. package/src/push.ts +200 -0
  40. package/src/syncMacros.ts +53 -0
  41. package/src/tsconfig.json +14 -0
  42. package/src/watch.ts +258 -0
  43. package/tsconfig.json +23 -0
  44. package/vitest.config.ts +19 -0
package/src/bin/hsm.ts ADDED
@@ -0,0 +1,754 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+ import type { Replace } from "@samual/lib"
4
+ import type { Info } from ".."
5
+ import { readFile, writeFile } from "fs/promises"
6
+ import { homedir as getHomeDirectory } from "os"
7
+ import {
8
+ basename as getPathBaseName, dirname as getPathDirectory, extname as getPathFileExtension,
9
+ relative as getRelativePath, resolve as resolvePath
10
+ } from "path"
11
+ import { assert } from "@samual/lib/assert"
12
+ import { AutoMap } from "@samual/lib/AutoMap"
13
+ import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
14
+ import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
15
+ import { version as moduleVersion } from "../../package.json"
16
+ import { supportedExtensions } from "../constants"
17
+ import { generateTypeDeclaration } from "../generateTypeDeclaration"
18
+ import { pull } from "../pull"
19
+ import { syncMacros } from "../syncMacros"
20
+
21
+ type OptionValue = boolean | string
22
+
23
+ const options = new Map<string, OptionValue>
24
+ const commands: string[] = []
25
+ let didParseOption = false
26
+ let didParseOptionAfterCommand = false
27
+
28
+ for (const argument of process.argv.slice(2)) {
29
+ if (argument[0] == `-`) {
30
+ const argumentEqualsIndex = argument.indexOf(`=`)
31
+ let key
32
+ let value
33
+
34
+ if (argumentEqualsIndex == -1) {
35
+ key = argument
36
+ value = true
37
+ } else {
38
+ key = argument.slice(0, argumentEqualsIndex)
39
+ value = argument.slice(argumentEqualsIndex + 1)
40
+
41
+ if (value == `true`)
42
+ value = true
43
+ else if (value == `false`)
44
+ value = false
45
+ }
46
+
47
+ if (argument[1] == `-`)
48
+ options.set(key!.slice(2), value)
49
+ else {
50
+ for (const option of key!.slice(1))
51
+ options.set(option, value)
52
+ }
53
+
54
+ didParseOption = true
55
+ } else {
56
+ if (didParseOption) {
57
+ didParseOptionAfterCommand = true
58
+ }
59
+
60
+ commands.push(argument)
61
+ }
62
+ }
63
+
64
+ const pushModule = import(`../push`)
65
+ const processScriptModule = import(`../processScript`)
66
+ const watchModule = import(`../watch`)
67
+ const chokidarModule = import(`chokidar`)
68
+ const { default: chalk } = await import(`chalk`)
69
+ const colourA = chalk.rgb(0xFF, 0xFF, 0xFF)
70
+ const colourB = chalk.rgb(0xCA, 0xCA, 0xCA)
71
+ const colourC = chalk.rgb(0x9B, 0x9B, 0x9B)
72
+ const colourD = chalk.rgb(0xFF, 0x00, 0x00)
73
+ const colourF = chalk.rgb(0xFF, 0x80, 0x00)
74
+ const colourJ = chalk.rgb(0xFF, 0xF4, 0x04)
75
+ const colourK = chalk.rgb(0xF3, 0xF9, 0x98)
76
+ const colourL = chalk.rgb(0x1E, 0xFF, 0x00)
77
+ const colourM = chalk.rgb(0xB3, 0xFF, 0x9B)
78
+ const colourN = chalk.rgb(0x00, 0xFF, 0xFF)
79
+ const colourS = chalk.rgb(0x7A, 0xB2, 0xF4)
80
+ const colourV = chalk.rgb(0xFF, 0x00, 0xEC)
81
+ const colourW = chalk.rgb(0xFF, 0x96, 0xE0)
82
+ const formatOption = (name: string) => colourN(`-${name.length == 1 ? `` : `-`}${name}`)
83
+
84
+ const userColours = new AutoMap<string, string>(user => {
85
+ let hash = 0
86
+
87
+ for (const char of user)
88
+ hash += (hash >> 1) + hash + `xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj`.indexOf(char) + 1
89
+
90
+ return [ colourJ, colourK, colourM, colourW, colourL, colourB ][hash % 6]!(user)
91
+ })
92
+
93
+ const log = (message: string) => console.log(colourS(message))
94
+
95
+ if (didParseOptionAfterCommand) {
96
+ process.exitCode = 1
97
+
98
+ console.warn(colourF(`\
99
+ ${chalk.bold(`Warning:`)} Options should come after commands when calling the script.
100
+ This warning will become an error in the next minor version of HSM.`
101
+ ))
102
+ }
103
+
104
+ if (process.version.startsWith(`v21.`)) {
105
+ process.exitCode = 1
106
+
107
+ console.warn(colourF(`\
108
+ ${chalk.bold(`Warning:`)} Support for Node.js 21 will be dropped in the next minor version of HSM
109
+ Your current version of Node.js is ${chalk.bold(process.version)}
110
+ You should update your version of Node.js
111
+ https://nodejs.org/en/download/package-manager\n`
112
+ ))
113
+ }
114
+
115
+ if (commands[0] == `v` || commands[0] == `version` || popOption(`version`, `v`)?.value) {
116
+ console.log(moduleVersion)
117
+ process.exit()
118
+ }
119
+
120
+ let warnedDeprecatedEmitDtsAlias = false
121
+
122
+ if (popOption(`help`, `h`)?.value) {
123
+ logHelp()
124
+ process.exit()
125
+ }
126
+
127
+ let autoExit = true
128
+
129
+ switch (commands[0]) {
130
+ case `push`:
131
+ case `dev`:
132
+ case `watch`:
133
+ case `golf`:
134
+ case `minify`: {
135
+ const noMinifyOption = popOption(`no-minify`, `skip-minify`)
136
+
137
+ if (noMinifyOption && noMinifyOption.name != `no-minify`) {
138
+ process.exitCode = 1
139
+
140
+ console.warn(colourF(`\
141
+ ${chalk.bold(`Warning:`)} ${formatOption(noMinifyOption.name)} is deprecated and will be removed in the next minor
142
+ release of HSM
143
+ You should switch to using its alias ${colourN(`--no-minify`)}\n`
144
+ ))
145
+ }
146
+
147
+ const mangleNamesOption = popOption(`mangle-names`)
148
+ const forceQuineCheatsOption = popOption(`force-quine-cheats`)
149
+ const noQuineCheatsOptions = popOption(`no-quine-cheats`)
150
+ const noMinifyIncompatibleOption = mangleNamesOption || forceQuineCheatsOption || noQuineCheatsOptions
151
+
152
+ if (noMinifyOption && noMinifyIncompatibleOption) {
153
+ logError(
154
+ `Options ${formatOption(noMinifyOption.name)} and ${formatOption(noMinifyIncompatibleOption.name)
155
+ } are incompatible\n`
156
+ )
157
+
158
+ logHelp()
159
+ process.exit(1)
160
+ }
161
+
162
+ if (forceQuineCheatsOption && noQuineCheatsOptions) {
163
+ logError(
164
+ `Options ${formatOption(forceQuineCheatsOption.name)} and ${formatOption(noQuineCheatsOptions.name)
165
+ } are incompatible\n`
166
+ )
167
+
168
+ logHelp()
169
+ process.exit(1)
170
+ }
171
+
172
+ if (noMinifyOption)
173
+ assertOptionIsBoolean(noMinifyOption)
174
+
175
+ if (mangleNamesOption)
176
+ assertOptionIsBoolean(mangleNamesOption)
177
+
178
+ if (forceQuineCheatsOption)
179
+ assertOptionIsBoolean(forceQuineCheatsOption)
180
+
181
+ if (noQuineCheatsOptions)
182
+ assertOptionIsBoolean(noQuineCheatsOptions)
183
+
184
+ const rootFolderPathOption = popOption(`root-folder-path`)
185
+ const rootFolderPath = rootFolderPathOption && resolvePath(String(rootFolderPathOption.value))
186
+
187
+ if (commands[0] == `golf` || commands[0] == `minify`) {
188
+ const watchOption = popOption(`watch`)
189
+ const target = commands[1]
190
+
191
+ if (!target) {
192
+ logError(`Must provide target\n`)
193
+ logHelp()
194
+ process.exit(1)
195
+ }
196
+
197
+ const fileExtension = getPathFileExtension(target)
198
+
199
+ if (!supportedExtensions.includes(fileExtension)) {
200
+ logError(`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${
201
+ supportedExtensions.map(extension => chalk.bold(extension)).join(`", "`)
202
+ }"`)
203
+
204
+ process.exit(1)
205
+ }
206
+
207
+ complainAboutUnrecognisedOptions()
208
+
209
+ const { processScript } = await processScriptModule
210
+ const fileBaseName = getPathBaseName(target, fileExtension)
211
+ const fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(`.src`)
212
+ const scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName
213
+
214
+ const scriptUser = (
215
+ getPathBaseName(resolvePath(target, `..`)) == `scripts` &&
216
+ getPathBaseName(resolvePath(target, `../../..`)) == `hackmud`
217
+ ) ? getPathBaseName(resolvePath(target, `../..`)) : undefined
218
+
219
+ let outputPath = commands[2] || resolvePath(
220
+ getPathDirectory(target),
221
+ fileBaseNameEndsWithDotSrc ?
222
+ `${scriptName}.js`
223
+ : fileExtension == `.js` ?
224
+ `${fileBaseName}.min.js`
225
+ : `${fileBaseName}.js`
226
+ )
227
+
228
+ const golfFile = () => readFile(target, { encoding: `utf8` }).then(async source => {
229
+ const timeStart = performance.now()
230
+
231
+ const { script, warnings } = await processScript(source, {
232
+ minify: noMinifyOption && !noMinifyOption.value,
233
+ scriptUser,
234
+ scriptName,
235
+ filePath: target,
236
+ mangleNames: mangleNamesOption?.value,
237
+ forceQuineCheats: forceQuineCheatsOption?.value ?? !noQuineCheatsOptions?.value,
238
+ rootFolderPath
239
+ })
240
+
241
+ const timeTook = performance.now() - timeStart
242
+
243
+ if (warnings.length)
244
+ process.exitCode = 1
245
+
246
+ for (const { message } of warnings)
247
+ console.warn(colourF(`${chalk.bold(`Warning:`)} ${message}`))
248
+
249
+ await writeFilePersistent(outputPath, script).catch((error: NodeJS.ErrnoException) => {
250
+ if (!commands[2] || error.code != `EISDIR`)
251
+ throw error
252
+
253
+ outputPath = resolvePath(outputPath, `${getPathBaseName(target, fileExtension)}.js`)
254
+
255
+ return writeFilePersistent(outputPath, script)
256
+ }).then(() => log(`Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${
257
+ chalk.bold(getRelativePath(`.`, outputPath))
258
+ } | took ${Math.round(timeTook * 100) / 100}ms`))
259
+ })
260
+
261
+ if (watchOption) {
262
+ const { watch: watchFile } = await chokidarModule
263
+
264
+ watchFile(target, { awaitWriteFinish: { stabilityThreshold: 100 } })
265
+ .on(`ready`, () => log(`Watching ${target}`))
266
+ .on(`change`, golfFile)
267
+
268
+ autoExit = false
269
+ } else
270
+ await golfFile()
271
+ } else {
272
+ const hackmudPath = getHackmudPath()
273
+ const sourcePath = commands[1]
274
+
275
+ if (!sourcePath) {
276
+ logError(`Must provide the directory to ${commands[0] == `push` ? `push from` : `watch`}\n`)
277
+ logHelp()
278
+ process.exit(1)
279
+ }
280
+
281
+ const scripts = commands.slice(2)
282
+
283
+ if (scripts.length) {
284
+ const invalidScript = scripts
285
+ .find(script => !/^(?:[a-z_][a-z\d_]{0,24}|\*)\.(?:[a-z_][a-z\d_]{0,24}|\*)$/.test(script))
286
+
287
+ if (invalidScript) {
288
+ logError(`Invalid script name: ${JSON.stringify(invalidScript)}\n`)
289
+ logHelp()
290
+ process.exit(1)
291
+ }
292
+ } else
293
+ scripts.push(`*.*`)
294
+
295
+ const watchOption = popOption(`watch`)
296
+
297
+ if (commands[0] == `push` && !watchOption?.value) {
298
+ const dtsPathOption = popOption(`dts-path`)
299
+
300
+ complainAboutUnrecognisedOptions()
301
+
302
+ let declarationPathPromise
303
+
304
+ if (dtsPathOption) {
305
+ if (typeof dtsPathOption.value != `string`) {
306
+ logError(
307
+ `Option ${formatOption(dtsPathOption.name)} must be a string, got ${colourV(dtsPathOption.value)}\n`
308
+ )
309
+
310
+ logHelp()
311
+ process.exit(1)
312
+ }
313
+
314
+ let typeDeclarationPath = resolvePath(dtsPathOption.value)
315
+ const typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
316
+
317
+ declarationPathPromise = writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
318
+ assert(error instanceof Error, HERE)
319
+
320
+ if ((error as NodeJS.ErrnoException).code != `EISDIR`)
321
+ throw error
322
+
323
+ typeDeclarationPath = resolvePath(typeDeclarationPath, `player.d.ts`)
324
+
325
+ return writeFile(typeDeclarationPath, typeDeclaration)
326
+ }).then(() => typeDeclarationPath)
327
+ }
328
+
329
+ const { push, MissingSourceFolderError, MissingHackmudFolderError, NoUsersError } = await pushModule
330
+
331
+ const infos = await push(sourcePath, hackmudPath, {
332
+ scripts,
333
+ onPush: info => logInfo(info, hackmudPath),
334
+ minify: noMinifyOption && !noMinifyOption.value,
335
+ mangleNames: mangleNamesOption?.value,
336
+ forceQuineCheats: forceQuineCheatsOption?.value ?? !noQuineCheatsOptions?.value,
337
+ rootFolderPath
338
+ })
339
+
340
+ if (infos instanceof Error) {
341
+ logError(infos.message)
342
+
343
+ if (infos instanceof MissingSourceFolderError || infos instanceof NoUsersError) {
344
+ console.log()
345
+ logHelp()
346
+ } else if (infos instanceof MissingHackmudFolderError) {
347
+ log(`\
348
+ If this is not where your hackmud folder is, you can specify it with the
349
+ ${colourN(`--hackmud-path`)}=${colourB(`<path>`)} option or ${colourN(`HSM_HACKMUD_PATH`)} environment variable`
350
+ )
351
+ }
352
+ } else if (!infos.length)
353
+ logError(`Could not find any scripts to push`)
354
+
355
+ if (declarationPathPromise)
356
+ log(`Wrote type declaration to ${chalk.bold(await declarationPathPromise)}`)
357
+ } else {
358
+ const dtsPathOption =
359
+ popOption(`dts-path`, `type-declaration-path`, `type-declaration`, `dts`, `gen-types`)
360
+
361
+ if (dtsPathOption && dtsPathOption.name != `dts-path` && dtsPathOption.name != `type-declaration-path`) {
362
+ process.exitCode = 1
363
+
364
+ console.warn(colourF(`\
365
+ ${chalk.bold(`Warning:`)} ${formatOption(dtsPathOption.name)} is deprecated and will be removed in the
366
+ next minor release of HSM
367
+ You should switch to using its alias ${colourN(`--dts-path`)}\n`
368
+ ))
369
+ }
370
+
371
+ complainAboutUnrecognisedOptions()
372
+
373
+ const { watch } = await watchModule
374
+
375
+ watch(sourcePath, hackmudPath, {
376
+ scripts,
377
+ onPush: info => logInfo(info, hackmudPath),
378
+ typeDeclarationPath: dtsPathOption?.value.toString(),
379
+ minify: noMinifyOption && !noMinifyOption.value,
380
+ mangleNames: mangleNamesOption?.value,
381
+ onReady: () => log(`Watching`),
382
+ forceQuineCheats: forceQuineCheatsOption?.value ?? !noQuineCheatsOptions?.value,
383
+ rootFolderPath
384
+ })
385
+
386
+ autoExit = false
387
+ }
388
+ }
389
+
390
+ break
391
+ }
392
+
393
+ case `pull`: {
394
+ const hackmudPath = getHackmudPath()
395
+ const script = commands[1]
396
+
397
+ if (!script) {
398
+ logError(`Must provide the script to pull\n`)
399
+ logHelp()
400
+ process.exit(1)
401
+ }
402
+
403
+ complainAboutUnrecognisedOptions()
404
+
405
+ const sourcePath = commands[2] || `.`
406
+
407
+ await pull(sourcePath, hackmudPath, script).catch(error => {
408
+ console.error(error)
409
+ logError(`Something went wrong, did you forget to ${colourC(`#down`)} the script?`)
410
+ })
411
+
412
+ break
413
+ }
414
+
415
+ case `sync-macros`: {
416
+ const hackmudPath = getHackmudPath()
417
+
418
+ complainAboutUnrecognisedOptions()
419
+
420
+ const { macrosSynced, usersSynced } = await syncMacros(hackmudPath)
421
+
422
+ log(`Synced ${macrosSynced} macros to ${usersSynced} users`)
423
+ break
424
+ }
425
+
426
+ case `generate-type-declaration`:
427
+ case `gen-type-declaration`:
428
+ case `gen-dts`:
429
+ case `gen-types`:
430
+ case `emit-dts`: {
431
+ if (commands[0] != `emit-dts` && commands[0] != `gen-dts`) {
432
+ warnedDeprecatedEmitDtsAlias = true
433
+ process.exitCode = 1
434
+
435
+ console.warn(colourF(`\
436
+ ${chalk.bold(`Warning:`)} ${colourC(`hsm`)} ${colourL(commands[0])} is deprecated and will be removed
437
+ in the next minor release of HSM
438
+ You should switch to using its alias ${colourC(`hsm`)} ${colourL(`emit-dts`)}\n`
439
+ ))
440
+ }
441
+
442
+ const hackmudPath = getHackmudPath()
443
+ const target = commands[1]
444
+
445
+ if (!target) {
446
+ logError(`Must provide target directory\n`)
447
+ logHelp()
448
+ process.exit(1)
449
+ }
450
+
451
+ complainAboutUnrecognisedOptions()
452
+
453
+ const sourcePath = resolvePath(target)
454
+ const outputPath = commands[2] || `./player.d.ts`
455
+
456
+ const typeDeclaration =
457
+ await generateTypeDeclaration(sourcePath, hackmudPath)
458
+
459
+ let typeDeclarationPath = resolvePath(outputPath)
460
+
461
+ await writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
462
+ assert(error instanceof Error, HERE)
463
+
464
+ if ((error as NodeJS.ErrnoException).code != `EISDIR`)
465
+ throw error
466
+
467
+ typeDeclarationPath = resolvePath(typeDeclarationPath, `player.d.ts`)
468
+
469
+ return writeFile(typeDeclarationPath, typeDeclaration)
470
+ })
471
+
472
+ log(`Wrote type declaration to ${chalk.bold(typeDeclarationPath)}`)
473
+ break
474
+ }
475
+
476
+ case `help`:
477
+ logHelp()
478
+ break
479
+
480
+ default: {
481
+ if (commands[0])
482
+ logError(`Unknown command: ${colourL(commands[0])}\n`)
483
+
484
+ logHelp()
485
+ }
486
+ }
487
+
488
+ if (autoExit)
489
+ process.exit()
490
+
491
+ function logHelp() {
492
+ const pushCommandDescription = `Push scripts from a directory to hackmud user's scripts directories`
493
+ const watchCommandDescription = `Watch a directory and push a script when modified`
494
+ const minifyCommandDescription = `Minify a script file on the spot`
495
+ const generateTypeDeclarationCommandDescription = `Generate a type declaration file for a directory of scripts`
496
+ const syncMacrosCommandDescription = `Sync macros across all hackmud users`
497
+ const pullCommandDescription = `Pull a script a from a hackmud user's script directory`
498
+ const noMinifyOptionDescription = `Skip minification to produce a "readable" script`
499
+ const mangleNamesOptionDescription = `Reduce character count further but lose function names in error call stacks`
500
+ const forceQuineCheatsOptionDescription = `Force quine cheats on or off`
501
+
502
+ const hackmudPathOption = `\
503
+ ${colourN(`--hackmud-path`)}=${colourB(`<path>`)}
504
+ Override hackmud path`
505
+
506
+ switch (commands[0]) {
507
+ case `dev`:
508
+ case `watch`:
509
+ case `push`:
510
+ console.log(colourS(`\
511
+ ${colourJ(commands[0] == `push` ? pushCommandDescription : watchCommandDescription)}
512
+
513
+ ${colourA(`Usage:`)}
514
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourB(`<directory> ["<script user>.<script name>"...]`)}
515
+
516
+ ${colourA(`Arguments:`)}
517
+ ${colourB(`<directory>`)}
518
+ The source directory containing your scripts
519
+ ${colourB(`<script user>`)}
520
+ A user to push script(s) to. Can be set to wild card (${colourV(`*`)}) which will try
521
+ and discover users to push to
522
+ ${colourB(`<script name>`)}
523
+ Name of a script to push. Can be set to wild card (${colourV(`*`)}) to find all scripts
524
+
525
+ ${colourA(`Options:`)}
526
+ ${colourN(`--no-minify`)}
527
+ ${noMinifyOptionDescription}
528
+ ${colourN(`--mangle-names`)}
529
+ ${mangleNamesOptionDescription}
530
+ ${colourN(`--force-quine-cheats`)}, ${colourN(`--no-quine-cheats`)}
531
+ ${forceQuineCheatsOptionDescription}
532
+ ${hackmudPathOption}
533
+ ${colourN(`--dts-path`)}=${colourB(`<path>`)}
534
+ Path to generate a type declaration (.d.ts) file for the scripts
535
+ ${colourN(`--watch`)}
536
+ Watch for changes
537
+ ${colourN(`--root-folder-path`)}
538
+ The folder that root will be aliased to in import statements
539
+
540
+ ${colourA(`Examples:`)}
541
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourV(`src`)}
542
+ Pushes all scripts found in ${colourV(`src`)} folder to all users
543
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourV(`src`)} ${colourC(`foo`)}${colourV(`.`)}${colourL(`bar`)}
544
+ Pushes a script named ${colourL(`bar`)} found in ${colourV(`src`)} folder to user ${userColours.get(`foo`)}
545
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourV(`src`)} ${colourC(`foo`)}${colourV(`.`)}${colourL(`bar`)} ${colourC(`baz`)}${colourV(`.`)}${colourL(`qux`)}
546
+ Multiple can be specified
547
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourV(`src`)} ${colourC(`foo`)}${colourV(`.`)}${colourL(`*`)}
548
+ Pushes all scripts found in ${colourV(`src`)} folder to user ${userColours.get(`foo`)}
549
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourV(`src`)} ${colourC(`*`)}${colourV(`.`)}${colourL(`foo`)}
550
+ Pushes all scripts named ${colourL(`foo`)} found in ${colourV(`src`)} folder to all user
551
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourV(`src`)} ${colourC(`*`)}${colourV(`.`)}${colourL(`*`)}
552
+ Pushes all scripts found in ${colourV(`src`)} folder to all users`
553
+ ))
554
+
555
+ break
556
+
557
+ case `pull`:
558
+ console.log(colourS(`\
559
+ ${colourJ(pullCommandDescription)}
560
+
561
+ ${colourA(`Usage:`)}
562
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourB(`<script user>`)}${colourV(`.`)}${colourB(`<script name>`)}
563
+
564
+ ${colourA(`Options:`)}
565
+ ${hackmudPathOption}`
566
+ ))
567
+
568
+ break
569
+
570
+ case `minify`:
571
+ case `golf`:
572
+ console.log(colourS(`\
573
+ ${colourJ(minifyCommandDescription)}
574
+
575
+ ${colourA(`Usage:`)}
576
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourB(`<target> [output path]`)}
577
+
578
+ ${colourA(`Options:`)}
579
+ ${colourN(`--no-minify`)}
580
+ ${noMinifyOptionDescription}
581
+ ${colourN(`--mangle-names`)}
582
+ ${mangleNamesOptionDescription}
583
+ ${colourN(`--force-quine-cheats`)}, ${colourN(`--no-quine-cheats`)}
584
+ ${forceQuineCheatsOptionDescription}
585
+ ${colourN(`--watch`)}
586
+ Watch for changes
587
+ ${colourN(`--root-folder-path`)}
588
+ The folder that root will be aliased to in import statements`
589
+ ))
590
+
591
+ break
592
+
593
+ case `generate-type-declaration`:
594
+ case `gen-type-declaration`:
595
+ case `gen-dts`:
596
+ case `gen-types`:
597
+ case `emit-dts`:
598
+ if (!warnedDeprecatedEmitDtsAlias && commands[0] != `emit-dts` && commands[0] != `gen-dts`) {
599
+ process.exitCode = 1
600
+
601
+ console.warn(colourF(`\
602
+ ${chalk.bold(`Warning:`)} ${colourC(`hsm`)} ${colourL(commands[0])} is deprecated and will be removed
603
+ in the next minor release of HSM
604
+ You should switch to using its alias ${colourC(`hsm`)} ${colourL(`emit-dts`)}\n`
605
+ ))
606
+ }
607
+
608
+ console.log(colourS(`\
609
+ ${colourJ(generateTypeDeclarationCommandDescription)}
610
+
611
+ ${colourA(`Usage:`)}
612
+ ${colourC(`hsm`)} ${colourL(commands[0])} ${colourB(`<directory> [output path]`)}
613
+
614
+ ${colourA(`Options:`)}
615
+ ${hackmudPathOption}`
616
+ ))
617
+
618
+ break
619
+
620
+ case `sync-macros`:
621
+ console.log(colourS(`\
622
+ ${colourJ(syncMacrosCommandDescription)}
623
+
624
+ ${colourA(`Options:`)}
625
+ ${hackmudPathOption}`
626
+ ))
627
+
628
+ break
629
+
630
+ default: {
631
+ console.log(colourS(`\
632
+ ${colourJ(`Hackmud Script Manager`)}
633
+ ${colourN(`Version`) + colourS(`: `) + colourV(moduleVersion)}
634
+
635
+ ${colourA(`Commands:`)}
636
+ ${colourL(`push`)}
637
+ ${pushCommandDescription}
638
+ ${colourL(`minify`)}
639
+ ${minifyCommandDescription}
640
+ ${colourL(`emit-dts`)}
641
+ ${generateTypeDeclarationCommandDescription}
642
+ ${colourL(`sync-macros`)}
643
+ ${syncMacrosCommandDescription}
644
+ ${colourL(`pull`)}
645
+ ${pullCommandDescription}
646
+
647
+ ${colourA(`Options:`)}
648
+ ${colourN(`--help`)}
649
+ Can be used on any command e.g. ${colourC(`hsm`)} ${colourL(`push`)} ${colourN(`--help`)} to show helpful information`
650
+ ))
651
+ }
652
+ }
653
+ }
654
+
655
+ function logInfo({ path, users, characterCount, error, warnings }: Info, hackmudPath: string) {
656
+ path = getRelativePath(`.`, path)
657
+
658
+ if (error) {
659
+ logError(`Error "${chalk.bold(error.message)}" in ${chalk.bold(path)}`)
660
+
661
+ return
662
+ }
663
+
664
+ if (warnings.length)
665
+ process.exitCode = 1
666
+
667
+ for (const warning of warnings)
668
+ console.warn(colourF(`${chalk.bold(`Warning:`)} ${warning.message}`))
669
+
670
+ log(`Pushed ${chalk.bold(path)} to ${users.map(user => chalk.bold(userColours.get(user))).join(`, `)} | ${
671
+ chalk.bold(String(characterCount))
672
+ } chars | ${chalk.bold(
673
+ `${resolvePath(hackmudPath!, users[0]!, `scripts`, getPathBaseName(path, getPathFileExtension(path)))}.js`
674
+ )}`)
675
+ }
676
+
677
+ function logError(message: string) {
678
+ console.error(colourD(message))
679
+ process.exitCode = 1
680
+ }
681
+
682
+ function getHackmudPath() {
683
+ const hackmudPathOption = popOption(`hackmud-path`)
684
+
685
+ if (hackmudPathOption) {
686
+ if (typeof hackmudPathOption.value != `string`) {
687
+ logError(`Option ${colourN(`--hackmud-path`)} must be a string, got ${colourV(hackmudPathOption.value)}\n`)
688
+ logHelp()
689
+ process.exit(1)
690
+ }
691
+
692
+ if (!hackmudPathOption.value) {
693
+ logError(`Option ${colourN(`--hackmud-path`)} was specified but empty\n`)
694
+ logHelp()
695
+ process.exit(1)
696
+ }
697
+
698
+ return hackmudPathOption.value
699
+ }
700
+
701
+ if (process.env.HSM_HACKMUD_PATH != undefined) {
702
+ if (!process.env.HSM_HACKMUD_PATH) {
703
+ logError(`Environment variable ${colourN(`HSM_HACKMUD_PATH`)} was specified but empty\n`)
704
+ logHelp()
705
+ process.exit(1)
706
+ }
707
+
708
+ return process.env.HSM_HACKMUD_PATH
709
+ }
710
+
711
+ return process.platform == `win32` ?
712
+ resolvePath(process.env.APPDATA!, `hackmud`)
713
+ : resolvePath(getHomeDirectory(), `.config/hackmud`)
714
+ }
715
+
716
+ type Option = { name: string, value: OptionValue }
717
+
718
+ function assertOptionIsBoolean(option: Option): asserts option is Replace<Option, { value: boolean }> {
719
+ if (typeof option.value != `boolean`) {
720
+ logError(`The value for ${formatOption(option.name)} must be ${colourV(`true`)} or ${colourV(`false`)}\n`)
721
+ logHelp()
722
+ process.exit(1)
723
+ }
724
+ }
725
+
726
+ function popOption(...names: string[]): Option | undefined {
727
+ const presentOptionNames = names.filter(name => options.has(name))
728
+
729
+ if (!presentOptionNames.length)
730
+ return undefined
731
+
732
+ if (presentOptionNames.length > 1) {
733
+ logError(`The options ${presentOptionNames.map(formatOption).join(`, `)
734
+ } are aliases for each other. Please only specify one`)
735
+
736
+ process.exit(1)
737
+ }
738
+
739
+ const value = options.get(presentOptionNames[0]!)!
740
+
741
+ options.delete(presentOptionNames[0]!)
742
+
743
+ return { name: presentOptionNames[0]!, value }
744
+ }
745
+
746
+ function complainAboutUnrecognisedOptions(): void {
747
+ if (options.size) {
748
+ logError(
749
+ `Unrecognised option${options.size > 1 ? `s` : ``}: ${[ ...options.keys() ].map(formatOption).join(`, `)}`
750
+ )
751
+
752
+ process.exit(1)
753
+ }
754
+ }