Skip to content
This repository was archived by the owner on Sep 8, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contributed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Also, note that we do not currently accept NSFW scripts.

The following are links to more AID scripts located externally.

* More soon! Please send PRs.
- More soon! Please send PRs.
45 changes: 45 additions & 0 deletions contributed/local typescript developer toolkit/REDME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Local Typescript Developer Toolkit

As title suggests, it's not a script per se, but a set of preset tools for creating one in typescript and transpiling into 4 JS files.

## Why should I use it?

Why not? But really, type safety and unit tests can let you pick up and avoid errors much earlier in writing your code. Moreover, most IDEs will help you more, since now they will know the type of your variables.

## Perquisites

1. node.js
2. Python 3
3. run `npm install` in the location of this file

## How to use

Write your `.ts` files in the folder according to expected output file. If you want to add a subdirectory, see [adding a subdirectory](#adding-a-subdirectory). Once you are done, I recommend creating tests in the `Source/Modules/Tests` folder, using jest, with `.tests.ts` or `.test.ts` extension.

If you want to use proxies for other objects (history, info, etc.), leave their files in `Source/Modules`. This way they won't be transpiled with the things you want to.

Once you are done, you can run tests with `npm test`, and transpile with `npm run build`.
If `Source/build` directory will not be created, or will be empty, but `Source/build-intermediate` will have `.js` files inside, you can manually run `combine files.py`. You may have to run it as administrator/root.

## Known issues

- When using `export default`, file name must be the exactly the same (case dependent) as exported element.

## Adding a subdirectory

If you want to add a subdirectory for, let's say, commands, you have to include it into Python script by:

1. Call `combineFilesInPath` under other calls in `main`
2. Append it to another file

If you want to see an example made by me, `combine_files_example.py` shows how I added Input Modifier/Commands and inserted it to the beginning of the end file.

## MIT License

Copyright 2023 Gutek8134

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import re
from os import listdir
from os.path import isfile
from pathlib import Path


IMPORT_REGEX_PATTERN: re.Pattern[str] = re.compile(
r'(^const (?P<namespace>\w+) = require\(.+\);$)', re.I | re.M)

CONTEXT_MODIFIER_INTERMEDIATE_FILES_PATH = "./build-intermediate/Context Modifier"
INPUT_MODIFIER_INTERMEDIATE_FILES_PATH = "./build-intermediate/Input Modifier"
OUTPUT_MODIFIER_INTERMEDIATE_FILES_PATH = "./build-intermediate/Output Modifier"
SHARED_LIBRARY_INTERMEDIATE_FILES_PATH = "./build-intermediate/Shared Library"

BUILD_PATH = "./build"
CONTEXT_MODIFIER_BUILD_FILE_NAME = "contextModifier.js"
INPUT_MODIFIER_BUILD_FILE_NAME = "inputModifier.js"
OUTPUT_MODIFIER_BUILD_FILE_NAME = "outputModifier.js"
SHARED_LIBRARY_BUILD_FILE_NAME = "sharedLibrary.js"

# Shortening access path as "constants"
COMMANDS_INTERMEDIATE_FILES_PATH = "./build-intermediate/Input Modifier/Commands"
COMMANDS_BUILD_FILE_NAME = "commands.intermediate.js"

def stripBeginning(fileContent: str) -> str:
lines: list[str] = fileContent.splitlines()
return "\n".join(lines[2:])


def removeExports(fileContent: str) -> str:
lines: list[str] = fileContent.splitlines()
for line in lines.copy():
if re.search(r"exports\.(\w+) = (?:void 0|\1)|exports.default = \w+", line) is not None:
lines.remove(line)
return "\n".join(lines)


def removeImportNamespacesAndImports(fileContent: str) -> str:
namespaces: list[str] = []
match: re.Match | None = re.search(IMPORT_REGEX_PATTERN, fileContent)
while match is not None:
fileContent = fileContent[:match.start()] + fileContent[match.end()+1:]
namespaces.append(match.group("namespace"))
match = re.search(IMPORT_REGEX_PATTERN, fileContent)

fileContent = fileContent.replace("exports.", "")
for namespace in namespaces:
fileContent = fileContent.replace(
f"{namespace}.default", namespace[:-2])
fileContent = fileContent.replace(namespace + ".", "")

return fileContent


def prepareFileContents(fileContents: str) -> str:
return \
removeImportNamespacesAndImports(
removeExports(
stripBeginning(fileContents)
)
)


def combineFilesInPath(inPath: str, outFileName: str) -> None:
with open(f"{BUILD_PATH}/{outFileName}", "w") as outFile:
for name in listdir(inPath):
if isfile(f"{inPath}/{name}"):
with open(f"{inPath}/{name}", "r") as inFile:
outFile.write(prepareFileContents(inFile.read())+"\n\n")


def main():
buildDirPath: Path = Path(BUILD_PATH)
if not buildDirPath.exists():
buildDirPath.mkdir()

combineFilesInPath(
CONTEXT_MODIFIER_INTERMEDIATE_FILES_PATH,
CONTEXT_MODIFIER_BUILD_FILE_NAME
)
combineFilesInPath(
INPUT_MODIFIER_INTERMEDIATE_FILES_PATH,
INPUT_MODIFIER_BUILD_FILE_NAME
)
combineFilesInPath(
OUTPUT_MODIFIER_INTERMEDIATE_FILES_PATH,
OUTPUT_MODIFIER_BUILD_FILE_NAME
)
combineFilesInPath(
SHARED_LIBRARY_INTERMEDIATE_FILES_PATH,
SHARED_LIBRARY_BUILD_FILE_NAME
)
# Combining Input Modifier/Commands files into single commands.intermediate.js
combineFilesInPath(
COMMANDS_INTERMEDIATE_FILES_PATH,
COMMANDS_BUILD_FILE_NAME
)

# Inserting content of commands.intermediate.js at the beginning of input modifier
with open(f"{BUILD_PATH}/{INPUT_MODIFIER_BUILD_FILE_NAME}", "r") as inputModifierFile:
cache = inputModifierFile.read()

with open(f"{BUILD_PATH}/{INPUT_MODIFIER_BUILD_FILE_NAME}", "w") as inputModifierFile:
with open(f"{BUILD_PATH}/{COMMANDS_BUILD_FILE_NAME}") as commandsFile:
inputModifierFile.write(commandsFile.read()+cache)

# Adding modifier call
for modifierFileName in (CONTEXT_MODIFIER_BUILD_FILE_NAME, INPUT_MODIFIER_BUILD_FILE_NAME, OUTPUT_MODIFIER_BUILD_FILE_NAME):
with open(f"{BUILD_PATH}/{modifierFileName}", "a") as modifierFile:
modifierFile.write("modifier(text);")

# Deleting commands.intermediate.js
commandsFilePath: Path = Path(f"{BUILD_PATH}/{COMMANDS_BUILD_FILE_NAME}")
if commandsFilePath.exists() and commandsFilePath.is_file():
commandsFilePath.unlink()


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const state: {
//Used in modifiers other than Input
in: string;
ctxt: string;
out: string;
message:
| string
| { text: string; visibleTo: string[] }
| { text: string; visibleTo: string[] }[];
memory: { context: string; frontMemory: string; authorsNote: string };
} = {
in: "",
ctxt: "",
out: "",
message: "",
memory: { context: "", frontMemory: "", authorsNote: "" },
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "CommonJS",
"allowUnreachableCode": false,
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"strict": true,
"outDir": "../build-intermediate",
"target": "ES6"
},
"exclude": ["./Tests"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import re
from os import listdir
from os.path import isfile
from pathlib import Path


IMPORT_REGEX_PATTERN: re.Pattern[str] = re.compile(
r'(^const (?P<namespace>\w+) = require\(.+\);$)', re.I | re.M)

CONTEXT_MODIFIER_INTERMEDIATE_FILES_PATH = "./build-intermediate/Context Modifier"
INPUT_MODIFIER_INTERMEDIATE_FILES_PATH = "./build-intermediate/Input Modifier"
OUTPUT_MODIFIER_INTERMEDIATE_FILES_PATH = "./build-intermediate/Output Modifier"
SHARED_LIBRARY_INTERMEDIATE_FILES_PATH = "./build-intermediate/Shared Library"

BUILD_PATH = "./build"
CONTEXT_MODIFIER_BUILD_FILE_NAME = "contextModifier.js"
INPUT_MODIFIER_BUILD_FILE_NAME = "inputModifier.js"
OUTPUT_MODIFIER_BUILD_FILE_NAME = "outputModifier.js"
SHARED_LIBRARY_BUILD_FILE_NAME = "sharedLibrary.js"


def stripBeginning(fileContent: str) -> str:
lines: list[str] = fileContent.splitlines()
return "\n".join(lines[2:])


def removeExports(fileContent: str) -> str:
lines: list[str] = fileContent.splitlines()
for line in lines.copy():
if re.search(r"exports\.(\w+) = (?:void 0|\1)|exports.default = \w+", line) is not None:
lines.remove(line)
return "\n".join(lines)


def removeImportNamespacesAndImports(fileContent: str) -> str:
namespaces: list[str] = []
match: re.Match | None = re.search(IMPORT_REGEX_PATTERN, fileContent)
while match is not None:
fileContent = fileContent[:match.start()] + fileContent[match.end()+1:]
namespaces.append(match.group("namespace"))
match = re.search(IMPORT_REGEX_PATTERN, fileContent)

fileContent = fileContent.replace("exports.", "")
for namespace in namespaces:
fileContent = fileContent.replace(
f"{namespace}.default", namespace[:-2])
fileContent = fileContent.replace(namespace + ".", "")

return fileContent


def prepareFileContents(fileContents: str) -> str:
return \
removeImportNamespacesAndImports(
removeExports(
stripBeginning(fileContents)
)
)


def combineFilesInPath(inPath: str, outFileName: str) -> None:
with open(f"{BUILD_PATH}/{outFileName}", "w") as outFile:
for name in listdir(inPath):
if isfile(f"{inPath}/{name}"):
with open(f"{inPath}/{name}", "r") as inFile:
outFile.write(prepareFileContents(inFile.read())+"\n\n")


def main():
buildDirPath: Path = Path(BUILD_PATH)
if not buildDirPath.exists():
buildDirPath.mkdir()

combineFilesInPath(
CONTEXT_MODIFIER_INTERMEDIATE_FILES_PATH,
CONTEXT_MODIFIER_BUILD_FILE_NAME
)
combineFilesInPath(
INPUT_MODIFIER_INTERMEDIATE_FILES_PATH,
INPUT_MODIFIER_BUILD_FILE_NAME
)
combineFilesInPath(
OUTPUT_MODIFIER_INTERMEDIATE_FILES_PATH,
OUTPUT_MODIFIER_BUILD_FILE_NAME
)
combineFilesInPath(
SHARED_LIBRARY_INTERMEDIATE_FILES_PATH,
SHARED_LIBRARY_BUILD_FILE_NAME
)

for modifierFileName in (CONTEXT_MODIFIER_BUILD_FILE_NAME, INPUT_MODIFIER_BUILD_FILE_NAME, OUTPUT_MODIFIER_BUILD_FILE_NAME):
with open(f"{BUILD_PATH}/{modifierFileName}", "a") as modifierFile:
modifierFile.write("modifier(text);")


if __name__ == "__main__":
main()
Loading