hackmud-script-manager-ng 0.0.0-stage → 0.26.5
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/.envrc +1 -0
- package/.github/FUNDING.yml +14 -0
- package/.github/workflows/publish.yml +27 -0
- package/.github/workflows/test.yml +18 -0
- package/.vscode/settings.json +55 -0
- package/CONTRIBUTING.md +68 -0
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/env.d.ts +1305 -0
- package/eslint.config.js +81 -0
- package/game-scripts-tests/array-rest-works.ts +6 -0
- package/game-scripts-tests/this_array_in_object.ts +16 -0
- package/game-scripts-tests/this_function_array.ts +9 -0
- package/game-scripts-tests/this_in_default_argument.ts +35 -0
- package/game-scripts-tests/this_in_lambda_in_object.ts +26 -0
- package/package.json +113 -3
- package/pnpm-workspace.yaml +2 -0
- package/rollup.config.js +69 -0
- package/scripts/dev.sh +3 -0
- package/scripts/emit-declarations.sh +3 -0
- package/scripts/emit-package-json.js +9 -0
- package/scripts/lint.sh +5 -0
- package/scripts/package.sh +7 -0
- package/scripts/update.sh +4 -0
- package/scripts/version-dev.js +14 -0
- package/src/bin/hsm.ts +754 -0
- package/src/constants.ts +2 -0
- package/src/env.d.ts +13 -0
- package/src/generateTypeDeclaration.ts +119 -0
- package/src/index.ts +13 -0
- package/src/processScript/__snapshots__/index.ts.snap +8 -0
- package/src/processScript/index.ts +474 -0
- package/src/processScript/minify.ts +594 -0
- package/src/processScript/postprocess.ts +19 -0
- package/src/processScript/preprocess.ts +130 -0
- package/src/processScript/shared.ts +33 -0
- package/src/processScript/transform.ts +978 -0
- package/src/pull.ts +20 -0
- package/src/push.ts +200 -0
- package/src/syncMacros.ts +53 -0
- package/src/tsconfig.json +14 -0
- package/src/watch.ts +258 -0
- package/tsconfig.json +23 -0
- package/vitest.config.ts +19 -0
package/src/pull.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { resolve as resolvePath } from "path"
|
|
2
|
+
import { copyFilePersistent } from "@samual/lib/copyFilePersistent"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Copies script from hackmud to local source folder.
|
|
6
|
+
* @param sourceFolderPath path to folder containing source files
|
|
7
|
+
* @param hackmudPath path to hackmud directory
|
|
8
|
+
* @param script to pull in `user.name` format
|
|
9
|
+
*/
|
|
10
|
+
export async function pull(sourceFolderPath: string, hackmudPath: string, script: string): Promise<void> {
|
|
11
|
+
const [ user, name ] = script.split(`.`)
|
|
12
|
+
|
|
13
|
+
if (!user || !name)
|
|
14
|
+
throw new Error(`\`script\` argument must be in "user.name" format`)
|
|
15
|
+
|
|
16
|
+
await copyFilePersistent(
|
|
17
|
+
resolvePath(hackmudPath, user, `scripts`, `${name}.js`),
|
|
18
|
+
resolvePath(sourceFolderPath, user, `${name}.js`)
|
|
19
|
+
)
|
|
20
|
+
}
|
package/src/push.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/check-param-names */
|
|
2
|
+
import type { LaxPartial } from "@samual/lib"
|
|
3
|
+
import type { Info } from "."
|
|
4
|
+
import { readFile } from "fs/promises"
|
|
5
|
+
import { basename as getBaseName, resolve as resolvePath } from "path"
|
|
6
|
+
import { assert, ensure } from "@samual/lib/assert"
|
|
7
|
+
import { AutoMap } from "@samual/lib/AutoMap"
|
|
8
|
+
import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
|
|
9
|
+
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
|
10
|
+
import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
|
|
11
|
+
import { processScript } from "./processScript"
|
|
12
|
+
|
|
13
|
+
export type PushOptions = LaxPartial<{
|
|
14
|
+
/** whether to do the minify step (defaults to `true`) */ minify: boolean
|
|
15
|
+
/** whether to mangle function and class names (defaults to `false`) */ mangleNames: boolean
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* array of scripts in the format `foo.bar`
|
|
19
|
+
*
|
|
20
|
+
* also accepts wild card (`*`) e.g. `*.bar` or `foo.*`
|
|
21
|
+
*
|
|
22
|
+
* pushes everything by default (`*.*`)
|
|
23
|
+
*/
|
|
24
|
+
scripts: string[]
|
|
25
|
+
|
|
26
|
+
/** callback called on script push */ onPush: (info: Info) => void
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* when set to `true` forces use of quine cheats
|
|
30
|
+
*
|
|
31
|
+
* when set to `false` forces quine cheats not to be used
|
|
32
|
+
*
|
|
33
|
+
* when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
|
|
34
|
+
*/
|
|
35
|
+
forceQuineCheats: boolean
|
|
36
|
+
|
|
37
|
+
rootFolderPath: string
|
|
38
|
+
}>
|
|
39
|
+
|
|
40
|
+
export class MissingSourceFolderError extends Error {}
|
|
41
|
+
|
|
42
|
+
Object.defineProperty(MissingSourceFolderError.prototype, `name`, { value: `MissingSourceFolderError` })
|
|
43
|
+
|
|
44
|
+
export class MissingHackmudFolderError extends Error {}
|
|
45
|
+
|
|
46
|
+
Object.defineProperty(MissingHackmudFolderError.prototype, `name`, { value: `MissingHackmudFolderError` })
|
|
47
|
+
|
|
48
|
+
export class NoUsersError extends Error {}
|
|
49
|
+
|
|
50
|
+
Object.defineProperty(NoUsersError.prototype, `name`, { value: `NoUsersError` })
|
|
51
|
+
|
|
52
|
+
export class NoScriptsError extends Error {}
|
|
53
|
+
|
|
54
|
+
Object.defineProperty(NoScriptsError.prototype, `name`, { value: `NoScriptsError` })
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Push scripts from a source directory to the hackmud directory.
|
|
58
|
+
*
|
|
59
|
+
* Pushes files directly in the source folder to all users
|
|
60
|
+
* @param sourcePath directory containing source code
|
|
61
|
+
* @param hackmudPath directory created by hackmud containing user data including scripts
|
|
62
|
+
* @param options {@link PushOptions details}
|
|
63
|
+
* @returns array of info on pushed scripts
|
|
64
|
+
*/
|
|
65
|
+
export async function push(
|
|
66
|
+
sourcePath: string,
|
|
67
|
+
hackmudPath: string,
|
|
68
|
+
{ scripts = [ `*.*` ], onPush = () => {}, minify = true, mangleNames = false, forceQuineCheats, rootFolderPath }: PushOptions = {}
|
|
69
|
+
): Promise<MissingSourceFolderError | MissingHackmudFolderError | NoUsersError | NoScriptsError | Info[]> {
|
|
70
|
+
const [ sourceFolder, hackmudFolder ] = await Promise.all([
|
|
71
|
+
readDirectoryWithStats(sourcePath).catch(error => {
|
|
72
|
+
if (error && (error as NodeJS.ErrnoException).code == `ENOENT`)
|
|
73
|
+
return new MissingSourceFolderError(`There is no folder at ${sourcePath}`)
|
|
74
|
+
|
|
75
|
+
throw error
|
|
76
|
+
}),
|
|
77
|
+
readDirectoryWithStats(hackmudPath).catch(error => {
|
|
78
|
+
if (error && (error as NodeJS.ErrnoException).code == `ENOENT`)
|
|
79
|
+
return new MissingHackmudFolderError(`There is no folder at ${hackmudPath}`)
|
|
80
|
+
|
|
81
|
+
throw error
|
|
82
|
+
})
|
|
83
|
+
])
|
|
84
|
+
|
|
85
|
+
if (sourceFolder instanceof Error)
|
|
86
|
+
return sourceFolder
|
|
87
|
+
|
|
88
|
+
if (hackmudFolder instanceof Error)
|
|
89
|
+
return hackmudFolder
|
|
90
|
+
|
|
91
|
+
const sourceFolderFolders = sourceFolder.filter(({ name, stats }) => stats.isDirectory() && /^[a-z_][a-z\d_]{0,24}$/.test(name))
|
|
92
|
+
|
|
93
|
+
const allUsers = new Set([
|
|
94
|
+
...scripts.map(scriptName => ensure(scriptName.split(`.`)[0], HERE)).filter(name => name != `*`),
|
|
95
|
+
...sourceFolderFolders.map(({ name }) => name),
|
|
96
|
+
...hackmudFolder.filter(({ stats }) => stats.isDirectory()).map(({ name }) => name),
|
|
97
|
+
...hackmudFolder.filter(({ stats, name }) => stats.isFile() && name.endsWith(`.key`))
|
|
98
|
+
.map(({ name }) => name.slice(0, -4))
|
|
99
|
+
])
|
|
100
|
+
|
|
101
|
+
if (!allUsers.size) {
|
|
102
|
+
return new NoUsersError(
|
|
103
|
+
`Could not find any users. Either provide the names of your users or log into a user in hackmud`
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const usersToScriptsToPush =
|
|
108
|
+
new AutoMap((_user: string) => new Map<string, /* script path */ string>)
|
|
109
|
+
|
|
110
|
+
// const usersToScriptNames = new Cache((_user: string) => new Set)
|
|
111
|
+
// const pushEverything_ = scripts.includes(`*.*`)
|
|
112
|
+
const scriptNamesToUsers = new AutoMap((_scriptName: string) => new Set<string>)
|
|
113
|
+
|
|
114
|
+
for (const script of scripts) {
|
|
115
|
+
const [ user, scriptName ] = script.split(`.`)
|
|
116
|
+
|
|
117
|
+
assert(user, HERE)
|
|
118
|
+
assert(scriptName, HERE)
|
|
119
|
+
// usersToScriptNames.get(user).add(scriptName)
|
|
120
|
+
|
|
121
|
+
if (user == `*`)
|
|
122
|
+
scriptNamesToUsers.set(scriptName, allUsers)
|
|
123
|
+
else
|
|
124
|
+
scriptNamesToUsers.get(scriptName).add(user)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const sourceFolderFiles = sourceFolder.filter(({ stats }) => stats.isFile())
|
|
128
|
+
const wildScriptUsers_ = scriptNamesToUsers.get(`*`)
|
|
129
|
+
|
|
130
|
+
scriptNamesToUsers.delete(`*`)
|
|
131
|
+
|
|
132
|
+
for (const { name, path } of [
|
|
133
|
+
...sourceFolderFiles.filter(({ name }) => name.endsWith(`.js`)),
|
|
134
|
+
...sourceFolderFiles.filter(({ name }) => name.endsWith(`.ts`))
|
|
135
|
+
]) {
|
|
136
|
+
const scriptName = name.slice(0, -3)
|
|
137
|
+
|
|
138
|
+
for (const user of [ ...wildScriptUsers_!, ...scriptNamesToUsers.get(scriptName) ])
|
|
139
|
+
usersToScriptsToPush.get(user).set(scriptName, path)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
await Promise.all(sourceFolderFolders.map(async ({ name: user, path }) => {
|
|
143
|
+
const files = (await readDirectoryWithStats(path)).filter(({ stats }) => stats.isFile())
|
|
144
|
+
|
|
145
|
+
const scriptFiles =
|
|
146
|
+
[ ...files.filter(({ name }) => name.endsWith(`.js`)), ...files.filter(({ name }) => name.endsWith(`.ts`)) ]
|
|
147
|
+
|
|
148
|
+
for (const { name, path } of scriptFiles) {
|
|
149
|
+
const scriptName = name.slice(0, -3)
|
|
150
|
+
|
|
151
|
+
if ([ ...wildScriptUsers_!, ...scriptNamesToUsers.get(scriptName) ].includes(user))
|
|
152
|
+
usersToScriptsToPush.get(user).set(scriptName, path)
|
|
153
|
+
}
|
|
154
|
+
}))
|
|
155
|
+
|
|
156
|
+
for (const [ scriptName, users ] of scriptNamesToUsers) {
|
|
157
|
+
for (const user of users) {
|
|
158
|
+
if (!usersToScriptsToPush.get(user).has(scriptName))
|
|
159
|
+
return new NoScriptsError(`Could not find script ${user}.${scriptName} to push`)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const pathsToUsers = new AutoMap((_path: string) => new Set<string>)
|
|
164
|
+
|
|
165
|
+
for (const [ user, scriptsToPush ] of usersToScriptsToPush) {
|
|
166
|
+
for (const path of scriptsToPush.values())
|
|
167
|
+
pathsToUsers.get(path).add(user)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const allInfo: Info[] = []
|
|
171
|
+
|
|
172
|
+
await Promise.all([ ...pathsToUsers ].map(async ([ path, [ ...users ] ]) => {
|
|
173
|
+
const scriptName = getBaseName(path.slice(0, -3))
|
|
174
|
+
const uniqueId = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`)
|
|
175
|
+
|
|
176
|
+
const { script: minifiedCode, warnings } = await processScript(await readFile(path, { encoding: `utf8` }), {
|
|
177
|
+
minify,
|
|
178
|
+
scriptUser: true,
|
|
179
|
+
scriptName,
|
|
180
|
+
uniqueId,
|
|
181
|
+
filePath: path,
|
|
182
|
+
mangleNames,
|
|
183
|
+
forceQuineCheats,
|
|
184
|
+
rootFolderPath
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
const info: Info = { path, users, characterCount: countHackmudCharacters(minifiedCode), error: undefined, warnings }
|
|
188
|
+
|
|
189
|
+
await Promise.all(users.map(user => writeFilePersistent(
|
|
190
|
+
resolvePath(hackmudPath, user, `scripts/${scriptName}.js`),
|
|
191
|
+
minifiedCode.replace(new RegExp(`\\$${uniqueId}\\$SCRIPT_USER\\$`, `g`), user)
|
|
192
|
+
.replace(new RegExp(`\\$${uniqueId}\\$FULL_SCRIPT_NAME\\$`, `g`), `${user}.${scriptName}`)
|
|
193
|
+
)))
|
|
194
|
+
|
|
195
|
+
allInfo.push(info)
|
|
196
|
+
onPush(info)
|
|
197
|
+
}))
|
|
198
|
+
|
|
199
|
+
return allInfo
|
|
200
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { stat as getFileStatus, readFile, writeFile } from "fs/promises"
|
|
2
|
+
import { basename as getBaseName, extname as getFileExtension, resolve as resolvePath } from "path"
|
|
3
|
+
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
|
4
|
+
|
|
5
|
+
export async function syncMacros(hackmudPath: string): Promise<{ macrosSynced: number, usersSynced: number }> {
|
|
6
|
+
const files = await readDirectoryWithStats(hackmudPath)
|
|
7
|
+
const macros = new Map<string, { macro: string, date: Date }>
|
|
8
|
+
const users: string[] = []
|
|
9
|
+
|
|
10
|
+
await Promise.all(files.map(async file => {
|
|
11
|
+
if (!file.stats.isFile())
|
|
12
|
+
return
|
|
13
|
+
|
|
14
|
+
switch (getFileExtension(file.name)) {
|
|
15
|
+
case `.macros`: {
|
|
16
|
+
const [ lines, date ] = await Promise.all([
|
|
17
|
+
readFile(resolvePath(hackmudPath, file.name), { encoding: `utf8` }).then(file => file.split(/\r?\n/)),
|
|
18
|
+
getFileStatus(resolvePath(hackmudPath, file.name)).then(({ mtime }) => mtime)
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
for (let index = 0; (index * 2) + 1 < lines.length; index++) {
|
|
22
|
+
const macroName = lines[index * 2]!
|
|
23
|
+
const currentMacro = macros.get(macroName)
|
|
24
|
+
|
|
25
|
+
if (!currentMacro || date > currentMacro.date)
|
|
26
|
+
macros.set(macroName, { date, macro: lines[(index * 2) + 1]! })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
break
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
case `.key`:
|
|
33
|
+
users.push(getBaseName(file.name, `.key`))
|
|
34
|
+
break
|
|
35
|
+
}
|
|
36
|
+
}))
|
|
37
|
+
|
|
38
|
+
let macroFile = ``
|
|
39
|
+
let macrosSynced = 0
|
|
40
|
+
|
|
41
|
+
for (const [ name, { macro } ] of
|
|
42
|
+
[ ...macros ].sort(([ a ], [ b ]) => (a as any > b as any) - (a as any < b as any))
|
|
43
|
+
) {
|
|
44
|
+
if (macro[0] == macro[0]!.toLowerCase()) {
|
|
45
|
+
macroFile += `${name}\n${macro}\n`
|
|
46
|
+
macrosSynced++
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await Promise.all(users.map(async user => writeFile(resolvePath(hackmudPath, `${user}.macros`), macroFile)))
|
|
51
|
+
|
|
52
|
+
return { macrosSynced, usersSynced: users.length }
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "..",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": ".",
|
|
5
|
+
"module": "es2022",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"types": [ "node", "@total-typescript/ts-reset", "babel-plugin-here/env", "vitest/importMeta" ],
|
|
8
|
+
"checkJs": false,
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"outDir": "../dist",
|
|
11
|
+
"isolatedDeclarations": true
|
|
12
|
+
},
|
|
13
|
+
"include": [ "**/*" ]
|
|
14
|
+
}
|
package/src/watch.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/check-param-names */
|
|
2
|
+
import type { LaxPartial } from "@samual/lib"
|
|
3
|
+
import type { PushOptions } from "./push"
|
|
4
|
+
import { stat as getFileStats, readFile, writeFile } from "fs/promises"
|
|
5
|
+
import { extname as getFileExtension, basename as getPathBaseName, resolve as resolvePath } from "path"
|
|
6
|
+
import { assert } from "@samual/lib/assert"
|
|
7
|
+
import { AutoMap } from "@samual/lib/AutoMap"
|
|
8
|
+
import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
|
|
9
|
+
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
|
10
|
+
import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
|
|
11
|
+
import { watch as watchDirectory } from "chokidar"
|
|
12
|
+
import { supportedExtensions } from "./constants"
|
|
13
|
+
import { generateTypeDeclaration } from "./generateTypeDeclaration"
|
|
14
|
+
import { processScript } from "./processScript"
|
|
15
|
+
|
|
16
|
+
export type WatchOptions = PushOptions & LaxPartial<{
|
|
17
|
+
/**
|
|
18
|
+
* if provided, will write typescript type declarations for all the scripts on every change detected
|
|
19
|
+
*
|
|
20
|
+
* writing the type declarations enables interscript type checking and autocompletetes for the args
|
|
21
|
+
*/
|
|
22
|
+
typeDeclarationPath: string
|
|
23
|
+
|
|
24
|
+
onReady: () => void
|
|
25
|
+
rootFolderPath: string
|
|
26
|
+
}>
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Watches target file or folder for updates and builds and pushes updated file.
|
|
30
|
+
* @param sourceDirectory path to folder containing source files
|
|
31
|
+
* @param hackmudDirectory path to hackmud directory
|
|
32
|
+
* @param options {@link WatchOptions details} and {@link PushOptions more details}
|
|
33
|
+
*/
|
|
34
|
+
export async function watch(sourceDirectory: string, hackmudDirectory: string, {
|
|
35
|
+
scripts = [ `*.*` ],
|
|
36
|
+
onPush,
|
|
37
|
+
minify = true,
|
|
38
|
+
mangleNames = false,
|
|
39
|
+
typeDeclarationPath: typeDeclarationPath_,
|
|
40
|
+
onReady,
|
|
41
|
+
forceQuineCheats,
|
|
42
|
+
rootFolderPath
|
|
43
|
+
}: WatchOptions = {}): Promise<void> {
|
|
44
|
+
if (!scripts.length)
|
|
45
|
+
throw new Error(`scripts option was an empty array`)
|
|
46
|
+
|
|
47
|
+
const sourceFolderStats = await getFileStats(sourceDirectory)
|
|
48
|
+
|
|
49
|
+
if (!sourceFolderStats.isDirectory())
|
|
50
|
+
throw Error(`Target folder must be a folder`)
|
|
51
|
+
|
|
52
|
+
const scriptNamesToUsers = new AutoMap((_scriptName: string) => new Set<string>)
|
|
53
|
+
const wildScriptUsers = new Set<string>
|
|
54
|
+
const wildUserScripts = new Set<string>
|
|
55
|
+
let pushEverything = false
|
|
56
|
+
|
|
57
|
+
for (const fullScriptName of scripts) {
|
|
58
|
+
const [ user, scriptName ] = fullScriptName.split(`.`)
|
|
59
|
+
|
|
60
|
+
if (!user || user == `*`) {
|
|
61
|
+
if (!scriptName || scriptName == `*`)
|
|
62
|
+
pushEverything = true
|
|
63
|
+
else
|
|
64
|
+
wildUserScripts.add(scriptName)
|
|
65
|
+
} else if (!scriptName || scriptName == `*`)
|
|
66
|
+
wildScriptUsers.add(user)
|
|
67
|
+
else
|
|
68
|
+
scriptNamesToUsers.get(scriptName).add(user)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const watcher = watchDirectory(`.`, {
|
|
72
|
+
cwd: sourceDirectory,
|
|
73
|
+
awaitWriteFinish: { stabilityThreshold: 100 },
|
|
74
|
+
ignored: (path, stats) => !!stats?.isFile() &&
|
|
75
|
+
!((path.endsWith(`.js`) || (path.endsWith(`.ts`) && !path.endsWith(`.d.ts`))))
|
|
76
|
+
}).on(`change`, async path => {
|
|
77
|
+
if (path.endsWith(`.d.ts`))
|
|
78
|
+
return
|
|
79
|
+
|
|
80
|
+
const extension = getFileExtension(path)
|
|
81
|
+
|
|
82
|
+
if (!supportedExtensions.includes(extension))
|
|
83
|
+
return
|
|
84
|
+
|
|
85
|
+
const scriptName = getPathBaseName(path, extension)
|
|
86
|
+
|
|
87
|
+
// if the path is still the same after getting just the base, it means it's directly in the source directory
|
|
88
|
+
// (global)
|
|
89
|
+
if (path == getPathBaseName(path)) {
|
|
90
|
+
if (!pushEverything && !wildScriptUsers.size && !wildUserScripts.has(scriptName) &&
|
|
91
|
+
!scriptNamesToUsers.has(scriptName)
|
|
92
|
+
)
|
|
93
|
+
return
|
|
94
|
+
|
|
95
|
+
const scriptNamesToUsersToSkip = new AutoMap((_scriptName: string): string[] => [])
|
|
96
|
+
|
|
97
|
+
await Promise.all((await readDirectoryWithStats(sourceDirectory)).map(async ({ stats, name, path }) => {
|
|
98
|
+
if (stats.isDirectory()) {
|
|
99
|
+
for (const child of await readDirectoryWithStats(path)) {
|
|
100
|
+
if (child.stats.isFile()) {
|
|
101
|
+
const fileExtension = getFileExtension(child.name)
|
|
102
|
+
|
|
103
|
+
if (supportedExtensions.includes(fileExtension))
|
|
104
|
+
scriptNamesToUsersToSkip.get(getPathBaseName(child.name, fileExtension)).push(name)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}))
|
|
109
|
+
|
|
110
|
+
const usersToPushToSet = new Set<string>
|
|
111
|
+
|
|
112
|
+
if (pushEverything || wildUserScripts.has(scriptName)) {
|
|
113
|
+
for (const { stats, name } of await readDirectoryWithStats(sourceDirectory)) {
|
|
114
|
+
if (stats.isDirectory())
|
|
115
|
+
usersToPushToSet.add(name)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (const { stats, name } of await readDirectoryWithStats(hackmudDirectory)) {
|
|
119
|
+
if (stats.isDirectory())
|
|
120
|
+
usersToPushToSet.add(name)
|
|
121
|
+
else if (stats.isFile() && name.endsWith(`.key`))
|
|
122
|
+
usersToPushToSet.add(name.slice(0, -4))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const users of scriptNamesToUsers.values()) {
|
|
126
|
+
for (const user of users)
|
|
127
|
+
usersToPushToSet.add(user)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const user of wildScriptUsers)
|
|
132
|
+
usersToPushToSet.add(user)
|
|
133
|
+
|
|
134
|
+
for (const user of scriptNamesToUsers.get(scriptName))
|
|
135
|
+
usersToPushToSet.add(user)
|
|
136
|
+
|
|
137
|
+
const usersToPushTo = [ ...usersToPushToSet ].filter(user => !scriptNamesToUsersToSkip.has(user))
|
|
138
|
+
|
|
139
|
+
if (!usersToPushTo.length) {
|
|
140
|
+
onPush?.({ path, users: [], characterCount: 0, error: new Error(`no users to push to`), warnings: [] })
|
|
141
|
+
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const uniqueId = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`)
|
|
146
|
+
const filePath = resolvePath(sourceDirectory, path)
|
|
147
|
+
let minifiedCode: string
|
|
148
|
+
let warnings
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
({ script: minifiedCode, warnings } = await processScript(
|
|
152
|
+
await readFile(filePath, { encoding: `utf8` }),
|
|
153
|
+
{
|
|
154
|
+
minify,
|
|
155
|
+
scriptUser: true,
|
|
156
|
+
scriptName,
|
|
157
|
+
uniqueId,
|
|
158
|
+
filePath,
|
|
159
|
+
mangleNames,
|
|
160
|
+
forceQuineCheats,
|
|
161
|
+
rootFolderPath
|
|
162
|
+
}
|
|
163
|
+
))
|
|
164
|
+
} catch (error) {
|
|
165
|
+
assert(error instanceof Error, HERE)
|
|
166
|
+
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
|
167
|
+
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
await Promise.all(usersToPushTo.map(user => writeFilePersistent(
|
|
172
|
+
resolvePath(hackmudDirectory, user, `scripts/${scriptName}.js`),
|
|
173
|
+
minifiedCode.replace(new RegExp(`\\$${uniqueId}\\$SCRIPT_USER\\$`, `g`), user)
|
|
174
|
+
.replace(new RegExp(`\\$${uniqueId}\\$FULL_SCRIPT_NAME\\$`, `g`), `${user}.${scriptName}`)
|
|
175
|
+
)))
|
|
176
|
+
|
|
177
|
+
onPush?.({
|
|
178
|
+
path,
|
|
179
|
+
users: usersToPushTo,
|
|
180
|
+
characterCount: countHackmudCharacters(minifiedCode),
|
|
181
|
+
error: undefined,
|
|
182
|
+
warnings
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const user = getPathBaseName(resolvePath(path, `..`))
|
|
189
|
+
|
|
190
|
+
if (!pushEverything && !wildScriptUsers.size && !wildUserScripts.has(scriptName) &&
|
|
191
|
+
!scriptNamesToUsers.get(scriptName).has(user)
|
|
192
|
+
)
|
|
193
|
+
return
|
|
194
|
+
|
|
195
|
+
const sourceDirectoryResolved = resolvePath(sourceDirectory)
|
|
196
|
+
const filePath = resolvePath(sourceDirectoryResolved, path)
|
|
197
|
+
const sourceCode = await readFile(filePath, { encoding: `utf8` })
|
|
198
|
+
let script
|
|
199
|
+
let warnings
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
({ script, warnings } = await processScript(sourceCode, {
|
|
203
|
+
minify,
|
|
204
|
+
scriptUser: user,
|
|
205
|
+
scriptName,
|
|
206
|
+
filePath,
|
|
207
|
+
mangleNames,
|
|
208
|
+
forceQuineCheats,
|
|
209
|
+
rootFolderPath
|
|
210
|
+
}))
|
|
211
|
+
} catch (error) {
|
|
212
|
+
assert(error instanceof Error, HERE)
|
|
213
|
+
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
|
214
|
+
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
await writeFilePersistent(resolvePath(hackmudDirectory, user, `scripts`, `${scriptName}.js`), script)
|
|
219
|
+
onPush?.({ path, users: [ user ], characterCount: countHackmudCharacters(script), error: undefined, warnings })
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
if (onReady)
|
|
223
|
+
watcher.on(`ready`, onReady)
|
|
224
|
+
|
|
225
|
+
if (!typeDeclarationPath_)
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
let typeDeclarationPath = typeDeclarationPath_
|
|
229
|
+
|
|
230
|
+
/*
|
|
231
|
+
this currently works because the generated type declaration effectively
|
|
232
|
+
just connects things and doesn't actually read the scripts it's
|
|
233
|
+
generating type declarations for
|
|
234
|
+
|
|
235
|
+
if I ever change `generateTypings()` to actually read the scripts to
|
|
236
|
+
grab their types, this will need to change
|
|
237
|
+
*/
|
|
238
|
+
|
|
239
|
+
const writeTypeDeclaration = async () => {
|
|
240
|
+
const typeDeclaration = await generateTypeDeclaration(sourceDirectory, hackmudDirectory)
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
await writeFile(typeDeclarationPath, typeDeclaration)
|
|
244
|
+
} catch (error) {
|
|
245
|
+
assert(error instanceof Error, HERE)
|
|
246
|
+
|
|
247
|
+
if (!((error as NodeJS.ErrnoException).code == `EISDIR`))
|
|
248
|
+
throw error
|
|
249
|
+
|
|
250
|
+
typeDeclarationPath = resolvePath(typeDeclarationPath, `player.d.ts`)
|
|
251
|
+
await writeFile(typeDeclarationPath, typeDeclaration)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
await writeTypeDeclaration()
|
|
256
|
+
watcher.on(`add`, writeTypeDeclaration)
|
|
257
|
+
watcher.on(`unlink`, writeTypeDeclaration)
|
|
258
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2022",
|
|
4
|
+
"lib": [ "es2023" ],
|
|
5
|
+
"module": "nodenext",
|
|
6
|
+
"resolveJsonModule": true,
|
|
7
|
+
"types": [ "node", "@total-typescript/ts-reset" ],
|
|
8
|
+
"checkJs": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"allowUnreachableCode": false,
|
|
11
|
+
"allowUnusedLabels": false,
|
|
12
|
+
"exactOptionalPropertyTypes": true,
|
|
13
|
+
"noFallthroughCasesInSwitch": true,
|
|
14
|
+
"noImplicitOverride": true,
|
|
15
|
+
"noUncheckedIndexedAccess": true,
|
|
16
|
+
"noUnusedLocals": true,
|
|
17
|
+
"noUnusedParameters": true,
|
|
18
|
+
"noEmit": true,
|
|
19
|
+
"isolatedModules": true,
|
|
20
|
+
"skipLibCheck": true
|
|
21
|
+
},
|
|
22
|
+
"include": [ "*", "scripts/*", "game-scripts/*" ]
|
|
23
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ViteUserConfig } from "vitest/config"
|
|
2
|
+
// @ts-expect-error it does exist, it just doesn't have typings
|
|
3
|
+
import babelPluginSyntaxTypescript from "@babel/plugin-syntax-typescript"
|
|
4
|
+
import { babel } from "@rollup/plugin-babel"
|
|
5
|
+
import { babelPluginHere } from "babel-plugin-here"
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
test: { includeSource: [ `src/**/*.ts` ] },
|
|
9
|
+
plugins: [
|
|
10
|
+
{
|
|
11
|
+
...babel({
|
|
12
|
+
babelHelpers: `bundled`,
|
|
13
|
+
extensions: [ `.ts` ],
|
|
14
|
+
plugins: [ babelPluginSyntaxTypescript, babelPluginHere() ]
|
|
15
|
+
}) as any,
|
|
16
|
+
enforce: `pre`
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
} satisfies ViteUserConfig
|