hackmud-script-manager 0.21.1-583d190 → 0.21.1-5970667
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.
- package/README.md +3 -1
- package/bin/hsm.js +31 -22
- package/env.d.ts +24 -25
- package/generateTypeDeclaration.js +23 -22
- package/index.js +0 -1
- package/package.json +4 -2
- package/processScript/index.js +7 -6
- package/processScript/minify.js +3 -6
- package/processScript/preprocess.js +8 -8
- package/processScript/transform.js +36 -32
- package/push.js +3 -1
- package/watch.js +23 -14
package/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Hackmud Script Manager
|
2
2
|
Command made for [Hackmud Scripting Environment](https://github.com/samualtnorman/hackmud-environment), which is a scripting environment for hackmud with minification, autocompletes / intellisense, and TypeScript support.
|
3
3
|
|
4
|
+
Join [our Discord server](https://discord.gg/RSa4Sc6pNA)!
|
5
|
+
|
4
6
|
[](https://ko-fi.com/R6R0XN5CX)
|
5
7
|
|
6
8
|
You can read about how HSM works [in my blog post](https://samual.uk/blog/js-code-transformation-niche-environment/).
|
@@ -12,7 +14,7 @@ You can read about how HSM works [in my blog post](https://samual.uk/blog/js-cod
|
|
12
14
|
## Usage
|
13
15
|
1. Run `#dir` in game, then `cd` to that folder
|
14
16
|
2. Name your source script file to `<name>.src.js`
|
15
|
-
3. Run `hsm
|
17
|
+
3. Run `hsm minify <name>.src.js` and it will create a minified script file called `<name>.js`
|
16
18
|
|
17
19
|
> **NOTE:** If you get an error message that looks like this:
|
18
20
|
> ```
|
package/bin/hsm.js
CHANGED
@@ -11,7 +11,6 @@ import { generateTypeDeclaration } from "../generateTypeDeclaration.js"
|
|
11
11
|
import { pull } from "../pull.js"
|
12
12
|
import { syncMacros } from "../syncMacros.js"
|
13
13
|
import "@samual/lib/readDirectoryWithStats"
|
14
|
-
import "path/posix"
|
15
14
|
import "@samual/lib/copyFilePersistent"
|
16
15
|
const formatOption = name => colourN(`-${1 == name.length ? "" : "-"}${name}`),
|
17
16
|
options = new Map(),
|
@@ -55,14 +54,16 @@ const pushModule = import("../push.js"),
|
|
55
54
|
colourS = chalk.rgb(122, 178, 244),
|
56
55
|
colourV = chalk.rgb(255, 0, 236),
|
57
56
|
colourW = chalk.rgb(255, 150, 224)
|
58
|
-
process.version.startsWith("v21.")
|
57
|
+
if (process.version.startsWith("v21.")) {
|
58
|
+
process.exitCode = 1
|
59
59
|
console.warn(
|
60
60
|
colourF(
|
61
61
|
`${chalk.bold("Warning:")} Support for Node.js 21 will be dropped in the next minor version of HSM\n Your current version of Node.js is ${chalk.bold(process.version)}\n You should update your version of Node.js\n https://nodejs.org/en/download/package-manager\n`
|
62
62
|
)
|
63
63
|
)
|
64
|
+
}
|
64
65
|
if ("v" == commands[0] || "version" == commands[0] || popOption("version", "v")?.value) {
|
65
|
-
console.log("0.21.1-
|
66
|
+
console.log("0.21.1-5970667")
|
66
67
|
process.exit()
|
67
68
|
}
|
68
69
|
let warnedDeprecatedEmitDtsAlias = !1
|
@@ -79,13 +80,14 @@ switch (commands[0]) {
|
|
79
80
|
case "minify":
|
80
81
|
{
|
81
82
|
const noMinifyOption = popOption("no-minify", "skip-minify")
|
82
|
-
noMinifyOption &&
|
83
|
-
|
83
|
+
if (noMinifyOption && "no-minify" != noMinifyOption.name) {
|
84
|
+
process.exitCode = 1
|
84
85
|
console.warn(
|
85
86
|
colourF(
|
86
87
|
`${chalk.bold("Warning:")} ${formatOption(noMinifyOption.name)} is deprecated and will be removed in the next minor\n release of HSM\n You should switch to using its alias ${colourN("--no-minify")}\n`
|
87
88
|
)
|
88
89
|
)
|
90
|
+
}
|
89
91
|
const mangleNamesOption = popOption("mangle-names"),
|
90
92
|
forceQuineCheatsOption = popOption("force-quine-cheats"),
|
91
93
|
noQuineCheatsOptions = popOption("no-quine-cheats"),
|
@@ -131,19 +133,19 @@ switch (commands[0]) {
|
|
131
133
|
fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src"),
|
132
134
|
scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName,
|
133
135
|
scriptUser =
|
134
|
-
(
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
basename(resolve(target, "../.."))
|
139
|
-
: void 0
|
136
|
+
"scripts" == basename(resolve(target, "..")) &&
|
137
|
+
"hackmud" == basename(resolve(target, "../../.."))
|
138
|
+
? basename(resolve(target, "../.."))
|
139
|
+
: void 0
|
140
140
|
let outputPath =
|
141
141
|
commands[2] ||
|
142
142
|
resolve(
|
143
143
|
dirname(target),
|
144
|
-
fileBaseNameEndsWithDotSrc
|
145
|
-
|
146
|
-
|
144
|
+
fileBaseNameEndsWithDotSrc
|
145
|
+
? scriptName + ".js"
|
146
|
+
: ".js" == fileExtension
|
147
|
+
? fileBaseName + ".min.js"
|
148
|
+
: fileBaseName + ".js"
|
147
149
|
)
|
148
150
|
const golfFile = () =>
|
149
151
|
readFile(target, { encoding: "utf8" }).then(async source => {
|
@@ -158,6 +160,7 @@ switch (commands[0]) {
|
|
158
160
|
rootFolderPath
|
159
161
|
}),
|
160
162
|
timeTook = performance.now() - timeStart
|
163
|
+
warnings.length && (process.exitCode = 1)
|
161
164
|
for (const { message } of warnings)
|
162
165
|
console.warn(colourF(`${chalk.bold("Warning:")} ${message}`))
|
163
166
|
await writeFilePersistent(outputPath, script)
|
@@ -207,14 +210,18 @@ switch (commands[0]) {
|
|
207
210
|
"dts",
|
208
211
|
"gen-types"
|
209
212
|
)
|
210
|
-
|
213
|
+
if (
|
214
|
+
dtsPathOption &&
|
211
215
|
"dts-path" != dtsPathOption.name &&
|
212
|
-
"type-declaration-path" != dtsPathOption.name
|
216
|
+
"type-declaration-path" != dtsPathOption.name
|
217
|
+
) {
|
218
|
+
process.exitCode = 1
|
213
219
|
console.warn(
|
214
220
|
colourF(
|
215
221
|
`${chalk.bold("Warning:")} ${formatOption(dtsPathOption.name)} is deprecated and will be removed in the\n next minor release of HSM\n You should switch to using its alias ${colourN("--dts-path")}\n`
|
216
222
|
)
|
217
223
|
)
|
224
|
+
}
|
218
225
|
complainAboutUnrecognisedOptions()
|
219
226
|
const { watch } = await watchModule
|
220
227
|
watch(sourcePath, hackmudPath, {
|
@@ -244,7 +251,7 @@ switch (commands[0]) {
|
|
244
251
|
const typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
|
245
252
|
declarationPathPromise = writeFile(typeDeclarationPath, typeDeclaration)
|
246
253
|
.catch(error => {
|
247
|
-
assert(error instanceof Error, "src/bin/hsm.ts:
|
254
|
+
assert(error instanceof Error, "src/bin/hsm.ts:299:38")
|
248
255
|
if ("EISDIR" != error.code) throw error
|
249
256
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
250
257
|
return writeFile(typeDeclarationPath, typeDeclaration)
|
@@ -311,6 +318,7 @@ switch (commands[0]) {
|
|
311
318
|
{
|
312
319
|
if ("emit-dts" != commands[0] && "gen-dts" != commands[0]) {
|
313
320
|
warnedDeprecatedEmitDtsAlias = !0
|
321
|
+
process.exitCode = 1
|
314
322
|
console.warn(
|
315
323
|
colourF(
|
316
324
|
`${chalk.bold("Warning:")} ${colourC("hsm")} ${colourL(commands[0])} is deprecated and will be removed\n in the next minor release of HSM\n You should switch to using its alias ${colourC("hsm")} ${colourL("emit-dts")}\n`
|
@@ -330,7 +338,7 @@ switch (commands[0]) {
|
|
330
338
|
typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
|
331
339
|
let typeDeclarationPath = resolve(outputPath)
|
332
340
|
await writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
|
333
|
-
assert(error instanceof Error, "src/bin/hsm.ts:
|
341
|
+
assert(error instanceof Error, "src/bin/hsm.ts:438:35")
|
334
342
|
if ("EISDIR" != error.code) throw error
|
335
343
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
336
344
|
return writeFile(typeDeclarationPath, typeDeclaration)
|
@@ -379,14 +387,14 @@ function logHelp() {
|
|
379
387
|
case "gen-dts":
|
380
388
|
case "gen-types":
|
381
389
|
case "emit-dts":
|
382
|
-
warnedDeprecatedEmitDtsAlias
|
383
|
-
|
384
|
-
"gen-dts" == commands[0] ||
|
390
|
+
if (!warnedDeprecatedEmitDtsAlias && "emit-dts" != commands[0] && "gen-dts" != commands[0]) {
|
391
|
+
process.exitCode = 1
|
385
392
|
console.warn(
|
386
393
|
colourF(
|
387
394
|
`${chalk.bold("Warning:")} ${colourC("hsm")} ${colourL(commands[0])} is deprecated and will be removed\n in the next minor release of HSM\n You should switch to using its alias ${colourC("hsm")} ${colourL("emit-dts")}\n`
|
388
395
|
)
|
389
396
|
)
|
397
|
+
}
|
390
398
|
console.log(
|
391
399
|
colourS(
|
392
400
|
`${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}`
|
@@ -403,7 +411,7 @@ function logHelp() {
|
|
403
411
|
default:
|
404
412
|
console.log(
|
405
413
|
colourS(
|
406
|
-
`${colourJ("Hackmud Script Manager")}\n${colourN("Version") + colourS(": ") + colourV("0.21.1-
|
414
|
+
`${colourJ("Hackmud Script Manager")}\n${colourN("Version") + colourS(": ") + colourV("0.21.1-5970667")}\n\n${colourA("Commands:")}\n${colourL("push")}\n ${pushCommandDescription}\n${colourL("minify")}\n Minify a script file on the spot\n${colourL("emit-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\n\n${colourA("Options:")}\n${colourN("--help")}\n Can be used on any command e.g. ${colourC("hsm")} ${colourL("push")} ${colourN("--help")} to show helpful information`
|
407
415
|
)
|
408
416
|
)
|
409
417
|
}
|
@@ -412,6 +420,7 @@ function logInfo({ path, users, characterCount, error, warnings }, hackmudPath)
|
|
412
420
|
path = relative(".", path)
|
413
421
|
if (error) logError(`Error "${chalk.bold(error.message)}" in ${chalk.bold(path)}`)
|
414
422
|
else {
|
423
|
+
warnings.length && (process.exitCode = 1)
|
415
424
|
for (const warning of warnings) console.warn(colourF(`${chalk.bold("Warning:")} ${warning.message}`))
|
416
425
|
log(
|
417
426
|
`Pushed ${chalk.bold(path)} to ${users.map(user => chalk.bold(userColours.get(user))).join(", ")} | ${chalk.bold(characterCount + "")} chars | ${chalk.bold(resolve(hackmudPath, users[0], "scripts", basename(path, extname(path))) + ".js")}`
|
package/env.d.ts
CHANGED
@@ -189,7 +189,7 @@ type Fullsec = Subscripts & PlayerFullsec & {
|
|
189
189
|
|
190
190
|
/** @returns A random element from `array`, selected with a random number generated using `rng`
|
191
191
|
* (defaults to `Math.random`). */
|
192
|
-
sample: (array:
|
192
|
+
sample: <T>(array: T[], rng?: ()=>number) => T
|
193
193
|
|
194
194
|
/** @returns Whether two MongoDB `ObjectId`s are equivalent. */ are_ids_eq: (id1: any, id2: any) => boolean
|
195
195
|
/** Convert a MongoDB `ObjectId` to a string. */ id_to_str: (id: string | {$oid: string}) => any
|
@@ -270,7 +270,7 @@ type Fullsec = Subscripts & PlayerFullsec & {
|
|
270
270
|
map: <T, U>(array: T[], callback: (index: number, value: T) => U) => U[]
|
271
271
|
|
272
272
|
/** @returns A new object derived from `obj` with only the keys specified in `keys`. */
|
273
|
-
pick: (obj:
|
273
|
+
pick: <TObj extends object, TKeys extends keyof TObj>(obj: TObj, keys: TKeys[]) => { [K in TKeys]: TObj[K] }
|
274
274
|
|
275
275
|
/** @returns An array with the elements from `array` in a random order. */ shuffle: <T>(array: T[]) => T[]
|
276
276
|
|
@@ -298,7 +298,7 @@ type Fullsec = Subscripts & PlayerFullsec & {
|
|
298
298
|
security_level_names: [ "NULLSEC", "LOWSEC", "MIDSEC", "HIGHSEC", "FULLSEC" ]
|
299
299
|
|
300
300
|
/** @returns The string name of a numeric security level. */
|
301
|
-
get_security_level_name: (security_level: number) =>
|
301
|
+
get_security_level_name: (security_level: number) => string
|
302
302
|
|
303
303
|
/** @param result The return value of a call to `$db.i()` or `$db.r()`.
|
304
304
|
* @param nModified The expected value of `result.nModified`.
|
@@ -699,18 +699,18 @@ type Nullsec = Lowsec & PlayerNullsec & {
|
|
699
699
|
// database
|
700
700
|
type MongoPrimitive = null | boolean | number | Date | string
|
701
701
|
type MongoValue = MongoPrimitive | MongoValue[] | MongoObject
|
702
|
-
type MongoObject = { [k: string]: MongoValue
|
702
|
+
type MongoObject = { [k: string]: MongoValue }
|
703
703
|
type MongoQueryValue = MongoPrimitive | MongoQueryValue[] | MongoQueryObject
|
704
704
|
|
705
705
|
type MongoQueryObject =
|
706
|
-
{ [k: string]: MongoQueryValue, [k: `$${string}`]: MongoValue
|
706
|
+
{ [k: string]: MongoQueryValue, [k: `$${string}`]: MongoValue } & { $type?: keyof MongoTypeStringsToTypes | (string & {}) }
|
707
707
|
|
708
708
|
type MongoTypeStringsToTypes = {
|
709
709
|
double: number
|
710
710
|
string: string
|
711
711
|
object: MongoObject
|
712
712
|
array: MongoValue[]
|
713
|
-
objectId:
|
713
|
+
objectId: MongoObjectId
|
714
714
|
bool: boolean
|
715
715
|
date: Date
|
716
716
|
null: null
|
@@ -720,8 +720,8 @@ type MongoTypeStringsToTypes = {
|
|
720
720
|
|
721
721
|
type MongoTypeString = keyof MongoTypeStringsToTypes
|
722
722
|
type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127
|
723
|
-
type MongoId = Exclude<MongoPrimitive, null> | MongoObject
|
724
|
-
type MongoQueryId = Exclude<MongoPrimitive, null> | MongoQueryObject
|
723
|
+
type MongoId = Exclude<MongoPrimitive, null | false> | MongoObjectId | MongoObject
|
724
|
+
type MongoQueryId = Exclude<MongoPrimitive, null | false> | MongoObjectId | MongoQueryObject
|
725
725
|
type MongoDocument = MongoObject & { _id?: MongoId }
|
726
726
|
|
727
727
|
type MongoQueryType<TQuery extends MongoQueryObject> = {
|
@@ -755,11 +755,16 @@ type MongoQuerySelector<T extends MongoValue> = Partial<
|
|
755
755
|
|
756
756
|
type MongoQuery<T extends MongoObject> = { [K in keyof T]?: T[K] | MongoQuerySelector<T[K]> } & { _id?: MongoId }
|
757
757
|
|
758
|
-
type
|
758
|
+
type MongoUpdateArrayOperatorUniversalModifiers<T> = { $each?: T extends [] ? T : T[] }
|
759
|
+
|
760
|
+
type MongoUpdateArrayOperatorModifiers<T> = MongoUpdateArrayOperatorUniversalModifiers<T> &
|
761
|
+
{ $position?: number, $slice?: number, $sort?: 1 | -1 }
|
762
|
+
|
763
|
+
type MongoUpdateCommand<T extends MongoObject> = Partial<{
|
759
764
|
/* Universal operators */
|
760
|
-
$set: Partial<Record<string
|
761
|
-
$setOnInsert: Partial<Record<string
|
762
|
-
$unset: Partial<Record<string, ""
|
765
|
+
$set: Partial<Record<(string & {}) | keyof T, MongoCommandValue>>
|
766
|
+
$setOnInsert: Partial<Record<(string & {}) | keyof T, MongoCommandValue>>
|
767
|
+
$unset: Partial<Record<(string & {}) | keyof T, "">>
|
763
768
|
|
764
769
|
$rename: Partial<Record<string, string> & { [key in keyof T]: string }>
|
765
770
|
|
@@ -792,13 +797,6 @@ type MongoUpdateOperators<T extends MongoObject> = Partial<{
|
|
792
797
|
$pullAll: Record<string, MongoCommandValue> & { [K in keyof T as T[K] extends [] ? K : never]?: T[K] }
|
793
798
|
}>
|
794
799
|
|
795
|
-
type MongoUpdateArrayOperatorUniversalModifiers<T> = { $each?: T extends [] ? T : T[] }
|
796
|
-
|
797
|
-
type MongoUpdateArrayOperatorModifiers<T> = MongoUpdateArrayOperatorUniversalModifiers<T> &
|
798
|
-
{ $position?: number, $slice?: number, $sort?: 1 | -1 }
|
799
|
-
|
800
|
-
type MongoUpdateCommand<Schema extends MongoObject> = MongoUpdateOperators<Schema>
|
801
|
-
|
802
800
|
type SortOrder = { [key: string]: 1 | -1 | SortOrder }
|
803
801
|
|
804
802
|
type Cursor<T> = {
|
@@ -865,8 +863,8 @@ type BrainContext = Replace<CliContext, { /** Whether the script is being run vi
|
|
865
863
|
// when anyField: true is given, other fields (except _id) are omitted
|
866
864
|
|
867
865
|
type MongoProject<TDocument, TProjection> =
|
868
|
-
|
869
|
-
|
866
|
+
(TProjection extends { _id: false | 0 } ? {} : { _id: TDocument extends { _id: infer TId } ? TId : MongoId }) & (
|
867
|
+
true extends (1 extends TProjection[keyof TProjection] ? true : TProjection[keyof TProjection]) ?
|
870
868
|
{
|
871
869
|
[K in
|
872
870
|
keyof TDocument as K extends keyof TProjection ? TProjection[K] extends true | 1 ? K : never : never
|
@@ -877,7 +875,8 @@ type MongoProject<TDocument, TProjection> =
|
|
877
875
|
keyof TProjection as TProjection[K] extends true | 1 ? K extends keyof TDocument ? never : K : never
|
878
876
|
]?: MongoValue
|
879
877
|
}
|
880
|
-
|
878
|
+
: { [k: string]: MongoValue } & { [K in keyof TDocument as K extends keyof TProjection ? never : K]: TDocument[K] }
|
879
|
+
)
|
881
880
|
|
882
881
|
type DeepFreeze<T> = { readonly [P in keyof T]: DeepFreeze<T[P]> }
|
883
882
|
|
@@ -885,9 +884,9 @@ declare global {
|
|
885
884
|
type ScriptSuccess<T = unknown> = { ok: true } & T
|
886
885
|
type ScriptFailure = { ok: false, msg?: string }
|
887
886
|
type ScriptResponse<T = unknown> = ScriptSuccess<T> | ScriptFailure
|
888
|
-
type Scriptor<TArgs
|
887
|
+
type Scriptor<TArgs = any> = { name: string, call: (args: TArgs) => unknown }
|
889
888
|
type Context = CliContext | SubscriptContext | ScriptorContext | BrainContext
|
890
|
-
type
|
889
|
+
type MongoObjectId = { $oid: string }
|
891
890
|
|
892
891
|
interface PlayerFullsec {}
|
893
892
|
interface PlayerHighsec {}
|
@@ -978,7 +977,7 @@ declare global {
|
|
978
977
|
us: <T extends MongoDocument>(query: MongoQuery<T> | MongoQuery<T>[], command: MongoUpdateCommand<T>) =>
|
979
978
|
{ n: number, ok: 0 | 1, opTime: { t: number }, nModified: number }[]
|
980
979
|
|
981
|
-
ObjectId: () =>
|
980
|
+
ObjectId: () => MongoObjectId
|
982
981
|
}
|
983
982
|
|
984
983
|
/** Debug Log.
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
2
2
|
import { basename, resolve } from "path"
|
3
|
-
import * as PathPosix from "path/posix"
|
4
3
|
async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
|
5
4
|
const users = new Set()
|
6
5
|
if (hackmudPath)
|
@@ -11,33 +10,35 @@ async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
|
|
11
10
|
allScripts = {},
|
12
11
|
allAnyScripts = {}
|
13
12
|
await Promise.all(
|
14
|
-
(await readDirectoryWithStats(sourceDirectory))
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
13
|
+
(await readDirectoryWithStats(sourceDirectory))
|
14
|
+
.filter(({ stats, name }) => !stats.isDirectory() || /^[a-z_][a-z\d_]{0,24}$/.test(name))
|
15
|
+
.map(async ({ stats, name }) => {
|
16
|
+
if (stats.isFile())
|
17
|
+
name.endsWith(".ts")
|
18
|
+
? name.endsWith(".d.ts") || wildScripts.push(basename(name, ".ts"))
|
19
|
+
: name.endsWith(".js") && wildAnyScripts.push(basename(name, ".js"))
|
20
|
+
else if (stats.isDirectory()) {
|
21
|
+
const scripts = [],
|
22
|
+
anyScripts = []
|
23
|
+
allScripts[name] = scripts
|
24
|
+
allAnyScripts[name] = anyScripts
|
25
|
+
users.add(name)
|
26
|
+
for (const child of await readDirectoryWithStats(resolve(sourceDirectory, name)))
|
27
|
+
child.stats.isFile() &&
|
28
|
+
(child.name.endsWith(".ts")
|
29
|
+
? name.endsWith(".d.ts") || scripts.push(basename(child.name, ".ts"))
|
30
|
+
: child.name.endsWith(".js") && anyScripts.push(basename(child.name, ".js")))
|
31
|
+
}
|
32
|
+
})
|
32
33
|
)
|
33
|
-
sourceDirectory = PathPosix.resolve(sourceDirectory)
|
34
34
|
let o = ""
|
35
|
-
for (const script of wildScripts)
|
35
|
+
for (const script of wildScripts)
|
36
|
+
o += `type $${script}$ = typeof import(${JSON.stringify(resolve(sourceDirectory, script))}).default\n`
|
36
37
|
o += "\n"
|
37
38
|
for (const user in allScripts) {
|
38
39
|
const scripts = allScripts[user]
|
39
40
|
for (const script of scripts)
|
40
|
-
o += `type $${user}$${script}$ = typeof import(
|
41
|
+
o += `type $${user}$${script}$ = typeof import(${JSON.stringify(resolve(sourceDirectory, user, script))}).default\n`
|
41
42
|
}
|
42
43
|
o +=
|
43
44
|
"\ntype ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never\n\ntype Subscript<T extends (...args: any) => any> =\n\t(...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure\n\ntype WildFullsec = Record<string, () => ScriptFailure> & {\n"
|
package/index.js
CHANGED
@@ -7,7 +7,6 @@ export { syncMacros } from "./syncMacros.js"
|
|
7
7
|
export { watch } from "./watch.js"
|
8
8
|
import "@samual/lib/readDirectoryWithStats"
|
9
9
|
import "path"
|
10
|
-
import "path/posix"
|
11
10
|
import "@babel/generator"
|
12
11
|
import "@babel/parser"
|
13
12
|
import "@babel/plugin-proposal-decorators"
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "hackmud-script-manager",
|
3
|
-
"version": "0.21.1-
|
3
|
+
"version": "0.21.1-5970667",
|
4
4
|
"description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
|
5
5
|
"keywords": [
|
6
6
|
"api",
|
@@ -23,7 +23,9 @@
|
|
23
23
|
"author": "Samual Norman <me@samual.uk> (https://samual.uk/)",
|
24
24
|
"contributors": [
|
25
25
|
"Daniel Swann (https://github.com/danswann)",
|
26
|
-
"Longboyy"
|
26
|
+
"Longboyy",
|
27
|
+
"Helloman892",
|
28
|
+
"Sarah Klocke (https://sarahisweird.dev/)"
|
27
29
|
],
|
28
30
|
"main": "index.js",
|
29
31
|
"repository": {
|
package/processScript/index.js
CHANGED
@@ -23,7 +23,7 @@ import rollupPluginCommonJS from "@rollup/plugin-commonjs"
|
|
23
23
|
import rollupPluginJSON from "@rollup/plugin-json"
|
24
24
|
import rollupPluginNodeResolve from "@rollup/plugin-node-resolve"
|
25
25
|
import { assert } from "@samual/lib/assert"
|
26
|
-
import { relative } from "path"
|
26
|
+
import { relative, isAbsolute, sep } from "path"
|
27
27
|
import prettier from "prettier"
|
28
28
|
import { rollup } from "rollup"
|
29
29
|
import { supportedExtensions } from "../constants.js"
|
@@ -210,7 +210,8 @@ async function processScript(
|
|
210
210
|
{
|
211
211
|
name: "hackmud-script-manager",
|
212
212
|
async transform(code, id) {
|
213
|
-
if (!id.includes(
|
213
|
+
if (isAbsolute(id) && !id.includes(`${sep}node_modules${sep}`))
|
214
|
+
return (await preprocess(code, { uniqueId })).code
|
214
215
|
let program
|
215
216
|
traverse(parse(code, { sourceType: "module" }), {
|
216
217
|
Program(path) {
|
@@ -221,10 +222,10 @@ async function processScript(
|
|
221
222
|
for (const referencePath of getReferencePathsToGlobal("JSON", program))
|
222
223
|
"MemberExpression" == referencePath.parentPath.node.type &&
|
223
224
|
"Identifier" == referencePath.parentPath.node.property.type &&
|
224
|
-
("parse" == referencePath.parentPath.node.property.name
|
225
|
-
(referencePath.parentPath.node.property.name = "oparse")
|
226
|
-
|
227
|
-
|
225
|
+
("parse" == referencePath.parentPath.node.property.name
|
226
|
+
? (referencePath.parentPath.node.property.name = "oparse")
|
227
|
+
: "stringify" == referencePath.parentPath.node.property.name &&
|
228
|
+
(referencePath.parentPath.node.property.name = "ostringify"))
|
228
229
|
return generate(program.node).code
|
229
230
|
}
|
230
231
|
},
|
package/processScript/minify.js
CHANGED
@@ -359,12 +359,9 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
|
|
359
359
|
}
|
360
360
|
if (1 == forceQuineCheats) return code
|
361
361
|
assert(scriptBeforeJSONValueReplacement, "src/processScript/minify.ts:485:43")
|
362
|
-
return (
|
363
|
-
|
364
|
-
|
365
|
-
) ?
|
366
|
-
scriptBeforeJSONValueReplacement
|
367
|
-
: code
|
362
|
+
return countHackmudCharacters(scriptBeforeJSONValueReplacement) <= countHackmudCharacters(code) + Number(hasComment)
|
363
|
+
? scriptBeforeJSONValueReplacement
|
364
|
+
: code
|
368
365
|
}
|
369
366
|
function parseObjectExpression(node, o) {
|
370
367
|
if (!node.properties.length) return !1
|
@@ -80,14 +80,14 @@ async function preprocess(code, { uniqueId = "00000000000" } = {}) {
|
|
80
80
|
;(needRecord || needTuple) &&
|
81
81
|
file.program.body.unshift(
|
82
82
|
t.importDeclaration(
|
83
|
-
needRecord
|
84
|
-
needTuple
|
85
|
-
[
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
83
|
+
needRecord
|
84
|
+
? needTuple
|
85
|
+
? [
|
86
|
+
t.importSpecifier(t.identifier("Record"), t.identifier("Record")),
|
87
|
+
t.importSpecifier(t.identifier("Tuple"), t.identifier("Tuple"))
|
88
|
+
]
|
89
|
+
: [t.importSpecifier(t.identifier("Record"), t.identifier("Record"))]
|
90
|
+
: [t.importSpecifier(t.identifier("Tuple"), t.identifier("Tuple"))],
|
91
91
|
t.stringLiteral("@bloomberg/record-tuple-polyfill")
|
92
92
|
)
|
93
93
|
)
|
@@ -253,29 +253,29 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
253
253
|
mainFunction = t.functionDeclaration(
|
254
254
|
t.identifier(topFunctionName),
|
255
255
|
declarator.init.params,
|
256
|
-
"BlockStatement" == declarator.init.body.type
|
257
|
-
declarator.init.body
|
258
|
-
|
256
|
+
"BlockStatement" == declarator.init.body.type
|
257
|
+
? declarator.init.body
|
258
|
+
: t.blockStatement([t.returnStatement(declarator.init.body)])
|
259
259
|
)
|
260
260
|
else
|
261
|
-
"FunctionDeclaration" == statement.type
|
262
|
-
statement.id.name == exportDefaultName
|
263
|
-
(mainFunction = statement)
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
261
|
+
"FunctionDeclaration" == statement.type
|
262
|
+
? statement.id.name == exportDefaultName
|
263
|
+
? (mainFunction = statement)
|
264
|
+
: globalBlock.body.push(
|
265
|
+
t.variableDeclaration("let", [
|
266
|
+
t.variableDeclarator(
|
267
|
+
statement.id,
|
268
|
+
t.functionExpression(
|
269
|
+
void 0,
|
270
|
+
statement.params,
|
271
|
+
statement.body,
|
272
|
+
statement.generator,
|
273
|
+
statement.async
|
274
|
+
)
|
274
275
|
)
|
275
|
-
)
|
276
|
-
|
277
|
-
|
278
|
-
: globalBlock.body.push(statement)
|
276
|
+
])
|
277
|
+
)
|
278
|
+
: globalBlock.body.push(statement)
|
279
279
|
mainFunction ||= t.functionDeclaration(
|
280
280
|
t.identifier(topFunctionName),
|
281
281
|
[t.identifier("context"), t.identifier("args")],
|
@@ -515,9 +515,11 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
515
515
|
"let",
|
516
516
|
[...neededDbMethodLets].map(name => {
|
517
517
|
const getArgs = () =>
|
518
|
-
"ObjectId" == name
|
519
|
-
|
520
|
-
|
518
|
+
"ObjectId" == name
|
519
|
+
? []
|
520
|
+
: "i" == name || "r" == name
|
521
|
+
? [t.identifier("a")]
|
522
|
+
: [t.identifier("a"), t.identifier("b")]
|
521
523
|
return t.variableDeclarator(
|
522
524
|
t.identifier(`_${uniqueId}_CONSOLE_METHOD_${name}_`),
|
523
525
|
t.arrowFunctionExpression(
|
@@ -593,7 +595,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
593
595
|
thisIsReferenced = !0
|
594
596
|
path.replaceWith(t.identifier(`_${uniqueId}_THIS_`))
|
595
597
|
},
|
596
|
-
Function
|
598
|
+
Function(path) {
|
599
|
+
"ArrowFunctionExpression" != path.node.type && path.skip()
|
600
|
+
}
|
597
601
|
},
|
598
602
|
scope
|
599
603
|
)
|
@@ -663,9 +667,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
663
667
|
const bigIntAsNumber = Number(path.node.value)
|
664
668
|
path.replaceWith(
|
665
669
|
t.callExpression(t.identifier("BigInt"), [
|
666
|
-
BigInt(bigIntAsNumber) == BigInt(path.node.value)
|
667
|
-
t.numericLiteral(bigIntAsNumber)
|
668
|
-
|
670
|
+
BigInt(bigIntAsNumber) == BigInt(path.node.value)
|
671
|
+
? t.numericLiteral(bigIntAsNumber)
|
672
|
+
: t.stringLiteral(path.node.value)
|
669
673
|
])
|
670
674
|
)
|
671
675
|
}
|
@@ -680,23 +684,23 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
680
684
|
}
|
681
685
|
function processFakeSubscriptObject(fakeSubscriptObjectName, seclevel) {
|
682
686
|
for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
|
683
|
-
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:
|
687
|
+
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:811:60")
|
684
688
|
assert("Identifier" == referencePath.parent.property.type)
|
685
689
|
assert(
|
686
690
|
"MemberExpression" == referencePath.parentPath.parentPath?.node.type,
|
687
|
-
"src/processScript/transform.ts:
|
691
|
+
"src/processScript/transform.ts:813:81"
|
688
692
|
)
|
689
693
|
assert(
|
690
694
|
"Identifier" == referencePath.parentPath.parentPath.node.property.type,
|
691
|
-
"src/processScript/transform.ts:
|
695
|
+
"src/processScript/transform.ts:814:83"
|
692
696
|
)
|
693
697
|
assert(
|
694
698
|
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
|
695
|
-
`src/processScript/transform.ts:
|
699
|
+
`src/processScript/transform.ts:818:8 invalid user "${referencePath.parent.property.name}" in subscript`
|
696
700
|
)
|
697
701
|
assert(
|
698
702
|
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
|
699
|
-
`src/processScript/transform.ts:
|
703
|
+
`src/processScript/transform.ts:823:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
|
700
704
|
)
|
701
705
|
if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
|
702
706
|
referencePath.parentPath.parentPath.replaceWith(
|
package/push.js
CHANGED
@@ -70,7 +70,9 @@ async function push(
|
|
70
70
|
])
|
71
71
|
if (sourceFolder instanceof Error) return sourceFolder
|
72
72
|
if (hackmudFolder instanceof Error) return hackmudFolder
|
73
|
-
const sourceFolderFolders = sourceFolder.filter(
|
73
|
+
const sourceFolderFolders = sourceFolder.filter(
|
74
|
+
({ name, stats }) => stats.isDirectory() && /^[a-z_][a-z\d_]{0,24}$/.test(name)
|
75
|
+
),
|
74
76
|
allUsers = new Set([
|
75
77
|
...scripts
|
76
78
|
.map(scriptName => ensure(scriptName.split(".")[0], "src/push.ts:85:65"))
|
package/watch.js
CHANGED
@@ -9,7 +9,6 @@ import { extname, basename, resolve } from "path"
|
|
9
9
|
import { supportedExtensions } from "./constants.js"
|
10
10
|
import { generateTypeDeclaration } from "./generateTypeDeclaration.js"
|
11
11
|
import { processScript } from "./processScript/index.js"
|
12
|
-
import "path/posix"
|
13
12
|
import "@babel/generator"
|
14
13
|
import "@babel/parser"
|
15
14
|
import "@babel/plugin-proposal-decorators"
|
@@ -68,12 +67,13 @@ async function watch(
|
|
68
67
|
let pushEverything = !1
|
69
68
|
for (const fullScriptName of scripts) {
|
70
69
|
const [user, scriptName] = fullScriptName.split(".")
|
71
|
-
user && "*" != user
|
72
|
-
scriptName && "*" != scriptName
|
73
|
-
scriptNamesToUsers.get(scriptName).add(user)
|
74
|
-
|
75
|
-
|
76
|
-
|
70
|
+
user && "*" != user
|
71
|
+
? scriptName && "*" != scriptName
|
72
|
+
? scriptNamesToUsers.get(scriptName).add(user)
|
73
|
+
: wildScriptUsers.add(user)
|
74
|
+
: scriptName && "*" != scriptName
|
75
|
+
? wildUserScripts.add(scriptName)
|
76
|
+
: (pushEverything = !0)
|
77
77
|
}
|
78
78
|
const watcher = watch$1(".", {
|
79
79
|
cwd: sourceDirectory,
|
@@ -112,9 +112,9 @@ async function watch(
|
|
112
112
|
for (const { stats, name } of await readDirectoryWithStats(sourceDirectory))
|
113
113
|
stats.isDirectory() && usersToPushToSet.add(name)
|
114
114
|
for (const { stats, name } of await readDirectoryWithStats(hackmudDirectory))
|
115
|
-
stats.isDirectory()
|
116
|
-
usersToPushToSet.add(name)
|
117
|
-
|
115
|
+
stats.isDirectory()
|
116
|
+
? usersToPushToSet.add(name)
|
117
|
+
: stats.isFile() && name.endsWith(".key") && usersToPushToSet.add(name.slice(0, -4))
|
118
118
|
for (const users of scriptNamesToUsers.values()) for (const user of users) usersToPushToSet.add(user)
|
119
119
|
}
|
120
120
|
for (const user of wildScriptUsers) usersToPushToSet.add(user)
|
@@ -132,10 +132,19 @@ async function watch(
|
|
132
132
|
try {
|
133
133
|
;({ script: minifiedCode, warnings } = await processScript(
|
134
134
|
await readFile(filePath, { encoding: "utf8" }),
|
135
|
-
{
|
135
|
+
{
|
136
|
+
minify,
|
137
|
+
scriptUser: !0,
|
138
|
+
scriptName,
|
139
|
+
uniqueId,
|
140
|
+
filePath,
|
141
|
+
mangleNames,
|
142
|
+
forceQuineCheats,
|
143
|
+
rootFolderPath
|
144
|
+
}
|
136
145
|
))
|
137
146
|
} catch (error) {
|
138
|
-
assert(error instanceof Error, "src/watch.ts:
|
147
|
+
assert(error instanceof Error, "src/watch.ts:160:36")
|
139
148
|
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
140
149
|
return
|
141
150
|
}
|
@@ -183,7 +192,7 @@ async function watch(
|
|
183
192
|
rootFolderPath
|
184
193
|
}))
|
185
194
|
} catch (error) {
|
186
|
-
assert(error instanceof Error, "src/watch.ts:
|
195
|
+
assert(error instanceof Error, "src/watch.ts:207:35")
|
187
196
|
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
188
197
|
return
|
189
198
|
}
|
@@ -198,7 +207,7 @@ async function watch(
|
|
198
207
|
try {
|
199
208
|
await writeFile(typeDeclarationPath, typeDeclaration)
|
200
209
|
} catch (error) {
|
201
|
-
assert(error instanceof Error, "src/watch.ts:
|
210
|
+
assert(error instanceof Error, "src/watch.ts:240:35")
|
202
211
|
if ("EISDIR" != error.code) throw error
|
203
212
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
204
213
|
await writeFile(typeDeclarationPath, typeDeclaration)
|