hackmud-script-manager 0.20.3 → 0.20.4-06a037b
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/README.md +9 -1
- package/bin/hsm.js +195 -239
- package/env.d.ts +844 -1423
- package/generateTypeDeclaration.js +2 -1
- package/index.js +2 -1
- package/package.json +44 -40
- package/processScript/index.js +3 -3
- package/processScript/minify.js +18 -21
- package/processScript/postprocess.d.ts +1 -1
- package/processScript/postprocess.js +3 -3
- package/processScript/preprocess.js +5 -3
- package/processScript/transform.js +102 -96
- package/push.d.ts +9 -1
- package/push.js +36 -14
- package/syncMacros.js +1 -1
- package/watch.js +9 -7
package/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Hackmud Script Manager
|
2
|
-
Command made for [
|
2
|
+
Command made for [Hackmud Scripting Environment](https://github.com/samualtnorman/hackmud-environment), which is a scripting environment for hackmud with minification, autocompletes / intellisense, and TypeScript support.
|
3
|
+
|
4
|
+
[](https://ko-fi.com/R6R0XN5CX)
|
5
|
+
|
6
|
+
You can read about how HSM works [in my blog post](https://samual.uk/blog/js-code-transformation-niche-environment/).
|
3
7
|
|
4
8
|
## Install
|
5
9
|
1. [Install Node.js](https://nodejs.org/en/download)
|
@@ -16,6 +20,10 @@ Command made for [hackmud-environment](https://github.com/samualtnorman/hackmud-
|
|
16
20
|
> ```
|
17
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).
|
18
22
|
|
23
|
+

|
24
|
+

|
25
|
+

|
26
|
+
|
19
27
|
## Features
|
20
28
|
- Minification
|
21
29
|
- This includes auto quine cheating.
|
package/bin/hsm.js
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
import {
|
2
|
+
import { AutoMap } from "@samual/lib/AutoMap"
|
3
3
|
import { assert } from "@samual/lib/assert"
|
4
4
|
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
|
16
|
+
const formatOption = name => colourN(`-${1 == name.length ? "" : "-"}${name}`),
|
16
17
|
options = new Map(),
|
17
18
|
commands = [],
|
18
|
-
userColours = new
|
19
|
+
userColours = new AutoMap(user => {
|
19
20
|
let hash = 0
|
20
21
|
for (const char of user) hash += (hash >> 1) + hash + "xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj".indexOf(char) + 1
|
21
22
|
return [colourJ, colourK, colourM, colourW, colourL, colourB][hash % 6](user)
|
@@ -36,10 +37,6 @@ for (const argument of process.argv.slice(2))
|
|
36
37
|
if ("-" == argument[1]) options.set(key.slice(2), value)
|
37
38
|
else for (const option of key.slice(1)) options.set(option, value)
|
38
39
|
} 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
40
|
const pushModule = import("../push.js"),
|
44
41
|
processScriptModule = import("../processScript/index.js"),
|
45
42
|
watchModule = import("../watch.js"),
|
@@ -49,6 +46,7 @@ const pushModule = import("../push.js"),
|
|
49
46
|
colourB = chalk.rgb(202, 202, 202),
|
50
47
|
colourC = chalk.rgb(155, 155, 155),
|
51
48
|
colourD = chalk.rgb(255, 0, 0),
|
49
|
+
colourF = chalk.rgb(255, 128, 0),
|
52
50
|
colourJ = chalk.rgb(255, 244, 4),
|
53
51
|
colourK = chalk.rgb(243, 249, 152),
|
54
52
|
colourL = chalk.rgb(30, 255, 0),
|
@@ -57,153 +55,171 @@ const pushModule = import("../push.js"),
|
|
57
55
|
colourS = chalk.rgb(122, 178, 244),
|
58
56
|
colourV = chalk.rgb(255, 0, 236),
|
59
57
|
colourW = chalk.rgb(255, 150, 224)
|
60
|
-
|
58
|
+
process.version.startsWith("v21.") &&
|
59
|
+
console.warn(
|
60
|
+
colourF(
|
61
|
+
"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"
|
62
|
+
)
|
63
|
+
)
|
64
|
+
if ("v" == commands[0] || "version" == commands[0] || popOption("version", "v")?.value) {
|
65
|
+
console.log("0.20.4-06a037b")
|
66
|
+
process.exit()
|
67
|
+
}
|
68
|
+
if (popOption("help", "h")?.value) {
|
61
69
|
logHelp()
|
62
70
|
process.exit()
|
63
71
|
}
|
64
72
|
let autoExit = !0
|
65
73
|
switch (commands[0]) {
|
66
74
|
case "push":
|
75
|
+
case "dev":
|
76
|
+
case "watch":
|
77
|
+
case "golf":
|
78
|
+
case "minify":
|
67
79
|
{
|
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")) {
|
80
|
+
const noMinifyOption = popOption("no-minify", "skip-minify"),
|
81
|
+
mangleNamesOption = popOption("mangle-names"),
|
82
|
+
forceQuineCheatsOption = popOption("force-quine-cheats"),
|
83
|
+
noMinifyIncompatibleOption = mangleNamesOption || forceQuineCheatsOption
|
84
|
+
if (noMinifyOption && noMinifyIncompatibleOption) {
|
88
85
|
logError(
|
89
|
-
`Options ${
|
86
|
+
`Options ${formatOption(noMinifyOption.name)} and ${formatOption(noMinifyIncompatibleOption.name)} are incompatible\n`
|
90
87
|
)
|
91
88
|
logHelp()
|
92
|
-
|
89
|
+
process.exit(1)
|
93
90
|
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
91
|
+
noMinifyOption && assertOptionIsBoolean(noMinifyOption)
|
92
|
+
mangleNamesOption && assertOptionIsBoolean(mangleNamesOption)
|
93
|
+
forceQuineCheatsOption && assertOptionIsBoolean(forceQuineCheatsOption)
|
94
|
+
if ("golf" == commands[0] || "minify" == commands[0]) {
|
95
|
+
const watchOption = popOption("watch"),
|
96
|
+
target = commands[1]
|
97
|
+
if (!target) {
|
98
|
+
logError("Must provide target\n")
|
99
|
+
logHelp()
|
100
|
+
process.exit(1)
|
101
|
+
}
|
102
|
+
const fileExtension = extname(target)
|
103
|
+
if (!supportedExtensions.includes(fileExtension)) {
|
98
104
|
logError(
|
99
|
-
`
|
105
|
+
`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${supportedExtensions.map(extension => chalk.bold(extension)).join('", "')}"`
|
100
106
|
)
|
101
|
-
|
102
|
-
break
|
107
|
+
process.exit(1)
|
103
108
|
}
|
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
|
-
|
109
|
+
complainAboutUnrecognisedOptions()
|
110
|
+
const { processScript } = await processScriptModule,
|
111
|
+
fileBaseName = basename(target, fileExtension),
|
112
|
+
fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src"),
|
113
|
+
scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName,
|
114
|
+
scriptUser =
|
115
|
+
(
|
116
|
+
"scripts" == basename(resolve(target, "..")) &&
|
117
|
+
"hackmud" == basename(resolve(target, "../../.."))
|
118
|
+
) ?
|
119
|
+
basename(resolve(target, "../.."))
|
120
|
+
: void 0
|
121
|
+
let outputPath =
|
122
|
+
commands[2] ||
|
123
|
+
resolve(
|
124
|
+
dirname(target),
|
125
|
+
fileBaseNameEndsWithDotSrc ? scriptName + ".js"
|
126
|
+
: ".js" == fileExtension ? fileBaseName + ".min.js"
|
127
|
+
: fileBaseName + ".js"
|
128
|
+
)
|
129
|
+
const golfFile = () =>
|
130
|
+
readFile(target, { encoding: "utf8" }).then(async source => {
|
131
|
+
const timeStart = performance.now(),
|
132
|
+
{ script, warnings } = await processScript(source, {
|
133
|
+
minify: noMinifyOption && !noMinifyOption.value,
|
134
|
+
scriptUser,
|
135
|
+
scriptName,
|
136
|
+
filePath: target,
|
137
|
+
mangleNames: mangleNamesOption?.value,
|
138
|
+
forceQuineCheats: forceQuineCheatsOption?.value
|
139
|
+
}),
|
140
|
+
timeTook = performance.now() - timeStart
|
141
|
+
for (const { message, line } of warnings)
|
142
|
+
log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(line + "")}`)
|
143
|
+
await writeFilePersistent(outputPath, script)
|
144
|
+
.catch(error => {
|
145
|
+
if (!commands[2] || "EISDIR" != error.code) throw error
|
146
|
+
outputPath = resolve(outputPath, basename(target, fileExtension) + ".js")
|
147
|
+
return writeFilePersistent(outputPath, script)
|
148
|
+
})
|
149
|
+
.then(() =>
|
150
|
+
log(
|
151
|
+
`Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${chalk.bold(relative(".", outputPath))} | took ${Math.round(100 * timeTook) / 100}ms`
|
152
|
+
)
|
153
|
+
)
|
154
|
+
})
|
155
|
+
if (watchOption) {
|
156
|
+
const { watch: watchFile } = await chokidarModule
|
157
|
+
watchFile(target, { awaitWriteFinish: { stabilityThreshold: 100 } })
|
158
|
+
.on("ready", () => log("Watching " + target))
|
159
|
+
.on("change", golfFile)
|
160
|
+
autoExit = !1
|
161
|
+
} else await golfFile()
|
162
|
+
} else {
|
163
|
+
const hackmudPath = getHackmudPath(),
|
164
|
+
sourcePath = commands[1]
|
165
|
+
if (!sourcePath) {
|
166
|
+
logError(`Must provide the directory to ${"push" == commands[0] ? "push from" : "watch"}\n`)
|
151
167
|
logHelp()
|
152
|
-
|
168
|
+
process.exit(1)
|
153
169
|
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
`Options ${colourN("--mangle-names")} and ${colourN(optionsHasNoMinify ? "--no-minify" : "--skip-minify")} are incompatible\n`
|
159
|
-
)
|
160
|
-
logHelp()
|
161
|
-
break
|
162
|
-
}
|
163
|
-
const shouldSkipMinify = options.get("no-minify") || options.get("skip-minify")
|
164
|
-
let shouldMinify
|
165
|
-
if (null != shouldSkipMinify) {
|
166
|
-
if ("boolean" != typeof shouldSkipMinify) {
|
167
|
-
logError(
|
168
|
-
`The value for ${colourN(optionsHasNoMinify ? "--no-minify" : "--skip-minify")} must be ${colourV("true")} or ${colourV("false")}\n`
|
170
|
+
const scripts = commands.slice(2)
|
171
|
+
if (scripts.length) {
|
172
|
+
const invalidScript = scripts.find(
|
173
|
+
script => !/^(?:[a-z_][a-z\d_]{0,24}|\*)\.(?:[a-z_][a-z\d_]{0,24}|\*)$/.test(script)
|
169
174
|
)
|
170
|
-
|
171
|
-
|
175
|
+
if (invalidScript) {
|
176
|
+
logError(`Invalid script name: ${JSON.stringify(invalidScript)}\n`)
|
177
|
+
logHelp()
|
178
|
+
process.exit(1)
|
179
|
+
}
|
180
|
+
} else scripts.push("*.*")
|
181
|
+
if ("push" == commands[0]) {
|
182
|
+
const { push, MissingSourceFolderError, MissingHackmudFolderError, NoUsersError } = await pushModule
|
183
|
+
complainAboutUnrecognisedOptions()
|
184
|
+
const infos = await push(sourcePath, hackmudPath, {
|
185
|
+
scripts,
|
186
|
+
onPush: info => logInfo(info, hackmudPath),
|
187
|
+
minify: noMinifyOption && !noMinifyOption.value,
|
188
|
+
mangleNames: mangleNamesOption?.value,
|
189
|
+
forceQuineCheats: forceQuineCheatsOption?.value
|
190
|
+
})
|
191
|
+
if (infos instanceof Error) {
|
192
|
+
logError(infos.message)
|
193
|
+
if (infos instanceof MissingSourceFolderError || infos instanceof NoUsersError) {
|
194
|
+
console.log()
|
195
|
+
logHelp()
|
196
|
+
} else
|
197
|
+
infos instanceof MissingHackmudFolderError &&
|
198
|
+
log(
|
199
|
+
`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`
|
200
|
+
)
|
201
|
+
} else infos.length || logError("Could not find any scripts to push")
|
202
|
+
} else {
|
203
|
+
const typeDeclarationPathOption = popOption(
|
204
|
+
"type-declaration-path",
|
205
|
+
"type-declaration",
|
206
|
+
"dts",
|
207
|
+
"gen-types"
|
208
|
+
)
|
209
|
+
complainAboutUnrecognisedOptions()
|
210
|
+
const { watch } = await watchModule
|
211
|
+
watch(sourcePath, hackmudPath, {
|
212
|
+
scripts,
|
213
|
+
onPush: info => logInfo(info, hackmudPath),
|
214
|
+
typeDeclarationPath: typeDeclarationPathOption?.value.toString(),
|
215
|
+
minify: noMinifyOption && !noMinifyOption.value,
|
216
|
+
mangleNames: mangleNamesOption?.value,
|
217
|
+
onReady: () => log("Watching"),
|
218
|
+
forceQuineCheats: forceQuineCheatsOption?.value
|
219
|
+
})
|
220
|
+
autoExit = !1
|
172
221
|
}
|
173
|
-
shouldMinify = !shouldSkipMinify
|
174
|
-
}
|
175
|
-
const shouldMangleNames = options.get("mangle-names")
|
176
|
-
if (null != shouldMangleNames && "boolean" != typeof shouldMangleNames) {
|
177
|
-
logError(
|
178
|
-
`The value for ${colourN("--mangle-names")} must be ${colourV("true")} or ${colourV("false")}\n`
|
179
|
-
)
|
180
|
-
logHelp()
|
181
|
-
break
|
182
|
-
}
|
183
|
-
const shouldforceQuineCheats = options.get("force-quine-cheats")
|
184
|
-
if (null != shouldforceQuineCheats && "boolean" != typeof shouldforceQuineCheats) {
|
185
|
-
logError(
|
186
|
-
`The value for ${colourN("--force-quine-cheats")} must be ${colourV("true")} or ${colourV("false")}\n`
|
187
|
-
)
|
188
|
-
logHelp()
|
189
|
-
break
|
190
222
|
}
|
191
|
-
const { watch } = await watchModule
|
192
|
-
watch(sourcePath, hackmudPath, {
|
193
|
-
scripts,
|
194
|
-
onPush: info => logInfo(info, hackmudPath),
|
195
|
-
typeDeclarationPath: (
|
196
|
-
options.get("type-declaration-path") ||
|
197
|
-
options.get("type-declaration") ||
|
198
|
-
options.get("dts") ||
|
199
|
-
options.get("gen-types")
|
200
|
-
)?.toString(),
|
201
|
-
minify: shouldMinify,
|
202
|
-
mangleNames: shouldMangleNames,
|
203
|
-
onReady: () => log("Watching"),
|
204
|
-
forceQuineCheats: shouldforceQuineCheats
|
205
|
-
})
|
206
|
-
autoExit = !1
|
207
223
|
}
|
208
224
|
break
|
209
225
|
case "pull":
|
@@ -213,8 +229,9 @@ switch (commands[0]) {
|
|
213
229
|
if (!script) {
|
214
230
|
logError("Must provide the script to pull\n")
|
215
231
|
logHelp()
|
216
|
-
|
232
|
+
process.exit(1)
|
217
233
|
}
|
234
|
+
complainAboutUnrecognisedOptions()
|
218
235
|
const sourcePath = commands[2] || "."
|
219
236
|
await pull(sourcePath, hackmudPath, script).catch(error => {
|
220
237
|
console.error(error)
|
@@ -224,8 +241,9 @@ switch (commands[0]) {
|
|
224
241
|
break
|
225
242
|
case "sync-macros":
|
226
243
|
{
|
227
|
-
const hackmudPath = getHackmudPath()
|
228
|
-
|
244
|
+
const hackmudPath = getHackmudPath()
|
245
|
+
complainAboutUnrecognisedOptions()
|
246
|
+
const { macrosSynced, usersSynced } = await syncMacros(hackmudPath)
|
229
247
|
log(`Synced ${macrosSynced} macros to ${usersSynced} users`)
|
230
248
|
}
|
231
249
|
break
|
@@ -234,18 +252,20 @@ switch (commands[0]) {
|
|
234
252
|
case "gen-dts":
|
235
253
|
case "gen-types":
|
236
254
|
{
|
237
|
-
const
|
255
|
+
const hackmudPath = getHackmudPath(),
|
256
|
+
target = commands[1]
|
238
257
|
if (!target) {
|
239
258
|
logError("Must provide target directory\n")
|
240
259
|
logHelp()
|
241
|
-
|
260
|
+
process.exit(1)
|
242
261
|
}
|
262
|
+
complainAboutUnrecognisedOptions()
|
243
263
|
const sourcePath = resolve(target),
|
244
264
|
outputPath = commands[2] || "./player.d.ts",
|
245
|
-
typeDeclaration = await generateTypeDeclaration(sourcePath,
|
265
|
+
typeDeclaration = await generateTypeDeclaration(sourcePath, hackmudPath)
|
246
266
|
let typeDeclarationPath = resolve(outputPath)
|
247
267
|
await writeFile(typeDeclarationPath, typeDeclaration).catch(error => {
|
248
|
-
assert(error instanceof Error, "src/bin/hsm.ts:
|
268
|
+
assert(error instanceof Error, "src/bin/hsm.ts:343:35")
|
249
269
|
if ("EISDIR" != error.code) throw error
|
250
270
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
251
271
|
return writeFile(typeDeclarationPath, typeDeclaration)
|
@@ -257,99 +277,6 @@ switch (commands[0]) {
|
|
257
277
|
case "h":
|
258
278
|
logHelp()
|
259
279
|
break
|
260
|
-
case "golf":
|
261
|
-
case "minify":
|
262
|
-
{
|
263
|
-
const target = commands[1]
|
264
|
-
if (!target) {
|
265
|
-
logError("Must provide target\n")
|
266
|
-
logHelp()
|
267
|
-
break
|
268
|
-
}
|
269
|
-
const fileExtension = extname(target)
|
270
|
-
if (!supportedExtensions.includes(fileExtension)) {
|
271
|
-
logError(
|
272
|
-
`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${supportedExtensions.map(extension => chalk.bold(extension)).join('", "')}"`
|
273
|
-
)
|
274
|
-
break
|
275
|
-
}
|
276
|
-
const { processScript } = await processScriptModule,
|
277
|
-
fileBaseName = basename(target, fileExtension),
|
278
|
-
fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src"),
|
279
|
-
scriptName = fileBaseNameEndsWithDotSrc ? fileBaseName.slice(0, -4) : fileBaseName,
|
280
|
-
scriptUser =
|
281
|
-
"scripts" == basename(resolve(target, "..")) && "hackmud" == basename(resolve(target, "../../..")) ?
|
282
|
-
basename(resolve(target, "../.."))
|
283
|
-
: void 0,
|
284
|
-
optionsHasNoMinify = options.has("no-minify")
|
285
|
-
if ((optionsHasNoMinify || options.has("skip-minify")) && options.has("mangle-names")) {
|
286
|
-
logError(
|
287
|
-
`Options ${colourN("--mangle-names")} and ${colourN(optionsHasNoMinify ? "--no-minify" : "--skip-minify")} are incompatible\n`
|
288
|
-
)
|
289
|
-
logHelp()
|
290
|
-
break
|
291
|
-
}
|
292
|
-
const mangleNames_ = options.get("mangle-names")
|
293
|
-
if (null != mangleNames_ && "boolean" != typeof mangleNames_) {
|
294
|
-
logError(
|
295
|
-
`The value for ${colourN("--mangle-names")} must be ${colourV("true")} or ${colourV("false")}\n`
|
296
|
-
)
|
297
|
-
logHelp()
|
298
|
-
break
|
299
|
-
}
|
300
|
-
const mangleNames = mangleNames_,
|
301
|
-
forceQuineCheats_ = options.get("force-quine-cheats")
|
302
|
-
if (null != forceQuineCheats_ && "boolean" != typeof forceQuineCheats_) {
|
303
|
-
logError(
|
304
|
-
`the value for ${colourN("--force-quine-cheats")} must be ${colourV("true")} or ${colourV("false")}\n`
|
305
|
-
)
|
306
|
-
logHelp()
|
307
|
-
break
|
308
|
-
}
|
309
|
-
const forceQuineCheats = forceQuineCheats_
|
310
|
-
let outputPath =
|
311
|
-
commands[2] ||
|
312
|
-
resolve(
|
313
|
-
dirname(target),
|
314
|
-
fileBaseNameEndsWithDotSrc ? scriptName + ".js"
|
315
|
-
: ".js" == fileExtension ? fileBaseName + ".min.js"
|
316
|
-
: fileBaseName + ".js"
|
317
|
-
)
|
318
|
-
const golfFile = () =>
|
319
|
-
readFile(target, { encoding: "utf8" }).then(async source => {
|
320
|
-
const timeStart = performance.now(),
|
321
|
-
{ script, warnings } = await processScript(source, {
|
322
|
-
minify: !(options.get("no-minify") || options.get("skip-minify")),
|
323
|
-
scriptUser,
|
324
|
-
scriptName,
|
325
|
-
filePath: target,
|
326
|
-
mangleNames,
|
327
|
-
forceQuineCheats
|
328
|
-
}),
|
329
|
-
timeTook = performance.now() - timeStart
|
330
|
-
for (const { message, line } of warnings)
|
331
|
-
log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(line + "")}`)
|
332
|
-
await writeFilePersistent(outputPath, script)
|
333
|
-
.catch(error => {
|
334
|
-
if (!commands[2] || "EISDIR" != error.code) throw error
|
335
|
-
outputPath = resolve(outputPath, basename(target, fileExtension) + ".js")
|
336
|
-
return writeFilePersistent(outputPath, script)
|
337
|
-
})
|
338
|
-
.then(() =>
|
339
|
-
log(
|
340
|
-
`Wrote ${chalk.bold(countHackmudCharacters(script))} chars to ${chalk.bold(relative(".", outputPath))} | took ${Math.round(100 * timeTook) / 100}ms`
|
341
|
-
)
|
342
|
-
)
|
343
|
-
})
|
344
|
-
if (options.get("watch")) {
|
345
|
-
const { watch: watchFile } = await chokidarModule
|
346
|
-
watchFile(target, { awaitWriteFinish: { stabilityThreshold: 100 } })
|
347
|
-
.on("ready", () => log("Watching " + target))
|
348
|
-
.on("change", golfFile)
|
349
|
-
autoExit = !1
|
350
|
-
} else await golfFile()
|
351
|
-
}
|
352
|
-
break
|
353
280
|
default:
|
354
281
|
commands[0] && logError(`Unknown command: ${colourL(commands[0])}\n`)
|
355
282
|
logHelp()
|
@@ -359,14 +286,14 @@ function logHelp() {
|
|
359
286
|
const pushCommandDescription = "Push scripts from a directory to hackmud user's scripts directories",
|
360
287
|
forceQuineCheatsOptionDescription = `Force quine cheats on. Use ${colourN("--force-quine-cheats")}=${colourV("false")} to force off`,
|
361
288
|
hackmudPathOption = `${colourN("--hackmud-path")}=${colourB("<path>")}\n Override hackmud path`
|
362
|
-
console.log(colourN("Version") + colourS(": ") + colourV(
|
289
|
+
console.log(colourN("Version") + colourS(": ") + colourV("0.20.4-06a037b"))
|
363
290
|
switch (commands[0]) {
|
364
291
|
case "dev":
|
365
292
|
case "watch":
|
366
293
|
case "push":
|
367
294
|
console.log(
|
368
295
|
colourS(
|
369
|
-
`\n${colourJ("push" == commands[0] ? pushCommandDescription : "Watch a directory and push a script when modified")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB('<directory> ["<script user>.<script name>"...]')}\n\n${colourA("Arguments:")}\n${colourB("<directory>")}\n The source directory containing your scripts\n${colourB("<script user>")}\n A user to push script(s) to. Can be set to wild card (${colourV("*")}) which will try\n and discover users to push to\n${colourB("<script name>")}\n Name of a script to push. Can be set to wild card (${colourV("*")}) to find all scripts\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n Reduce character count further but lose function names in error call stacks\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}\n${hackmudPathOption}\n${"push" == commands[0] ? "" : `${colourN("--type-declaration-path")}=${colourB("<path>")}\n Path to generate a type declaration file for the scripts\n`}\n${colourA("Examples:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")}\n
|
296
|
+
`\n${colourJ("push" == commands[0] ? pushCommandDescription : "Watch a directory and push a script when modified")}\n\n${colourA("Usage:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourB('<directory> ["<script user>.<script name>"...]')}\n\n${colourA("Arguments:")}\n${colourB("<directory>")}\n The source directory containing your scripts\n${colourB("<script user>")}\n A user to push script(s) to. Can be set to wild card (${colourV("*")}) which will try\n and discover users to push to\n${colourB("<script name>")}\n Name of a script to push. Can be set to wild card (${colourV("*")}) to find all scripts\n\n${colourA("Options:")}\n${colourN("--no-minify")}\n Skip minification to produce a "readable" script\n${colourN("--mangle-names")}\n Reduce character count further but lose function names in error call stacks\n${colourN("--force-quine-cheats")}\n ${forceQuineCheatsOptionDescription}\n${hackmudPathOption}\n${"push" == commands[0] ? "" : `${colourN("--type-declaration-path")}=${colourB("<path>")}\n Path to generate a type declaration file for the scripts\n`}\n${colourA("Examples:")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")}\n Pushes all scripts found in ${colourV("src")} folder to all users\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("foo")}${colourV(".")}${colourL("bar")}\n Pushes a script named ${colourL("bar")} found in ${colourV("src")} folder to user ${userColours.get("foo")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("foo")}${colourV(".")}${colourL("bar")} ${colourC("baz")}${colourV(".")}${colourL("qux")}\n Multiple can be specified\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("foo")}${colourV(".")}${colourL("*")}\n Pushes all scripts found in ${colourV("src")} folder to user ${userColours.get("foo")}\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("*")}${colourV(".")}${colourL("foo")}\n Pushes all scripts named ${colourL("foo")} found in ${colourV("src")} folder to all user\n${colourC("hsm")} ${colourL(commands[0])} ${colourV("src")} ${colourC("*")}${colourV(".")}${colourL("*")}\n Pushes all scripts found in ${colourV("src")} folder to all users`
|
370
297
|
)
|
371
298
|
)
|
372
299
|
break
|
@@ -423,7 +350,7 @@ function logError(message) {
|
|
423
350
|
process.exitCode = 1
|
424
351
|
}
|
425
352
|
function getHackmudPath() {
|
426
|
-
const hackmudPathOption =
|
353
|
+
const hackmudPathOption = popOption("hackmud-path")
|
427
354
|
if (null != hackmudPathOption && "string" != typeof hackmudPathOption) {
|
428
355
|
logError(`Option ${colourN("--hackmud-path")} must be a string, got ${colourV(hackmudPathOption)}\n`)
|
429
356
|
logHelp()
|
@@ -435,3 +362,32 @@ function getHackmudPath() {
|
|
435
362
|
("win32" == process.platform ? resolve(process.env.APPDATA, "hackmud") : resolve(homedir(), ".config/hackmud"))
|
436
363
|
)
|
437
364
|
}
|
365
|
+
function assertOptionIsBoolean(option) {
|
366
|
+
if ("boolean" != typeof option.value) {
|
367
|
+
logError(`The value for ${formatOption(option.name)} must be ${colourV("true")} or ${colourV("false")}\n`)
|
368
|
+
logHelp()
|
369
|
+
process.exit(1)
|
370
|
+
}
|
371
|
+
}
|
372
|
+
function popOption(...names) {
|
373
|
+
const presentOptionNames = names.filter(name => options.has(name))
|
374
|
+
if (!presentOptionNames.length) return
|
375
|
+
const presentOptionNamesWithDashDash = presentOptionNames.map(formatOption)
|
376
|
+
if (presentOptionNames.length > 1) {
|
377
|
+
logError(
|
378
|
+
`The options ${presentOptionNamesWithDashDash.join(", ")} are aliases for each other. Please only specify one`
|
379
|
+
)
|
380
|
+
process.exit(1)
|
381
|
+
}
|
382
|
+
const value = options.get(presentOptionNames[0])
|
383
|
+
options.delete(presentOptionNames[0])
|
384
|
+
return { name: presentOptionNamesWithDashDash[0], value }
|
385
|
+
}
|
386
|
+
function complainAboutUnrecognisedOptions() {
|
387
|
+
if (options.size) {
|
388
|
+
logError(
|
389
|
+
`Unrecognised option${options.size > 1 ? "s" : ""}: ${[...options.keys()].map(formatOption).join(", ")}`
|
390
|
+
)
|
391
|
+
process.exit(1)
|
392
|
+
}
|
393
|
+
}
|