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

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.
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
@@ -1,116 +0,0 @@
1
- import fs from "fs"
2
- import { basename as getBaseName, extname as getFileExtension, resolve as resolvePath } from "path"
3
-
4
- const { readdir: readDirectory, writeFile } = fs.promises
5
-
6
- export async function generateTypings(srcDir: string, target: string, hackmudPath?: string) {
7
- const users = new Set<string>()
8
-
9
- if (hackmudPath) {
10
- for (const dirent of await readDirectory(hackmudPath, { withFileTypes: true })) {
11
- if (dirent.isFile() && getFileExtension(dirent.name) == ".key")
12
- users.add(getBaseName(dirent.name, ".key"))
13
- }
14
- }
15
-
16
- const wildScripts: string[] = []
17
- const wildAnyScripts: string[] = []
18
- const allScripts: Record<string, string[]> = {}
19
- const allAnyScripts: Record<string, string[]> = {}
20
-
21
- for (const dirent of await readDirectory(srcDir, { withFileTypes: true })) {
22
- if (dirent.isFile()) {
23
- if (getFileExtension(dirent.name) == ".ts")
24
- wildScripts.push(getBaseName(dirent.name, ".ts"))
25
- else if (getFileExtension(dirent.name) == ".js")
26
- wildAnyScripts.push(getBaseName(dirent.name, ".js"))
27
- } else if (dirent.isDirectory()) {
28
- const scripts: string[] = allScripts[dirent.name] = []
29
- const anyScripts: string[] = allAnyScripts[dirent.name] = []
30
-
31
- users.add(dirent.name)
32
-
33
- for (const file of await readDirectory(resolvePath(srcDir, dirent.name), { withFileTypes: true })) {
34
- if (file.isFile()) {
35
- if (getFileExtension(file.name) == ".ts")
36
- scripts.push(getBaseName(file.name, ".ts"))
37
- else if (getFileExtension(file.name) == ".js")
38
- anyScripts.push(getBaseName(file.name, ".js"))
39
- }
40
- }
41
- }
42
- }
43
-
44
- let o = ""
45
-
46
- for (const script of wildScripts)
47
- o += `import { script as $${script}$ } from "./src/${script}"\n`
48
-
49
- o += "\n"
50
-
51
- for (const user in allScripts) {
52
- const scripts = allScripts[user]
53
-
54
- for (const script of scripts)
55
- o += `import { script as $${user}$${script}$ } from "./src/${user}/${script}"\n`
56
- }
57
-
58
- // TODO detect security level and generate apropriate code
59
-
60
- // TODO accurate function signatures
61
- // currently I lose the generic-ness of my functions when I wrap them
62
- // just regexing isn't enough and it looks like I'm going to need to parse the files in TypeScript to extract the signature
63
-
64
- o += `
65
- type ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never
66
-
67
- type Subscript<T extends (...args: any) => any> =
68
- (...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure
69
-
70
- type WildFullsec = Record<string, () => ScriptFailure> & {
71
- `
72
-
73
- for (const script of wildScripts)
74
- o += `\t${script}: Subscript<typeof $${script}$>\n`
75
-
76
- for (const script of wildAnyScripts)
77
- o += `\t${script}: (...args: any) => any\n`
78
-
79
- o += "}\n\ndeclare global {\n\tinterface PlayerFullsec {"
80
-
81
- let lastWasMultiLine = true
82
-
83
- for (const user of users) {
84
- const scripts = allScripts[user]
85
- const anyScripts = allAnyScripts[user]
86
-
87
- if ((scripts && scripts.length) || (anyScripts && anyScripts.length)) {
88
- lastWasMultiLine = true
89
-
90
- o += `\n\t\t${user}: WildFullsec & {\n`
91
-
92
- for (const script of scripts)
93
- o += `\t\t\t${script}: Subscript<typeof $${user}$${script}$>\n`
94
-
95
- for (const script of anyScripts)
96
- o += `\t\t\t${script}: (...args: any) => any\n`
97
-
98
- o += "\t\t}"
99
- } else {
100
- if (lastWasMultiLine) {
101
- o += "\n"
102
- lastWasMultiLine = false
103
- }
104
-
105
- o += `\t\t${user}: WildFullsec`
106
- }
107
-
108
- o += "\n"
109
- }
110
-
111
- o += "\t}\n}\n"
112
-
113
- await writeFile(target, o)
114
- }
115
-
116
- export default generateTypings
package/src/index.ts DELETED
@@ -1,19 +0,0 @@
1
- export { supportedExtensions } from "./constants.json"
2
- export { generateTypings } from "./generateTypings"
3
- export { processScript } from "./processScript"
4
- export { pull } from "./pull"
5
- export { push } from "./push"
6
- export { syncMacros } from "./syncMacros"
7
- export { test } from "./test"
8
- export { watch } from "./watch"
9
-
10
- // TODO `clean()` function that delete all scripts in hackmud directory #70
11
- // TODO optional argument (defaults to false) for `clean()` that makes it only remove scripts without a source file #70
12
-
13
- export interface Info {
14
- file: string
15
- users: string[]
16
- srcLength: number
17
- minLength: number
18
- error: Error | null
19
- }
package/src/modules.d.ts DELETED
@@ -1,5 +0,0 @@
1
- declare module "@babel/plugin-*" {
2
- export default {
3
- default: (...args: any[]) => any
4
- }
5
- }
@@ -1,198 +0,0 @@
1
- import babelGenerator from "@babel/generator"
2
- import { parse } from "@babel/parser"
3
- import babelPluginProposalClassProperties from "@babel/plugin-proposal-class-properties"
4
- import babelPluginProposalClassStaticBlock from "@babel/plugin-proposal-class-static-block"
5
- import babelPluginProposalDecorators from "@babel/plugin-proposal-decorators"
6
- import babelPluginProposalDoExpressions from "@babel/plugin-proposal-do-expressions"
7
- import babelPluginProposalFunctionBind from "@babel/plugin-proposal-function-bind"
8
- import babelPluginProposalFunctionSent from "@babel/plugin-proposal-function-sent"
9
- import babelPluginProposalJSONStrings from "@babel/plugin-proposal-json-strings"
10
- import babelPluginProposalLogicalAssignmentOperators from "@babel/plugin-proposal-logical-assignment-operators"
11
- import babelPluginProposalNullishCoalescingOperator from "@babel/plugin-proposal-nullish-coalescing-operator"
12
- import babelPluginProposalNumericSeparator from "@babel/plugin-proposal-numeric-separator"
13
- import babelPluginProposalObjectRestSpread from "@babel/plugin-proposal-object-rest-spread"
14
- import babelPluginProposalOptionalCatchBinding from "@babel/plugin-proposal-optional-catch-binding"
15
- import babelPluginProposalOptionalChaining from "@babel/plugin-proposal-optional-chaining"
16
- import babelPluginProposalPartialApplication from "@babel/plugin-proposal-partial-application"
17
- import babelPluginProposalPipelineOperator from "@babel/plugin-proposal-pipeline-operator"
18
- import babelPluginProposalPrivatePropertyInObject from "@babel/plugin-proposal-private-property-in-object"
19
- import babelPluginProposalRecordAndTuple from "@babel/plugin-proposal-record-and-tuple"
20
- import babelPluginProposalThrowExpressions from "@babel/plugin-proposal-throw-expressions"
21
- import babelPluginTransformExponentiationOperator from "@babel/plugin-transform-exponentiation-operator"
22
- import babelPluginTransformTypescript from "@babel/plugin-transform-typescript"
23
- import rollupPluginBabel_ from "@rollup/plugin-babel"
24
- import rollupPluginCommonJS from "@rollup/plugin-commonjs"
25
- import rollupPluginJSON from "@rollup/plugin-json"
26
- import rollupPluginNodeResolve from "@rollup/plugin-node-resolve"
27
- import { assert, countHackmudCharacters } from "@samual/lib"
28
- import { resolve as resolvePath } from "path"
29
- import { performance } from "perf_hooks"
30
- import prettier from "prettier"
31
- import { rollup } from "rollup"
32
- import { supportedExtensions as extensions } from "../constants.json"
33
- import minify from "./minify"
34
- import postprocess from "./postprocess"
35
- import preprocess from "./preprocess"
36
- import transform from "./transform"
37
-
38
- const { default: rollupPluginBabel } = rollupPluginBabel_ as any as typeof import("@rollup/plugin-babel")
39
- const { format } = prettier
40
- const { default: generate } = babelGenerator as any as typeof import("@babel/generator")
41
-
42
- export { minify } from "./minify"
43
- export { postprocess } from "./postprocess"
44
- export { preprocess } from "./preprocess"
45
- export { transform as compile } from "./transform"
46
-
47
- export type ProcessOptions = {
48
- /** whether to minify the given code */
49
- minify: boolean
50
-
51
- /** 11 a-z 0-9 characters */
52
- uniqueID: string
53
-
54
- /** the user the script will be uploaded to (or set to `true` if it is not yet known) */
55
- scriptUser: string | true
56
-
57
- /** the name of this script (or set to `true` if it is not yet known) */
58
- scriptName: string | true
59
-
60
- filePath: string
61
-
62
- /** whether to mangle function and class names (defaults to `false`) */
63
- mangleNames: boolean
64
- }
65
-
66
- /**
67
- * Minifies a given script
68
- *
69
- * @param code JavaScript or TypeScript code
70
- * @param options {@link ProcessOptions details}
71
- */
72
- export async function processScript(
73
- code: string,
74
- {
75
- minify: shouldMinify = true,
76
- uniqueID = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, "0"),
77
- scriptUser = "UNKNOWN",
78
- scriptName = "UNKNOWN",
79
- filePath,
80
- mangleNames = false
81
- }: Partial<ProcessOptions> = {}
82
- ): Promise<{
83
- srcLength: number
84
- script: string
85
- warnings: { message: string, line: number }[]
86
- timeTook: number
87
- }> {
88
- assert(uniqueID.match(/^\w{11}$/))
89
-
90
- if (filePath)
91
- filePath = resolvePath(filePath)
92
- else
93
- filePath = "script"
94
-
95
- const time = performance.now()
96
- const sourceCode = code
97
- let autocomplete
98
- let seclevel
99
- let semicolons
100
-
101
- ({ autocomplete, code, seclevel, semicolons } = preprocess(code, { uniqueID }))
102
- assert(uniqueID.match(/^\w{11}$/))
103
-
104
- const filePathResolved = filePath
105
- ? resolvePath(filePath)
106
- : "script"
107
-
108
- const bundle = await rollup({
109
- plugins: [
110
- {
111
- name: "emit script",
112
- buildStart() {
113
- this.emitFile({
114
- type: "chunk",
115
- id: filePathResolved
116
- })
117
- },
118
- load(id) {
119
- if (id == filePathResolved)
120
- return code
121
-
122
- return null
123
- },
124
- transform(code) {
125
- return preprocess(code, { uniqueID }).code
126
- }
127
- },
128
- rollupPluginBabel({
129
- babelHelpers: "bundled",
130
- plugins: [
131
- [ babelPluginTransformTypescript.default ],
132
- [ babelPluginProposalDecorators.default, { decoratorsBeforeExport: true } ],
133
- [ babelPluginProposalDoExpressions.default ],
134
- [ babelPluginProposalFunctionBind.default ],
135
- [ babelPluginProposalFunctionSent.default ],
136
- [ babelPluginProposalPartialApplication.default ],
137
- [ babelPluginProposalPipelineOperator.default, { proposal: "hack", topicToken: "%" } ],
138
- [ babelPluginProposalThrowExpressions.default ],
139
- [ babelPluginProposalRecordAndTuple.default, { syntaxType: "hash", importPolyfill: true } ],
140
- [ babelPluginProposalClassProperties.default ],
141
- [ babelPluginProposalClassStaticBlock.default ],
142
- [ babelPluginProposalPrivatePropertyInObject.default ],
143
- [ babelPluginProposalLogicalAssignmentOperators.default ],
144
- [ babelPluginProposalNumericSeparator.default ],
145
- [ babelPluginProposalNullishCoalescingOperator.default ],
146
- [ babelPluginProposalOptionalChaining.default ],
147
- [ babelPluginProposalOptionalCatchBinding.default ],
148
- [ babelPluginProposalJSONStrings.default ],
149
- [ babelPluginProposalObjectRestSpread.default ],
150
- [ babelPluginTransformExponentiationOperator.default ]
151
- ],
152
- configFile: false,
153
- extensions
154
- }),
155
- rollupPluginCommonJS(),
156
- rollupPluginNodeResolve({ extensions }),
157
- rollupPluginJSON()
158
- ]
159
- })
160
-
161
- code = (await bundle.generate({})).output[0].code
162
-
163
- const file = parse(code, { sourceType: "module" })
164
-
165
- code = generate(await transform(file, sourceCode, { uniqueID, scriptUser, scriptName, seclevel })!).code
166
-
167
- // TODO fix incorrect source length again
168
-
169
- // the typescript inserts semicolons where they weren't already so we take
170
- // all semicolons out of the count and add the number of semicolons in the
171
- // source to make things fair
172
- let srcLength = countHackmudCharacters(code.replace(/^function\s*\w+\(/, "function("))
173
- - (code.match(/;/g)?.length || 0)
174
- + semicolons
175
- // + (code.match(/SC\$[a-zA-Z_][a-zA-Z0-9_]*\$[a-zA-Z_][a-zA-Z0-9_]*\(/g)?.length ?? 0)
176
- // + (code.match(/DB\$/g)?.length ?? 0)
177
-
178
- if (shouldMinify)
179
- code = await minify(code, autocomplete, { uniqueID, mangleNames })
180
- else {
181
- code = format(code, {
182
- parser: "babel",
183
- arrowParens: "avoid",
184
- semi: false
185
- })
186
- }
187
-
188
- code = postprocess(code, seclevel, uniqueID)
189
-
190
- return {
191
- srcLength,
192
- script: code,
193
- warnings: [],
194
- timeTook: performance.now() - time
195
- }
196
- }
197
-
198
- export default processScript