hackmud-script-manager 0.13.0-c461329 → 0.13.0-f373e9c

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. package/assert-1b7dada8.js +1 -0
  2. package/bin/hsm.d.ts +2 -0
  3. package/bin/hsm.js +2 -0
  4. package/generateTypings.d.ts +2 -0
  5. package/generateTypings.js +1 -0
  6. package/index.d.ts +15 -0
  7. package/index.js +1 -0
  8. package/package.json +35 -11
  9. package/processScript/index.d.ts +33 -0
  10. package/processScript/index.js +1 -0
  11. package/processScript/minify.d.ts +14 -0
  12. package/processScript/minify.js +1 -0
  13. package/processScript/postprocess.d.ts +2 -0
  14. package/processScript/postprocess.js +1 -0
  15. package/processScript/preprocess.d.ts +13 -0
  16. package/processScript/preprocess.js +1 -0
  17. package/processScript/shared.d.ts +3 -0
  18. package/processScript/shared.js +1 -0
  19. package/processScript/transform.d.ts +22 -0
  20. package/processScript/transform.js +1 -0
  21. package/pull.d.ts +9 -0
  22. package/pull.js +1 -0
  23. package/push.d.ts +28 -0
  24. package/push.js +1 -0
  25. package/spliceString-2c6f214f.js +1 -0
  26. package/syncMacros.d.ts +5 -0
  27. package/syncMacros.js +1 -0
  28. package/test.d.ts +6 -0
  29. package/test.js +1 -0
  30. package/watch.d.ts +14 -0
  31. package/watch.js +1 -0
  32. package/.gitattributes +0 -1
  33. package/.github/workflows/codeql-analysis.yml +0 -39
  34. package/.github/workflows/publish.yml +0 -42
  35. package/.vscode/settings.json +0 -6
  36. package/babel.config.json +0 -6
  37. package/rollup.config.js +0 -110
  38. package/scripts/build-package-json.js +0 -36
  39. package/scripts/jsconfig.json +0 -5
  40. package/scripts/version-dev.js +0 -25
  41. package/src/bin/hsm.ts +0 -505
  42. package/src/constants.json +0 -3
  43. package/src/generateTypings.ts +0 -116
  44. package/src/index.ts +0 -19
  45. package/src/modules.d.ts +0 -5
  46. package/src/processScript/index.ts +0 -198
  47. package/src/processScript/minify.ts +0 -529
  48. package/src/processScript/postprocess.ts +0 -38
  49. package/src/processScript/preprocess.ts +0 -146
  50. package/src/processScript/transform.ts +0 -760
  51. package/src/pull.ts +0 -16
  52. package/src/push.ts +0 -314
  53. package/src/syncMacros.ts +0 -52
  54. package/src/test.ts +0 -59
  55. package/src/tsconfig.json +0 -20
  56. package/src/watch.ts +0 -156
  57. package/tsconfig.json +0 -12
package/rollup.config.js DELETED
@@ -1,110 +0,0 @@
1
- import babel from "@rollup/plugin-babel"
2
- import commonJS from "@rollup/plugin-commonjs"
3
- import json from "@rollup/plugin-json"
4
- import nodeResolve from "@rollup/plugin-node-resolve"
5
- import fs from "fs"
6
- import preserveShebang from "rollup-plugin-preserve-shebang"
7
- import { terser } from "rollup-plugin-terser"
8
- import packageConfig from "./package.json"
9
-
10
- const { readdir: readDirectory } = fs.promises
11
-
12
- /** @typedef {import("rollup").RollupOptions} RollupOptions */
13
-
14
- const plugins = [
15
- babel({
16
- babelHelpers: "bundled",
17
- extensions: [ ".ts" ]
18
- }),
19
- commonJS(),
20
- json({ preferConst: true }),
21
- nodeResolve({ extensions: [ ".ts" ] }),
22
- preserveShebang()
23
- ]
24
-
25
- const external = []
26
-
27
- if ("dependencies" in packageConfig)
28
- external.push(...Object.keys(packageConfig.dependencies))
29
-
30
- const sourceDirectory = "src"
31
- const findFilesPromise = findFiles(sourceDirectory)
32
-
33
- /** @type {(command: Record<string, unknown>) => Promise<RollupOptions>} */
34
- export default async ({ w }) => {
35
- if (!w) {
36
- plugins.push(terser({
37
- ecma: 2019,
38
- keep_classnames: true,
39
- keep_fnames: true
40
- }))
41
- } else if ("devDependencies" in packageConfig)
42
- external.push(...Object.keys(packageConfig.devDependencies).map(name => new RegExp(`^${name}(?:/|$)`)))
43
-
44
- return {
45
- input: Object.fromEntries(
46
- (await findFilesPromise)
47
- .filter(path => path.endsWith(".ts") && !path.endsWith(".d.ts"))
48
- .map(path => [ path.slice(sourceDirectory.length + 1, -3), path ])
49
- ),
50
- output: {
51
- dir: "dist",
52
- interop: "auto"
53
- },
54
- plugins,
55
- external,
56
- preserveEntrySignatures: "allow-extension",
57
- treeshake: {
58
- moduleSideEffects: "no-external"
59
- }
60
- }
61
- }
62
-
63
- /**
64
- * @param path the directory to start recursively finding files in
65
- * @param filter either a blacklist or a filter function that returns false to ignore file name
66
- * @returns promise that resolves to array of found files
67
- * @type {(path: string, filter?: string[] | ((name: string) => boolean)) => Promise<string[]>}
68
- */
69
- async function findFiles(path, filter = []) {
70
- const paths = []
71
- let /** @type {(name: string) => boolean} */ filterFunction
72
-
73
- if (Array.isArray(filter))
74
- filterFunction = name => !filter.includes(name)
75
- else
76
- filterFunction = filter
77
-
78
- for (const dirent of await readDirectory(path, { withFileTypes: true })) {
79
- if (!filterFunction(dirent.name))
80
- continue
81
-
82
- const direntPath = `${path}/${dirent.name}`
83
-
84
- if (dirent.isDirectory())
85
- await findFilesSub(direntPath, filterFunction, paths)
86
- else if (dirent.isFile())
87
- paths.push(direntPath)
88
- }
89
-
90
- return paths
91
- }
92
-
93
- async function findFilesSub(path, filterFunction, paths) {
94
- const promises = []
95
-
96
- for (const dirent of await readDirectory(path, { withFileTypes: true })) {
97
- if (!filterFunction(dirent.name))
98
- continue
99
-
100
- const direntPath = `${path}/${dirent.name}`
101
-
102
- if (dirent.isDirectory())
103
- promises.push(findFilesSub(direntPath, filterFunction, paths))
104
- else if (dirent.isFile())
105
- paths.push(direntPath)
106
- }
107
-
108
- await Promise.all(promises)
109
- return paths
110
- }
@@ -1,36 +0,0 @@
1
- import findFiles from "@samual/lib/findFiles"
2
- import fs from "fs"
3
-
4
- const { writeFile, readFile } = fs.promises
5
-
6
- ;(async () => {
7
- const packageConfig = JSON.parse(await readFile("package.json", { encoding: "utf-8" }))
8
-
9
- delete packageConfig.private
10
- delete packageConfig.scripts
11
-
12
- packageConfig.bin = {}
13
-
14
- for (let name of await findFiles("dist")) {
15
- name = `.${name.slice(4)}`
16
-
17
- if (name.startsWith("./bin/") && name.endsWith(".js")) {
18
- packageConfig.bin[name.slice(6, -3)] = name
19
- continue
20
- }
21
-
22
- if (!name.endsWith(".d.ts"))
23
- continue
24
-
25
- name = name.slice(0, -5)
26
-
27
- const nameWithExtension = `${name}.js`
28
-
29
- packageConfig.exports[name] = nameWithExtension
30
-
31
- if (name != "./index" && name.endsWith("/index"))
32
- packageConfig.exports[name.slice(0, -6)] = nameWithExtension
33
- }
34
-
35
- await writeFile("dist/package.json", JSON.stringify(packageConfig, null, "\t"))
36
- })()
@@ -1,5 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "ES2015"
4
- }
5
- }
@@ -1,25 +0,0 @@
1
- import { exec as execute_ } from "child_process"
2
- import fs from "fs"
3
- import semver from "semver"
4
- import { promisify } from "util"
5
-
6
- const { readFile } = fs.promises
7
- const execute = promisify(execute_)
8
-
9
- ;(async () => {
10
- const [ packageJSONFile, { stdout: gitGetHashStdout } ] = await Promise.all([
11
- readFile("package.json", { encoding: "utf-8" }),
12
- execute("git rev-parse --short HEAD")
13
- ])
14
-
15
- const { stdout, stderr } = await execute(
16
- `npm version ${
17
- semver.inc(JSON.parse(packageJSONFile).version, "minor")
18
- }-${
19
- gitGetHashStdout.trim()
20
- }`
21
- )
22
-
23
- process.stdout.write(stdout)
24
- process.stderr.write(stderr)
25
- })()
package/src/bin/hsm.ts DELETED
@@ -1,505 +0,0 @@
1
- #!/usr/bin/env node
2
- import { countHackmudCharacters, DynamicMap, writeFilePersistent } from "@samual/lib"
3
- import chalk from "chalk"
4
- import fs from "fs"
5
- import { homedir as getHomeDirectory } from "os"
6
- import { basename as getBaseName, dirname as getPathDirectory, extname as getFileExtension, relative as relativePath, resolve as resolvePath } from "path"
7
- import { generateTypings, Info, processScript, pull, push, syncMacros, test, watch } from ".."
8
- import { version as moduleVersion } from "../../package.json"
9
- import { supportedExtensions } from "../constants.json"
10
-
11
- const { readFile, rmdir: removeDirectory, writeFile, mkdir: makeDirectory } = fs.promises
12
-
13
- type ArgValue = boolean | number | string/* | ArgValue[]*/
14
-
15
- const configDirPath = resolvePath(getHomeDirectory(), ".config")
16
- const configFilePath = resolvePath(configDirPath, "hsm.json")
17
-
18
- const options = new Map<string, ArgValue>()
19
- const commands: string[] = []
20
-
21
- let config: Record<string, any> & Partial<{
22
- hackmudPath: string
23
- defaultUser: string
24
- users: Record<string, {
25
- colour: string
26
- }>
27
- }> | undefined
28
-
29
- const colourJ = chalk.rgb(0xFF, 0xF4, 0x04)
30
- const colourK = chalk.rgb(0xF3, 0xF9, 0x98)
31
- const colourM = chalk.rgb(0xB3, 0xFF, 0x9B)
32
- const colourW = chalk.rgb(0xFF, 0x96, 0xE0)
33
- const colourL = chalk.rgb(0x1E, 0xFF, 0x00)
34
- const colourB = chalk.rgb(0xCA, 0xCA, 0xCA)
35
-
36
- const userColours = new DynamicMap<string, string>(user => {
37
- let hash = 0
38
-
39
- for (const char of user)
40
- hash += (hash >> 1) + hash + "xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj".indexOf(char) + 1
41
-
42
- return [ colourJ, colourK, colourM, colourW, colourL, colourB ][hash % 6](user)
43
- })
44
-
45
- for (const arg of process.argv.slice(2)) {
46
- if (arg[0] == "-") {
47
- const [ key, valueRaw ] = arg.split("=")
48
- let value: ArgValue = valueRaw
49
-
50
- if (value) {
51
- if (value == "true")
52
- value = true
53
- else if (value == "false")
54
- value = false
55
- else {
56
- const number = Number(value)
57
-
58
- if (isFinite(number))
59
- value = number
60
- }
61
- } else
62
- value = true
63
-
64
- if (arg[1] == "-")
65
- options.set(key.slice(2), value)
66
- else {
67
- for (const option of key.slice(1))
68
- options.set(option, value)
69
- }
70
- } else
71
- commands.push(arg)
72
- }
73
-
74
- (async () => {
75
- if (options.get("version") || options.get("v")) {
76
- version()
77
- return
78
- }
79
-
80
- if (options.get("help") || options.get("h")) {
81
- help()
82
- return
83
- }
84
-
85
- switch (commands[0]) {
86
- case "push": {
87
- const config = await getConfig()
88
-
89
- if (!config.hackmudPath) {
90
- console.log("you need to set hackmudPath in config before you can use this command")
91
- break
92
- }
93
-
94
- const srcPath = commands[1] || "."
95
- const hackmudPath = config.hackmudPath
96
- const scripts = commands.slice(2)
97
-
98
- if (!scripts.length)
99
- scripts.push("*.*")
100
-
101
- const infos = await push(
102
- srcPath,
103
- hackmudPath,
104
- {
105
- scripts,
106
- onPush: onPushLogger,
107
- minify: !options.get("skip-minify")
108
- }
109
- )
110
-
111
- if (!infos.length)
112
- console.warn("couldn't find any scripts to push")
113
-
114
- updateConfig()
115
- } break
116
-
117
- case "dev":
118
- case "watch": {
119
- const config = await getConfig()
120
-
121
- if (!config.hackmudPath) {
122
- console.log("you need to set hackmudPath in config before you can use this command")
123
- break
124
- }
125
-
126
- const srcPath = commands[1] || "."
127
- const hackmudPath = config.hackmudPath
128
- const users = options.get("users")?.toString().split(",") || []
129
- const scripts = options.get("scripts")?.toString().split(",") || []
130
- const genTypes = options.get("gen-types")?.toString()
131
-
132
- watch(srcPath, hackmudPath, users, scripts, onPushLogger, { genTypes })
133
- } break
134
-
135
- case "pull": {
136
- const config = await getConfig()
137
-
138
- if (!config.hackmudPath) {
139
- console.log("you need to set hackmudPath in config before you can use this command")
140
- break
141
- }
142
-
143
- const script = commands[1]
144
-
145
- if (!script) {
146
- help()
147
- break
148
- }
149
-
150
- const srcPath = commands[2] || "."
151
- const hackmudPath = config.hackmudPath
152
-
153
- try {
154
- await pull(srcPath, hackmudPath, script)
155
- } catch (error) {
156
- console.log("something went wrong, did you forget to #down the script?")
157
- }
158
- } break
159
-
160
- case "sync-macros": {
161
- const { hackmudPath } = await getConfig()
162
-
163
- if (!hackmudPath) {
164
- console.log("you need to set hackmudPath in config before you can use this command")
165
- break
166
- }
167
-
168
- const { macrosSynced, usersSynced } = await syncMacros(hackmudPath)
169
- console.log(`synced ${macrosSynced} macros to ${usersSynced} users`)
170
- } break
171
-
172
- case "test": {
173
- const srcPath = resolvePath(commands[1] || ".")
174
- let errors = 0
175
-
176
- console.log(`testing scripts in ${chalk.bold(srcPath)}\n`)
177
-
178
- for (const { file, line, message } of await test(srcPath)) {
179
- console.log(`error "${chalk.bold(message)}" in ${chalk.bold(file)} on line ${chalk.bold(String(line))}`)
180
- errors++
181
- }
182
-
183
- if (!errors) {
184
- console.log("no errors found")
185
- break
186
- }
187
-
188
- if (errors) {
189
- process.exitCode = 1
190
- console.log(`\nencountered ${chalk.bold(String(errors))} errors`)
191
- break
192
- }
193
-
194
- console.log("no errors found")
195
- } break
196
-
197
- case "gen-types": {
198
- const srcPath = resolvePath(commands[1] || ".")
199
- let targetPath: string
200
-
201
- if (commands[2])
202
- targetPath = resolvePath(commands[2])
203
- else
204
- targetPath = resolvePath(srcPath, "../player.d.ts")
205
-
206
- generateTypings(srcPath, targetPath, (await getConfig()).hackmudPath)
207
- } break
208
-
209
- case "config":
210
- switch (commands[1]) {
211
- case "get": {
212
- console.log(exploreObject(await getConfig(), commands[2].split(".")))
213
- } break
214
-
215
- case "delete": {
216
- const keys = commands[2].split(".")
217
-
218
- if (!keys.length) {
219
- help()
220
- break
221
- }
222
-
223
- const config = await getConfig()
224
-
225
- delete exploreObject(config, keys)?.[commands[3]]
226
-
227
- console.log(config)
228
- } break
229
-
230
- case "set": {
231
- const keys = commands[2].split(".")
232
-
233
- if (!keys.length) {
234
- help()
235
- break
236
- }
237
-
238
- const config = await getConfig()
239
- let object = config
240
-
241
- for (let key of keys.slice(0, -1))
242
- object = typeof object[key] == "object" ? object[key] : object[key] = {}
243
-
244
- object[keys.slice(-1)[0]] = commands[3]
245
-
246
- if (config.hackmudPath)
247
- config.hackmudPath = resolvePath(config.hackmudPath)
248
-
249
- console.log(config)
250
- } break
251
-
252
- default: {
253
- if (commands[1])
254
- console.log("unknown command")
255
-
256
- help()
257
- }
258
- } break
259
-
260
- case "help":
261
- case "h": {
262
- help()
263
- } break
264
-
265
- case "version":
266
- case "v": {
267
- version()
268
- } break
269
-
270
- case "golf":
271
- case "minify": {
272
- // TODO `--watch` option
273
-
274
- if (!commands[1]) {
275
- console.log(`Target required\nUsage: ${getBaseName(process.argv[1])} ${commands[0]} <target> [output]`)
276
- break
277
- }
278
-
279
- const fileExtension = getFileExtension(commands[1])
280
-
281
- if (!supportedExtensions.includes(fileExtension)) {
282
- console.log(`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${supportedExtensions.map(extension => chalk.bold(extension)).join('", "')}"`)
283
- break
284
- }
285
-
286
- await readFile(commands[1], { encoding: "utf-8" }).then(
287
- async source => {
288
- const fileBaseName = getBaseName(commands[1], fileExtension)
289
- const fileBaseNameEndsWithDotSrc = fileBaseName.endsWith(".src")
290
-
291
- const scriptName = fileBaseNameEndsWithDotSrc
292
- ? fileBaseName.slice(0, -4)
293
- : fileBaseName
294
-
295
- let scriptUser = "UNKNOWN"
296
-
297
- if (getBaseName(resolvePath(commands[1], "..")) == "scripts" && getBaseName(resolvePath(commands[1], "../../..")) == "hackmud")
298
- scriptUser = getBaseName(resolvePath(commands[1], "../.."))
299
-
300
- const minify = !options.get("skip-minify")
301
- const mangleNames = Boolean(options.get("mangle-names"))
302
-
303
- if (!minify && mangleNames)
304
- console.warn("warning: `--mangle-names` has no effect while `--skip-minify` is active")
305
-
306
- const { script, srcLength, warnings, timeTook } = await processScript(
307
- source,
308
- {
309
- minify,
310
- scriptUser,
311
- scriptName,
312
- filePath: commands[1],
313
- mangleNames
314
- }
315
- )
316
-
317
- for (const { message, line } of warnings)
318
- console.log(`warning "${chalk.bold(message)}" on line ${chalk.bold(String(line))}`)
319
-
320
- let outputPath: string
321
-
322
- if (commands[2])
323
- outputPath = commands[2]
324
- else {
325
- outputPath = resolvePath(
326
- getPathDirectory(commands[1]),
327
-
328
- fileBaseNameEndsWithDotSrc
329
- ? `${scriptName}.js` :
330
- fileExtension == ".js"
331
- ? `${fileBaseName}.min.js`
332
- : `${fileBaseName}.js`
333
- )
334
- }
335
-
336
- const scriptLength = countHackmudCharacters(script)
337
-
338
- await writeFilePersistent(outputPath, script)
339
- .catch(async (error: NodeJS.ErrnoException) => {
340
- if (!commands[2] || error.code != "EISDIR")
341
- throw error
342
-
343
- outputPath = resolvePath(outputPath, `${getBaseName(commands[1], fileExtension)}.js`)
344
-
345
- await writeFilePersistent(outputPath, script)
346
- })
347
- .then(
348
- () => console.log(`wrote ${chalk.bold(scriptLength)} chars to ${chalk.bold(relativePath(".", outputPath))} | saved ${chalk.bold(srcLength - scriptLength)} chars | took ${Math.round(timeTook * 100) / 100}ms`),
349
- (error: NodeJS.ErrnoException) => console.log(error.message)
350
- )
351
- },
352
- (error: NodeJS.ErrnoException) => console.log(error.message)
353
- )
354
- } break
355
-
356
- default: {
357
- if (commands[0])
358
- console.log("unknown command")
359
-
360
- help()
361
- }
362
- }
363
-
364
- updateConfig()
365
- })()
366
-
367
- function help() {
368
- switch (commands[0]) {
369
- case "config": {
370
- switch (commands[1]) {
371
- case "get": {
372
- console.log("hsm config get <key>")
373
- } break
374
-
375
- case "set": {
376
- console.log("hsm config set <key> <value>")
377
- } break
378
-
379
- case "delete": {
380
- console.log("hsm config delete <key>")
381
- } break
382
-
383
- default: {
384
- console.log("hsm config <get, delete, set>")
385
- }
386
- }
387
- } break
388
-
389
- case "push": {
390
- console.log("hsm push [<dir> [...\"<script user>.<script name>\"]]")
391
- } break
392
-
393
- case "watch": {
394
- console.log("hsm watch [dir]")
395
- } break
396
-
397
- case "pull": {
398
- console.log("hsm pull <script user>.<script name>")
399
- } break
400
-
401
- case "minify":
402
- case "golf": {
403
- console.log(`${getBaseName(process.argv[1])} ${commands[0]} <target> [output]`)
404
- } break
405
-
406
- default: {
407
- console.log("hsm <push, watch, pull, config, golf>")
408
- }
409
- }
410
- }
411
-
412
- async function version() {
413
- console.log(moduleVersion)
414
- }
415
-
416
- async function getConfig() {
417
- if (config)
418
- return config
419
-
420
- return config = await readFile(configFilePath, { encoding: "utf-8" })
421
- .then(configFile => {
422
- let tempConfig
423
-
424
- try {
425
- tempConfig = JSON.parse(configFile)
426
- } catch {
427
- // TODO log to error log file
428
- console.log("config file was corrupted, resetting")
429
- return {}
430
- }
431
-
432
- if (!tempConfig || typeof tempConfig != "object") {
433
- console.log("config file was corrupted, resetting")
434
- return {}
435
- }
436
-
437
- return tempConfig
438
- }, () => {
439
- console.log(`creating config file at ${configFilePath}`)
440
- return {}
441
- })
442
- }
443
-
444
- function exploreObject(object: any, keys: string[], createPath = false) {
445
- for (const key of keys) {
446
- if (createPath)
447
- object = typeof object[key] == "object" ? object[key] : object[key] = {}
448
- else
449
- object = object?.[key]
450
- }
451
-
452
- return object
453
- }
454
-
455
- function updateConfig() {
456
- if (config) {
457
- const json = JSON.stringify(config)
458
-
459
- writeFile(configFilePath, json).catch(async error => {
460
- switch (error.code) {
461
- case "EISDIR": {
462
- await removeDirectory(configFilePath)
463
- } break
464
-
465
- case "ENOENT": {
466
- await makeDirectory(configDirPath)
467
- } break
468
-
469
- default: {
470
- throw error
471
- }
472
- }
473
-
474
- writeFile(configFilePath, json)
475
- })
476
- }
477
- }
478
-
479
- function onPushLogger({ file, users, srcLength, minLength, error }: Info) {
480
- if (!users.length)
481
- return
482
-
483
- if (error) {
484
- console.log(`error "${chalk.bold(error.message)}" in ${chalk.bold(file)}`)
485
- return
486
- }
487
-
488
- console.log(
489
- `pushed ${
490
- chalk.bold(file)
491
- } to ${
492
- users.map(user => chalk.bold(userColours.get(user))).join(", ")
493
- } | ${
494
- chalk.bold(String(minLength))
495
- } chars from ${
496
- chalk.bold(String(srcLength))
497
- } | saved ${
498
- chalk.bold(String(srcLength - minLength))
499
- } (${
500
- chalk.bold(`${Math.round((1 - (minLength / srcLength)) * 100)}%`)
501
- }) | ${
502
- chalk.bold(`${resolvePath(config!.hackmudPath!, users[0], "scripts", getBaseName(file, getFileExtension(file)))}.js`)
503
- }`
504
- )
505
- }
@@ -1,3 +0,0 @@
1
- {
2
- "supportedExtensions": [ ".js", ".ts" ]
3
- }