hackmud-script-manager-ng 0.26.5 → 0.26.6
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/bin/hsm.d.ts +2 -0
- package/bin/hsm.js +493 -0
- package/constants.d.ts +2 -0
- package/constants.js +3 -0
- package/generateTypeDeclaration.d.ts +1 -0
- package/generateTypeDeclaration.js +70 -0
- package/index.d.ts +16 -0
- package/index.js +52 -0
- package/package.json +3 -22
- package/processScript/index.d.ts +34 -0
- package/processScript/index.js +362 -0
- package/processScript/minify.d.ts +20 -0
- package/processScript/minify.js +431 -0
- package/processScript/postprocess.d.ts +1 -0
- package/processScript/postprocess.js +21 -0
- package/processScript/preprocess.d.ts +11 -0
- package/processScript/preprocess.js +107 -0
- package/processScript/shared.d.ts +5 -0
- package/processScript/shared.js +32 -0
- package/processScript/transform.d.ts +24 -0
- package/processScript/transform.js +803 -0
- package/pull.d.ts +7 -0
- package/pull.js +11 -0
- package/push.d.ts +42 -0
- package/push.js +164 -0
- package/syncMacros.d.ts +4 -0
- package/syncMacros.js +42 -0
- package/watch.d.ts +19 -0
- package/watch.js +220 -0
- package/.envrc +0 -1
- package/.github/FUNDING.yml +0 -14
- package/.github/workflows/publish.yml +0 -27
- package/.github/workflows/test.yml +0 -18
- package/.vscode/settings.json +0 -55
- package/CONTRIBUTING.md +0 -68
- package/eslint.config.js +0 -81
- package/game-scripts-tests/array-rest-works.ts +0 -6
- package/game-scripts-tests/this_array_in_object.ts +0 -16
- package/game-scripts-tests/this_function_array.ts +0 -9
- package/game-scripts-tests/this_in_default_argument.ts +0 -35
- package/game-scripts-tests/this_in_lambda_in_object.ts +0 -26
- package/pnpm-workspace.yaml +0 -2
- package/rollup.config.js +0 -69
- package/scripts/dev.sh +0 -3
- package/scripts/emit-declarations.sh +0 -3
- package/scripts/emit-package-json.js +0 -9
- package/scripts/lint.sh +0 -5
- package/scripts/package.sh +0 -7
- package/scripts/update.sh +0 -4
- package/scripts/version-dev.js +0 -14
- package/src/bin/hsm.ts +0 -754
- package/src/constants.ts +0 -2
- package/src/env.d.ts +0 -13
- package/src/generateTypeDeclaration.ts +0 -119
- package/src/index.ts +0 -13
- package/src/processScript/__snapshots__/index.ts.snap +0 -8
- package/src/processScript/index.ts +0 -474
- package/src/processScript/minify.ts +0 -594
- package/src/processScript/postprocess.ts +0 -19
- package/src/processScript/preprocess.ts +0 -130
- package/src/processScript/shared.ts +0 -33
- package/src/processScript/transform.ts +0 -978
- package/src/pull.ts +0 -20
- package/src/push.ts +0 -200
- package/src/syncMacros.ts +0 -53
- package/src/tsconfig.json +0 -14
- package/src/watch.ts +0 -258
- package/tsconfig.json +0 -23
- package/vitest.config.ts +0 -19
package/src/constants.ts
DELETED
package/src/env.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare module "@babel/plugin-*" {
|
|
2
|
-
const value: { default: (...arguments_: any[]) => any }
|
|
3
|
-
|
|
4
|
-
export default value
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
// Taken from https://github.com/thw0rted/DefinitelyTyped/blob/542cacd0803f50d8537a94b2dfa3e3e125c837ac/types/node/perf_hooks.d.ts#L608
|
|
8
|
-
/**
|
|
9
|
-
* `performance` is a global reference for `require('perf_hooks').performance`
|
|
10
|
-
* https://nodejs.org/api/globals.html#performance
|
|
11
|
-
* @since v16.0.0
|
|
12
|
-
*/
|
|
13
|
-
declare const performance: typeof globalThis extends { onmessage: any, performance: infer T } ? T : typeof import("perf_hooks").performance
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { basename as getBaseName, resolve as resolvePath } from "path"
|
|
2
|
-
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
|
3
|
-
|
|
4
|
-
export async function generateTypeDeclaration(sourceDirectory: string, hackmudPath?: string): Promise<string> {
|
|
5
|
-
const users = new Set<string>
|
|
6
|
-
|
|
7
|
-
if (hackmudPath) {
|
|
8
|
-
for (const { stats, name } of await readDirectoryWithStats(hackmudPath)) {
|
|
9
|
-
if (stats.isFile() && name.endsWith(`.key`))
|
|
10
|
-
users.add(getBaseName(name, `.key`))
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const wildScripts: string[] = []
|
|
15
|
-
const wildAnyScripts: string[] = []
|
|
16
|
-
const allScripts: Record<string, string[]> = {}
|
|
17
|
-
const allAnyScripts: Record<string, string[]> = {}
|
|
18
|
-
|
|
19
|
-
await Promise.all((await readDirectoryWithStats(sourceDirectory)).filter(({ stats, name }) => !stats.isDirectory() || /^[a-z_][a-z\d_]{0,24}$/.test(name)).map(async ({ stats, name }) => {
|
|
20
|
-
if (stats.isFile()) {
|
|
21
|
-
if (name.endsWith(`.ts`)) {
|
|
22
|
-
if (!name.endsWith(`.d.ts`))
|
|
23
|
-
wildScripts.push(getBaseName(name, `.ts`))
|
|
24
|
-
} else if (name.endsWith(`.js`))
|
|
25
|
-
wildAnyScripts.push(getBaseName(name, `.js`))
|
|
26
|
-
} else if (stats.isDirectory()) {
|
|
27
|
-
const scripts: string[] = []
|
|
28
|
-
const anyScripts: string[] = []
|
|
29
|
-
|
|
30
|
-
allScripts[name] = scripts
|
|
31
|
-
allAnyScripts[name] = anyScripts
|
|
32
|
-
users.add(name)
|
|
33
|
-
|
|
34
|
-
for (const child of await readDirectoryWithStats(resolvePath(sourceDirectory, name))) {
|
|
35
|
-
if (child.stats.isFile()) {
|
|
36
|
-
if (child.name.endsWith(`.ts`)) {
|
|
37
|
-
if (!name.endsWith(`.d.ts`))
|
|
38
|
-
scripts.push(getBaseName(child.name, `.ts`))
|
|
39
|
-
} else if (child.name.endsWith(`.js`))
|
|
40
|
-
anyScripts.push(getBaseName(child.name, `.js`))
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}))
|
|
45
|
-
|
|
46
|
-
let o = ``
|
|
47
|
-
|
|
48
|
-
for (const script of wildScripts)
|
|
49
|
-
o += `type $${script}$ = typeof import(${JSON.stringify(resolvePath(sourceDirectory, script))}).default\n`
|
|
50
|
-
|
|
51
|
-
o += `\n`
|
|
52
|
-
|
|
53
|
-
for (const user in allScripts) {
|
|
54
|
-
const scripts = allScripts[user]!
|
|
55
|
-
|
|
56
|
-
for (const script of scripts)
|
|
57
|
-
o += `type $${user}$${script}$ = typeof import(${JSON.stringify(resolvePath(sourceDirectory, user, script))}).default\n`
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// TODO detect security level and generate apropriate code
|
|
61
|
-
|
|
62
|
-
// TODO accurate function signatures
|
|
63
|
-
// I lose the generic-ness of my functions when I wrap them
|
|
64
|
-
// regexing isn't enough and it looks like I'm going to need to parse the files in TypeScript to extract the signature
|
|
65
|
-
|
|
66
|
-
o += `
|
|
67
|
-
type ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never
|
|
68
|
-
|
|
69
|
-
type Subscript<T extends (...args: any) => any> =
|
|
70
|
-
(...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure
|
|
71
|
-
|
|
72
|
-
type WildFullsec = Record<string, () => ScriptFailure> & {
|
|
73
|
-
`
|
|
74
|
-
|
|
75
|
-
for (const script of wildScripts)
|
|
76
|
-
o += `\t${script}: Subscript<$${script}$>\n`
|
|
77
|
-
|
|
78
|
-
for (const script of wildAnyScripts)
|
|
79
|
-
o += `\t${script}: (...args: any) => any\n`
|
|
80
|
-
|
|
81
|
-
o += `}\n\ninterface PlayerFullsec {`
|
|
82
|
-
|
|
83
|
-
let lastWasMultiLine = true
|
|
84
|
-
|
|
85
|
-
for (const user of users) {
|
|
86
|
-
const scripts = allScripts[user]
|
|
87
|
-
const anyScripts = allAnyScripts[user]
|
|
88
|
-
|
|
89
|
-
if ((scripts && scripts.length) || (anyScripts && anyScripts.length)) {
|
|
90
|
-
lastWasMultiLine = true
|
|
91
|
-
o += `\n\t${user}: WildFullsec & {\n`
|
|
92
|
-
|
|
93
|
-
if (scripts) {
|
|
94
|
-
for (const script of scripts)
|
|
95
|
-
o += `\t\t${script}: Subscript<$${user}$${script}$>\n`
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (anyScripts) {
|
|
99
|
-
for (const script of anyScripts)
|
|
100
|
-
o += `\t\t${script}: (...args: any) => any\n`
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
o += `\t}`
|
|
104
|
-
} else {
|
|
105
|
-
if (lastWasMultiLine) {
|
|
106
|
-
o += `\n`
|
|
107
|
-
lastWasMultiLine = false
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
o += `\t${user}: WildFullsec`
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
o += `\n`
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
o += `}\n`
|
|
117
|
-
|
|
118
|
-
return o
|
|
119
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export { supportedExtensions } from "./constants"
|
|
2
|
-
export { generateTypeDeclaration } from "./generateTypeDeclaration"
|
|
3
|
-
export { processScript } from "./processScript"
|
|
4
|
-
export { pull } from "./pull"
|
|
5
|
-
export { push } from "./push"
|
|
6
|
-
export { syncMacros } from "./syncMacros"
|
|
7
|
-
export { watch } from "./watch"
|
|
8
|
-
|
|
9
|
-
// TODO `clean()` function that delete all scripts in hackmud directory #70
|
|
10
|
-
// TODO optional argument (defaults to false) for `clean()` that makes it keep scripts with a source file #70
|
|
11
|
-
|
|
12
|
-
export type Info =
|
|
13
|
-
{ path: string, users: string[], characterCount: number, error: Error | undefined, warnings: { message: string }[] }
|
|
@@ -1,474 +0,0 @@
|
|
|
1
|
-
/* eslint-disable jsdoc/check-param-names */
|
|
2
|
-
import type { NodePath, PluginItem } from "@babel/core"
|
|
3
|
-
import type { LVal, Program, VoidPattern, RestElement } from "@babel/types"
|
|
4
|
-
import type { LaxPartial } from "@samual/lib"
|
|
5
|
-
import { readFile, readdir as readFolder } from "fs/promises"
|
|
6
|
-
import { basename as getPathBasename, relative as getRelativePath, isAbsolute as isAbsolutePath, sep as pathSeparator, resolve as resolvePath } from "path"
|
|
7
|
-
import generate from "@babel/generator"
|
|
8
|
-
import { parse } from "@babel/parser"
|
|
9
|
-
import babelPluginProposalDecorators from "@babel/plugin-proposal-decorators"
|
|
10
|
-
import babelPluginProposalDestructuringPrivate from "@babel/plugin-proposal-destructuring-private"
|
|
11
|
-
import babelPluginTransformClassProperties from "@babel/plugin-transform-class-properties"
|
|
12
|
-
import babelPluginTransformClassStaticBlock from "@babel/plugin-transform-class-static-block"
|
|
13
|
-
import babelPluginTransformExplicitResourceManagement from "@babel/plugin-transform-explicit-resource-management"
|
|
14
|
-
import babelPluginTransformExponentiationOperator from "@babel/plugin-transform-exponentiation-operator"
|
|
15
|
-
import babelPluginTransformJsonStrings from "@babel/plugin-transform-json-strings"
|
|
16
|
-
import babelPluginTransformLogicalAssignmentOperators from "@babel/plugin-transform-logical-assignment-operators"
|
|
17
|
-
import babelPluginTransformNullishCoalescingOperator from "@babel/plugin-transform-nullish-coalescing-operator"
|
|
18
|
-
import babelPluginTransformNumericSeparator from "@babel/plugin-transform-numeric-separator"
|
|
19
|
-
import babelPluginTransformObjectRestSpread from "@babel/plugin-transform-object-rest-spread"
|
|
20
|
-
import babelPluginTransformOptionalCatchBinding from "@babel/plugin-transform-optional-catch-binding"
|
|
21
|
-
import babelPluginTransformOptionalChaining from "@babel/plugin-transform-optional-chaining"
|
|
22
|
-
import babelPluginTransformPrivatePropertyInObject from "@babel/plugin-transform-private-property-in-object"
|
|
23
|
-
import babelPluginTransformUnicodeSetsRegex from "@babel/plugin-transform-unicode-sets-regex"
|
|
24
|
-
import traverse from "@babel/traverse"
|
|
25
|
-
import t from "@babel/types"
|
|
26
|
-
import rollupPluginAlias from "@rollup/plugin-alias"
|
|
27
|
-
import { babel as rollupPluginBabel } from "@rollup/plugin-babel"
|
|
28
|
-
import rollupPluginCommonJS from "@rollup/plugin-commonjs"
|
|
29
|
-
import rollupPluginJSON from "@rollup/plugin-json"
|
|
30
|
-
import rollupPluginNodeResolve from "@rollup/plugin-node-resolve"
|
|
31
|
-
import { assert } from "@samual/lib/assert"
|
|
32
|
-
import prettier from "prettier"
|
|
33
|
-
import { rollup } from "rollup"
|
|
34
|
-
import { supportedExtensions as extensions } from "../constants"
|
|
35
|
-
import { minify } from "./minify"
|
|
36
|
-
import { postprocess } from "./postprocess"
|
|
37
|
-
import { preprocess } from "./preprocess"
|
|
38
|
-
import { getReferencePathsToGlobal, includesIllegalString, replaceUnsafeStrings } from "./shared"
|
|
39
|
-
import { transform } from "./transform"
|
|
40
|
-
|
|
41
|
-
const { format } = prettier
|
|
42
|
-
|
|
43
|
-
export { minify } from "./minify"
|
|
44
|
-
export { postprocess } from "./postprocess"
|
|
45
|
-
export { preprocess } from "./preprocess"
|
|
46
|
-
export { transform } from "./transform"
|
|
47
|
-
|
|
48
|
-
export type ProcessOptions = LaxPartial<{
|
|
49
|
-
/** whether to minify the given code */ minify: boolean
|
|
50
|
-
/** 11 a-z 0-9 characters */ uniqueId: string
|
|
51
|
-
/** the user going to be hosting this script (or set to `true` if not yet known) */ scriptUser: string | true
|
|
52
|
-
filePath: string
|
|
53
|
-
/** whether to mangle function and class names (defaults to `false`) */ mangleNames: boolean
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* when set to `true` forces use of quine cheats
|
|
57
|
-
*
|
|
58
|
-
* when set to `false` forces quine cheats not to be used
|
|
59
|
-
*
|
|
60
|
-
* when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
|
|
61
|
-
*/
|
|
62
|
-
forceQuineCheats: boolean
|
|
63
|
-
|
|
64
|
-
rootFolderPath: string
|
|
65
|
-
}> & { /** the name of this script (or set to `true` if not yet known) */ scriptName: string | true }
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Minifies a given script
|
|
69
|
-
* @param code JavaScript or TypeScript code
|
|
70
|
-
* @param options {@link ProcessOptions details}
|
|
71
|
-
*/
|
|
72
|
-
export async function processScript(code: string, {
|
|
73
|
-
minify: shouldMinify = true,
|
|
74
|
-
uniqueId = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`),
|
|
75
|
-
scriptUser,
|
|
76
|
-
scriptName,
|
|
77
|
-
filePath,
|
|
78
|
-
mangleNames = false,
|
|
79
|
-
forceQuineCheats,
|
|
80
|
-
rootFolderPath
|
|
81
|
-
}: ProcessOptions): Promise<{ script: string, warnings: { message: string }[] }> {
|
|
82
|
-
assert(/^\w{11}$/.exec(uniqueId), HERE)
|
|
83
|
-
|
|
84
|
-
const sourceCode = code
|
|
85
|
-
let autocomplete
|
|
86
|
-
let statedSeclevel
|
|
87
|
-
// TODO do seclevel detection and verification per module
|
|
88
|
-
const autocompleteMatch = /^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(code)
|
|
89
|
-
|
|
90
|
-
if (autocompleteMatch) {
|
|
91
|
-
code = `export default ${code}`;
|
|
92
|
-
({ autocomplete } = autocompleteMatch.groups!)
|
|
93
|
-
} else {
|
|
94
|
-
for (const line of code.split(`\n`)) {
|
|
95
|
-
const comment = /^\s*\/\/(?<commentContent>.+)/.exec(line)
|
|
96
|
-
|
|
97
|
-
if (!comment)
|
|
98
|
-
break
|
|
99
|
-
|
|
100
|
-
const commentContent = comment.groups!.commentContent!.trim()
|
|
101
|
-
|
|
102
|
-
if (commentContent.startsWith(`@autocomplete `))
|
|
103
|
-
autocomplete = commentContent.slice(14).trimStart()
|
|
104
|
-
else if (commentContent.startsWith(`@seclevel `)) {
|
|
105
|
-
const seclevelString = commentContent.slice(10).trimStart().toLowerCase()
|
|
106
|
-
|
|
107
|
-
switch (seclevelString) {
|
|
108
|
-
case `fullsec`:
|
|
109
|
-
case `full`:
|
|
110
|
-
case `fs`:
|
|
111
|
-
case `4s`:
|
|
112
|
-
case `f`:
|
|
113
|
-
case `4`:
|
|
114
|
-
statedSeclevel = 4
|
|
115
|
-
break
|
|
116
|
-
|
|
117
|
-
case `highsec`:
|
|
118
|
-
case `high`:
|
|
119
|
-
case `hs`:
|
|
120
|
-
case `3s`:
|
|
121
|
-
case `h`:
|
|
122
|
-
case `3`:
|
|
123
|
-
statedSeclevel = 3
|
|
124
|
-
break
|
|
125
|
-
|
|
126
|
-
case `midsec`:
|
|
127
|
-
case `mid`:
|
|
128
|
-
case `ms`:
|
|
129
|
-
case `2s`:
|
|
130
|
-
case `m`:
|
|
131
|
-
case `2`:
|
|
132
|
-
statedSeclevel = 2
|
|
133
|
-
break
|
|
134
|
-
|
|
135
|
-
case `lowsec`:
|
|
136
|
-
case `low`:
|
|
137
|
-
case `ls`:
|
|
138
|
-
case `1s`:
|
|
139
|
-
case `l`:
|
|
140
|
-
case `1`:
|
|
141
|
-
statedSeclevel = 1
|
|
142
|
-
break
|
|
143
|
-
|
|
144
|
-
case `nullsec`:
|
|
145
|
-
case `null`:
|
|
146
|
-
case `ns`:
|
|
147
|
-
case `0s`:
|
|
148
|
-
case `n`:
|
|
149
|
-
case `0`:
|
|
150
|
-
statedSeclevel = 0
|
|
151
|
-
break
|
|
152
|
-
|
|
153
|
-
default:
|
|
154
|
-
// TODO turn into warning when I get round to those
|
|
155
|
-
throw Error(`unrecognised seclevel "${seclevelString}"`)
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
assert(/^\w{11}$/.exec(uniqueId), HERE)
|
|
162
|
-
|
|
163
|
-
const plugins: PluginItem[] = [
|
|
164
|
-
[ babelPluginProposalDecorators, { decoratorsBeforeExport: true } ],
|
|
165
|
-
[ babelPluginTransformClassProperties ],
|
|
166
|
-
[ babelPluginTransformClassStaticBlock ],
|
|
167
|
-
[ babelPluginTransformPrivatePropertyInObject ],
|
|
168
|
-
[ babelPluginTransformLogicalAssignmentOperators ],
|
|
169
|
-
[ babelPluginTransformNumericSeparator ],
|
|
170
|
-
[ babelPluginTransformNullishCoalescingOperator ],
|
|
171
|
-
[ babelPluginTransformOptionalChaining ],
|
|
172
|
-
[ babelPluginTransformOptionalCatchBinding ],
|
|
173
|
-
[ babelPluginTransformJsonStrings ],
|
|
174
|
-
[ babelPluginTransformObjectRestSpread ],
|
|
175
|
-
[ babelPluginTransformExponentiationOperator ],
|
|
176
|
-
[ babelPluginTransformUnicodeSetsRegex ],
|
|
177
|
-
[ babelPluginProposalDestructuringPrivate ],
|
|
178
|
-
[ babelPluginTransformExplicitResourceManagement ]
|
|
179
|
-
]
|
|
180
|
-
|
|
181
|
-
let filePathResolved
|
|
182
|
-
|
|
183
|
-
if (filePath) {
|
|
184
|
-
filePathResolved = getRelativePath(`.`, filePath)
|
|
185
|
-
|
|
186
|
-
if (filePath.endsWith(`.ts`)) {
|
|
187
|
-
plugins.unshift([
|
|
188
|
-
(await import(`@babel/plugin-transform-typescript`)),
|
|
189
|
-
{ allowDeclareFields: true, optimizeConstEnums: true }
|
|
190
|
-
])
|
|
191
|
-
} else {
|
|
192
|
-
const [
|
|
193
|
-
babelPluginProposalDoExpressions,
|
|
194
|
-
babelPluginProposalFunctionBind,
|
|
195
|
-
babelPluginProposalFunctionSent,
|
|
196
|
-
babelPluginProposalPartialApplication,
|
|
197
|
-
babelPluginProposalPipelineOperator,
|
|
198
|
-
babelPluginProposalThrowExpressions,
|
|
199
|
-
babelPluginProposalRecordAndTuple
|
|
200
|
-
] = await Promise.all([
|
|
201
|
-
import(`@babel/plugin-proposal-do-expressions`),
|
|
202
|
-
import(`@babel/plugin-proposal-function-bind`),
|
|
203
|
-
import(`@babel/plugin-proposal-function-sent`),
|
|
204
|
-
import(`@babel/plugin-proposal-partial-application`),
|
|
205
|
-
import(`@babel/plugin-proposal-pipeline-operator`),
|
|
206
|
-
import(`@babel/plugin-proposal-throw-expressions`),
|
|
207
|
-
import(`@babel/plugin-proposal-record-and-tuple`)
|
|
208
|
-
])
|
|
209
|
-
|
|
210
|
-
plugins.push(
|
|
211
|
-
[ babelPluginProposalDoExpressions ],
|
|
212
|
-
[ babelPluginProposalFunctionBind ],
|
|
213
|
-
[ babelPluginProposalFunctionSent ],
|
|
214
|
-
[ babelPluginProposalPartialApplication ],
|
|
215
|
-
[ babelPluginProposalPipelineOperator, { proposal: `hack`, topicToken: `%` } ],
|
|
216
|
-
[ babelPluginProposalThrowExpressions ],
|
|
217
|
-
[ babelPluginProposalRecordAndTuple, { syntaxType: `hash`, importPolyfill: true } ]
|
|
218
|
-
)
|
|
219
|
-
}
|
|
220
|
-
} else {
|
|
221
|
-
filePathResolved = `${uniqueId}.ts`
|
|
222
|
-
|
|
223
|
-
const [
|
|
224
|
-
babelPluginTransformTypescript,
|
|
225
|
-
babelPluginProposalDoExpressions,
|
|
226
|
-
babelPluginProposalFunctionBind,
|
|
227
|
-
babelPluginProposalFunctionSent,
|
|
228
|
-
babelPluginProposalPartialApplication,
|
|
229
|
-
babelPluginProposalPipelineOperator,
|
|
230
|
-
babelPluginProposalThrowExpressions,
|
|
231
|
-
babelPluginProposalRecordAndTuple
|
|
232
|
-
] = await Promise.all([
|
|
233
|
-
import(`@babel/plugin-transform-typescript`),
|
|
234
|
-
import(`@babel/plugin-proposal-do-expressions`),
|
|
235
|
-
import(`@babel/plugin-proposal-function-bind`),
|
|
236
|
-
import(`@babel/plugin-proposal-function-sent`),
|
|
237
|
-
import(`@babel/plugin-proposal-partial-application`),
|
|
238
|
-
import(`@babel/plugin-proposal-pipeline-operator`),
|
|
239
|
-
import(`@babel/plugin-proposal-throw-expressions`),
|
|
240
|
-
import(`@babel/plugin-proposal-record-and-tuple`)
|
|
241
|
-
])
|
|
242
|
-
|
|
243
|
-
plugins.push(
|
|
244
|
-
[ babelPluginTransformTypescript, { allowDeclareFields: true, optimizeConstEnums: true } ],
|
|
245
|
-
[ babelPluginProposalDoExpressions ],
|
|
246
|
-
[ babelPluginProposalFunctionBind ],
|
|
247
|
-
[ babelPluginProposalFunctionSent ],
|
|
248
|
-
[ babelPluginProposalPartialApplication ],
|
|
249
|
-
[ babelPluginProposalPipelineOperator, { proposal: `hack`, topicToken: `%` } ],
|
|
250
|
-
[ babelPluginProposalThrowExpressions ],
|
|
251
|
-
[ babelPluginProposalRecordAndTuple, { syntaxType: `hash`, importPolyfill: true } ]
|
|
252
|
-
)
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const bundle = await rollup({
|
|
256
|
-
input: filePathResolved,
|
|
257
|
-
plugins: [
|
|
258
|
-
rollupPluginJSON({ preferConst: true }),
|
|
259
|
-
{
|
|
260
|
-
name: `hackmud-script-manager`,
|
|
261
|
-
resolveId(source) {
|
|
262
|
-
if (source == filePathResolved)
|
|
263
|
-
return filePathResolved
|
|
264
|
-
},
|
|
265
|
-
async load(id) {
|
|
266
|
-
if (id == filePathResolved)
|
|
267
|
-
return (await preprocess(code, { uniqueId })).code
|
|
268
|
-
},
|
|
269
|
-
async transform(code, id) {
|
|
270
|
-
if (id == filePathResolved)
|
|
271
|
-
return
|
|
272
|
-
|
|
273
|
-
if (isAbsolutePath(id) && !id.includes(`${pathSeparator}node_modules${pathSeparator}`))
|
|
274
|
-
return (await preprocess(code, { uniqueId })).code
|
|
275
|
-
|
|
276
|
-
let program!: NodePath<Program>
|
|
277
|
-
|
|
278
|
-
traverse(parse(code, { sourceType: `module` }), {
|
|
279
|
-
Program(path) {
|
|
280
|
-
program = path
|
|
281
|
-
path.skip()
|
|
282
|
-
}
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
for (const referencePath of getReferencePathsToGlobal(`JSON`, program)) {
|
|
286
|
-
if (referencePath.parentPath.node.type == `MemberExpression` &&
|
|
287
|
-
referencePath.parentPath.node.property.type == `Identifier`
|
|
288
|
-
) {
|
|
289
|
-
if (referencePath.parentPath.node.property.name == `parse`)
|
|
290
|
-
referencePath.parentPath.node.property.name = `oparse`
|
|
291
|
-
else if (referencePath.parentPath.node.property.name == `stringify`)
|
|
292
|
-
referencePath.parentPath.node.property.name = `ostringify`
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return generate(program.node).code
|
|
297
|
-
}
|
|
298
|
-
},
|
|
299
|
-
rollupPluginBabel({
|
|
300
|
-
babelHelpers: `bundled`,
|
|
301
|
-
plugins,
|
|
302
|
-
configFile: false,
|
|
303
|
-
extensions
|
|
304
|
-
}),
|
|
305
|
-
rollupPluginCommonJS(),
|
|
306
|
-
rollupPluginNodeResolve({ extensions }),
|
|
307
|
-
!!rootFolderPath && rollupPluginAlias({ entries: [ { find: /^\//, replacement: `${rootFolderPath}/` } ] })
|
|
308
|
-
],
|
|
309
|
-
treeshake: { moduleSideEffects: false }
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
const seclevelNames = [ `NULLSEC`, `LOWSEC`, `MIDSEC`, `HIGHSEC`, `FULLSEC` ]
|
|
313
|
-
|
|
314
|
-
code = (await bundle.generate({})).output[0].code
|
|
315
|
-
|
|
316
|
-
const { file, seclevel, warnings } =
|
|
317
|
-
transform(parse(code, { sourceType: `module` }), sourceCode, { uniqueId, scriptUser, scriptName })
|
|
318
|
-
|
|
319
|
-
if (statedSeclevel != undefined && seclevel < statedSeclevel) {
|
|
320
|
-
// TODO replace with a warning and build script anyway
|
|
321
|
-
throw Error(`detected seclevel ${seclevelNames[seclevel]} is lower than stated seclevel ${
|
|
322
|
-
seclevelNames[statedSeclevel]
|
|
323
|
-
}`)
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
code = generate(file).code
|
|
327
|
-
|
|
328
|
-
if (shouldMinify)
|
|
329
|
-
// code = await minify(file, { uniqueId, mangleNames, forceQuineCheats, autocomplete })
|
|
330
|
-
code = await minify(file, { uniqueId, mangleNames, forceQuineCheats })
|
|
331
|
-
else {
|
|
332
|
-
traverse(file, {
|
|
333
|
-
MemberExpression({ node: memberExpression }) {
|
|
334
|
-
if (memberExpression.computed)
|
|
335
|
-
return
|
|
336
|
-
|
|
337
|
-
assert(memberExpression.property.type == `Identifier`, HERE)
|
|
338
|
-
|
|
339
|
-
if (memberExpression.property.name == `prototype`) {
|
|
340
|
-
memberExpression.computed = true
|
|
341
|
-
memberExpression.property = t.stringLiteral(`prototype`)
|
|
342
|
-
} else if (memberExpression.property.name == `__proto__`) {
|
|
343
|
-
memberExpression.computed = true
|
|
344
|
-
memberExpression.property = t.stringLiteral(`__proto__`)
|
|
345
|
-
} else if (includesIllegalString(memberExpression.property.name)) {
|
|
346
|
-
memberExpression.computed = true
|
|
347
|
-
|
|
348
|
-
memberExpression.property = t.stringLiteral(
|
|
349
|
-
replaceUnsafeStrings(uniqueId, memberExpression.property.name)
|
|
350
|
-
)
|
|
351
|
-
}
|
|
352
|
-
},
|
|
353
|
-
VariableDeclarator(path) {
|
|
354
|
-
const renameVariables = (lValue: LVal | VoidPattern | RestElement) => {
|
|
355
|
-
switch (lValue.type) {
|
|
356
|
-
case `Identifier`:
|
|
357
|
-
if (includesIllegalString(lValue.name)) {
|
|
358
|
-
path.scope.rename(
|
|
359
|
-
lValue.name,
|
|
360
|
-
`$${Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`)}`
|
|
361
|
-
)
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
break
|
|
365
|
-
|
|
366
|
-
case `ObjectPattern`:
|
|
367
|
-
for (const property of lValue.properties) {
|
|
368
|
-
assert(property.type == `ObjectProperty`, HERE)
|
|
369
|
-
renameVariables(property.value as LVal)
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
break
|
|
373
|
-
|
|
374
|
-
case `ArrayPattern`:
|
|
375
|
-
for (const element of lValue.elements) {
|
|
376
|
-
if (element)
|
|
377
|
-
renameVariables(element as LVal)
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
break
|
|
381
|
-
case `RestElement`:
|
|
382
|
-
renameVariables(lValue.argument as LVal)
|
|
383
|
-
break
|
|
384
|
-
|
|
385
|
-
default:
|
|
386
|
-
throw Error(`unknown lValue type "${lValue.type}"`)
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
renameVariables(path.node.id as LVal)
|
|
391
|
-
},
|
|
392
|
-
ObjectProperty({ node: objectProperty }) {
|
|
393
|
-
if (objectProperty.key.type == `Identifier` && includesIllegalString(objectProperty.key.name)) {
|
|
394
|
-
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueId, objectProperty.key.name))
|
|
395
|
-
objectProperty.shorthand = false
|
|
396
|
-
}
|
|
397
|
-
},
|
|
398
|
-
StringLiteral({ node }) {
|
|
399
|
-
node.value = replaceUnsafeStrings(uniqueId, node.value)
|
|
400
|
-
},
|
|
401
|
-
TemplateLiteral({ node }) {
|
|
402
|
-
for (const templateElement of node.quasis) {
|
|
403
|
-
if (templateElement.value.cooked) {
|
|
404
|
-
templateElement.value.cooked = replaceUnsafeStrings(uniqueId, templateElement.value.cooked)
|
|
405
|
-
|
|
406
|
-
templateElement.value.raw = templateElement.value.cooked
|
|
407
|
-
.replaceAll(`\\`, `\\\\`)
|
|
408
|
-
.replaceAll(`\``, `\\\``)
|
|
409
|
-
.replaceAll(`\${`, `$\\{`)
|
|
410
|
-
} else
|
|
411
|
-
templateElement.value.raw = replaceUnsafeStrings(uniqueId, templateElement.value.raw)
|
|
412
|
-
}
|
|
413
|
-
},
|
|
414
|
-
RegExpLiteral(path) {
|
|
415
|
-
path.node.pattern = replaceUnsafeStrings(uniqueId, path.node.pattern)
|
|
416
|
-
delete path.node.extra
|
|
417
|
-
}
|
|
418
|
-
})
|
|
419
|
-
|
|
420
|
-
// we can't have comments because they may contain illegal strings
|
|
421
|
-
code = await format(
|
|
422
|
-
generate(file, { comments: false }).code,
|
|
423
|
-
{ parser: `babel`, arrowParens: `avoid`, semi: false, trailingComma: `none` }
|
|
424
|
-
)
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
code = postprocess(code, uniqueId, autocomplete)
|
|
428
|
-
|
|
429
|
-
if (includesIllegalString(code)) {
|
|
430
|
-
throw Error(
|
|
431
|
-
`you found a weird edge case where I wasn't able to replace illegal strings like "SC$", please report thx`
|
|
432
|
-
)
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return { script: code, warnings }
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/* eslint-disable no-console, no-eval */
|
|
439
|
-
if (import.meta.vitest) {
|
|
440
|
-
const DEBUG_LOG_PROCESSED_SCRIPTS = false
|
|
441
|
-
const TESTS_FOLDER = `game-scripts-tests`
|
|
442
|
-
const { test, expect } = import.meta.vitest
|
|
443
|
-
|
|
444
|
-
const testFiles = await Promise.all(
|
|
445
|
-
(await readFolder(TESTS_FOLDER, { withFileTypes: true }))
|
|
446
|
-
.filter(dirent => dirent.isFile())
|
|
447
|
-
.map(async dirent => {
|
|
448
|
-
const filePath = getRelativePath(`.`, resolvePath(TESTS_FOLDER, dirent.name))
|
|
449
|
-
const source = await readFile(filePath, `utf8`)
|
|
450
|
-
|
|
451
|
-
const { script, warnings } =
|
|
452
|
-
await processScript(source, { scriptName: getPathBasename(dirent.name), filePath, minify: false })
|
|
453
|
-
|
|
454
|
-
return { filePath, script, warnings }
|
|
455
|
-
})
|
|
456
|
-
)
|
|
457
|
-
|
|
458
|
-
for (const { filePath, script, warnings } of testFiles) {
|
|
459
|
-
test(filePath, () => {
|
|
460
|
-
if (DEBUG_LOG_PROCESSED_SCRIPTS)
|
|
461
|
-
console.debug(`${filePath} processed script:\n${script}`)
|
|
462
|
-
|
|
463
|
-
expect(warnings.length).toBe(0);
|
|
464
|
-
(0, eval)(`(${script})`)(import.meta.vitest)
|
|
465
|
-
})
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
test(`programmatic api works`, async () => {
|
|
469
|
-
const source = `function () { #fs.scripts.trust() }`
|
|
470
|
-
const processedScript = await processScript(source, { scriptName: true })
|
|
471
|
-
|
|
472
|
-
expect(processedScript).toMatchSnapshot()
|
|
473
|
-
})
|
|
474
|
-
}
|