hackmud-script-manager 0.20.4-7caccd9 → 0.20.4-9181c22

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