hackmud-script-manager 0.21.1-583d190 → 0.21.1-591fafe
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/README.md +47 -39
- package/bin/hsm.js +260 -244
- package/env.d.ts +326 -270
- package/generateTypeDeclaration.js +24 -23
- package/index.js +2 -3
- package/package.json +26 -24
- package/processScript/index.d.ts +12 -9
- package/processScript/index.js +91 -75
- package/processScript/minify.d.ts +11 -8
- package/processScript/minify.js +13 -16
- package/processScript/preprocess.d.ts +4 -2
- package/processScript/preprocess.js +15 -15
- package/processScript/transform.d.ts +8 -6
- package/processScript/transform.js +129 -50
- package/pull.d.ts +6 -4
- package/pull.js +1 -1
- package/push.d.ts +23 -18
- package/push.js +9 -7
- package/syncMacros.js +14 -15
- package/watch.d.ts +11 -7
- package/watch.js +26 -17
@@ -1,10 +1,10 @@
|
|
1
|
-
import
|
1
|
+
import gfaub8cx1g40ro6yhhb1tn8b from "@babel/traverse"
|
2
2
|
import t from "@babel/types"
|
3
3
|
import { assert } from "@samual/lib/assert"
|
4
4
|
import { clearObject } from "@samual/lib/clearObject"
|
5
5
|
import { validDBMethods } from "../constants.js"
|
6
6
|
import { getReferencePathsToGlobal } from "./shared.js"
|
7
|
-
const
|
7
|
+
const traverse = gfaub8cx1g40ro6yhhb1tn8b.default,
|
8
8
|
globalFunctionsUnder7Characters = [
|
9
9
|
"Map",
|
10
10
|
"Set",
|
@@ -141,12 +141,12 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
141
141
|
const neededDbMethodLets = new Set()
|
142
142
|
if (program.scope.hasGlobal("$db"))
|
143
143
|
for (const referencePath of getReferencePathsToGlobal("$db", program)) {
|
144
|
-
assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:
|
145
|
-
assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:
|
144
|
+
assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:198:69")
|
145
|
+
assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:199:72")
|
146
146
|
const databaseOpMethodName = referencePath.parentPath.node.property.name
|
147
147
|
assert(
|
148
148
|
validDBMethods.includes(databaseOpMethodName),
|
149
|
-
`src/processScript/transform.ts:
|
149
|
+
`src/processScript/transform.ts:205:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
|
150
150
|
)
|
151
151
|
if ("CallExpression" == referencePath.parentPath.parentPath?.type)
|
152
152
|
referencePath.parentPath.replaceWith(t.identifier(`$${uniqueId}$DB$${databaseOpMethodName}$`))
|
@@ -181,7 +181,7 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
181
181
|
if (program.scope.hasGlobal("Object"))
|
182
182
|
for (const referencePath of getReferencePathsToGlobal("Object", program))
|
183
183
|
if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
|
184
|
-
assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:
|
184
|
+
assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:255:64")
|
185
185
|
if ("getPrototypeOf" == referencePath.parent.property.name) {
|
186
186
|
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_GET_PROTOTYPE_OF_`))
|
187
187
|
needGetPrototypeOf = !0
|
@@ -194,7 +194,7 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
194
194
|
if (program.scope.hasGlobal("console"))
|
195
195
|
for (const referencePath of getReferencePathsToGlobal("console", program))
|
196
196
|
if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
|
197
|
-
assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:
|
197
|
+
assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:273:64")
|
198
198
|
referencePath.parentPath.replaceWith(
|
199
199
|
t.identifier(`_${uniqueId}_CONSOLE_METHOD_${referencePath.parent.property.name}_`)
|
200
200
|
)
|
@@ -202,13 +202,13 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
202
202
|
}
|
203
203
|
const lastStatement = program.node.body.at(-1)
|
204
204
|
let exportDefaultName
|
205
|
-
assert(lastStatement, "src/processScript/transform.ts:
|
205
|
+
assert(lastStatement, "src/processScript/transform.ts:287:27 program is empty")
|
206
206
|
if ("ExportNamedDeclaration" == lastStatement.type) {
|
207
207
|
program.node.body.pop()
|
208
208
|
for (const specifier of lastStatement.specifiers) {
|
209
209
|
assert(
|
210
210
|
"ExportSpecifier" == specifier.type,
|
211
|
-
`src/processScript/transform.ts:
|
211
|
+
`src/processScript/transform.ts:293:51 ${specifier.type} is currently unsupported`
|
212
212
|
)
|
213
213
|
if (
|
214
214
|
"default" !=
|
@@ -253,29 +253,29 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
253
253
|
mainFunction = t.functionDeclaration(
|
254
254
|
t.identifier(topFunctionName),
|
255
255
|
declarator.init.params,
|
256
|
-
"BlockStatement" == declarator.init.body.type
|
257
|
-
declarator.init.body
|
258
|
-
|
256
|
+
"BlockStatement" == declarator.init.body.type
|
257
|
+
? declarator.init.body
|
258
|
+
: t.blockStatement([t.returnStatement(declarator.init.body)])
|
259
259
|
)
|
260
260
|
else
|
261
|
-
"FunctionDeclaration" == statement.type
|
262
|
-
statement.id.name == exportDefaultName
|
263
|
-
(mainFunction = statement)
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
261
|
+
"FunctionDeclaration" == statement.type
|
262
|
+
? statement.id.name == exportDefaultName
|
263
|
+
? (mainFunction = statement)
|
264
|
+
: globalBlock.body.push(
|
265
|
+
t.variableDeclaration("let", [
|
266
|
+
t.variableDeclarator(
|
267
|
+
statement.id,
|
268
|
+
t.functionExpression(
|
269
|
+
void 0,
|
270
|
+
statement.params,
|
271
|
+
statement.body,
|
272
|
+
statement.generator,
|
273
|
+
statement.async
|
274
|
+
)
|
274
275
|
)
|
275
|
-
)
|
276
|
-
|
277
|
-
|
278
|
-
: globalBlock.body.push(statement)
|
276
|
+
])
|
277
|
+
)
|
278
|
+
: globalBlock.body.push(statement)
|
279
279
|
mainFunction ||= t.functionDeclaration(
|
280
280
|
t.identifier(topFunctionName),
|
281
281
|
[t.identifier("context"), t.identifier("args")],
|
@@ -310,11 +310,11 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
310
310
|
let hoistedGlobalBlockFunctions = 0
|
311
311
|
for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
|
312
312
|
if ("VariableDeclaration" == globalBlockStatement.type) {
|
313
|
-
assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:
|
313
|
+
assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:406:59")
|
314
314
|
const declarator = globalBlockStatement.declarations[0]
|
315
315
|
assert(
|
316
316
|
"Identifier" == declarator.id.type,
|
317
|
-
`src/processScript/transform.ts:
|
317
|
+
`src/processScript/transform.ts:410:51 declarator.id.type was "${declarator.id.type}"`
|
318
318
|
)
|
319
319
|
program.scope.crawl()
|
320
320
|
if (program.scope.hasGlobal(declarator.id.name)) {
|
@@ -329,9 +329,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
329
329
|
Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
|
330
330
|
) {
|
331
331
|
const binding = program.scope.getBinding(declarator.id.name)
|
332
|
-
assert(binding, "src/processScript/transform.ts:
|
332
|
+
assert(binding, "src/processScript/transform.ts:429:23")
|
333
333
|
for (const referencePath of binding.referencePaths) {
|
334
|
-
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:
|
334
|
+
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:432:56")
|
335
335
|
referencePath.replaceWith(
|
336
336
|
t.memberExpression(
|
337
337
|
t.identifier(`_${uniqueId}_G_`),
|
@@ -379,16 +379,16 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
379
379
|
} else globalBlockVariables.add(declarator.id.name)
|
380
380
|
} else if ("ClassDeclaration" == globalBlockStatement.type) {
|
381
381
|
program.scope.crawl()
|
382
|
-
assert(globalBlockStatement.id, "src/processScript/transform.ts:
|
382
|
+
assert(globalBlockStatement.id, "src/processScript/transform.ts:489:37")
|
383
383
|
if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
|
384
384
|
globalBlock.body.splice(globalBlockIndex, 1)
|
385
385
|
const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
|
386
386
|
[globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
|
387
387
|
program.scope.crawl()
|
388
388
|
const binding = program.scope.getBinding(globalBlockStatement.id.name)
|
389
|
-
assert(binding, "src/processScript/transform.ts:
|
389
|
+
assert(binding, "src/processScript/transform.ts:501:22")
|
390
390
|
for (const referencePath of binding.referencePaths) {
|
391
|
-
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:
|
391
|
+
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:504:55")
|
392
392
|
referencePath.replaceWith(
|
393
393
|
t.memberExpression(t.identifier(`_${uniqueId}_G_`), t.identifier(referencePath.node.name))
|
394
394
|
)
|
@@ -515,9 +515,11 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
515
515
|
"let",
|
516
516
|
[...neededDbMethodLets].map(name => {
|
517
517
|
const getArgs = () =>
|
518
|
-
"ObjectId" == name
|
519
|
-
|
520
|
-
|
518
|
+
"ObjectId" == name
|
519
|
+
? []
|
520
|
+
: "i" == name || "r" == name
|
521
|
+
? [t.identifier("a")]
|
522
|
+
: [t.identifier("a"), t.identifier("b")]
|
521
523
|
return t.variableDeclarator(
|
522
524
|
t.identifier(`_${uniqueId}_CONSOLE_METHOD_${name}_`),
|
523
525
|
t.arrowFunctionExpression(
|
@@ -560,6 +562,65 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
560
562
|
t.variableDeclarator(t.identifier(`_${uniqueId}_G_`), t.identifier(`$${uniqueId}$GLOBAL$`))
|
561
563
|
])
|
562
564
|
)
|
565
|
+
const replaceAllThisWith = (node, scope, thisId) => {
|
566
|
+
let thisIsReferenced = !1
|
567
|
+
traverse(
|
568
|
+
node,
|
569
|
+
{
|
570
|
+
ThisExpression(path) {
|
571
|
+
thisIsReferenced = !0
|
572
|
+
path.replaceWith(t.identifier(thisId))
|
573
|
+
},
|
574
|
+
Function(path) {
|
575
|
+
"ArrowFunctionExpression" != path.node.type && path.skip()
|
576
|
+
}
|
577
|
+
},
|
578
|
+
scope
|
579
|
+
)
|
580
|
+
return thisIsReferenced
|
581
|
+
},
|
582
|
+
replaceThisInObjectLikeDefinition = path => {
|
583
|
+
const { node: object, scope, parent } = path,
|
584
|
+
evenMoreUniqueId = Math.floor(Math.random() * 2 ** 52)
|
585
|
+
.toString(36)
|
586
|
+
.padStart(11, "0"),
|
587
|
+
reuseDeclaredName =
|
588
|
+
"VariableDeclarator" == parent.type &&
|
589
|
+
"VariableDeclaration" == path.parentPath?.parentPath?.node?.type &&
|
590
|
+
"const" == path.parentPath?.parentPath?.node?.kind &&
|
591
|
+
"Identifier" == parent.id.type,
|
592
|
+
thisId = reuseDeclaredName ? parent.id.name : `_${evenMoreUniqueId}_THIS_`
|
593
|
+
let thisIsReferenced = !1
|
594
|
+
if ("ObjectExpression" == object.type)
|
595
|
+
for (const property of object.properties)
|
596
|
+
"ObjectMethod" == property.type &&
|
597
|
+
(thisIsReferenced ||= replaceAllThisWith(property, scope, thisId))
|
598
|
+
else
|
599
|
+
for (const element of object.elements)
|
600
|
+
null != element && (thisIsReferenced ||= replaceAllThisWith(element, scope, thisId))
|
601
|
+
if (!thisIsReferenced) return
|
602
|
+
if (reuseDeclaredName) return
|
603
|
+
path.replaceWith(t.assignmentExpression("=", t.identifier(thisId), object))
|
604
|
+
const parentBlock = (path => {
|
605
|
+
let someBlock = null,
|
606
|
+
currentParent = path
|
607
|
+
for (; currentParent && currentParent && currentParent.node; ) {
|
608
|
+
if (t.isBlock(currentParent.node)) {
|
609
|
+
someBlock = currentParent.node
|
610
|
+
break
|
611
|
+
}
|
612
|
+
if (t.isArrowFunctionExpression(currentParent.parentPath?.node)) {
|
613
|
+
currentParent.replaceWith(t.blockStatement([t.returnStatement(currentParent.node)]))
|
614
|
+
someBlock = currentParent.node
|
615
|
+
break
|
616
|
+
}
|
617
|
+
currentParent = currentParent.parentPath
|
618
|
+
}
|
619
|
+
assert(null != someBlock, "src/processScript/transform.ts:709:29")
|
620
|
+
return someBlock
|
621
|
+
})(path)
|
622
|
+
parentBlock.body.unshift(t.variableDeclaration("let", [t.variableDeclarator(t.identifier(thisId), null)]))
|
623
|
+
}
|
563
624
|
traverse(file, {
|
564
625
|
BlockStatement({ node: blockStatement }) {
|
565
626
|
for (const [index, functionDeclaration] of blockStatement.body.entries())
|
@@ -579,8 +640,14 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
579
640
|
)
|
580
641
|
}
|
581
642
|
},
|
643
|
+
ObjectExpression(path) {
|
644
|
+
replaceThisInObjectLikeDefinition(path)
|
645
|
+
},
|
646
|
+
ArrayExpression(path) {
|
647
|
+
replaceThisInObjectLikeDefinition(path)
|
648
|
+
},
|
582
649
|
ClassBody({ node: classBody, scope, parent }) {
|
583
|
-
assert(t.isClass(parent), "src/processScript/transform.ts:
|
650
|
+
assert(t.isClass(parent), "src/processScript/transform.ts:814:30")
|
584
651
|
let thisIsReferenced = !1
|
585
652
|
for (const classMethod of classBody.body) {
|
586
653
|
if ("ClassMethod" != classMethod.type) continue
|
@@ -593,7 +660,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
593
660
|
thisIsReferenced = !0
|
594
661
|
path.replaceWith(t.identifier(`_${uniqueId}_THIS_`))
|
595
662
|
},
|
596
|
-
Function
|
663
|
+
Function(path) {
|
664
|
+
"ArrowFunctionExpression" != path.node.type && path.skip()
|
665
|
+
}
|
597
666
|
},
|
598
667
|
scope
|
599
668
|
)
|
@@ -654,7 +723,17 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
654
723
|
!parent.superClass && thisIsReferenced && (parent.superClass = t.identifier("Object"))
|
655
724
|
},
|
656
725
|
VariableDeclaration({ node: variableDeclaration }) {
|
657
|
-
"const" == variableDeclaration.kind
|
726
|
+
if ("const" == variableDeclaration.kind) {
|
727
|
+
variableDeclaration.kind = "let"
|
728
|
+
variableDeclaration.extra = { ...variableDeclaration.extra, usedToBeConst: !0 }
|
729
|
+
}
|
730
|
+
},
|
731
|
+
AssignmentExpression({ node: assignment, scope }) {
|
732
|
+
const lhs = assignment.left
|
733
|
+
if ("Identifier" != lhs.type) return
|
734
|
+
const binding = scope.getBinding(lhs.name)
|
735
|
+
if (binding?.path?.parentPath?.node?.extra?.usedToBeConst)
|
736
|
+
throw Error(`Reassignment to const variable ${lhs.name} is not allowed!`)
|
658
737
|
},
|
659
738
|
ThisExpression: path => {
|
660
739
|
path.replaceWith(t.identifier("undefined"))
|
@@ -663,9 +742,9 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
663
742
|
const bigIntAsNumber = Number(path.node.value)
|
664
743
|
path.replaceWith(
|
665
744
|
t.callExpression(t.identifier("BigInt"), [
|
666
|
-
BigInt(bigIntAsNumber) == BigInt(path.node.value)
|
667
|
-
t.numericLiteral(bigIntAsNumber)
|
668
|
-
|
745
|
+
BigInt(bigIntAsNumber) == BigInt(path.node.value)
|
746
|
+
? t.numericLiteral(bigIntAsNumber)
|
747
|
+
: t.stringLiteral(path.node.value)
|
669
748
|
])
|
670
749
|
)
|
671
750
|
}
|
@@ -680,23 +759,23 @@ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scr
|
|
680
759
|
}
|
681
760
|
function processFakeSubscriptObject(fakeSubscriptObjectName, seclevel) {
|
682
761
|
for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
|
683
|
-
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:
|
762
|
+
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:937:60")
|
684
763
|
assert("Identifier" == referencePath.parent.property.type)
|
685
764
|
assert(
|
686
765
|
"MemberExpression" == referencePath.parentPath.parentPath?.node.type,
|
687
|
-
"src/processScript/transform.ts:
|
766
|
+
"src/processScript/transform.ts:939:81"
|
688
767
|
)
|
689
768
|
assert(
|
690
769
|
"Identifier" == referencePath.parentPath.parentPath.node.property.type,
|
691
|
-
"src/processScript/transform.ts:
|
770
|
+
"src/processScript/transform.ts:940:83"
|
692
771
|
)
|
693
772
|
assert(
|
694
773
|
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
|
695
|
-
`src/processScript/transform.ts:
|
774
|
+
`src/processScript/transform.ts:944:8 invalid user "${referencePath.parent.property.name}" in subscript`
|
696
775
|
)
|
697
776
|
assert(
|
698
777
|
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
|
699
|
-
`src/processScript/transform.ts:
|
778
|
+
`src/processScript/transform.ts:949:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
|
700
779
|
)
|
701
780
|
if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
|
702
781
|
referencePath.parentPath.parentPath.replaceWith(
|
package/pull.d.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
/**
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
/**
|
2
|
+
* Copies script from hackmud to local source folder.
|
3
|
+
* @param sourceFolderPath path to folder containing source files
|
4
|
+
* @param hackmudPath path to hackmud directory
|
5
|
+
* @param script to pull in `user.name` format
|
6
|
+
*/
|
5
7
|
export declare function pull(sourceFolderPath: string, hackmudPath: string, script: string): Promise<void>;
|
package/pull.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { copyFilePersistent } from "@samual/lib/copyFilePersistent"
|
2
1
|
import { resolve } from "path"
|
2
|
+
import { copyFilePersistent } from "@samual/lib/copyFilePersistent"
|
3
3
|
async function pull(sourceFolderPath, hackmudPath, script) {
|
4
4
|
const [user, name] = script.split(".")
|
5
5
|
if (!user || !name) throw Error('`script` argument must be in "user.name" format')
|
package/push.d.ts
CHANGED
@@ -3,19 +3,22 @@ import type { Info } from ".";
|
|
3
3
|
export type PushOptions = LaxPartial<{
|
4
4
|
/** whether to do the minify step (defaults to `true`) */ minify: boolean;
|
5
5
|
/** whether to mangle function and class names (defaults to `false`) */ mangleNames: boolean;
|
6
|
-
/**
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
/**
|
7
|
+
* array of scripts in the format `foo.bar`
|
8
|
+
*
|
9
|
+
* also accepts wild card (`*`) e.g. `*.bar` or `foo.*`
|
10
|
+
*
|
11
|
+
* pushes everything by default (`*.*`)
|
12
|
+
*/
|
11
13
|
scripts: string[];
|
12
14
|
/** callback called on script push */ onPush: (info: Info) => void;
|
13
|
-
/**
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
/**
|
16
|
+
* when set to `true` forces use of quine cheats
|
17
|
+
*
|
18
|
+
* when set to `false` forces quine cheats not to be used
|
19
|
+
*
|
20
|
+
* when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
|
21
|
+
*/
|
19
22
|
forceQuineCheats: boolean;
|
20
23
|
rootFolderPath: string;
|
21
24
|
}>;
|
@@ -27,11 +30,13 @@ export declare class NoUsersError extends Error {
|
|
27
30
|
}
|
28
31
|
export declare class NoScriptsError extends Error {
|
29
32
|
}
|
30
|
-
/**
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
/**
|
34
|
+
* Push scripts from a source directory to the hackmud directory.
|
35
|
+
*
|
36
|
+
* Pushes files directly in the source folder to all users
|
37
|
+
* @param sourcePath directory containing source code
|
38
|
+
* @param hackmudPath directory created by hackmud containing user data including scripts
|
39
|
+
* @param options {@link PushOptions details}
|
40
|
+
* @returns array of info on pushed scripts
|
41
|
+
*/
|
37
42
|
export declare function push(sourcePath: string, hackmudPath: string, { scripts, onPush, minify, mangleNames, forceQuineCheats, rootFolderPath }?: PushOptions): Promise<MissingSourceFolderError | MissingHackmudFolderError | NoUsersError | NoScriptsError | Info[]>;
|
package/push.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
import {
|
1
|
+
import { readFile } from "fs/promises"
|
2
|
+
import { basename, resolve } from "path"
|
2
3
|
import { ensure, assert } from "@samual/lib/assert"
|
4
|
+
import { AutoMap } from "@samual/lib/AutoMap"
|
3
5
|
import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
|
4
6
|
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
5
7
|
import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
|
6
|
-
import { readFile } from "fs/promises"
|
7
|
-
import { basename, resolve } from "path"
|
8
8
|
import { processScript } from "./processScript/index.js"
|
9
9
|
import "@babel/generator"
|
10
10
|
import "@babel/parser"
|
@@ -70,10 +70,12 @@ async function push(
|
|
70
70
|
])
|
71
71
|
if (sourceFolder instanceof Error) return sourceFolder
|
72
72
|
if (hackmudFolder instanceof Error) return hackmudFolder
|
73
|
-
const sourceFolderFolders = sourceFolder.filter(
|
73
|
+
const sourceFolderFolders = sourceFolder.filter(
|
74
|
+
({ name, stats }) => stats.isDirectory() && /^[a-z_][a-z\d_]{0,24}$/.test(name)
|
75
|
+
),
|
74
76
|
allUsers = new Set([
|
75
77
|
...scripts
|
76
|
-
.map(scriptName => ensure(scriptName.split(".")[0], "src/push.ts:
|
78
|
+
.map(scriptName => ensure(scriptName.split(".")[0], "src/push.ts:94:65"))
|
77
79
|
.filter(name => "*" != name),
|
78
80
|
...sourceFolderFolders.map(({ name }) => name),
|
79
81
|
...hackmudFolder.filter(({ stats }) => stats.isDirectory()).map(({ name }) => name),
|
@@ -89,8 +91,8 @@ async function push(
|
|
89
91
|
scriptNamesToUsers = new AutoMap(_scriptName => new Set())
|
90
92
|
for (const script of scripts) {
|
91
93
|
const [user, scriptName] = script.split(".")
|
92
|
-
assert(user, "src/push.ts:
|
93
|
-
assert(scriptName, "src/push.ts:
|
94
|
+
assert(user, "src/push.ts:117:16")
|
95
|
+
assert(scriptName, "src/push.ts:118:22")
|
94
96
|
"*" == user ? scriptNamesToUsers.set(scriptName, allUsers) : scriptNamesToUsers.get(scriptName).add(user)
|
95
97
|
}
|
96
98
|
const sourceFolderFiles = sourceFolder.filter(({ stats }) => stats.isFile()),
|
package/syncMacros.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
2
1
|
import { readFile, stat, writeFile } from "fs/promises"
|
3
2
|
import { extname, basename, resolve } from "path"
|
3
|
+
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
4
4
|
async function syncMacros(hackmudPath) {
|
5
5
|
const files = await readDirectoryWithStats(hackmudPath),
|
6
6
|
macros = new Map(),
|
@@ -9,22 +9,21 @@ async function syncMacros(hackmudPath) {
|
|
9
9
|
files.map(async file => {
|
10
10
|
if (file.stats.isFile())
|
11
11
|
switch (extname(file.name)) {
|
12
|
-
case ".macros":
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
macros.set(macroName, { date, macro: lines[2 * index + 1] })
|
25
|
-
}
|
12
|
+
case ".macros": {
|
13
|
+
const [lines, date] = await Promise.all([
|
14
|
+
readFile(resolve(hackmudPath, file.name), { encoding: "utf8" }).then(file =>
|
15
|
+
file.split("\n")
|
16
|
+
),
|
17
|
+
stat(resolve(hackmudPath, file.name)).then(({ mtime }) => mtime)
|
18
|
+
])
|
19
|
+
for (let index = 0; index < lines.length / 2 - 1; index++) {
|
20
|
+
const macroName = lines[2 * index],
|
21
|
+
currentMacro = macros.get(macroName)
|
22
|
+
;(!currentMacro || date > currentMacro.date) &&
|
23
|
+
macros.set(macroName, { date, macro: lines[2 * index + 1] })
|
26
24
|
}
|
27
25
|
break
|
26
|
+
}
|
28
27
|
case ".key":
|
29
28
|
users.push(basename(file.name, ".key"))
|
30
29
|
}
|
package/watch.d.ts
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
import type { LaxPartial } from "@samual/lib";
|
2
2
|
import type { PushOptions } from "./push";
|
3
3
|
export type WatchOptions = PushOptions & LaxPartial<{
|
4
|
-
/**
|
5
|
-
|
6
|
-
|
4
|
+
/**
|
5
|
+
* if provided, will write typescript type declarations for all the scripts on every change detected
|
6
|
+
*
|
7
|
+
* writing the type declarations enables interscript type checking and autocompletetes for the args
|
8
|
+
*/
|
7
9
|
typeDeclarationPath: string;
|
8
10
|
onReady: () => void;
|
9
11
|
rootFolderPath: string;
|
10
12
|
}>;
|
11
|
-
/**
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
/**
|
14
|
+
* Watches target file or folder for updates and builds and pushes updated file.
|
15
|
+
* @param sourceDirectory path to folder containing source files
|
16
|
+
* @param hackmudDirectory path to hackmud directory
|
17
|
+
* @param options {@link WatchOptions details} and {@link PushOptions more details}
|
18
|
+
*/
|
15
19
|
export declare function watch(sourceDirectory: string, hackmudDirectory: string, { scripts, onPush, minify, mangleNames, typeDeclarationPath: typeDeclarationPath_, onReady, forceQuineCheats, rootFolderPath }?: WatchOptions): Promise<void>;
|
package/watch.js
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
import {
|
1
|
+
import { stat, readFile, writeFile } from "fs/promises"
|
2
|
+
import { extname, basename, resolve } from "path"
|
2
3
|
import { assert } from "@samual/lib/assert"
|
4
|
+
import { AutoMap } from "@samual/lib/AutoMap"
|
3
5
|
import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
|
4
6
|
import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
|
5
7
|
import { writeFilePersistent } from "@samual/lib/writeFilePersistent"
|
6
8
|
import { watch as watch$1 } from "chokidar"
|
7
|
-
import { stat, readFile, writeFile } from "fs/promises"
|
8
|
-
import { extname, basename, resolve } from "path"
|
9
9
|
import { supportedExtensions } from "./constants.js"
|
10
10
|
import { generateTypeDeclaration } from "./generateTypeDeclaration.js"
|
11
11
|
import { processScript } from "./processScript/index.js"
|
12
|
-
import "path/posix"
|
13
12
|
import "@babel/generator"
|
14
13
|
import "@babel/parser"
|
15
14
|
import "@babel/plugin-proposal-decorators"
|
@@ -68,12 +67,13 @@ async function watch(
|
|
68
67
|
let pushEverything = !1
|
69
68
|
for (const fullScriptName of scripts) {
|
70
69
|
const [user, scriptName] = fullScriptName.split(".")
|
71
|
-
user && "*" != user
|
72
|
-
scriptName && "*" != scriptName
|
73
|
-
scriptNamesToUsers.get(scriptName).add(user)
|
74
|
-
|
75
|
-
|
76
|
-
|
70
|
+
user && "*" != user
|
71
|
+
? scriptName && "*" != scriptName
|
72
|
+
? scriptNamesToUsers.get(scriptName).add(user)
|
73
|
+
: wildScriptUsers.add(user)
|
74
|
+
: scriptName && "*" != scriptName
|
75
|
+
? wildUserScripts.add(scriptName)
|
76
|
+
: (pushEverything = !0)
|
77
77
|
}
|
78
78
|
const watcher = watch$1(".", {
|
79
79
|
cwd: sourceDirectory,
|
@@ -112,9 +112,9 @@ async function watch(
|
|
112
112
|
for (const { stats, name } of await readDirectoryWithStats(sourceDirectory))
|
113
113
|
stats.isDirectory() && usersToPushToSet.add(name)
|
114
114
|
for (const { stats, name } of await readDirectoryWithStats(hackmudDirectory))
|
115
|
-
stats.isDirectory()
|
116
|
-
usersToPushToSet.add(name)
|
117
|
-
|
115
|
+
stats.isDirectory()
|
116
|
+
? usersToPushToSet.add(name)
|
117
|
+
: stats.isFile() && name.endsWith(".key") && usersToPushToSet.add(name.slice(0, -4))
|
118
118
|
for (const users of scriptNamesToUsers.values()) for (const user of users) usersToPushToSet.add(user)
|
119
119
|
}
|
120
120
|
for (const user of wildScriptUsers) usersToPushToSet.add(user)
|
@@ -132,10 +132,19 @@ async function watch(
|
|
132
132
|
try {
|
133
133
|
;({ script: minifiedCode, warnings } = await processScript(
|
134
134
|
await readFile(filePath, { encoding: "utf8" }),
|
135
|
-
{
|
135
|
+
{
|
136
|
+
minify,
|
137
|
+
scriptUser: !0,
|
138
|
+
scriptName,
|
139
|
+
uniqueId,
|
140
|
+
filePath,
|
141
|
+
mangleNames,
|
142
|
+
forceQuineCheats,
|
143
|
+
rootFolderPath
|
144
|
+
}
|
136
145
|
))
|
137
146
|
} catch (error) {
|
138
|
-
assert(error instanceof Error, "src/watch.ts:
|
147
|
+
assert(error instanceof Error, "src/watch.ts:165:36")
|
139
148
|
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
140
149
|
return
|
141
150
|
}
|
@@ -183,7 +192,7 @@ async function watch(
|
|
183
192
|
rootFolderPath
|
184
193
|
}))
|
185
194
|
} catch (error) {
|
186
|
-
assert(error instanceof Error, "src/watch.ts:
|
195
|
+
assert(error instanceof Error, "src/watch.ts:212:35")
|
187
196
|
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
|
188
197
|
return
|
189
198
|
}
|
@@ -198,7 +207,7 @@ async function watch(
|
|
198
207
|
try {
|
199
208
|
await writeFile(typeDeclarationPath, typeDeclaration)
|
200
209
|
} catch (error) {
|
201
|
-
assert(error instanceof Error, "src/watch.ts:
|
210
|
+
assert(error instanceof Error, "src/watch.ts:245:35")
|
202
211
|
if ("EISDIR" != error.code) throw error
|
203
212
|
typeDeclarationPath = resolve(typeDeclarationPath, "player.d.ts")
|
204
213
|
await writeFile(typeDeclarationPath, typeDeclaration)
|