hackmud-script-manager 0.20.5-97101bc → 0.21.1-0b2f476

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