hackmud-script-manager 0.20.4-550e28d → 0.20.4-66e0e13

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/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"
@@ -29,6 +29,7 @@ import "@babel/plugin-transform-private-property-in-object"
29
29
  import "@babel/plugin-transform-unicode-sets-regex"
30
30
  import "@babel/traverse"
31
31
  import "@babel/types"
32
+ import "@rollup/plugin-alias"
32
33
  import "@rollup/plugin-babel"
33
34
  import "@rollup/plugin-commonjs"
34
35
  import "@rollup/plugin-json"
@@ -59,7 +60,8 @@ async function watch(
59
60
  } = {}
60
61
  ) {
61
62
  if (!scripts.length) throw Error("scripts option was an empty array")
62
- const scriptNamesToUsers = new Cache(_scriptName => new Set()),
63
+ if (!(await stat(sourceDirectory)).isDirectory()) throw Error("Target folder must be a folder")
64
+ const scriptNamesToUsers = new AutoMap(_scriptName => new Set()),
63
65
  wildScriptUsers = new Set(),
64
66
  wildUserScripts = new Set()
65
67
  let pushEverything = !1
@@ -72,10 +74,11 @@ async function watch(
72
74
  : scriptName && "*" != scriptName ? wildUserScripts.add(scriptName)
73
75
  : (pushEverything = !0)
74
76
  }
75
- const watcher = watch$1(["*.ts", "*.js", "*/*.ts", "*/*.js"], {
77
+ const watcher = watch$1(".", {
76
78
  cwd: sourceDirectory,
77
79
  awaitWriteFinish: { stabilityThreshold: 100 },
78
- ignored: "*.d.ts"
80
+ ignored: (path, stats) =>
81
+ !!stats?.isFile() && !(path.endsWith(".js") || (path.endsWith(".ts") && !path.endsWith(".d.ts")))
79
82
  }).on("change", async path => {
80
83
  if (path.endsWith(".d.ts")) return
81
84
  const extension = extname(path)
@@ -91,7 +94,7 @@ async function watch(
91
94
  )
92
95
  )
93
96
  return
94
- const scriptNamesToUsersToSkip = new Cache(_scriptName => [])
97
+ const scriptNamesToUsersToSkip = new AutoMap(_scriptName => [])
95
98
  await Promise.all(
96
99
  (await readDirectoryWithStats(sourceDirectory)).map(async ({ stats, name, path }) => {
97
100
  if (stats.isDirectory())
@@ -117,27 +120,22 @@ async function watch(
117
120
  for (const user of scriptNamesToUsers.get(scriptName)) usersToPushToSet.add(user)
118
121
  const usersToPushTo = [...usersToPushToSet].filter(user => !scriptNamesToUsersToSkip.has(user))
119
122
  if (!usersToPushTo.length) {
120
- onPush?.({ path, users: [], characterCount: 0, error: Error("no users to push to") })
123
+ onPush?.({ path, users: [], characterCount: 0, error: Error("no users to push to"), warnings: [] })
121
124
  return
122
125
  }
123
126
  const uniqueId = Math.floor(Math.random() * 2 ** 52)
124
127
  .toString(36)
125
128
  .padStart(11, "0"),
126
129
  filePath = resolve(sourceDirectory, path)
127
- let minifiedCode
130
+ let minifiedCode, warnings
128
131
  try {
129
- ;({ script: minifiedCode } = await processScript(await readFile(filePath, { encoding: "utf8" }), {
130
- minify,
131
- scriptUser: !0,
132
- scriptName,
133
- uniqueId,
134
- filePath,
135
- mangleNames,
136
- forceQuineCheats
137
- }))
132
+ ;({ script: minifiedCode, warnings } = await processScript(
133
+ await readFile(filePath, { encoding: "utf8" }),
134
+ { minify, scriptUser: !0, scriptName, uniqueId, filePath, mangleNames, forceQuineCheats }
135
+ ))
138
136
  } catch (error) {
139
- assert(error instanceof Error, "src/watch.ts:141:36")
140
- onPush?.({ path, users: [], characterCount: 0, error })
137
+ assert(error instanceof Error, "src/watch.ts:149:36")
138
+ onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
141
139
  return
142
140
  }
143
141
  await Promise.all(
@@ -154,7 +152,8 @@ async function watch(
154
152
  path,
155
153
  users: usersToPushTo,
156
154
  characterCount: countHackmudCharacters(minifiedCode),
157
- error: void 0
155
+ error: void 0,
156
+ warnings
158
157
  })
159
158
  return
160
159
  }
@@ -168,25 +167,27 @@ async function watch(
168
167
  )
169
168
  )
170
169
  return
171
- const filePath = resolve(sourceDirectory, path),
170
+ const sourceDirectoryResolved = resolve(sourceDirectory),
171
+ filePath = resolve(sourceDirectoryResolved, path),
172
172
  sourceCode = await readFile(filePath, { encoding: "utf8" })
173
- let script
173
+ let script, warnings
174
174
  try {
175
- ;({ script } = await processScript(sourceCode, {
175
+ ;({ script, warnings } = await processScript(sourceCode, {
176
176
  minify,
177
177
  scriptUser: user,
178
178
  scriptName,
179
179
  filePath,
180
180
  mangleNames,
181
- forceQuineCheats
181
+ forceQuineCheats,
182
+ rootFolderPath: sourceDirectoryResolved
182
183
  }))
183
184
  } catch (error) {
184
- assert(error instanceof Error, "src/watch.ts:177:35")
185
- onPush?.({ path, users: [], characterCount: 0, error })
185
+ assert(error instanceof Error, "src/watch.ts:196:35")
186
+ onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
186
187
  return
187
188
  }
188
189
  await writeFilePersistent(resolve(hackmudDirectory, user, "scripts", scriptName + ".js"), script)
189
- onPush?.({ path, users: [user], characterCount: countHackmudCharacters(script), error: void 0 })
190
+ onPush?.({ path, users: [user], characterCount: countHackmudCharacters(script), error: void 0, warnings })
190
191
  })
191
192
  onReady && watcher.on("ready", onReady)
192
193
  if (!typeDeclarationPath_) return
@@ -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:229:35")
200
201
  if ("EISDIR" != error.code) throw error
201
202
  typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
202
203
  await writeFile(typeDeclarationPath, typeDeclaration)