hackmud-script-manager 0.20.4-abe4703 → 0.20.4-ae3052c

Sign up to get free protection for your applications and to get access to all the features.
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"
@@ -58,7 +59,8 @@ async function watch(
58
59
  } = {}
59
60
  ) {
60
61
  if (!scripts.length) throw Error("scripts option was an empty array")
61
- 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()),
62
64
  wildScriptUsers = new Set(),
63
65
  wildUserScripts = new Set()
64
66
  let pushEverything = !1
@@ -71,10 +73,11 @@ async function watch(
71
73
  : scriptName && "*" != scriptName ? wildUserScripts.add(scriptName)
72
74
  : (pushEverything = !0)
73
75
  }
74
- const watcher = watch$1(["*.ts", "*.js", "*/*.ts", "*/*.js"], {
76
+ const watcher = watch$1(".", {
75
77
  cwd: sourceDirectory,
76
78
  awaitWriteFinish: { stabilityThreshold: 100 },
77
- ignored: "*.d.ts"
79
+ ignored: (path, stats) =>
80
+ !!stats?.isFile() && !(path.endsWith(".js") || (path.endsWith(".ts") && !path.endsWith(".d.ts")))
78
81
  }).on("change", async path => {
79
82
  if (path.endsWith(".d.ts")) return
80
83
  const extension = extname(path)
@@ -90,7 +93,7 @@ async function watch(
90
93
  )
91
94
  )
92
95
  return
93
- const scriptNamesToUsersToSkip = new Cache(_scriptName => [])
96
+ const scriptNamesToUsersToSkip = new AutoMap(_scriptName => [])
94
97
  await Promise.all(
95
98
  (await readDirectoryWithStats(sourceDirectory)).map(async ({ stats, name, path }) => {
96
99
  if (stats.isDirectory())
@@ -116,27 +119,22 @@ async function watch(
116
119
  for (const user of scriptNamesToUsers.get(scriptName)) usersToPushToSet.add(user)
117
120
  const usersToPushTo = [...usersToPushToSet].filter(user => !scriptNamesToUsersToSkip.has(user))
118
121
  if (!usersToPushTo.length) {
119
- onPush?.({ path, users: [], characterCount: 0, error: Error("no users to push to") })
122
+ onPush?.({ path, users: [], characterCount: 0, error: Error("no users to push to"), warnings: [] })
120
123
  return
121
124
  }
122
125
  const uniqueId = Math.floor(Math.random() * 2 ** 52)
123
126
  .toString(36)
124
127
  .padStart(11, "0"),
125
128
  filePath = resolve(sourceDirectory, path)
126
- let minifiedCode
129
+ let minifiedCode, warnings
127
130
  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
- }))
131
+ ;({ script: minifiedCode, warnings } = await processScript(
132
+ await readFile(filePath, { encoding: "utf8" }),
133
+ { minify, scriptUser: !0, scriptName, uniqueId, filePath, mangleNames, forceQuineCheats }
134
+ ))
137
135
  } catch (error) {
138
- assert(error instanceof Error, "src/watch.ts:141:36")
139
- onPush?.({ path, users: [], characterCount: 0, error })
136
+ assert(error instanceof Error, "src/watch.ts:149:36")
137
+ onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
140
138
  return
141
139
  }
142
140
  await Promise.all(
@@ -153,7 +151,8 @@ async function watch(
153
151
  path,
154
152
  users: usersToPushTo,
155
153
  characterCount: countHackmudCharacters(minifiedCode),
156
- error: void 0
154
+ error: void 0,
155
+ warnings
157
156
  })
158
157
  return
159
158
  }
@@ -169,9 +168,9 @@ async function watch(
169
168
  return
170
169
  const filePath = resolve(sourceDirectory, path),
171
170
  sourceCode = await readFile(filePath, { encoding: "utf8" })
172
- let script
171
+ let script, warnings
173
172
  try {
174
- ;({ script } = await processScript(sourceCode, {
173
+ ;({ script, warnings } = await processScript(sourceCode, {
175
174
  minify,
176
175
  scriptUser: user,
177
176
  scriptName,
@@ -180,12 +179,12 @@ async function watch(
180
179
  forceQuineCheats
181
180
  }))
182
181
  } catch (error) {
183
- assert(error instanceof Error, "src/watch.ts:177:35")
184
- onPush?.({ path, users: [], characterCount: 0, error })
182
+ assert(error instanceof Error, "src/watch.ts:190:35")
183
+ onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
185
184
  return
186
185
  }
187
186
  await writeFilePersistent(resolve(hackmudDirectory, user, "scripts", scriptName + ".js"), script)
188
- onPush?.({ path, users: [user], characterCount: countHackmudCharacters(script), error: void 0 })
187
+ onPush?.({ path, users: [user], characterCount: countHackmudCharacters(script), error: void 0, warnings })
189
188
  })
190
189
  onReady && watcher.on("ready", onReady)
191
190
  if (!typeDeclarationPath_) return
@@ -195,7 +194,7 @@ async function watch(
195
194
  try {
196
195
  await writeFile(typeDeclarationPath, typeDeclaration)
197
196
  } catch (error) {
198
- assert(error instanceof Error, "src/watch.ts:210:35")
197
+ assert(error instanceof Error, "src/watch.ts:223:35")
199
198
  if ("EISDIR" != error.code) throw error
200
199
  typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
201
200
  await writeFile(typeDeclarationPath, typeDeclaration)