hackmud-script-manager 0.20.4-550e28d → 0.20.4-6815ce0

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -46,7 +46,7 @@ import "import-meta-resolve"
46
46
  import "./processScript/transform.js"
47
47
  import "@samual/lib/clearObject"
48
48
  import "@samual/lib/copyFilePersistent"
49
- import "@samual/lib/Cache"
49
+ import "@samual/lib/AutoMap"
50
50
  import "@samual/lib/writeFilePersistent"
51
51
  import "fs/promises"
52
52
  import "chokidar"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hackmud-script-manager",
3
- "version": "0.20.4-550e28d",
3
+ "version": "0.20.4-6815ce0",
4
4
  "description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
5
5
  "keywords": [
6
6
  "api",
@@ -31,50 +31,50 @@
31
31
  "url": "https://github.com/samualtnorman/hackmud-script-manager.git"
32
32
  },
33
33
  "dependencies": {
34
- "@babel/generator": "^7.24.4",
35
- "@babel/parser": "^7.24.4",
36
- "@babel/plugin-proposal-decorators": "^7.24.1",
37
- "@babel/plugin-proposal-destructuring-private": "^7.24.1",
38
- "@babel/plugin-proposal-do-expressions": "^7.24.1",
39
- "@babel/plugin-proposal-explicit-resource-management": "^7.24.1",
40
- "@babel/plugin-proposal-function-bind": "^7.24.1",
41
- "@babel/plugin-proposal-function-sent": "^7.24.1",
42
- "@babel/plugin-proposal-partial-application": "^7.24.1",
43
- "@babel/plugin-proposal-pipeline-operator": "^7.24.1",
44
- "@babel/plugin-proposal-record-and-tuple": "^7.24.1",
45
- "@babel/plugin-proposal-throw-expressions": "^7.24.1",
46
- "@babel/plugin-transform-class-properties": "^7.24.1",
47
- "@babel/plugin-transform-class-static-block": "^7.24.4",
48
- "@babel/plugin-transform-exponentiation-operator": "^7.24.1",
49
- "@babel/plugin-transform-json-strings": "^7.24.1",
50
- "@babel/plugin-transform-logical-assignment-operators": "^7.24.1",
51
- "@babel/plugin-transform-nullish-coalescing-operator": "^7.24.1",
52
- "@babel/plugin-transform-numeric-separator": "^7.24.1",
53
- "@babel/plugin-transform-object-rest-spread": "^7.24.1",
54
- "@babel/plugin-transform-optional-catch-binding": "^7.24.1",
55
- "@babel/plugin-transform-optional-chaining": "^7.24.1",
56
- "@babel/plugin-transform-private-property-in-object": "^7.24.1",
57
- "@babel/plugin-transform-typescript": "^7.24.4",
58
- "@babel/plugin-transform-unicode-sets-regex": "^7.24.1",
59
- "@babel/traverse": "^7.24.1",
60
- "@babel/types": "^7.24.0",
34
+ "@babel/generator": "^7.26.2",
35
+ "@babel/parser": "^7.26.2",
36
+ "@babel/plugin-proposal-decorators": "^7.25.9",
37
+ "@babel/plugin-proposal-destructuring-private": "^7.26.0",
38
+ "@babel/plugin-proposal-do-expressions": "^7.25.9",
39
+ "@babel/plugin-proposal-explicit-resource-management": "^7.25.9",
40
+ "@babel/plugin-proposal-function-bind": "^7.25.9",
41
+ "@babel/plugin-proposal-function-sent": "^7.25.9",
42
+ "@babel/plugin-proposal-partial-application": "^7.25.9",
43
+ "@babel/plugin-proposal-pipeline-operator": "^7.25.9",
44
+ "@babel/plugin-proposal-record-and-tuple": "^7.25.9",
45
+ "@babel/plugin-proposal-throw-expressions": "^7.25.9",
46
+ "@babel/plugin-transform-class-properties": "^7.25.9",
47
+ "@babel/plugin-transform-class-static-block": "^7.26.0",
48
+ "@babel/plugin-transform-exponentiation-operator": "^7.25.9",
49
+ "@babel/plugin-transform-json-strings": "^7.25.9",
50
+ "@babel/plugin-transform-logical-assignment-operators": "^7.25.9",
51
+ "@babel/plugin-transform-nullish-coalescing-operator": "^7.25.9",
52
+ "@babel/plugin-transform-numeric-separator": "^7.25.9",
53
+ "@babel/plugin-transform-object-rest-spread": "^7.25.9",
54
+ "@babel/plugin-transform-optional-catch-binding": "^7.25.9",
55
+ "@babel/plugin-transform-optional-chaining": "^7.25.9",
56
+ "@babel/plugin-transform-private-property-in-object": "^7.25.9",
57
+ "@babel/plugin-transform-typescript": "^7.25.9",
58
+ "@babel/plugin-transform-unicode-sets-regex": "^7.25.9",
59
+ "@babel/traverse": "^7.25.9",
60
+ "@babel/types": "^7.26.0",
61
61
  "@bloomberg/record-tuple-polyfill": "^0.0.4",
62
62
  "@rollup/plugin-babel": "^6.0.4",
63
- "@rollup/plugin-commonjs": "^25.0.7",
63
+ "@rollup/plugin-commonjs": "^28.0.1",
64
64
  "@rollup/plugin-json": "^6.1.0",
65
- "@rollup/plugin-node-resolve": "^15.2.3",
66
- "@samual/lib": "0.11.0",
67
- "acorn": "^8.11.3",
65
+ "@rollup/plugin-node-resolve": "^15.3.0",
66
+ "@samual/lib": "^0.13.0",
67
+ "acorn": "^8.14.0",
68
68
  "chalk": "^5.3.0",
69
- "chokidar": "^3.6.0",
70
- "import-meta-resolve": "^4.0.0",
71
- "prettier": "^3.2.5",
69
+ "chokidar": "^4.0.1",
70
+ "import-meta-resolve": "^4.1.0",
71
+ "prettier": "^3.3.3",
72
72
  "proxy-polyfill": "^0.3.2",
73
- "rollup": "^4.16.4",
74
- "terser": "^5.30.4"
73
+ "rollup": "^4.27.4",
74
+ "terser": "^5.36.0"
75
75
  },
76
76
  "peerDependencies": {
77
- "typescript": "5.4.5"
77
+ "typescript": "^5.4.5"
78
78
  },
79
79
  "type": "module",
80
80
  "exports": {
@@ -204,6 +204,7 @@ async function processScript(
204
204
  const bundle = await rollup({
205
205
  input: filePathResolved,
206
206
  plugins: [
207
+ rollupPluginJSON({ preferConst: !0 }),
207
208
  {
208
209
  name: "hackmud-script-manager",
209
210
  async transform(code, id) {
@@ -227,8 +228,7 @@ async function processScript(
227
228
  },
228
229
  babel({ babelHelpers: "bundled", plugins, configFile: !1, extensions: supportedExtensions }),
229
230
  rollupPluginCommonJS(),
230
- rollupPluginNodeResolve({ extensions: supportedExtensions }),
231
- rollupPluginJSON()
231
+ rollupPluginNodeResolve({ extensions: supportedExtensions })
232
232
  ],
233
233
  treeshake: { moduleSideEffects: !1 }
234
234
  }),
@@ -162,21 +162,25 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
162
162
  if (program.scope.hasGlobal("_SECLEVEL"))
163
163
  for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
164
164
  referencePath.replaceWith(t.numericLiteral(seclevel))
165
- let needGetPrototypeOf = !1
165
+ let needGetPrototypeOf = !1,
166
+ needHasOwn = !1
166
167
  if (program.scope.hasGlobal("Object"))
167
168
  for (const referencePath of getReferencePathsToGlobal("Object", program))
168
169
  if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
169
- assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:241:64")
170
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:242:64")
170
171
  if ("getPrototypeOf" == referencePath.parent.property.name) {
171
172
  referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_GET_PROTOTYPE_OF_`))
172
173
  needGetPrototypeOf = !0
174
+ } else if ("hasOwn" == referencePath.parent.property.name) {
175
+ referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_HAS_OWN_`))
176
+ needHasOwn = !0
173
177
  }
174
178
  }
175
179
  const consoleMethodsReferenced = new Set()
176
180
  if (program.scope.hasGlobal("console"))
177
181
  for (const referencePath of getReferencePathsToGlobal("console", program))
178
182
  if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
179
- assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:256:64")
183
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:260:64")
180
184
  referencePath.parentPath.replaceWith(
181
185
  t.identifier(`_${uniqueId}_CONSOLE_METHOD_${referencePath.parent.property.name}_`)
182
186
  )
@@ -184,13 +188,13 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
184
188
  }
185
189
  const lastStatement = program.node.body.at(-1)
186
190
  let exportDefaultName
187
- assert(lastStatement, "src/processScript/transform.ts:270:27 program is empty")
191
+ assert(lastStatement, "src/processScript/transform.ts:274:27 program is empty")
188
192
  if ("ExportNamedDeclaration" == lastStatement.type) {
189
193
  program.node.body.pop()
190
194
  for (const specifier of lastStatement.specifiers) {
191
195
  assert(
192
196
  "ExportSpecifier" == specifier.type,
193
- `src/processScript/transform.ts:276:51 ${specifier.type} is currently unsupported`
197
+ `src/processScript/transform.ts:280:51 ${specifier.type} is currently unsupported`
194
198
  )
195
199
  if (
196
200
  "default" !=
@@ -292,11 +296,11 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
292
296
  let hoistedGlobalBlockFunctions = 0
293
297
  for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
294
298
  if ("VariableDeclaration" == globalBlockStatement.type) {
295
- assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:390:59")
299
+ assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:394:59")
296
300
  const declarator = globalBlockStatement.declarations[0]
297
301
  assert(
298
302
  "Identifier" == declarator.id.type,
299
- `src/processScript/transform.ts:394:51 declarator.id.type was "${declarator.id.type}"`
303
+ `src/processScript/transform.ts:398:51 declarator.id.type was "${declarator.id.type}"`
300
304
  )
301
305
  program.scope.crawl()
302
306
  if (program.scope.hasGlobal(declarator.id.name)) {
@@ -311,9 +315,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
311
315
  Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
312
316
  ) {
313
317
  const binding = program.scope.getBinding(declarator.id.name)
314
- assert(binding, "src/processScript/transform.ts:413:23")
318
+ assert(binding, "src/processScript/transform.ts:417:23")
315
319
  for (const referencePath of binding.referencePaths) {
316
- assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:416:56")
320
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:420:56")
317
321
  referencePath.replaceWith(
318
322
  t.memberExpression(
319
323
  t.identifier(`_${uniqueId}_G_`),
@@ -361,16 +365,16 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
361
365
  } else globalBlockVariables.add(declarator.id.name)
362
366
  } else if ("ClassDeclaration" == globalBlockStatement.type) {
363
367
  program.scope.crawl()
364
- assert(globalBlockStatement.id, "src/processScript/transform.ts:473:37")
368
+ assert(globalBlockStatement.id, "src/processScript/transform.ts:477:37")
365
369
  if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
366
370
  globalBlock.body.splice(globalBlockIndex, 1)
367
371
  const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
368
372
  [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
369
373
  program.scope.crawl()
370
374
  const binding = program.scope.getBinding(globalBlockStatement.id.name)
371
- assert(binding, "src/processScript/transform.ts:485:22")
375
+ assert(binding, "src/processScript/transform.ts:489:22")
372
376
  for (const referencePath of binding.referencePaths) {
373
- assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:488:55")
377
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:492:55")
374
378
  referencePath.replaceWith(
375
379
  t.memberExpression(t.identifier(`_${uniqueId}_G_`), t.identifier(referencePath.node.name))
376
380
  )
@@ -448,6 +452,31 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
448
452
  )
449
453
  ])
450
454
  )
455
+ needHasOwn &&
456
+ mainFunction.body.body.unshift(
457
+ t.variableDeclaration("let", [
458
+ t.variableDeclarator(
459
+ t.identifier(`_${uniqueId}_HAS_OWN_`),
460
+ t.callExpression(
461
+ t.memberExpression(
462
+ t.memberExpression(
463
+ t.identifier(
464
+ globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
465
+ ),
466
+ t.identifier("call")
467
+ ),
468
+ t.identifier("bind")
469
+ ),
470
+ [
471
+ t.memberExpression(
472
+ t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
473
+ t.identifier("hasOwnProperty")
474
+ )
475
+ ]
476
+ )
477
+ )
478
+ ])
479
+ )
451
480
  consoleMethodsReferenced.size &&
452
481
  mainFunction.body.body.unshift(
453
482
  t.variableDeclaration(
@@ -537,7 +566,7 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
537
566
  }
538
567
  },
539
568
  ClassBody({ node: classBody, scope, parent }) {
540
- assert(t.isClass(parent), "src/processScript/transform.ts:658:30")
569
+ assert(t.isClass(parent), "src/processScript/transform.ts:687:30")
541
570
  let thisIsReferenced = !1
542
571
  for (const classMethod of classBody.body) {
543
572
  if ("ClassMethod" != classMethod.type) continue
@@ -629,39 +658,31 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
629
658
  })
630
659
  return { file, seclevel }
631
660
  function createGetFunctionPrototypeNode() {
632
- for (const globalFunction of globalFunctionsUnder7Characters)
633
- if (!program.scope.hasOwnBinding(globalFunction))
634
- return t.memberExpression(
635
- t.memberExpression(t.identifier(globalFunction), t.identifier("constructor")),
636
- t.identifier("prototype")
637
- )
661
+ const name = globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
638
662
  return t.memberExpression(
639
- t.memberExpression(
640
- t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
641
- t.identifier("constructor")
642
- ),
643
- t.identifier("prototype")
663
+ name ? t.identifier(name) : t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
664
+ t.identifier("__proto__")
644
665
  )
645
666
  }
646
667
  function processFakeSubscriptObject(fakeSubscriptObjectName, seclevel) {
647
668
  for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
648
- assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:774:60")
669
+ assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:793:60")
649
670
  assert("Identifier" == referencePath.parent.property.type)
650
671
  assert(
651
672
  "MemberExpression" == referencePath.parentPath.parentPath?.node.type,
652
- "src/processScript/transform.ts:776:81"
673
+ "src/processScript/transform.ts:795:81"
653
674
  )
654
675
  assert(
655
676
  "Identifier" == referencePath.parentPath.parentPath.node.property.type,
656
- "src/processScript/transform.ts:777:83"
677
+ "src/processScript/transform.ts:796:83"
657
678
  )
658
679
  assert(
659
680
  /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
660
- `src/processScript/transform.ts:781:8 invalid user "${referencePath.parent.property.name}" in subscript`
681
+ `src/processScript/transform.ts:800:8 invalid user "${referencePath.parent.property.name}" in subscript`
661
682
  )
662
683
  assert(
663
684
  /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
664
- `src/processScript/transform.ts:786:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
685
+ `src/processScript/transform.ts:805:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
665
686
  )
666
687
  if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
667
688
  referencePath.parentPath.parentPath.replaceWith(
package/push.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Cache } from "@samual/lib/Cache"
1
+ import { AutoMap } from "@samual/lib/AutoMap"
2
2
  import { ensure, assert } from "@samual/lib/assert"
3
3
  import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
4
4
  import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
@@ -84,8 +84,8 @@ async function push(
84
84
  return new NoUsersError(
85
85
  "Could not find any users. Either provide the names of your users or log into a user in hackmud"
86
86
  )
87
- const usersToScriptsToPush = new Cache(_user => new Map()),
88
- scriptNamesToUsers = new Cache(_scriptName => new Set())
87
+ const usersToScriptsToPush = new AutoMap(_user => new Map()),
88
+ scriptNamesToUsers = new AutoMap(_scriptName => new Set())
89
89
  for (const script of scripts) {
90
90
  const [user, scriptName] = script.split(".")
91
91
  assert(user, "src/push.ts:105:16")
@@ -121,7 +121,7 @@ async function push(
121
121
  for (const user of users)
122
122
  if (!usersToScriptsToPush.get(user).has(scriptName))
123
123
  return new NoScriptsError(`Could not find script ${user}.${scriptName} to push`)
124
- const pathsToUsers = new Cache(_path => new Set())
124
+ const pathsToUsers = new AutoMap(_path => new Set())
125
125
  for (const [user, scriptsToPush] of usersToScriptsToPush)
126
126
  for (const path of scriptsToPush.values()) pathsToUsers.get(path).add(user)
127
127
  const allInfo = []
package/watch.js CHANGED
@@ -1,10 +1,10 @@
1
- import { Cache } from "@samual/lib/Cache"
1
+ import { AutoMap } from "@samual/lib/AutoMap"
2
2
  import { assert } from "@samual/lib/assert"
3
3
  import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
4
4
  import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
5
5
  import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
6
6
  import { watch as watch$1 } from "chokidar"
7
- import { readFile, writeFile } from "fs/promises"
7
+ import { stat, readFile, writeFile } from "fs/promises"
8
8
  import { extname, basename, resolve } from "path"
9
9
  import { supportedExtensions } from "./constants.js"
10
10
  import { generateTypeDeclaration } from "./generateTypeDeclaration.js"
@@ -59,7 +59,8 @@ async function watch(
59
59
  } = {}
60
60
  ) {
61
61
  if (!scripts.length) throw Error("scripts option was an empty array")
62
- const scriptNamesToUsers = new Cache(_scriptName => new Set()),
62
+ if (!(await stat(sourceDirectory)).isDirectory()) throw Error("Target folder must be a folder")
63
+ const scriptNamesToUsers = new AutoMap(_scriptName => new Set()),
63
64
  wildScriptUsers = new Set(),
64
65
  wildUserScripts = new Set()
65
66
  let pushEverything = !1
@@ -91,7 +92,7 @@ async function watch(
91
92
  )
92
93
  )
93
94
  return
94
- const scriptNamesToUsersToSkip = new Cache(_scriptName => [])
95
+ const scriptNamesToUsersToSkip = new AutoMap(_scriptName => [])
95
96
  await Promise.all(
96
97
  (await readDirectoryWithStats(sourceDirectory)).map(async ({ stats, name, path }) => {
97
98
  if (stats.isDirectory())
@@ -136,7 +137,7 @@ async function watch(
136
137
  forceQuineCheats
137
138
  }))
138
139
  } catch (error) {
139
- assert(error instanceof Error, "src/watch.ts:141:36")
140
+ assert(error instanceof Error, "src/watch.ts:146:36")
140
141
  onPush?.({ path, users: [], characterCount: 0, error })
141
142
  return
142
143
  }
@@ -181,7 +182,7 @@ async function watch(
181
182
  forceQuineCheats
182
183
  }))
183
184
  } catch (error) {
184
- assert(error instanceof Error, "src/watch.ts:177:35")
185
+ assert(error instanceof Error, "src/watch.ts:182:35")
185
186
  onPush?.({ path, users: [], characterCount: 0, error })
186
187
  return
187
188
  }
@@ -196,7 +197,7 @@ async function watch(
196
197
  try {
197
198
  await writeFile(typeDeclarationPath, typeDeclaration)
198
199
  } catch (error) {
199
- assert(error instanceof Error, "src/watch.ts:210:35")
200
+ assert(error instanceof Error, "src/watch.ts:215:35")
200
201
  if ("EISDIR" != error.code) throw error
201
202
  typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
202
203
  await writeFile(typeDeclarationPath, typeDeclaration)