hackmud-script-manager 0.21.1-2db6aca → 0.21.1-377522a

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/bin/hsm.js CHANGED
@@ -64,7 +64,7 @@ if (process.version.startsWith("v21.")) {
64
64
  )
65
65
  }
66
66
  if ("v" == commands[0] || "version" == commands[0] || popOption("version", "v")?.value) {
67
- console.log("0.21.1-2db6aca")
67
+ console.log("0.21.1-377522a")
68
68
  process.exit()
69
69
  }
70
70
  let warnedDeprecatedEmitDtsAlias = !1
@@ -412,7 +412,7 @@ function logHelp() {
412
412
  default:
413
413
  console.log(
414
414
  colourS(
415
- `${colourJ("Hackmud Script Manager")}\n${colourN("Version") + colourS(": ") + colourV("0.21.1-2db6aca")}\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`
415
+ `${colourJ("Hackmud Script Manager")}\n${colourN("Version") + colourS(": ") + colourV("0.21.1-377522a")}\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`
416
416
  )
417
417
  )
418
418
  }
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: any[], rng?: ()=>number) => any
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: object, keys: string[]) => any
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) => any
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, [k: `$${string}`]: never }
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, $type?: keyof MongoTypeStringsToTypes | (string & {}) }
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: 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 MongoUpdateOperators<T extends MongoObject> = Partial<{
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, MongoCommandValue> & T>
761
- $setOnInsert: Partial<Record<string, MongoCommandValue> & T>
762
- $unset: Partial<Record<string, ""> & T>
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
- true extends (1 extends TProjection[keyof TProjection] ? true : TProjection[keyof TProjection]) ?
869
- (TProjection extends { _id: false | 0 } ? {} : { _id: TDocument extends { _id: infer TId } ? TId : MongoId }) &
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
- : { [k: string]: MongoValue } & { [K in keyof TDocument as K extends keyof TProjection ? never : K]: TDocument[K] }
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 extends any[] = any[]> = { name: string, call: (...args: TArgs) => unknown }
887
+ type Scriptor<TArgs = any> = { name: string, call: (args: TArgs) => unknown }
889
888
  type Context = CliContext | SubscriptContext | ScriptorContext | BrainContext
890
- type ObjectId = { $oid: string }
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: () => ObjectId
980
+ ObjectId: () => MongoObjectId
982
981
  }
983
982
 
984
983
  /** Debug Log.
@@ -11,24 +11,26 @@ async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
11
11
  allScripts = {},
12
12
  allAnyScripts = {}
13
13
  await Promise.all(
14
- (await readDirectoryWithStats(sourceDirectory)).map(async ({ stats, name }) => {
15
- if (stats.isFile())
16
- name.endsWith(".ts")
17
- ? name.endsWith(".d.ts") || wildScripts.push(basename(name, ".ts"))
18
- : name.endsWith(".js") && wildAnyScripts.push(basename(name, ".js"))
19
- else if (stats.isDirectory()) {
20
- const scripts = [],
21
- anyScripts = []
22
- allScripts[name] = scripts
23
- allAnyScripts[name] = anyScripts
24
- users.add(name)
25
- for (const child of await readDirectoryWithStats(resolve(sourceDirectory, name)))
26
- child.stats.isFile() &&
27
- (child.name.endsWith(".ts")
28
- ? name.endsWith(".d.ts") || scripts.push(basename(child.name, ".ts"))
29
- : child.name.endsWith(".js") && anyScripts.push(basename(child.name, ".js")))
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.1-2db6aca",
3
+ "version": "0.21.1-377522a",
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": {
@@ -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("/node_modules/")) return (await preprocess(code, { uniqueId })).code
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) {
@@ -4,23 +4,11 @@ import babelTraverse from "@babel/traverse"
4
4
  import t from "@babel/types"
5
5
  import { assert } from "@samual/lib/assert"
6
6
  import { spliceString } from "@samual/lib/spliceString"
7
- import { tokenizer, tokTypes } from "acorn"
8
7
  import { resolve } from "import-meta-resolve"
9
8
  const { default: traverse } = babelTraverse,
10
- { default: generate } = babelGenerator,
11
- SUBSCRIPT_PREFIXES = ["s", "fs", "4s", "hs", "3s", "ms", "2s", "ls", "1s", "ns", "0s"]
9
+ { default: generate } = babelGenerator
12
10
  async function preprocess(code, { uniqueId = "00000000000" } = {}) {
13
- assert(/^\w{11}$/.test(uniqueId), "src/processScript/preprocess.ts:26:36")
14
- const tokensIterable = tokenizer(code, { ecmaVersion: "latest" })
15
- for (const token of tokensIterable) {
16
- assert("value" in token, "src/processScript/preprocess.ts:31:28")
17
- if (token.type != tokTypes.privateId) continue
18
- assert("string" == typeof token.value, "src/processScript/preprocess.ts:36:42")
19
- if (!SUBSCRIPT_PREFIXES.includes(token.value)) continue
20
- const nextToken = tokensIterable.getToken()
21
- if (nextToken.type != tokTypes._in && nextToken.type != tokTypes.dot)
22
- throw SyntaxError("Subscripts must be in the form of #fs.foo.bar")
23
- }
11
+ assert(/^\w{11}$/.test(uniqueId), "src/processScript/preprocess.ts:23:36")
24
12
  const sourceCode = code
25
13
  let lengthBefore, file, program
26
14
  do {
@@ -59,7 +47,7 @@ async function preprocess(code, { uniqueId = "00000000000" } = {}) {
59
47
  })
60
48
  break
61
49
  } catch (error_) {
62
- assert(error_ instanceof SyntaxError, "src/processScript/preprocess.ts:89:42")
50
+ assert(error_ instanceof SyntaxError, "src/processScript/preprocess.ts:67:42")
63
51
  error = error_
64
52
  }
65
53
  if ("BABEL_PARSER_SYNTAX_ERROR" != error.code || "PrivateInExpectedIn" != error.reasonCode) {
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(({ stats }) => stats.isDirectory()),
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
@@ -133,10 +133,19 @@ async function watch(
133
133
  try {
134
134
  ;({ script: minifiedCode, warnings } = await processScript(
135
135
  await readFile(filePath, { encoding: "utf8" }),
136
- { minify, scriptUser: !0, scriptName, uniqueId, filePath, mangleNames, forceQuineCheats }
136
+ {
137
+ minify,
138
+ scriptUser: !0,
139
+ scriptName,
140
+ uniqueId,
141
+ filePath,
142
+ mangleNames,
143
+ forceQuineCheats,
144
+ rootFolderPath
145
+ }
137
146
  ))
138
147
  } catch (error) {
139
- assert(error instanceof Error, "src/watch.ts:151:36")
148
+ assert(error instanceof Error, "src/watch.ts:160:36")
140
149
  onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
141
150
  return
142
151
  }
@@ -184,7 +193,7 @@ async function watch(
184
193
  rootFolderPath
185
194
  }))
186
195
  } catch (error) {
187
- assert(error instanceof Error, "src/watch.ts:198:35")
196
+ assert(error instanceof Error, "src/watch.ts:207:35")
188
197
  onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
189
198
  return
190
199
  }
@@ -199,7 +208,7 @@ async function watch(
199
208
  try {
200
209
  await writeFile(typeDeclarationPath, typeDeclaration)
201
210
  } catch (error) {
202
- assert(error instanceof Error, "src/watch.ts:231:35")
211
+ assert(error instanceof Error, "src/watch.ts:240:35")
203
212
  if ("EISDIR" != error.code) throw error
204
213
  typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
205
214
  await writeFile(typeDeclarationPath, typeDeclaration)