hackmud-script-manager 0.21.1-f88f20e → 0.21.2-6d8b74e
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 -3
- package/bin/hsm.js +31 -21
- package/env.d.ts +21 -22
- package/generateTypeDeclaration.js +20 -18
- 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 -13
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
|
> ```
|
@@ -60,8 +62,6 @@ You can read about how HSM works [in my blog post](https://samual.uk/blog/js-cod
|
|
60
62
|
- Subscript and `#db` methods names are verified.
|
61
63
|
- All references to preprocessor syntax functions not being called are turned into arrow function wrappers e.g. `let debug = #D;` -> `let debug = v => #D(v);`.
|
62
64
|
- `_SECLEVEL` is replaced with a number (`0` to `4`) representing the seclevel of the script.
|
63
|
-
- When `export`s are present in the script, it becomes a script that returns an object of the `export`ed values.
|
64
|
-
- `_EXPORTS` becomes an array of the names of the exported values.
|
65
65
|
- And Neat Weird Fixes
|
66
66
|
- Like `.__proto__` and `.prototype` being converted to `["__proto__"]` and `["prototype"]`.
|
67
67
|
- Illegal and unsafe strings.
|
package/bin/hsm.js
CHANGED
@@ -55,14 +55,16 @@ const pushModule = import("../push.js"),
|
|
55
55
|
colourS = chalk.rgb(122, 178, 244),
|
56
56
|
colourV = chalk.rgb(255, 0, 236),
|
57
57
|
colourW = chalk.rgb(255, 150, 224)
|
58
|
-
process.version.startsWith("v21.")
|
58
|
+
if (process.version.startsWith("v21.")) {
|
59
|
+
process.exitCode = 1
|
59
60
|
console.warn(
|
60
61
|
colourF(
|
61
62
|
`${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
63
|
)
|
63
64
|
)
|
65
|
+
}
|
64
66
|
if ("v" == commands[0] || "version" == commands[0] || popOption("version", "v")?.value) {
|
65
|
-
console.log("0.21.
|
67
|
+
console.log("0.21.2-6d8b74e")
|
66
68
|
process.exit()
|
67
69
|
}
|
68
70
|
let warnedDeprecatedEmitDtsAlias = !1
|
@@ -79,13 +81,14 @@ switch (commands[0]) {
|
|
79
81
|
case "minify":
|
80
82
|
{
|
81
83
|
const noMinifyOption = popOption("no-minify", "skip-minify")
|
82
|
-
noMinifyOption &&
|
83
|
-
|
84
|
+
if (noMinifyOption && "no-minify" != noMinifyOption.name) {
|
85
|
+
process.exitCode = 1
|
84
86
|
console.warn(
|
85
87
|
colourF(
|
86
88
|
`${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
89
|
)
|
88
90
|
)
|
91
|
+
}
|
89
92
|
const mangleNamesOption = popOption("mangle-names"),
|
90
93
|
forceQuineCheatsOption = popOption("force-quine-cheats"),
|
91
94
|
noQuineCheatsOptions = popOption("no-quine-cheats"),
|
@@ -131,19 +134,19 @@ switch (commands[0]) {
|
|
131
134
|
fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src"),
|
132
135
|
scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName,
|
133
136
|
scriptUser =
|
134
|
-
(
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
basename(resolve(target, "../.."))
|
139
|
-
: void 0
|
137
|
+
"scripts" == basename(resolve(target, "..")) &&
|
138
|
+
"hackmud" == basename(resolve(target, "../../.."))
|
139
|
+
? basename(resolve(target, "../.."))
|
140
|
+
: void 0
|
140
141
|
let outputPath =
|
141
142
|
commands[2] ||
|
142
143
|
resolve(
|
143
144
|
dirname(target),
|
144
|
-
fileBaseNameEndsWithDotSrc
|
145
|
-
|
146
|
-
|
145
|
+
fileBaseNameEndsWithDotSrc
|
146
|
+
? scriptName + ".js"
|
147
|
+
: ".js" == fileExtension
|
148
|
+
? fileBaseName + ".min.js"
|
149
|
+
: fileBaseName + ".js"
|
147
150
|
)
|
148
151
|
const golfFile = () =>
|
149
152
|
readFile(target, { encoding: "utf8" }).then(async source => {
|
@@ -158,6 +161,7 @@ switch (commands[0]) {
|
|
158
161
|
rootFolderPath
|
159
162
|
}),
|
160
163
|
timeTook = performance.now() - timeStart
|
164
|
+
warnings.length && (process.exitCode = 1)
|
161
165
|
for (const { message } of warnings)
|
162
166
|
console.warn(colourF(`${chalk.bold("Warning:")} ${message}`))
|
163
167
|
await writeFilePersistent(outputPath, script)
|
@@ -207,14 +211,18 @@ switch (commands[0]) {
|
|
207
211
|
"dts",
|
208
212
|
"gen-types"
|
209
213
|
)
|
210
|
-
|
214
|
+
if (
|
215
|
+
dtsPathOption &&
|
211
216
|
"dts-path" != dtsPathOption.name &&
|
212
|
-
"type-declaration-path" != dtsPathOption.name
|
217
|
+
"type-declaration-path" != dtsPathOption.name
|
218
|
+
) {
|
219
|
+
process.exitCode = 1
|
213
220
|
console.warn(
|
214
221
|
colourF(
|
215
222
|
`${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
223
|
)
|
217
224
|
)
|
225
|
+
}
|
218
226
|
complainAboutUnrecognisedOptions()
|
219
227
|
const { watch } = await watchModule
|
220
228
|
watch(sourcePath, hackmudPath, {
|
@@ -244,7 +252,7 @@ switch (commands[0]) {
|
|
244
252
|
const typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
|
245
253
|
declarationPathPromise = writeFile(typeDeclarationPath, typeDeclaration)
|
246
254
|
.catch(error => {
|
247
|
-
assert(error instanceof Error, "src/bin/hsm.ts:
|
255
|
+
assert(error instanceof Error, "src/bin/hsm.ts:299:38")
|
248
256
|
if ("EISDIR" != error.code) throw error
|
249
257
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
250
258
|
return writeFile(typeDeclarationPath, typeDeclaration)
|
@@ -311,6 +319,7 @@ switch (commands[0]) {
|
|
311
319
|
{
|
312
320
|
if ("emit-dts" != commands[0] && "gen-dts" != commands[0]) {
|
313
321
|
warnedDeprecatedEmitDtsAlias = !0
|
322
|
+
process.exitCode = 1
|
314
323
|
console.warn(
|
315
324
|
colourF(
|
316
325
|
`${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 +339,7 @@ switch (commands[0]) {
|
|
330
339
|
typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
|
331
340
|
let typeDeclarationPath = resolve(outputPath)
|
332
341
|
await writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
|
333
|
-
assert(error instanceof Error, "src/bin/hsm.ts:
|
342
|
+
assert(error instanceof Error, "src/bin/hsm.ts:438:35")
|
334
343
|
if ("EISDIR" != error.code) throw error
|
335
344
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
336
345
|
return writeFile(typeDeclarationPath, typeDeclaration)
|
@@ -379,14 +388,14 @@ function logHelp() {
|
|
379
388
|
case "gen-dts":
|
380
389
|
case "gen-types":
|
381
390
|
case "emit-dts":
|
382
|
-
warnedDeprecatedEmitDtsAlias
|
383
|
-
|
384
|
-
"gen-dts" == commands[0] ||
|
391
|
+
if (!warnedDeprecatedEmitDtsAlias && "emit-dts" != commands[0] && "gen-dts" != commands[0]) {
|
392
|
+
process.exitCode = 1
|
385
393
|
console.warn(
|
386
394
|
colourF(
|
387
395
|
`${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
396
|
)
|
389
397
|
)
|
398
|
+
}
|
390
399
|
console.log(
|
391
400
|
colourS(
|
392
401
|
`${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 +412,7 @@ function logHelp() {
|
|
403
412
|
default:
|
404
413
|
console.log(
|
405
414
|
colourS(
|
406
|
-
`${colourJ("Hackmud Script Manager")}\n${colourN("Version") + colourS(": ") + colourV("0.21.
|
415
|
+
`${colourJ("Hackmud Script Manager")}\n${colourN("Version") + colourS(": ") + colourV("0.21.2-6d8b74e")}\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
416
|
)
|
408
417
|
)
|
409
418
|
}
|
@@ -412,6 +421,7 @@ function logInfo({ path, users, characterCount, error, warnings }, hackmudPath)
|
|
412
421
|
path = relative(".", path)
|
413
422
|
if (error) logError(`Error "${chalk.bold(error.message)}" in ${chalk.bold(path)}`)
|
414
423
|
else {
|
424
|
+
warnings.length && (process.exitCode = 1)
|
415
425
|
for (const warning of warnings) console.warn(colourF(`${chalk.bold("Warning:")} ${warning.message}`))
|
416
426
|
log(
|
417
427
|
`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
@@ -1,4 +1,4 @@
|
|
1
|
-
type Replace<A, B> = Omit<A, keyof B> &
|
1
|
+
type Replace<A, B> = Omit<A, keyof B> & B
|
2
2
|
type ErrorScripts = Record<string, () => ScriptFailure>
|
3
3
|
|
4
4
|
type Subscripts = Record<string, Record<string, (...args: any) => any>> & {
|
@@ -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,11 +699,11 @@ 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
|
@@ -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> | ObjectId | MongoObject
|
724
|
+
type MongoQueryId = Exclude<MongoPrimitive, null | false> | ObjectId | 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
|
|
@@ -11,24 +11,26 @@ async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
|
|
11
11
|
allScripts = {},
|
12
12
|
allAnyScripts = {}
|
13
13
|
await Promise.all(
|
14
|
-
(await readDirectoryWithStats(sourceDirectory))
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
14
|
+
(await readDirectoryWithStats(sourceDirectory))
|
15
|
+
.filter(({ stats, name }) => !stats.isDirectory() || /^[a-z_][a-z\d_]{0,24}$/.test(name))
|
16
|
+
.map(async ({ stats, name }) => {
|
17
|
+
if (stats.isFile())
|
18
|
+
name.endsWith(".ts")
|
19
|
+
? name.endsWith(".d.ts") || wildScripts.push(basename(name, ".ts"))
|
20
|
+
: name.endsWith(".js") && wildAnyScripts.push(basename(name, ".js"))
|
21
|
+
else if (stats.isDirectory()) {
|
22
|
+
const scripts = [],
|
23
|
+
anyScripts = []
|
24
|
+
allScripts[name] = scripts
|
25
|
+
allAnyScripts[name] = anyScripts
|
26
|
+
users.add(name)
|
27
|
+
for (const child of await readDirectoryWithStats(resolve(sourceDirectory, name)))
|
28
|
+
child.stats.isFile() &&
|
29
|
+
(child.name.endsWith(".ts")
|
30
|
+
? name.endsWith(".d.ts") || scripts.push(basename(child.name, ".ts"))
|
31
|
+
: child.name.endsWith(".js") && anyScripts.push(basename(child.name, ".js")))
|
32
|
+
}
|
33
|
+
})
|
32
34
|
)
|
33
35
|
sourceDirectory = PathPosix.resolve(sourceDirectory)
|
34
36
|
let o = ""
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "hackmud-script-manager",
|
3
|
-
"version": "0.21.
|
3
|
+
"version": "0.21.2-6d8b74e",
|
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
@@ -68,12 +68,13 @@ async function watch(
|
|
68
68
|
let pushEverything = !1
|
69
69
|
for (const fullScriptName of scripts) {
|
70
70
|
const [user, scriptName] = fullScriptName.split(".")
|
71
|
-
user && "*" != user
|
72
|
-
scriptName && "*" != scriptName
|
73
|
-
scriptNamesToUsers.get(scriptName).add(user)
|
74
|
-
|
75
|
-
|
76
|
-
|
71
|
+
user && "*" != user
|
72
|
+
? scriptName && "*" != scriptName
|
73
|
+
? scriptNamesToUsers.get(scriptName).add(user)
|
74
|
+
: wildScriptUsers.add(user)
|
75
|
+
: scriptName && "*" != scriptName
|
76
|
+
? wildUserScripts.add(scriptName)
|
77
|
+
: (pushEverything = !0)
|
77
78
|
}
|
78
79
|
const watcher = watch$1(".", {
|
79
80
|
cwd: sourceDirectory,
|
@@ -112,9 +113,9 @@ async function watch(
|
|
112
113
|
for (const { stats, name } of await readDirectoryWithStats(sourceDirectory))
|
113
114
|
stats.isDirectory() && usersToPushToSet.add(name)
|
114
115
|
for (const { stats, name } of await readDirectoryWithStats(hackmudDirectory))
|
115
|
-
stats.isDirectory()
|
116
|
-
usersToPushToSet.add(name)
|
117
|
-
|
116
|
+
stats.isDirectory()
|
117
|
+
? usersToPushToSet.add(name)
|
118
|
+
: stats.isFile() && name.endsWith(".key") && usersToPushToSet.add(name.slice(0, -4))
|
118
119
|
for (const users of scriptNamesToUsers.values()) for (const user of users) usersToPushToSet.add(user)
|
119
120
|
}
|
120
121
|
for (const user of wildScriptUsers) usersToPushToSet.add(user)
|
@@ -132,10 +133,19 @@ async function watch(
|
|
132
133
|
try {
|
133
134
|
;({ script: minifiedCode, warnings } = await processScript(
|
134
135
|
await readFile(filePath, { encoding: "utf8" }),
|
135
|
-
{
|
136
|
+
{
|
137
|
+
minify,
|
138
|
+
scriptUser: !0,
|
139
|
+
scriptName,
|
140
|
+
uniqueId,
|
141
|
+
filePath,
|
142
|
+
mangleNames,
|
143
|
+
forceQuineCheats,
|
144
|
+
rootFolderPath
|
145
|
+
}
|
136
146
|
))
|
137
147
|
} catch (error) {
|
138
|
-
assert(error instanceof Error, "src/watch.ts:
|
148
|
+
assert(error instanceof Error, "src/watch.ts:160:36")
|
139
149
|
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
140
150
|
return
|
141
151
|
}
|
@@ -183,7 +193,7 @@ async function watch(
|
|
183
193
|
rootFolderPath
|
184
194
|
}))
|
185
195
|
} catch (error) {
|
186
|
-
assert(error instanceof Error, "src/watch.ts:
|
196
|
+
assert(error instanceof Error, "src/watch.ts:207:35")
|
187
197
|
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
188
198
|
return
|
189
199
|
}
|
@@ -198,7 +208,7 @@ async function watch(
|
|
198
208
|
try {
|
199
209
|
await writeFile(typeDeclarationPath, typeDeclaration)
|
200
210
|
} catch (error) {
|
201
|
-
assert(error instanceof Error, "src/watch.ts:
|
211
|
+
assert(error instanceof Error, "src/watch.ts:240:35")
|
202
212
|
if ("EISDIR" != error.code) throw error
|
203
213
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
204
214
|
await writeFile(typeDeclarationPath, typeDeclaration)
|