hackmud-script-manager 0.13.0-c14e977 → 0.13.0-c461329

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. package/.gitattributes +1 -0
  2. package/.github/workflows/codeql-analysis.yml +39 -0
  3. package/.github/workflows/publish.yml +42 -0
  4. package/.vscode/settings.json +6 -0
  5. package/babel.config.json +6 -0
  6. package/package.json +17 -23
  7. package/rollup.config.js +110 -0
  8. package/scripts/build-package-json.js +36 -0
  9. package/scripts/jsconfig.json +5 -0
  10. package/scripts/version-dev.js +25 -0
  11. package/src/bin/hsm.ts +505 -0
  12. package/src/constants.json +3 -0
  13. package/src/generateTypings.ts +116 -0
  14. package/src/index.ts +19 -0
  15. package/src/modules.d.ts +5 -0
  16. package/src/processScript/index.ts +198 -0
  17. package/src/processScript/minify.ts +529 -0
  18. package/src/processScript/postprocess.ts +38 -0
  19. package/src/processScript/preprocess.ts +146 -0
  20. package/src/processScript/transform.ts +760 -0
  21. package/src/pull.ts +16 -0
  22. package/src/push.ts +314 -0
  23. package/src/syncMacros.ts +52 -0
  24. package/src/test.ts +59 -0
  25. package/src/tsconfig.json +20 -0
  26. package/src/watch.ts +156 -0
  27. package/tsconfig.json +12 -0
  28. package/assert-1b7dada8.js +0 -1
  29. package/bin/hsm.d.ts +0 -2
  30. package/bin/hsm.js +0 -2
  31. package/generateTypings.d.ts +0 -2
  32. package/generateTypings.js +0 -1
  33. package/index.d.ts +0 -15
  34. package/index.js +0 -1
  35. package/processScript/compile.d.ts +0 -17
  36. package/processScript/compile.js +0 -1
  37. package/processScript/index.d.ts +0 -30
  38. package/processScript/index.js +0 -1
  39. package/processScript/minify.d.ts +0 -7
  40. package/processScript/minify.js +0 -1
  41. package/processScript/postProcess.d.ts +0 -2
  42. package/processScript/postProcess.js +0 -1
  43. package/processScript/preProcess.d.ts +0 -15
  44. package/processScript/preProcess.js +0 -1
  45. package/pull.d.ts +0 -9
  46. package/pull.js +0 -1
  47. package/push.d.ts +0 -26
  48. package/push.js +0 -1
  49. package/spliceString-2c6f214f.js +0 -1
  50. package/syncMacros.d.ts +0 -5
  51. package/syncMacros.js +0 -1
  52. package/test.d.ts +0 -6
  53. package/test.js +0 -1
  54. package/watch.d.ts +0 -14
  55. package/watch.js +0 -1
@@ -0,0 +1,198 @@
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