hackmud-script-manager 0.20.4-c1681d7 → 0.20.4-cf7622f
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -0
- package/bin/hsm.js +192 -244
- package/env.d.ts +4 -7
- package/generateTypeDeclaration.js +2 -1
- package/index.js +1 -0
- package/package.json +1 -1
- package/processScript/index.js +2 -2
- package/processScript/minify.js +5 -14
- package/processScript/postprocess.js +1 -1
- package/processScript/preprocess.js +5 -3
- package/processScript/transform.js +53 -79
- package/watch.js +6 -4
package/README.md
CHANGED
@@ -20,6 +20,10 @@ You can read about how HSM works [in my blog post](https://samual.uk/blog/js-cod
|
|
20
20
|
> ```
|
21
21
|
> You will need to run `Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser` in PowerShell as an administrator. For more information, see [Microsoft's page about Execution Policies](https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.4).
|
22
22
|
|
23
|
+
![image](https://github.com/samualtnorman/hackmud-script-manager/assets/18307063/69a371fe-f8c8-43fe-b3c7-39f3735ce6fb)
|
24
|
+
![image](https://github.com/samualtnorman/hackmud-script-manager/assets/18307063/08103f9e-74fa-4a56-a739-94858ba8c139)
|
25
|
+
![image](https://github.com/samualtnorman/hackmud-script-manager/assets/18307063/25ccb86d-1fe3-4632-b703-ac47f5b32c9c)
|
26
|
+
|
23
27
|
## Features
|
24
28
|
- Minification
|
25
29
|
- This includes auto quine cheating.
|
package/bin/hsm.js
CHANGED
@@ -5,14 +5,16 @@ import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
|
|
5
5
|
import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
|
6
6
|
import { writeFile, readFile } from "fs/promises"
|
7
7
|
import { homedir } from "os"
|
8
|
-
import { extname, basename,
|
8
|
+
import { resolve, extname, basename, dirname, relative } from "path"
|
9
9
|
import { supportedExtensions } from "../constants.js"
|
10
10
|
import { generateTypeDeclaration } from "../generateTypeDeclaration.js"
|
11
11
|
import { pull } from "../pull.js"
|
12
12
|
import { syncMacros } from "../syncMacros.js"
|
13
13
|
import "@samual/lib/readDirectoryWithStats"
|
14
|
+
import "path/posix"
|
14
15
|
import "@samual/lib/copyFilePersistent"
|
15
|
-
const version = "0.20.4-
|
16
|
+
const version = "0.20.4-cf7622f",
|
17
|
+
formatOption = name => colourN(`-${1 == name.length ? "" : "-"}${name}`),
|
16
18
|
options = new Map(),
|
17
19
|
commands = [],
|
18
20
|
userColours = new Cache(user => {
|
@@ -36,10 +38,6 @@ for (const argument of process.argv.slice(2))
|
|
36
38
|
if ("-" == argument[1]) options.set(key.slice(2), value)
|
37
39
|
else for (const option of key.slice(1)) options.set(option, value)
|
38
40
|
} else commands.push(argument)
|
39
|
-
if ("v" == commands[0] || "version" == commands[0] || options.get("version") || options.get("v")) {
|
40
|
-
console.log(version)
|
41
|
-
process.exit()
|
42
|
-
}
|
43
41
|
const pushModule = import("../push.js"),
|
44
42
|
processScriptModule = import("../processScript/index.js"),
|
45
43
|
watchModule = import("../watch.js"),
|
@@ -49,6 +47,7 @@ const pushModule = import("../push.js"),
|
|
49
47
|
colourB = chalk.rgb(202, 202, 202),
|
50
48
|
colourC = chalk.rgb(155, 155, 155),
|
51
49
|
colourD = chalk.rgb(255, 0, 0),
|
50
|
+
colourF = chalk.rgb(255, 128, 0),
|
52
51
|
colourJ = chalk.rgb(255, 244, 4),
|
53
52
|
colourK = chalk.rgb(243, 249, 152),
|
54
53
|
colourL = chalk.rgb(30, 255, 0),
|
@@ -57,162 +56,171 @@ const pushModule = import("../push.js"),
|
|
57
56
|
colourS = chalk.rgb(122, 178, 244),
|
58
57
|
colourV = chalk.rgb(255, 0, 236),
|
59
58
|
colourW = chalk.rgb(255, 150, 224)
|
60
|
-
|
59
|
+
process.version.startsWith("v21.") &&
|
60
|
+
console.warn(
|
61
|
+
colourF(
|
62
|
+
"Warning: Support for Node.js 21 will be dropped in the next minor version of HSM\n You should update your version of Node.js\n https://nodejs.org/en/download/package-manager"
|
63
|
+
)
|
64
|
+
)
|
65
|
+
if ("v" == commands[0] || "version" == commands[0] || popOption("version", "v")?.value) {
|
66
|
+
console.log(version)
|
67
|
+
process.exit()
|
68
|
+
}
|
69
|
+
if (popOption("help", "h")?.value) {
|
61
70
|
logHelp()
|
62
71
|
process.exit()
|
63
72
|
}
|
64
73
|
let autoExit = !0
|
65
74
|
switch (commands[0]) {
|
66
75
|
case "push":
|
76
|
+
case "dev":
|
77
|
+
case "watch":
|
78
|
+
case "golf":
|
79
|
+
case "minify":
|
67
80
|
{
|
68
|
-
const
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
break
|
74
|
-
}
|
75
|
-
const scripts = commands.slice(2)
|
76
|
-
if (scripts.length) {
|
77
|
-
const invalidScript = scripts.find(
|
78
|
-
script => !/^(?:[a-z_][a-z\d_]{0,24}|\*)\.(?:[a-z_][a-z\d_]{0,24}|\*)$/.test(script)
|
79
|
-
)
|
80
|
-
if (invalidScript) {
|
81
|
-
logError(`Invalid script name: ${JSON.stringify(invalidScript)}\n`)
|
82
|
-
logHelp()
|
83
|
-
break
|
84
|
-
}
|
85
|
-
} else scripts.push("*.*")
|
86
|
-
const optionsHasNoMinify = options.has("no-minify")
|
87
|
-
if ((optionsHasNoMinify || options.has("skip-minify")) && options.has("mangle-names")) {
|
81
|
+
const noMinifyOption = popOption("no-minify", "skip-minify"),
|
82
|
+
mangleNamesOption = popOption("mangle-names"),
|
83
|
+
forceQuineCheatsOption = popOption("force-quine-cheats"),
|
84
|
+
noMinifyIncompatibleOption = mangleNamesOption || forceQuineCheatsOption
|
85
|
+
if (noMinifyOption && noMinifyIncompatibleOption) {
|
88
86
|
logError(
|
89
|
-
`Options ${
|
87
|
+
`Options ${formatOption(noMinifyOption.name)} and ${formatOption(noMinifyIncompatibleOption.name)} are incompatible\n`
|
90
88
|
)
|
91
89
|
logHelp()
|
92
|
-
|
90
|
+
process.exit(1)
|
93
91
|
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
92
|
+
noMinifyOption && assertOptionIsBoolean(noMinifyOption)
|
93
|
+
mangleNamesOption && assertOptionIsBoolean(mangleNamesOption)
|
94
|
+
forceQuineCheatsOption && assertOptionIsBoolean(forceQuineCheatsOption)
|
95
|
+
if ("golf" == commands[0] || "minify" == commands[0]) {
|
96
|
+
const watchOption = popOption("watch"),
|
97
|
+
target = commands[1]
|
98
|
+
if (!target) {
|
99
|
+
logError("Must provide target\n")
|
100
|
+
logHelp()
|
101
|
+
process.exit(1)
|
102
|
+
}
|
103
|
+
const fileExtension = extname(target)
|
104
|
+
if (!supportedExtensions.includes(fileExtension)) {
|
98
105
|
logError(
|
99
|
-
`
|
106
|
+
`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${supportedExtensions.map(extension => chalk.bold(extension)).join('", "')}"`
|
100
107
|
)
|
101
|
-
|
102
|
-
break
|
108
|
+
process.exit(1)
|
103
109
|
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
110
|
+
complainAboutUnrecognisedOptions()
|
111
|
+
const { processScript } = await processScriptModule,
|
112
|
+
fileBaseName = basename(target, fileExtension),
|
113
|
+
fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src"),
|
114
|
+
scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName,
|
115
|
+
scriptUser =
|
116
|
+
(
|
117
|
+
"scripts" == basename(resolve(target, "..")) &&
|
118
|
+
"hackmud" == basename(resolve(target, "../../.."))
|
119
|
+
) ?
|
120
|
+
basename(resolve(target, "../.."))
|
121
|
+
: void 0
|
122
|
+
let outputPath =
|
123
|
+
commands[2] ||
|
124
|
+
resolve(
|
125
|
+
dirname(target),
|
126
|
+
fileBaseNameEndsWithDotSrc ? scriptName + ".js"
|
127
|
+
: ".js" == fileExtension ? fileBaseName + ".min.js"
|
128
|
+
: fileBaseName + ".js"
|
129
|
+
)
|
130
|
+
const golfFile = () =>
|
131
|
+
readFile(target, { encoding: "utf8" }).then(async source => {
|
132
|
+
const timeStart = performance.now(),
|
133
|
+
{ script, warnings } = await processScript(source, {
|
134
|
+
minify: noMinifyOption && !noMinifyOption.value,
|
135
|
+
scriptUser,
|
136
|
+
scriptName,
|
137
|
+
filePath: target,
|
138
|
+
mangleNames: mangleNamesOption?.value,
|
139
|
+
forceQuineCheats: forceQuineCheatsOption?.value
|
140
|
+
}),
|
141
|
+
timeTook = performance.now() - timeStart
|
142
|
+
for (const { message, line } of warnings)
|
143
|
+
log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(line + "")}`)
|
144
|
+
await writeFilePersistent(outputPath, script)
|
145
|
+
.catch(error => {
|
146
|
+
if (!commands[2] || "EISDIR" != error.code) throw error
|
147
|
+
outputPath = resolve(outputPath, basename(target, fileExtension) + ".js")
|
148
|
+
return writeFilePersistent(outputPath, script)
|
149
|
+
})
|
150
|
+
.then(() =>
|
151
|
+
log(
|
152
|
+
`Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${chalk.bold(relative(".", outputPath))} | took ${Math.round(100 * timeTook) / 100}ms`
|
153
|
+
)
|
154
|
+
)
|
155
|
+
})
|
156
|
+
if (watchOption) {
|
157
|
+
const { watch: watchFile } = await chokidarModule
|
158
|
+
watchFile(target, { awaitWriteFinish: { stabilityThreshold: 100 } })
|
159
|
+
.on("ready", () => log("Watching " + target))
|
160
|
+
.on("change", golfFile)
|
161
|
+
autoExit = !1
|
162
|
+
} else await golfFile()
|
163
|
+
} else {
|
164
|
+
const hackmudPath = getHackmudPath(),
|
165
|
+
sourcePath = commands[1]
|
166
|
+
if (!sourcePath) {
|
167
|
+
logError(`Must provide the directory to ${"push" == commands[0] ? "push from" : "watch"}\n`)
|
160
168
|
logHelp()
|
161
|
-
|
169
|
+
process.exit(1)
|
162
170
|
}
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
`Options ${colourN("--mangle-names")} and ${colourN(optionsHasNoMinify ? "--no-minify" : "--skip-minify")} are incompatible\n`
|
168
|
-
)
|
169
|
-
logHelp()
|
170
|
-
break
|
171
|
-
}
|
172
|
-
const shouldSkipMinify = options.get("no-minify") || options.get("skip-minify")
|
173
|
-
let shouldMinify
|
174
|
-
if (null != shouldSkipMinify) {
|
175
|
-
if ("boolean" != typeof shouldSkipMinify) {
|
176
|
-
logError(
|
177
|
-
`The value for ${colourN(optionsHasNoMinify ? "--no-minify" : "--skip-minify")} must be ${colourV("true")} or ${colourV("false")}\n`
|
171
|
+
const scripts = commands.slice(2)
|
172
|
+
if (scripts.length) {
|
173
|
+
const invalidScript = scripts.find(
|
174
|
+
script => !/^(?:[a-z_][a-z\d_]{0,24}|\*)\.(?:[a-z_][a-z\d_]{0,24}|\*)$/.test(script)
|
178
175
|
)
|
179
|
-
|
180
|
-
|
176
|
+
if (invalidScript) {
|
177
|
+
logError(`Invalid script name: ${JSON.stringify(invalidScript)}\n`)
|
178
|
+
logHelp()
|
179
|
+
process.exit(1)
|
180
|
+
}
|
181
|
+
} else scripts.push("*.*")
|
182
|
+
if ("push" == commands[0]) {
|
183
|
+
const { push, MissingSourceFolderError, MissingHackmudFolderError, NoUsersError } = await pushModule
|
184
|
+
complainAboutUnrecognisedOptions()
|
185
|
+
const infos = await push(sourcePath, hackmudPath, {
|
186
|
+
scripts,
|
187
|
+
onPush: info => logInfo(info, hackmudPath),
|
188
|
+
minify: noMinifyOption && !noMinifyOption.value,
|
189
|
+
mangleNames: mangleNamesOption?.value,
|
190
|
+
forceQuineCheats: forceQuineCheatsOption?.value
|
191
|
+
})
|
192
|
+
if (infos instanceof Error) {
|
193
|
+
logError(infos.message)
|
194
|
+
if (infos instanceof MissingSourceFolderError || infos instanceof NoUsersError) {
|
195
|
+
console.log()
|
196
|
+
logHelp()
|
197
|
+
} else
|
198
|
+
infos instanceof MissingHackmudFolderError &&
|
199
|
+
log(
|
200
|
+
`If this is not where your hackmud folder is, you can specify it with the\n${colourN("--hackmud-path")}=${colourB("<path>")} option or ${colourN("HSM_HACKMUD_PATH")} environment variable`
|
201
|
+
)
|
202
|
+
} else infos.length || logError("Could not find any scripts to push")
|
203
|
+
} else {
|
204
|
+
const typeDeclarationPathOption = popOption(
|
205
|
+
"type-declaration-path",
|
206
|
+
"type-declaration",
|
207
|
+
"dts",
|
208
|
+
"gen-types"
|
209
|
+
)
|
210
|
+
complainAboutUnrecognisedOptions()
|
211
|
+
const { watch } = await watchModule
|
212
|
+
watch(sourcePath, hackmudPath, {
|
213
|
+
scripts,
|
214
|
+
onPush: info => logInfo(info, hackmudPath),
|
215
|
+
typeDeclarationPath: typeDeclarationPathOption?.value.toString(),
|
216
|
+
minify: noMinifyOption && !noMinifyOption.value,
|
217
|
+
mangleNames: mangleNamesOption?.value,
|
218
|
+
onReady: () => log("Watching"),
|
219
|
+
forceQuineCheats: forceQuineCheatsOption?.value
|
220
|
+
})
|
221
|
+
autoExit = !1
|
181
222
|
}
|
182
|
-
shouldMinify = !shouldSkipMinify
|
183
223
|
}
|
184
|
-
const shouldMangleNames = options.get("mangle-names")
|
185
|
-
if (null != shouldMangleNames && "boolean" != typeof shouldMangleNames) {
|
186
|
-
logError(
|
187
|
-
`The value for ${colourN("--mangle-names")} must be ${colourV("true")} or ${colourV("false")}\n`
|
188
|
-
)
|
189
|
-
logHelp()
|
190
|
-
break
|
191
|
-
}
|
192
|
-
const shouldforceQuineCheats = options.get("force-quine-cheats")
|
193
|
-
if (null != shouldforceQuineCheats && "boolean" != typeof shouldforceQuineCheats) {
|
194
|
-
logError(
|
195
|
-
`The value for ${colourN("--force-quine-cheats")} must be ${colourV("true")} or ${colourV("false")}\n`
|
196
|
-
)
|
197
|
-
logHelp()
|
198
|
-
break
|
199
|
-
}
|
200
|
-
const { watch } = await watchModule
|
201
|
-
watch(sourcePath, hackmudPath, {
|
202
|
-
scripts,
|
203
|
-
onPush: info => logInfo(info, hackmudPath),
|
204
|
-
typeDeclarationPath: (
|
205
|
-
options.get("type-declaration-path") ||
|
206
|
-
options.get("type-declaration") ||
|
207
|
-
options.get("dts") ||
|
208
|
-
options.get("gen-types")
|
209
|
-
)?.toString(),
|
210
|
-
minify: shouldMinify,
|
211
|
-
mangleNames: shouldMangleNames,
|
212
|
-
onReady: () => log("Watching"),
|
213
|
-
forceQuineCheats: shouldforceQuineCheats
|
214
|
-
})
|
215
|
-
autoExit = !1
|
216
224
|
}
|
217
225
|
break
|
218
226
|
case "pull":
|
@@ -222,8 +230,9 @@ switch (commands[0]) {
|
|
222
230
|
if (!script) {
|
223
231
|
logError("Must provide the script to pull\n")
|
224
232
|
logHelp()
|
225
|
-
|
233
|
+
process.exit(1)
|
226
234
|
}
|
235
|
+
complainAboutUnrecognisedOptions()
|
227
236
|
const sourcePath = commands[2] || "."
|
228
237
|
await pull(sourcePath, hackmudPath, script).catch(error => {
|
229
238
|
console.error(error)
|
@@ -233,8 +242,9 @@ switch (commands[0]) {
|
|
233
242
|
break
|
234
243
|
case "sync-macros":
|
235
244
|
{
|
236
|
-
const hackmudPath = getHackmudPath()
|
237
|
-
|
245
|
+
const hackmudPath = getHackmudPath()
|
246
|
+
complainAboutUnrecognisedOptions()
|
247
|
+
const { macrosSynced, usersSynced } = await syncMacros(hackmudPath)
|
238
248
|
log(`Synced ${macrosSynced} macros to ${usersSynced} users`)
|
239
249
|
}
|
240
250
|
break
|
@@ -243,18 +253,20 @@ switch (commands[0]) {
|
|
243
253
|
case "gen-dts":
|
244
254
|
case "gen-types":
|
245
255
|
{
|
246
|
-
const
|
256
|
+
const hackmudPath = getHackmudPath(),
|
257
|
+
target = commands[1]
|
247
258
|
if (!target) {
|
248
259
|
logError("Must provide target directory\n")
|
249
260
|
logHelp()
|
250
|
-
|
261
|
+
process.exit(1)
|
251
262
|
}
|
263
|
+
complainAboutUnrecognisedOptions()
|
252
264
|
const sourcePath = resolve(target),
|
253
265
|
outputPath = commands[2] || "./player.d.ts",
|
254
|
-
typeDeclaration = await generateTypeDeclaration(sourcePath,
|
266
|
+
typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
|
255
267
|
let typeDeclarationPath = resolve(outputPath)
|
256
268
|
await writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
|
257
|
-
assert(error instanceof Error, "src/bin/hsm.ts:
|
269
|
+
assert(error instanceof Error, "src/bin/hsm.ts:343:35")
|
258
270
|
if ("EISDIR" != error.code) throw error
|
259
271
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
260
272
|
return writeFile(typeDeclarationPath, typeDeclaration)
|
@@ -266,99 +278,6 @@ switch (commands[0]) {
|
|
266
278
|
case "h":
|
267
279
|
logHelp()
|
268
280
|
break
|
269
|
-
case "golf":
|
270
|
-
case "minify":
|
271
|
-
{
|
272
|
-
const target = commands[1]
|
273
|
-
if (!target) {
|
274
|
-
logError("Must provide target\n")
|
275
|
-
logHelp()
|
276
|
-
break
|
277
|
-
}
|
278
|
-
const fileExtension = extname(target)
|
279
|
-
if (!supportedExtensions.includes(fileExtension)) {
|
280
|
-
logError(
|
281
|
-
`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${supportedExtensions.map(extension => chalk.bold(extension)).join('", "')}"`
|
282
|
-
)
|
283
|
-
break
|
284
|
-
}
|
285
|
-
const { processScript } = await processScriptModule,
|
286
|
-
fileBaseName = basename(target, fileExtension),
|
287
|
-
fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src"),
|
288
|
-
scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName,
|
289
|
-
scriptUser =
|
290
|
-
"scripts" == basename(resolve(target, "..")) && "hackmud" == basename(resolve(target, "../../..")) ?
|
291
|
-
basename(resolve(target, "../.."))
|
292
|
-
: void 0,
|
293
|
-
optionsHasNoMinify = options.has("no-minify")
|
294
|
-
if ((optionsHasNoMinify || options.has("skip-minify")) && options.has("mangle-names")) {
|
295
|
-
logError(
|
296
|
-
`Options ${colourN("--mangle-names")} and ${colourN(optionsHasNoMinify ? "--no-minify" : "--skip-minify")} are incompatible\n`
|
297
|
-
)
|
298
|
-
logHelp()
|
299
|
-
break
|
300
|
-
}
|
301
|
-
const mangleNames_ = options.get("mangle-names")
|
302
|
-
if (null != mangleNames_ && "boolean" != typeof mangleNames_) {
|
303
|
-
logError(
|
304
|
-
`The value for ${colourN("--mangle-names")} must be ${colourV("true")} or ${colourV("false")}\n`
|
305
|
-
)
|
306
|
-
logHelp()
|
307
|
-
break
|
308
|
-
}
|
309
|
-
const mangleNames = mangleNames_,
|
310
|
-
forceQuineCheats_ = options.get("force-quine-cheats")
|
311
|
-
if (null != forceQuineCheats_ && "boolean" != typeof forceQuineCheats_) {
|
312
|
-
logError(
|
313
|
-
`the value for ${colourN("--force-quine-cheats")} must be ${colourV("true")} or ${colourV("false")}\n`
|
314
|
-
)
|
315
|
-
logHelp()
|
316
|
-
break
|
317
|
-
}
|
318
|
-
const forceQuineCheats = forceQuineCheats_
|
319
|
-
let outputPath =
|
320
|
-
commands[2] ||
|
321
|
-
resolve(
|
322
|
-
dirname(target),
|
323
|
-
fileBaseNameEndsWithDotSrc ? scriptName + ".js"
|
324
|
-
: ".js" == fileExtension ? fileBaseName + ".min.js"
|
325
|
-
: fileBaseName + ".js"
|
326
|
-
)
|
327
|
-
const golfFile = () =>
|
328
|
-
readFile(target, { encoding: "utf8" }).then(async source => {
|
329
|
-
const timeStart = performance.now(),
|
330
|
-
{ script, warnings } = await processScript(source, {
|
331
|
-
minify: !(options.get("no-minify") || options.get("skip-minify")),
|
332
|
-
scriptUser,
|
333
|
-
scriptName,
|
334
|
-
filePath: target,
|
335
|
-
mangleNames,
|
336
|
-
forceQuineCheats
|
337
|
-
}),
|
338
|
-
timeTook = performance.now() - timeStart
|
339
|
-
for (const { message, line } of warnings)
|
340
|
-
log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(line + "")}`)
|
341
|
-
await writeFilePersistent(outputPath, script)
|
342
|
-
.catch(error => {
|
343
|
-
if (!commands[2] || "EISDIR" != error.code) throw error
|
344
|
-
outputPath = resolve(outputPath, basename(target, fileExtension) + ".js")
|
345
|
-
return writeFilePersistent(outputPath, script)
|
346
|
-
})
|
347
|
-
.then(() =>
|
348
|
-
log(
|
349
|
-
`Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${chalk.bold(relative(".", outputPath))} | took ${Math.round(100 * timeTook) / 100}ms`
|
350
|
-
)
|
351
|
-
)
|
352
|
-
})
|
353
|
-
if (options.get("watch")) {
|
354
|
-
const { watch: watchFile } = await chokidarModule
|
355
|
-
watchFile(target, { awaitWriteFinish: { stabilityThreshold: 100 } })
|
356
|
-
.on("ready", () => log("Watching " + target))
|
357
|
-
.on("change", golfFile)
|
358
|
-
autoExit = !1
|
359
|
-
} else await golfFile()
|
360
|
-
}
|
361
|
-
break
|
362
281
|
default:
|
363
282
|
commands[0] && logError(`Unknown command: ${colourL(commands[0])}\n`)
|
364
283
|
logHelp()
|
@@ -432,7 +351,7 @@ function logError(message) {
|
|
432
351
|
process.exitCode = 1
|
433
352
|
}
|
434
353
|
function getHackmudPath() {
|
435
|
-
const hackmudPathOption =
|
354
|
+
const hackmudPathOption = popOption("hackmud-path")
|
436
355
|
if (null != hackmudPathOption && "string" != typeof hackmudPathOption) {
|
437
356
|
logError(`Option ${colourN("--hackmud-path")} must be a string, got ${colourV(hackmudPathOption)}\n`)
|
438
357
|
logHelp()
|
@@ -444,3 +363,32 @@ function getHackmudPath() {
|
|
444
363
|
("win32" == process.platform ? resolve(process.env.APPDATA, "hackmud") : resolve(homedir(), ".config/hackmud"))
|
445
364
|
)
|
446
365
|
}
|
366
|
+
function assertOptionIsBoolean(option) {
|
367
|
+
if ("boolean" != typeof option.value) {
|
368
|
+
logError(`The value for ${formatOption(option.name)} must be ${colourV("true")} or ${colourV("false")}\n`)
|
369
|
+
logHelp()
|
370
|
+
process.exit(1)
|
371
|
+
}
|
372
|
+
}
|
373
|
+
function popOption(...names) {
|
374
|
+
const presentOptionNames = names.filter(name => options.has(name))
|
375
|
+
if (!presentOptionNames.length) return
|
376
|
+
const presentOptionNamesWithDashDash = presentOptionNames.map(formatOption)
|
377
|
+
if (presentOptionNames.length > 1) {
|
378
|
+
logError(
|
379
|
+
`The options ${presentOptionNamesWithDashDash.join(", ")} are aliases for each other. Please only specify one`
|
380
|
+
)
|
381
|
+
process.exit(1)
|
382
|
+
}
|
383
|
+
const value = options.get(presentOptionNames[0])
|
384
|
+
options.delete(presentOptionNames[0])
|
385
|
+
return { name: presentOptionNamesWithDashDash[0], value }
|
386
|
+
}
|
387
|
+
function complainAboutUnrecognisedOptions() {
|
388
|
+
if (options.size) {
|
389
|
+
logError(
|
390
|
+
`Unrecognised option${options.size > 1 ? "s" : ""}: ${[...options.keys()].map(formatOption).join(", ")}`
|
391
|
+
)
|
392
|
+
process.exit(1)
|
393
|
+
}
|
394
|
+
}
|
package/env.d.ts
CHANGED
@@ -3,13 +3,7 @@ type ScriptSuccess<T = object> = { ok: true } & T
|
|
3
3
|
type ScriptFailure = { ok: false, msg?: string }
|
4
4
|
type ScriptResponse<T = object> = ScriptSuccess<T> | ScriptFailure
|
5
5
|
type ErrorScripts = Record<string, () => ScriptFailure>
|
6
|
-
|
7
|
-
type AllOptional<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? true : false }[keyof T]
|
8
|
-
|
9
|
-
type Scriptor<Args = unknown, Ret = unknown> = {
|
10
|
-
name: string
|
11
|
-
call: AllOptional<Args> extends true ? (args?: Args) => Ret : (args: Args) => Ret
|
12
|
-
}
|
6
|
+
type Scriptor<TArgs extends any[] = any[]> = { name: string, call: (...args: TArgs) => unknown }
|
13
7
|
|
14
8
|
type Subscripts = Record<string, Record<string, (...args: any) => any>> & {
|
15
9
|
accts: ErrorScripts
|
@@ -635,6 +629,9 @@ type Lowsec = Midsec & PlayerLowsec & {
|
|
635
629
|
(args: { i: number | number[], to: string, memo?: string }): ScriptResponse
|
636
630
|
(args: { sn: string | string[], to: string, memo?: string }): ScriptResponse
|
637
631
|
}
|
632
|
+
/** **LOWSEC** */ expose_access_log: (args: { target: string }) => ScriptResponse
|
633
|
+
/** **LOWSEC** */ xfer_gc_from: (args: { target: string, amount: number | string }) => ScriptResponse
|
634
|
+
/** **LOWSEC** */ expose_balance: (args: { target: string }) => ScriptResponse
|
638
635
|
}
|
639
636
|
}
|
640
637
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
2
2
|
import { basename, resolve } from "path"
|
3
|
+
import * as PathPosix from "path/posix"
|
3
4
|
async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
|
4
5
|
const users = new Set()
|
5
6
|
if (hackmudPath)
|
@@ -29,7 +30,7 @@ async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
|
|
29
30
|
}
|
30
31
|
})
|
31
32
|
)
|
32
|
-
sourceDirectory = resolve(sourceDirectory)
|
33
|
+
sourceDirectory = PathPosix.resolve(sourceDirectory)
|
33
34
|
let o = ""
|
34
35
|
for (const script of wildScripts) o += `type $${script}$ = typeof import("${sourceDirectory}/${script}").default\n`
|
35
36
|
o += "\n"
|
package/index.js
CHANGED
@@ -7,6 +7,7 @@ export { syncMacros } from "./syncMacros.js"
|
|
7
7
|
export { watch } from "./watch.js"
|
8
8
|
import "@samual/lib/readDirectoryWithStats"
|
9
9
|
import "path"
|
10
|
+
import "path/posix"
|
10
11
|
import "@babel/generator"
|
11
12
|
import "@babel/parser"
|
12
13
|
import "@babel/plugin-proposal-decorators"
|
package/package.json
CHANGED
package/processScript/index.js
CHANGED
@@ -204,6 +204,7 @@ async function processScript(
|
|
204
204
|
const bundle = await rollup({
|
205
205
|
input: filePathResolved,
|
206
206
|
plugins: [
|
207
|
+
rollupPluginJSON({ preferConst: !0 }),
|
207
208
|
{
|
208
209
|
name: "hackmud-script-manager",
|
209
210
|
async transform(code, id) {
|
@@ -227,8 +228,7 @@ async function processScript(
|
|
227
228
|
},
|
228
229
|
babel({ babelHelpers: "bundled", plugins, configFile: !1, extensions: supportedExtensions }),
|
229
230
|
rollupPluginCommonJS(),
|
230
|
-
rollupPluginNodeResolve({ extensions: supportedExtensions })
|
231
|
-
rollupPluginJSON()
|
231
|
+
rollupPluginNodeResolve({ extensions: supportedExtensions })
|
232
232
|
],
|
233
233
|
treeshake: { moduleSideEffects: !1 }
|
234
234
|
}),
|
package/processScript/minify.js
CHANGED
@@ -43,15 +43,6 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
|
|
43
43
|
)
|
44
44
|
}
|
45
45
|
}
|
46
|
-
const hashGReferencePaths = getReferencePathsToGlobal(`$${uniqueId}$GLOBAL$`, program)
|
47
|
-
if (hashGReferencePaths.length > 3) {
|
48
|
-
for (const path of hashGReferencePaths) path.replaceWith(t.identifier(`_${uniqueId}_G_`))
|
49
|
-
mainFunctionPath.node.body.body.unshift(
|
50
|
-
t.variableDeclaration("let", [
|
51
|
-
t.variableDeclarator(t.identifier(`_${uniqueId}_G_`), t.identifier(`$${uniqueId}$GLOBAL$`))
|
52
|
-
])
|
53
|
-
)
|
54
|
-
}
|
55
46
|
const jsonValues = []
|
56
47
|
let scriptBeforeJSONValueReplacement,
|
57
48
|
comment,
|
@@ -61,7 +52,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
|
|
61
52
|
traverse(fileBeforeJSONValueReplacement, {
|
62
53
|
MemberExpression({ node: memberExpression }) {
|
63
54
|
if (!memberExpression.computed) {
|
64
|
-
assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:
|
55
|
+
assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:115:60")
|
65
56
|
if ("prototype" == memberExpression.property.name) {
|
66
57
|
memberExpression.computed = !0
|
67
58
|
memberExpression.property = t.identifier(`_${uniqueId}_PROTOTYPE_PROPERTY_`)
|
@@ -173,7 +164,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
|
|
173
164
|
},
|
174
165
|
MemberExpression({ node: memberExpression }) {
|
175
166
|
if (!memberExpression.computed) {
|
176
|
-
assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:
|
167
|
+
assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:249:62")
|
177
168
|
if (!(memberExpression.property.name.length < 3)) {
|
178
169
|
memberExpression.computed = !0
|
179
170
|
memberExpression.property = t.stringLiteral(memberExpression.property.name)
|
@@ -247,7 +238,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
|
|
247
238
|
})
|
248
239
|
await Promise.all(promises)
|
249
240
|
const functionDeclaration = file.program.body[0]
|
250
|
-
assert("FunctionDeclaration" == functionDeclaration.type, "src/processScript/minify.ts:
|
241
|
+
assert("FunctionDeclaration" == functionDeclaration.type, "src/processScript/minify.ts:354:61")
|
251
242
|
if (jsonValues.length) {
|
252
243
|
hasComment = !0
|
253
244
|
if (1 == jsonValues.length)
|
@@ -367,7 +358,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
|
|
367
358
|
)
|
368
359
|
}
|
369
360
|
if (1 == forceQuineCheats) return code
|
370
|
-
assert(scriptBeforeJSONValueReplacement, "src/processScript/minify.ts:
|
361
|
+
assert(scriptBeforeJSONValueReplacement, "src/processScript/minify.ts:485:43")
|
371
362
|
return (
|
372
363
|
countHackmudCharacters(scriptBeforeJSONValueReplacement) <=
|
373
364
|
countHackmudCharacters(code) + Number(hasComment)
|
@@ -383,7 +374,7 @@ function parseObjectExpression(node, o) {
|
|
383
374
|
"Identifier" == property.key.type ||
|
384
375
|
"NumericLiteral" == property.key.type ||
|
385
376
|
"StringLiteral" == property.key.type,
|
386
|
-
"src/processScript/minify.ts:
|
377
|
+
"src/processScript/minify.ts:507:4"
|
387
378
|
)
|
388
379
|
if ("ArrayExpression" == property.value.type) {
|
389
380
|
const childArray = []
|
@@ -1,6 +1,6 @@
|
|
1
1
|
const postprocess = (code, uniqueId) =>
|
2
2
|
code
|
3
|
-
.replace(/^function\s
|
3
|
+
.replace(/^function\s*[\w$]+\(/, "function(")
|
4
4
|
.replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$SC_DOLLAR\\$`, "g"), "S\\C$")
|
5
5
|
.replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$DB_DOLLAR\\$`, "g"), "D\\B$")
|
6
6
|
.replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$D\\$`, "g"), "_\\_D_S")
|
@@ -98,8 +98,10 @@ async function preprocess(code, { uniqueId = "00000000000" } = {}) {
|
|
98
98
|
t.stringLiteral(resolve("proxy-polyfill/src/proxy.js", import.meta.url).slice(7))
|
99
99
|
)
|
100
100
|
)
|
101
|
-
|
102
|
-
|
103
|
-
|
101
|
+
if (1 == program.node.body.length && "FunctionDeclaration" == program.node.body[0].type)
|
102
|
+
throw Error(
|
103
|
+
"Scripts that only contain a single function declaration are no longer supported.\nPrefix the function declaration with `export default`."
|
104
|
+
)
|
105
|
+
return { code: generate(file).code }
|
104
106
|
}
|
105
107
|
export { preprocess }
|
@@ -22,9 +22,7 @@ const { default: traverse } = babelTraverse,
|
|
22
22
|
"BigInt"
|
23
23
|
]
|
24
24
|
function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scriptName, seclevel = 4 }) {
|
25
|
-
const topFunctionName = `_${uniqueId}_SCRIPT_
|
26
|
-
exports = new Map(),
|
27
|
-
liveExports = new Map()
|
25
|
+
const topFunctionName = `_${uniqueId}_SCRIPT_`
|
28
26
|
let program
|
29
27
|
traverse(file, {
|
30
28
|
Program(path) {
|
@@ -70,30 +68,30 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
70
68
|
const referencePath = FunctionReferencePaths[0]
|
71
69
|
assert(
|
72
70
|
"MemberExpression" == referencePath.parent.type,
|
73
|
-
"src/processScript/transform.ts:
|
71
|
+
"src/processScript/transform.ts:103:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
74
72
|
)
|
75
73
|
assert(
|
76
74
|
"Identifier" == referencePath.parent.property.type,
|
77
|
-
"src/processScript/transform.ts:
|
75
|
+
"src/processScript/transform.ts:108:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
78
76
|
)
|
79
77
|
assert(
|
80
78
|
"prototype" == referencePath.parent.property.name,
|
81
|
-
"src/processScript/transform.ts:
|
79
|
+
"src/processScript/transform.ts:113:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
82
80
|
)
|
83
81
|
referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
|
84
82
|
} else {
|
85
83
|
for (const referencePath of FunctionReferencePaths) {
|
86
84
|
assert(
|
87
85
|
"MemberExpression" == referencePath.parent.type,
|
88
|
-
"src/processScript/transform.ts:
|
86
|
+
"src/processScript/transform.ts:121:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
89
87
|
)
|
90
88
|
assert(
|
91
89
|
"Identifier" == referencePath.parent.property.type,
|
92
|
-
"src/processScript/transform.ts:
|
90
|
+
"src/processScript/transform.ts:126:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
93
91
|
)
|
94
92
|
assert(
|
95
93
|
"prototype" == referencePath.parent.property.name,
|
96
|
-
"src/processScript/transform.ts:
|
94
|
+
"src/processScript/transform.ts:131:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
97
95
|
)
|
98
96
|
functionDotPrototypeIsReferencedMultipleTimes = !0
|
99
97
|
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_FUNCTION_DOT_PROTOTYPE_`))
|
@@ -129,12 +127,12 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
129
127
|
const neededDbMethodLets = new Set()
|
130
128
|
if (program.scope.hasGlobal("$db"))
|
131
129
|
for (const referencePath of getReferencePathsToGlobal("$db", program)) {
|
132
|
-
assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:
|
133
|
-
assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:
|
130
|
+
assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:185:69")
|
131
|
+
assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:186:72")
|
134
132
|
const databaseOpMethodName = referencePath.parentPath.node.property.name
|
135
133
|
assert(
|
136
134
|
validDBMethods.includes(databaseOpMethodName),
|
137
|
-
`src/processScript/transform.ts:
|
135
|
+
`src/processScript/transform.ts:192:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
|
138
136
|
)
|
139
137
|
if ("CallExpression" == referencePath.parentPath.parentPath?.type)
|
140
138
|
referencePath.parentPath.replaceWith(t.identifier(`$${uniqueId}$DB$${databaseOpMethodName}$`))
|
@@ -157,9 +155,10 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
157
155
|
if (program.scope.hasGlobal("$FMCL"))
|
158
156
|
for (const referencePath of getReferencePathsToGlobal("$FMCL", program))
|
159
157
|
referencePath.replaceWith(t.identifier(`$${uniqueId}$FMCL$`))
|
160
|
-
|
158
|
+
let needG = program.scope.hasGlobal("$G")
|
159
|
+
if (needG)
|
161
160
|
for (const referencePath of getReferencePathsToGlobal("$G", program))
|
162
|
-
referencePath.replaceWith(t.identifier(
|
161
|
+
referencePath.replaceWith(t.identifier(`_${uniqueId}_G_`))
|
163
162
|
if (program.scope.hasGlobal("_SECLEVEL"))
|
164
163
|
for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
|
165
164
|
referencePath.replaceWith(t.numericLiteral(seclevel))
|
@@ -193,11 +192,12 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
193
192
|
"ExportSpecifier" == specifier.type,
|
194
193
|
`src/processScript/transform.ts:276:51 ${specifier.type} is currently unsupported`
|
195
194
|
)
|
196
|
-
|
197
|
-
"
|
198
|
-
|
199
|
-
|
200
|
-
|
195
|
+
if (
|
196
|
+
"default" !=
|
197
|
+
("Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value)
|
198
|
+
)
|
199
|
+
throw Error("Only default exports are supported")
|
200
|
+
exportDefaultName = specifier.local.name
|
201
201
|
}
|
202
202
|
}
|
203
203
|
const globalBlock = t.blockStatement([])
|
@@ -223,10 +223,6 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
223
223
|
t.returnStatement(t.callExpression(t.identifier(exportDefaultName), []))
|
224
224
|
])
|
225
225
|
))
|
226
|
-
if ("const" != statement.kind && exports.has(identifierName)) {
|
227
|
-
liveExports.set(identifierName, exports.get(identifierName))
|
228
|
-
exports.delete(identifierName)
|
229
|
-
}
|
230
226
|
globalBlock.body.push(
|
231
227
|
t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
|
232
228
|
)
|
@@ -291,34 +287,16 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
291
287
|
}
|
292
288
|
program.node.body = [mainFunction]
|
293
289
|
if (globalBlock.body.length) {
|
294
|
-
;(exports.size || liveExports.size) &&
|
295
|
-
mainFunction.body.body.push(
|
296
|
-
t.returnStatement(
|
297
|
-
t.objectExpression([
|
298
|
-
...[...exports].map(([local, exported]) =>
|
299
|
-
t.objectProperty(t.identifier(exported), t.identifier(local))
|
300
|
-
),
|
301
|
-
...[...liveExports].map(([local, exported]) =>
|
302
|
-
t.objectMethod(
|
303
|
-
"get",
|
304
|
-
t.identifier(exported),
|
305
|
-
[],
|
306
|
-
t.blockStatement([t.returnStatement(t.identifier(local))])
|
307
|
-
)
|
308
|
-
)
|
309
|
-
])
|
310
|
-
)
|
311
|
-
)
|
312
290
|
program.scope.crawl()
|
313
291
|
const globalBlockVariables = new Set()
|
314
292
|
let hoistedGlobalBlockFunctions = 0
|
315
293
|
for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
|
316
294
|
if ("VariableDeclaration" == globalBlockStatement.type) {
|
317
|
-
assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:
|
295
|
+
assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:390:59")
|
318
296
|
const declarator = globalBlockStatement.declarations[0]
|
319
297
|
assert(
|
320
298
|
"Identifier" == declarator.id.type,
|
321
|
-
`src/processScript/transform.ts:
|
299
|
+
`src/processScript/transform.ts:394:51 declarator.id.type was "${declarator.id.type}"`
|
322
300
|
)
|
323
301
|
program.scope.crawl()
|
324
302
|
if (program.scope.hasGlobal(declarator.id.name)) {
|
@@ -333,15 +311,16 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
333
311
|
Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
|
334
312
|
) {
|
335
313
|
const binding = program.scope.getBinding(declarator.id.name)
|
336
|
-
assert(binding, "src/processScript/transform.ts:
|
314
|
+
assert(binding, "src/processScript/transform.ts:413:23")
|
337
315
|
for (const referencePath of binding.referencePaths) {
|
338
|
-
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:
|
316
|
+
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:416:56")
|
339
317
|
referencePath.replaceWith(
|
340
318
|
t.memberExpression(
|
341
|
-
t.identifier(
|
319
|
+
t.identifier(`_${uniqueId}_G_`),
|
342
320
|
t.identifier(referencePath.node.name)
|
343
321
|
)
|
344
322
|
)
|
323
|
+
needG = !0
|
345
324
|
}
|
346
325
|
for (const referencePath of binding.constantViolations)
|
347
326
|
if ("AssignmentExpression" == referencePath.node.type)
|
@@ -350,12 +329,13 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
350
329
|
clearObject(node)
|
351
330
|
Object.assign(
|
352
331
|
node,
|
353
|
-
t.memberExpression(t.identifier(
|
332
|
+
t.memberExpression(t.identifier(`_${uniqueId}_G_`), t.identifier(name))
|
354
333
|
)
|
334
|
+
needG = !0
|
355
335
|
}
|
356
336
|
globalBlockPath.remove()
|
357
337
|
globalBlockStatementPath.remove()
|
358
|
-
declarator.init
|
338
|
+
if (declarator.init) {
|
359
339
|
globalBlock.body.splice(
|
360
340
|
globalBlockIndex,
|
361
341
|
0,
|
@@ -363,13 +343,15 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
363
343
|
t.assignmentExpression(
|
364
344
|
"=",
|
365
345
|
t.memberExpression(
|
366
|
-
t.identifier(
|
346
|
+
t.identifier(`_${uniqueId}_G_`),
|
367
347
|
t.identifier(declarator.id.name)
|
368
348
|
),
|
369
349
|
declarator.init
|
370
350
|
)
|
371
351
|
)
|
372
352
|
)
|
353
|
+
needG = !0
|
354
|
+
}
|
373
355
|
} else {
|
374
356
|
globalBlockPath.remove()
|
375
357
|
globalBlockStatementPath.remove()
|
@@ -379,22 +361,20 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
379
361
|
} else globalBlockVariables.add(declarator.id.name)
|
380
362
|
} else if ("ClassDeclaration" == globalBlockStatement.type) {
|
381
363
|
program.scope.crawl()
|
382
|
-
assert(globalBlockStatement.id, "src/processScript/transform.ts:
|
364
|
+
assert(globalBlockStatement.id, "src/processScript/transform.ts:473:37")
|
383
365
|
if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
|
384
366
|
globalBlock.body.splice(globalBlockIndex, 1)
|
385
367
|
const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
|
386
368
|
[globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
|
387
369
|
program.scope.crawl()
|
388
370
|
const binding = program.scope.getBinding(globalBlockStatement.id.name)
|
389
|
-
assert(binding, "src/processScript/transform.ts:
|
371
|
+
assert(binding, "src/processScript/transform.ts:485:22")
|
390
372
|
for (const referencePath of binding.referencePaths) {
|
391
|
-
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:
|
373
|
+
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:488:55")
|
392
374
|
referencePath.replaceWith(
|
393
|
-
t.memberExpression(
|
394
|
-
t.identifier(`$${uniqueId}$GLOBAL$`),
|
395
|
-
t.identifier(referencePath.node.name)
|
396
|
-
)
|
375
|
+
t.memberExpression(t.identifier(`_${uniqueId}_G_`), t.identifier(referencePath.node.name))
|
397
376
|
)
|
377
|
+
needG = !0
|
398
378
|
}
|
399
379
|
globalBlockPath.remove()
|
400
380
|
globalBlockStatementPath.remove()
|
@@ -405,7 +385,7 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
405
385
|
t.assignmentExpression(
|
406
386
|
"=",
|
407
387
|
t.memberExpression(
|
408
|
-
t.identifier(
|
388
|
+
t.identifier(`_${uniqueId}_G_`),
|
409
389
|
t.identifier(globalBlockStatement.id.name)
|
410
390
|
),
|
411
391
|
t.classExpression(
|
@@ -417,13 +397,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
417
397
|
)
|
418
398
|
)
|
419
399
|
)
|
400
|
+
needG = !0
|
420
401
|
}
|
421
402
|
}
|
422
|
-
if (program.scope.hasGlobal("_EXPORTS"))
|
423
|
-
for (const referencePath of getReferencePathsToGlobal("_EXPORTS", program))
|
424
|
-
referencePath.replaceWith(
|
425
|
-
t.arrayExpression([...exports.keys(), ...liveExports.keys()].map(name => t.stringLiteral(name)))
|
426
|
-
)
|
427
403
|
globalBlock.body.length &&
|
428
404
|
mainFunction.body.body.splice(
|
429
405
|
hoistedGlobalBlockFunctions,
|
@@ -535,6 +511,12 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
535
511
|
)
|
536
512
|
)
|
537
513
|
)
|
514
|
+
needG &&
|
515
|
+
mainFunction.body.body.unshift(
|
516
|
+
t.variableDeclaration("let", [
|
517
|
+
t.variableDeclarator(t.identifier(`_${uniqueId}_G_`), t.identifier(`$${uniqueId}$GLOBAL$`))
|
518
|
+
])
|
519
|
+
)
|
538
520
|
traverse(file, {
|
539
521
|
BlockStatement({ node: blockStatement }) {
|
540
522
|
for (const [index, functionDeclaration] of blockStatement.body.entries())
|
@@ -555,7 +537,7 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
555
537
|
}
|
556
538
|
},
|
557
539
|
ClassBody({ node: classBody, scope, parent }) {
|
558
|
-
assert(t.isClass(parent), "src/processScript/transform.ts:
|
540
|
+
assert(t.isClass(parent), "src/processScript/transform.ts:658:30")
|
559
541
|
let thisIsReferenced = !1
|
560
542
|
for (const classMethod of classBody.body) {
|
561
543
|
if ("ClassMethod" != classMethod.type) continue
|
@@ -647,39 +629,31 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
647
629
|
})
|
648
630
|
return { file, seclevel }
|
649
631
|
function createGetFunctionPrototypeNode() {
|
650
|
-
|
651
|
-
if (!program.scope.hasOwnBinding(globalFunction))
|
652
|
-
return t.memberExpression(
|
653
|
-
t.memberExpression(t.identifier(globalFunction), t.identifier("constructor")),
|
654
|
-
t.identifier("prototype")
|
655
|
-
)
|
632
|
+
const name = globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
|
656
633
|
return t.memberExpression(
|
657
|
-
t.
|
658
|
-
|
659
|
-
t.identifier("constructor")
|
660
|
-
),
|
661
|
-
t.identifier("prototype")
|
634
|
+
name ? t.identifier(name) : t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
|
635
|
+
t.identifier("__proto__")
|
662
636
|
)
|
663
637
|
}
|
664
638
|
function processFakeSubscriptObject(fakeSubscriptObjectName, seclevel) {
|
665
639
|
for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
|
666
|
-
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:
|
640
|
+
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:764:60")
|
667
641
|
assert("Identifier" == referencePath.parent.property.type)
|
668
642
|
assert(
|
669
643
|
"MemberExpression" == referencePath.parentPath.parentPath?.node.type,
|
670
|
-
"src/processScript/transform.ts:
|
644
|
+
"src/processScript/transform.ts:766:81"
|
671
645
|
)
|
672
646
|
assert(
|
673
647
|
"Identifier" == referencePath.parentPath.parentPath.node.property.type,
|
674
|
-
"src/processScript/transform.ts:
|
648
|
+
"src/processScript/transform.ts:767:83"
|
675
649
|
)
|
676
650
|
assert(
|
677
651
|
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
|
678
|
-
`src/processScript/transform.ts:
|
652
|
+
`src/processScript/transform.ts:771:8 invalid user "${referencePath.parent.property.name}" in subscript`
|
679
653
|
)
|
680
654
|
assert(
|
681
655
|
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
|
682
|
-
`src/processScript/transform.ts:
|
656
|
+
`src/processScript/transform.ts:776:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
|
683
657
|
)
|
684
658
|
if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
|
685
659
|
referencePath.parentPath.parentPath.replaceWith(
|
package/watch.js
CHANGED
@@ -4,11 +4,12 @@ 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,6 +59,7 @@ async function watch(
|
|
58
59
|
} = {}
|
59
60
|
) {
|
60
61
|
if (!scripts.length) throw Error("scripts option was an empty array")
|
62
|
+
if (!(await stat(sourceDirectory)).isDirectory()) throw Error("Target folder must be a folder")
|
61
63
|
const scriptNamesToUsers = new Cache(_scriptName => new Set()),
|
62
64
|
wildScriptUsers = new Set(),
|
63
65
|
wildUserScripts = new Set()
|
@@ -135,7 +137,7 @@ async function watch(
|
|
135
137
|
forceQuineCheats
|
136
138
|
}))
|
137
139
|
} catch (error) {
|
138
|
-
assert(error instanceof Error, "src/watch.ts:
|
140
|
+
assert(error instanceof Error, "src/watch.ts:146:36")
|
139
141
|
onPush?.({ path, users: [], characterCount: 0, error })
|
140
142
|
return
|
141
143
|
}
|
@@ -180,7 +182,7 @@ async function watch(
|
|
180
182
|
forceQuineCheats
|
181
183
|
}))
|
182
184
|
} catch (error) {
|
183
|
-
assert(error instanceof Error, "src/watch.ts:
|
185
|
+
assert(error instanceof Error, "src/watch.ts:182:35")
|
184
186
|
onPush?.({ path, users: [], characterCount: 0, error })
|
185
187
|
return
|
186
188
|
}
|
@@ -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:
|
200
|
+
assert(error instanceof Error, "src/watch.ts:215:35")
|
199
201
|
if ("EISDIR" != error.code) throw error
|
200
202
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
201
203
|
await writeFile(typeDeclarationPath, typeDeclaration)
|