@sf-kmods/kdataforgejs-cli 0.1.19 → 2026.4.19
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 +15 -3
- package/dist/cli.js +23 -0
- package/dist/commands/format.js +29 -0
- package/dist/commands/gameData.js +8 -2
- package/dist/commands/lint.js +43 -0
- package/dist/commands/sync.js +97 -0
- package/dist/pack/scriptFiles.js +14 -0
- package/dist/pack/usage.js +102 -0
- package/dist/types/biome.js +25 -0
- package/dist/types/format.js +9 -0
- package/dist/types/organize.js +184 -0
- package/package.json +4 -2
- package/templates/default/DataForge/scripts/lib.ts +1 -1
- package/templates/default/DataForge/scripts/main.ts +2 -2
- package/templates/default/README.md +8 -2
- package/templates/default/_gitignore +1 -0
- package/templates/default/package.json +7 -1
package/README.md
CHANGED
|
@@ -15,9 +15,12 @@ npm run verify
|
|
|
15
15
|
| --- | --- |
|
|
16
16
|
| `kdf init [dir]` | Creates a project: `package.json`, `tsconfig.json` and a `DataForge/` pack with `pack.yml` and `scripts/main.ts`. `--name` sets the pack ref, `--force` writes into a non-empty folder. |
|
|
17
17
|
| `kdf check [dir]` | Type-checks the project against `@sf-kmods/kdataforgejs` with the bundled TypeScript. Nothing is emitted. |
|
|
18
|
+
| `kdf format [dir]` | Formats JavaScript and TypeScript files below `DataForge/scripts/` with bundled Biome. |
|
|
19
|
+
| `kdf lint [dir]` | Lints JavaScript and TypeScript files below `DataForge/scripts/` with bundled Biome recommended rules. |
|
|
18
20
|
| `kdf validate [dir]` | Checks `pack.yml`, the entry file, the static import graph and the digests of declared imports. |
|
|
21
|
+
| `kdf sync [dir]` | Generates sorted `imports`, SHA-256 digests and `reflectionClasses` from TypeScript usage and imported YAML targets. |
|
|
19
22
|
| `kdf hash [dir]` | Rewrites stale `sha256` values of declared imports and leaves your comments alone. |
|
|
20
|
-
| `kdf game-data [dir]` |
|
|
23
|
+
| `kdf game-data [dir]` | Runs manifest sync, finds a modded Satisfactory installation, starts a full reflection export, closes the game and imports the archive plus generated TypeScript declarations. Use `--game-path <root-or-executable>` to select an installation explicitly. |
|
|
21
24
|
|
|
22
25
|
Exit code `0` means all good, `1` means findings, `2` means the command was used wrong.
|
|
23
26
|
|
|
@@ -25,7 +28,13 @@ Exit code `0` means all good, `1` means findings, `2` means the command was used
|
|
|
25
28
|
It does not replace the runtime: catalog, authority and limits are still decided in game.
|
|
26
29
|
|
|
27
30
|
The generated project keeps `package.json` and `tsconfig.json` next to the `DataForge` folder, never inside it. The pack
|
|
28
|
-
itself has no `node_modules`.
|
|
31
|
+
itself has no `node_modules`. `npm run refresh` runs `kdf game-data`; `npm run fix` formats scripts and synchronizes
|
|
32
|
+
generated manifest fields. `format`, `lint`, `check`, `validate`, `hash`, `sync`, `test`, and `verify` are available too.
|
|
33
|
+
|
|
34
|
+
Literal `ctx.import(kind, path)` calls and reflected paths used by `ctx.getClass`, `ctx.getObject` and
|
|
35
|
+
`ctx.getDataAsset` are source of truth. YAML imports are scanned for `target` paths. Run `kdf sync`; do not maintain
|
|
36
|
+
manifest import digests or reflection class lists by hand. Dynamic import paths are rejected because CLI cannot grant
|
|
37
|
+
their authority safely.
|
|
29
38
|
|
|
30
39
|
## Import game data
|
|
31
40
|
|
|
@@ -47,4 +56,7 @@ The CLI loads the newest local save automatically, starts a full reflection expo
|
|
|
47
56
|
minutes by default; override this with `--timeout <seconds>`. It writes `.kdataforge/satisfactory.kdfdata`, imports
|
|
48
57
|
generated declarations below `DataForge/typescript/kdataforgejs/`, creates `tsconfig.kdf.json`, and makes later
|
|
49
58
|
`kdf check` calls use those installed-game types automatically. `.kdataforge/` is added to `.gitignore` because the
|
|
50
|
-
archive can be large.
|
|
59
|
+
archive can be large. Declarations are split into `runtime.d.ts`, `globals.d.ts`, alphabetical `satisfactory/` groups,
|
|
60
|
+
and Unreal-path `paths/` groups behind `index.d.ts`. Biome formats generated TypeScript and JSON through its JavaScript
|
|
61
|
+
API. Projects created by `kdf init` also ignore `DataForge/typescript/kdataforgejs/`; regenerate it for the local game
|
|
62
|
+
and mod set. Temporary export and staging directories are removed after the game exits.
|
package/dist/cli.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { parseArgs } from "node:util";
|
|
2
2
|
import { checkProject } from "./commands/check.js";
|
|
3
|
+
import { formatProject } from "./commands/format.js";
|
|
3
4
|
import { importGameData } from "./commands/gameData.js";
|
|
4
5
|
import { hashProject } from "./commands/hash.js";
|
|
5
6
|
import { initProject } from "./commands/init.js";
|
|
7
|
+
import { lintProject } from "./commands/lint.js";
|
|
8
|
+
import { syncProject } from "./commands/sync.js";
|
|
6
9
|
import { validateProject } from "./commands/validate.js";
|
|
7
10
|
import { formatDiagnostic, hasErrors } from "./diagnostics.js";
|
|
8
11
|
const USAGE = `kdf - KDataForgeJS project tooling
|
|
@@ -10,7 +13,10 @@ const USAGE = `kdf - KDataForgeJS project tooling
|
|
|
10
13
|
Usage:
|
|
11
14
|
kdf init [dir] [--name <PackRef>] [--force] Create a new KDataForge project with a TypeScript script
|
|
12
15
|
kdf check [dir] Type-check the project (tsconfig.json, no emit)
|
|
16
|
+
kdf format [dir] Format DataForge scripts with bundled Biome
|
|
17
|
+
kdf lint [dir] Lint DataForge scripts with bundled Biome rules
|
|
13
18
|
kdf validate [dir] Check pack.yml, entry, static imports and import digests
|
|
19
|
+
kdf sync [dir] Generate imports, hashes and reflection classes from source
|
|
14
20
|
kdf hash [dir] Update sha256 of declared script imports in pack.yml
|
|
15
21
|
kdf game-data [dir] [--game-path <path>] Export installed game data and import it into the project
|
|
16
22
|
kdf --help | --version
|
|
@@ -50,14 +56,31 @@ async function runCli(request, dependencies) {
|
|
|
50
56
|
}
|
|
51
57
|
case "check":
|
|
52
58
|
return report(io, checkProject(directory), "Type check passed.");
|
|
59
|
+
case "format": {
|
|
60
|
+
const result = formatProject(directory);
|
|
61
|
+
io.out(result.changedFiles.length === 0
|
|
62
|
+
? "Files are already formatted."
|
|
63
|
+
: `Formatted files:\n${result.changedFiles.join("\n")}`);
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
case "lint":
|
|
67
|
+
return report(io, lintProject(directory), "Lint passed.");
|
|
53
68
|
case "validate":
|
|
54
69
|
return report(io, validateProject(directory), "Pack is valid.");
|
|
70
|
+
case "sync": {
|
|
71
|
+
const result = syncProject(directory);
|
|
72
|
+
io.out(result.changedPacks.length === 0
|
|
73
|
+
? "Manifest is already synchronized."
|
|
74
|
+
: `Synchronized manifests:\n${result.changedPacks.join("\n")}`);
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
55
77
|
case "hash": {
|
|
56
78
|
const changed = hashProject(directory);
|
|
57
79
|
io.out(changed.length === 0 ? "All import digests are up to date." : `Updated digests:\n${changed.join("\n")}`);
|
|
58
80
|
return 0;
|
|
59
81
|
}
|
|
60
82
|
case "game-data": {
|
|
83
|
+
syncProject(directory);
|
|
61
84
|
const timeoutMilliseconds = parseTimeout(values.timeout);
|
|
62
85
|
const result = await dependencies.importGameData({
|
|
63
86
|
destination: directory,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { findScriptFiles } from "../pack/scriptFiles.js";
|
|
4
|
+
import { biome, biomeProjectKey } from "../types/biome.js";
|
|
5
|
+
/** Formats JavaScript and TypeScript files in DataForge/scripts through bundled Biome. */
|
|
6
|
+
export function formatProject(projectRoot) {
|
|
7
|
+
const absoluteProjectRoot = resolve(projectRoot);
|
|
8
|
+
const scriptsRoot = join(absoluteProjectRoot, "DataForge/scripts");
|
|
9
|
+
if (!existsSync(scriptsRoot))
|
|
10
|
+
throw new Error("DataForge/scripts not found. Run 'kdf init' or pass project root.");
|
|
11
|
+
const formattedFiles = findScriptFiles(scriptsRoot)
|
|
12
|
+
.map((filePath) => formatFile(filePath, absoluteProjectRoot))
|
|
13
|
+
.filter((file) => file !== undefined);
|
|
14
|
+
for (const file of formattedFiles)
|
|
15
|
+
writeFileSync(file.path, file.content);
|
|
16
|
+
return { changedFiles: formattedFiles.map((file) => shownPath(file.path, absoluteProjectRoot)) };
|
|
17
|
+
}
|
|
18
|
+
function formatFile(filePath, projectRoot) {
|
|
19
|
+
const original = readFileSync(filePath, "utf8");
|
|
20
|
+
const result = biome.formatContent(biomeProjectKey, original, { filePath });
|
|
21
|
+
if (result.diagnostics.length > 0) {
|
|
22
|
+
const messages = result.diagnostics.map((diagnostic) => diagnostic.description).join("; ");
|
|
23
|
+
throw new Error(`${shownPath(filePath, projectRoot)}: Biome format failed: ${messages}`);
|
|
24
|
+
}
|
|
25
|
+
return result.content === original ? undefined : { path: filePath, content: result.content };
|
|
26
|
+
}
|
|
27
|
+
function shownPath(filePath, projectRoot) {
|
|
28
|
+
return relative(projectRoot, filePath).split(sep).join("/");
|
|
29
|
+
}
|
|
@@ -3,6 +3,8 @@ import { appendFileSync, copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSyn
|
|
|
3
3
|
import { homedir, tmpdir } from "node:os";
|
|
4
4
|
import { basename, extname, join, resolve } from "node:path";
|
|
5
5
|
import { detectGameInstallations, resolveGameInstallation, supportedPlatform } from "../game/installations.js";
|
|
6
|
+
import { formatGeneratedContent } from "../types/format.js";
|
|
7
|
+
import { organizeGeneratedDeclarations } from "../types/organize.js";
|
|
6
8
|
const DEFAULT_TIMEOUT_MILLISECONDS = 15 * 60 * 1_000;
|
|
7
9
|
const POLL_INTERVAL_MILLISECONDS = 500;
|
|
8
10
|
export async function importGameData(options, dependencies = {}) {
|
|
@@ -188,8 +190,12 @@ function installExport(destination, installation, artifacts) {
|
|
|
188
190
|
rmSync(typesDirectory, { recursive: true, force: true });
|
|
189
191
|
mkdirSync(typesDirectory, { recursive: true });
|
|
190
192
|
cpSync(artifacts.typesDirectory, typesDirectory, { recursive: true });
|
|
191
|
-
const declarationPath =
|
|
192
|
-
|
|
193
|
+
const declarationPath = organizeGeneratedDeclarations(typesDirectory);
|
|
194
|
+
const tsconfigPath = join(destination, "tsconfig.kdf.json");
|
|
195
|
+
writeFileSync(tsconfigPath, formatGeneratedContent(JSON.stringify({
|
|
196
|
+
extends: "./DataForge/typescript/kdataforgejs/tsconfig.json",
|
|
197
|
+
include: ["DataForge/**/*.ts"],
|
|
198
|
+
}), tsconfigPath));
|
|
193
199
|
ensureGitIgnoreEntry(destination, ".kdataforge/");
|
|
194
200
|
return { installation, archivePath, declarationPath };
|
|
195
201
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { spanInBytesToSpanInCodeUnits } from "@biomejs/js-api/nodejs";
|
|
4
|
+
import { findScriptFiles } from "../pack/scriptFiles.js";
|
|
5
|
+
import { biome, biomeProjectKey } from "../types/biome.js";
|
|
6
|
+
/** Lints every JavaScript and TypeScript source file in DataForge/scripts with bundled Biome rules. */
|
|
7
|
+
export function lintProject(projectRoot) {
|
|
8
|
+
const absoluteProjectRoot = resolve(projectRoot);
|
|
9
|
+
const scriptsRoot = join(absoluteProjectRoot, "DataForge/scripts");
|
|
10
|
+
if (!existsSync(scriptsRoot)) {
|
|
11
|
+
return [{
|
|
12
|
+
severity: "error",
|
|
13
|
+
code: "lint.no-scripts",
|
|
14
|
+
file: "DataForge/scripts",
|
|
15
|
+
message: "Scripts directory not found. Run 'kdf init' or pass project root.",
|
|
16
|
+
}];
|
|
17
|
+
}
|
|
18
|
+
return findScriptFiles(scriptsRoot).flatMap((filePath) => lintFile(filePath, absoluteProjectRoot));
|
|
19
|
+
}
|
|
20
|
+
function lintFile(filePath, projectRoot) {
|
|
21
|
+
const content = readFileSync(filePath, "utf8");
|
|
22
|
+
return biome.lintContent(biomeProjectKey, content, { filePath }).diagnostics
|
|
23
|
+
.filter((diagnostic) => !diagnostic.category?.startsWith("assist/"))
|
|
24
|
+
.map((diagnostic) => toDiagnostic(diagnostic, content, filePath, projectRoot));
|
|
25
|
+
}
|
|
26
|
+
function toDiagnostic(diagnostic, content, filePath, projectRoot) {
|
|
27
|
+
const location = diagnostic.location.span === undefined ? "" : diagnosticLocation(diagnostic.location.span, content);
|
|
28
|
+
const shownPath = relative(projectRoot, filePath).split(sep).join("/");
|
|
29
|
+
return {
|
|
30
|
+
severity: diagnostic.severity === "error" || diagnostic.severity === "fatal" ? "error" : "warning",
|
|
31
|
+
code: diagnostic.category ?? "biome",
|
|
32
|
+
file: `${shownPath}${location}`,
|
|
33
|
+
message: diagnostic.description,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function diagnosticLocation(span, content) {
|
|
37
|
+
const [start] = spanInBytesToSpanInCodeUnits(span, content);
|
|
38
|
+
const before = content.slice(0, start);
|
|
39
|
+
const line = before.split("\n").length;
|
|
40
|
+
const lastNewline = before.lastIndexOf("\n");
|
|
41
|
+
const column = start - lastNewline;
|
|
42
|
+
return `:${line}:${column}`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { isMap, isSeq, parseDocument, YAMLMap, YAMLSeq } from "yaml";
|
|
4
|
+
import { findPackRoots } from "../pack/discover.js";
|
|
5
|
+
import { sha256Of } from "../pack/imports.js";
|
|
6
|
+
import { discoverPackUsage } from "../pack/usage.js";
|
|
7
|
+
/** Generates manifest imports, digests, and reflection class authority from source usage. */
|
|
8
|
+
export function syncProject(projectRoot) {
|
|
9
|
+
if (!existsSync(projectRoot))
|
|
10
|
+
return { changedPacks: [] };
|
|
11
|
+
const changedPacks = findPackRoots(projectRoot).filter(syncPack);
|
|
12
|
+
return { changedPacks };
|
|
13
|
+
}
|
|
14
|
+
function syncPack(packRoot) {
|
|
15
|
+
const manifestFile = join(packRoot, "pack.yml");
|
|
16
|
+
const before = readFileSync(manifestFile, "utf8");
|
|
17
|
+
const document = parseDocument(before);
|
|
18
|
+
const scripts = document.get("scripts", true);
|
|
19
|
+
if (!isSeq(scripts))
|
|
20
|
+
return false;
|
|
21
|
+
for (const script of scripts.items) {
|
|
22
|
+
if (!isMap(script))
|
|
23
|
+
continue;
|
|
24
|
+
const entry = String(script.get("entry") ?? "");
|
|
25
|
+
if (entry === "")
|
|
26
|
+
continue;
|
|
27
|
+
const usage = discoverPackUsage(packRoot, entry);
|
|
28
|
+
setSequence(script, "reflectionClasses", usage.reflectionClasses);
|
|
29
|
+
setImports(script, packRoot, usage.imports);
|
|
30
|
+
}
|
|
31
|
+
const after = document.toString();
|
|
32
|
+
if (after === before)
|
|
33
|
+
return false;
|
|
34
|
+
writeFileSync(manifestFile, after);
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
function setSequence(map, key, values) {
|
|
38
|
+
if (values.length === 0) {
|
|
39
|
+
map.delete(key);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const sequence = new YAMLSeq();
|
|
43
|
+
sequence.items = [...values];
|
|
44
|
+
map.set(key, sequence);
|
|
45
|
+
}
|
|
46
|
+
function setImports(script, packRoot, imports) {
|
|
47
|
+
if (imports.length === 0) {
|
|
48
|
+
script.delete("imports");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const oldKeys = existingKeys(script);
|
|
52
|
+
const usedKeys = new Set();
|
|
53
|
+
const sequence = new YAMLSeq();
|
|
54
|
+
sequence.items = imports.map((item) => {
|
|
55
|
+
const target = join(packRoot, item.path);
|
|
56
|
+
if (!existsSync(target))
|
|
57
|
+
throw new Error(`Imported file '${item.path}' does not exist in ${packRoot}.`);
|
|
58
|
+
const preferred = oldKeys.get(`${item.kind}\n${item.path}`) ?? keyFrom(item);
|
|
59
|
+
const key = uniqueKey(preferred, usedKeys);
|
|
60
|
+
usedKeys.add(key);
|
|
61
|
+
const map = new YAMLMap();
|
|
62
|
+
map.set("key", key);
|
|
63
|
+
map.set("kind", item.kind);
|
|
64
|
+
map.set("path", item.path);
|
|
65
|
+
map.set("sha256", sha256Of(target));
|
|
66
|
+
return map;
|
|
67
|
+
});
|
|
68
|
+
script.set("imports", sequence);
|
|
69
|
+
}
|
|
70
|
+
function existingKeys(script) {
|
|
71
|
+
const result = new Map();
|
|
72
|
+
const imports = script.get("imports", true);
|
|
73
|
+
if (!isSeq(imports))
|
|
74
|
+
return result;
|
|
75
|
+
for (const item of imports.items) {
|
|
76
|
+
if (!isMap(item))
|
|
77
|
+
continue;
|
|
78
|
+
const kind = String(item.get("kind") ?? "");
|
|
79
|
+
const path = String(item.get("path") ?? "");
|
|
80
|
+
const key = String(item.get("key") ?? "");
|
|
81
|
+
if (kind !== "" && path !== "" && key !== "")
|
|
82
|
+
result.set(`${kind}\n${path}`, key);
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
function keyFrom(item) {
|
|
87
|
+
const normalized = item.path.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
88
|
+
return normalized === "" ? item.kind : normalized;
|
|
89
|
+
}
|
|
90
|
+
function uniqueKey(preferred, used) {
|
|
91
|
+
if (!used.has(preferred))
|
|
92
|
+
return preferred;
|
|
93
|
+
let suffix = 2;
|
|
94
|
+
while (used.has(`${preferred}-${suffix}`))
|
|
95
|
+
suffix += 1;
|
|
96
|
+
return `${preferred}-${suffix}`;
|
|
97
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { readdirSync } from "node:fs";
|
|
2
|
+
import { extname, join } from "node:path";
|
|
3
|
+
const SCRIPT_EXTENSIONS = new Set([".js", ".ts"]);
|
|
4
|
+
export function findScriptFiles(directory) {
|
|
5
|
+
const files = [];
|
|
6
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
7
|
+
const path = join(directory, entry.name);
|
|
8
|
+
if (entry.isDirectory())
|
|
9
|
+
files.push(...findScriptFiles(path));
|
|
10
|
+
else if (entry.isFile() && SCRIPT_EXTENSIONS.has(extname(entry.name).toLocaleLowerCase()))
|
|
11
|
+
files.push(path);
|
|
12
|
+
}
|
|
13
|
+
return files.sort((left, right) => left.localeCompare(right));
|
|
14
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, relative, resolve, sep } from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { parse } from "yaml";
|
|
5
|
+
import { RUNTIME_MODULE, SATISFACTORY_MODULE } from "../conventions.js";
|
|
6
|
+
const REFLECTION_METHODS = new Set(["getClass", "getObject", "getDataAsset"]);
|
|
7
|
+
/** Discovers manifest authority inputs from reachable TypeScript and imported YAML without executing pack code. */
|
|
8
|
+
export function discoverPackUsage(packRoot, entry) {
|
|
9
|
+
const entryFile = resolve(packRoot, entry);
|
|
10
|
+
const scriptsRoot = dirname(entryFile);
|
|
11
|
+
const queue = [entryFile];
|
|
12
|
+
const visited = new Set();
|
|
13
|
+
const imports = new Map();
|
|
14
|
+
const reflectionClasses = new Set();
|
|
15
|
+
while (queue.length > 0) {
|
|
16
|
+
const file = queue.pop();
|
|
17
|
+
if (visited.has(file))
|
|
18
|
+
continue;
|
|
19
|
+
visited.add(file);
|
|
20
|
+
if (!existsSync(file))
|
|
21
|
+
continue;
|
|
22
|
+
const source = ts.createSourceFile(file, readFileSync(file, "utf8"), ts.ScriptTarget.ES2020, true);
|
|
23
|
+
visit(source, (node) => {
|
|
24
|
+
const specifier = staticSpecifier(node);
|
|
25
|
+
if (specifier !== undefined) {
|
|
26
|
+
const target = resolveModule(scriptsRoot, file, specifier);
|
|
27
|
+
if (target !== undefined)
|
|
28
|
+
queue.push(target);
|
|
29
|
+
}
|
|
30
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
|
|
31
|
+
return;
|
|
32
|
+
const method = node.expression.name.text;
|
|
33
|
+
if (method === "import") {
|
|
34
|
+
const kind = stringLiteral(node.arguments[0]);
|
|
35
|
+
const path = stringLiteral(node.arguments[1]);
|
|
36
|
+
if (kind === undefined || path === undefined) {
|
|
37
|
+
throw new Error(`${shownPath(packRoot, file)}: ctx.import requires literal kind and path so CLI can secure manifest imports.`);
|
|
38
|
+
}
|
|
39
|
+
imports.set(`${kind}\n${path}`, { kind, path });
|
|
40
|
+
}
|
|
41
|
+
else if (REFLECTION_METHODS.has(method)) {
|
|
42
|
+
const path = stringLiteral(node.arguments[1]);
|
|
43
|
+
if (path !== undefined)
|
|
44
|
+
reflectionClasses.add(path);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
for (const item of imports.values()) {
|
|
49
|
+
if (item.kind !== "yaml")
|
|
50
|
+
continue;
|
|
51
|
+
const target = resolveInside(packRoot, item.path);
|
|
52
|
+
if (target === undefined || !existsSync(target))
|
|
53
|
+
continue;
|
|
54
|
+
collectYamlTargets(parse(readFileSync(target, "utf8")), reflectionClasses);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
imports: [...imports.values()].sort((left, right) => left.path.localeCompare(right.path) || left.kind.localeCompare(right.kind)),
|
|
58
|
+
reflectionClasses: [...reflectionClasses].sort((left, right) => left.localeCompare(right)),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function visit(node, callback) {
|
|
62
|
+
callback(node);
|
|
63
|
+
ts.forEachChild(node, (child) => visit(child, callback));
|
|
64
|
+
}
|
|
65
|
+
function staticSpecifier(node) {
|
|
66
|
+
if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && node.moduleSpecifier !== undefined &&
|
|
67
|
+
ts.isStringLiteral(node.moduleSpecifier))
|
|
68
|
+
return node.moduleSpecifier.text;
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
function resolveModule(scriptsRoot, importer, specifier) {
|
|
72
|
+
if (specifier === RUNTIME_MODULE || specifier === SATISFACTORY_MODULE ||
|
|
73
|
+
(!specifier.startsWith("./") && !specifier.startsWith("../")))
|
|
74
|
+
return undefined;
|
|
75
|
+
const extension = extname(specifier);
|
|
76
|
+
const target = resolve(dirname(importer), extension === "" ? `${specifier}.ts` : specifier);
|
|
77
|
+
return relative(scriptsRoot, target).startsWith("..") ? undefined : target;
|
|
78
|
+
}
|
|
79
|
+
function stringLiteral(node) {
|
|
80
|
+
return node !== undefined && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : undefined;
|
|
81
|
+
}
|
|
82
|
+
function resolveInside(packRoot, path) {
|
|
83
|
+
const target = resolve(packRoot, path);
|
|
84
|
+
return relative(packRoot, target).startsWith("..") ? undefined : target;
|
|
85
|
+
}
|
|
86
|
+
function collectYamlTargets(value, targets) {
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
for (const item of value)
|
|
89
|
+
collectYamlTargets(item, targets);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (typeof value !== "object" || value === null)
|
|
93
|
+
return;
|
|
94
|
+
for (const [key, child] of Object.entries(value)) {
|
|
95
|
+
if (key === "target" && typeof child === "string" && child.startsWith("/"))
|
|
96
|
+
targets.add(child);
|
|
97
|
+
collectYamlTargets(child, targets);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function shownPath(packRoot, file) {
|
|
101
|
+
return relative(packRoot, file).split(sep).join("/");
|
|
102
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Biome } from "@biomejs/js-api/nodejs";
|
|
2
|
+
export const biome = new Biome();
|
|
3
|
+
export const { projectKey: biomeProjectKey } = biome.openProject();
|
|
4
|
+
biome.applyConfiguration(biomeProjectKey, {
|
|
5
|
+
assist: {
|
|
6
|
+
enabled: false,
|
|
7
|
+
},
|
|
8
|
+
formatter: {
|
|
9
|
+
indentStyle: "tab",
|
|
10
|
+
lineEnding: "lf",
|
|
11
|
+
lineWidth: 120,
|
|
12
|
+
trailingNewline: true,
|
|
13
|
+
},
|
|
14
|
+
linter: {
|
|
15
|
+
enabled: true,
|
|
16
|
+
rules: {
|
|
17
|
+
recommended: true,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
json: {
|
|
21
|
+
formatter: {
|
|
22
|
+
expand: "always",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { biome, biomeProjectKey } from "./biome.js";
|
|
2
|
+
/** Formats generated TypeScript and JSON through Biome's in-process JavaScript API. */
|
|
3
|
+
export function formatGeneratedContent(content, filePath) {
|
|
4
|
+
const formatted = biome.formatContent(biomeProjectKey, content, { filePath });
|
|
5
|
+
if (formatted.diagnostics.length > 0) {
|
|
6
|
+
throw new Error(`Biome could not format generated file '${filePath}'.`);
|
|
7
|
+
}
|
|
8
|
+
return formatted.content;
|
|
9
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, sep } from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { formatGeneratedContent } from "./format.js";
|
|
5
|
+
const RUNTIME_MODULE = "@sf-kmods/kdataforgejs";
|
|
6
|
+
const SATISFACTORY_MODULE = "satisfactory";
|
|
7
|
+
const GENERATED_FILE = "kdataforge.generated.d.ts";
|
|
8
|
+
/** Splits exported ambient declarations into deterministic, navigable files and returns the entry declaration. */
|
|
9
|
+
export function organizeGeneratedDeclarations(typesDirectory) {
|
|
10
|
+
const generatedPath = join(typesDirectory, GENERATED_FILE);
|
|
11
|
+
const source = ts.createSourceFile(generatedPath, readFileSync(generatedPath, "utf8"), ts.ScriptTarget.ES2020, true, ts.ScriptKind.TS);
|
|
12
|
+
removePreviousOrganization(typesDirectory);
|
|
13
|
+
const references = [];
|
|
14
|
+
const runtimeDeclarations = [];
|
|
15
|
+
const globalStatements = [];
|
|
16
|
+
for (const statement of source.statements) {
|
|
17
|
+
if (!ts.isModuleDeclaration(statement)) {
|
|
18
|
+
globalStatements.push(statement);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const moduleName = stringModuleName(statement);
|
|
22
|
+
if (moduleName === SATISFACTORY_MODULE) {
|
|
23
|
+
references.push(...writeSatisfactoryGroups(typesDirectory, statement, source));
|
|
24
|
+
}
|
|
25
|
+
else if (moduleName === RUNTIME_MODULE && containsNativeClassPathMap(statement)) {
|
|
26
|
+
references.push(...writeClassPathGroups(typesDirectory, statement, source));
|
|
27
|
+
const remainingStatements = moduleStatements(statement)
|
|
28
|
+
.filter((node) => !isNativeClassPathMap(node))
|
|
29
|
+
.map((node) => node.getText(source));
|
|
30
|
+
if (remainingStatements.length > 0) {
|
|
31
|
+
runtimeDeclarations.push(ambientModule(RUNTIME_MODULE, remainingStatements));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
runtimeDeclarations.push(statement.getText(source));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (runtimeDeclarations.length > 0) {
|
|
39
|
+
writeDeclaration(typesDirectory, "runtime.d.ts", runtimeDeclarations.join("\n\n"));
|
|
40
|
+
references.push("runtime.d.ts");
|
|
41
|
+
}
|
|
42
|
+
if (globalStatements.length > 0) {
|
|
43
|
+
writeDeclaration(typesDirectory, "globals.d.ts", globalStatements.map((node) => node.getText(source)).join("\n"));
|
|
44
|
+
references.push("globals.d.ts");
|
|
45
|
+
}
|
|
46
|
+
references.sort();
|
|
47
|
+
const moduleAnchors = `declare module "${RUNTIME_MODULE}" {}\ndeclare module "${SATISFACTORY_MODULE}" {}`;
|
|
48
|
+
writeDeclaration(typesDirectory, "index.d.ts", `${references.map((path) => `/// <reference path="./${path}" />`).join("\n")}\n\n${moduleAnchors}`);
|
|
49
|
+
updateGeneratedConfigs(typesDirectory);
|
|
50
|
+
rmSync(generatedPath);
|
|
51
|
+
return join(typesDirectory, "index.d.ts");
|
|
52
|
+
}
|
|
53
|
+
function removePreviousOrganization(typesDirectory) {
|
|
54
|
+
for (const path of ["globals.d.ts", "index.d.ts", "runtime.d.ts", "paths", "satisfactory"]) {
|
|
55
|
+
rmSync(join(typesDirectory, path), { force: true, recursive: true });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function writeSatisfactoryGroups(typesDirectory, declaration, source) {
|
|
59
|
+
const statements = moduleStatements(declaration);
|
|
60
|
+
const imports = statements.filter(ts.isImportDeclaration).map((node) => node.getText(source));
|
|
61
|
+
const groups = new Map();
|
|
62
|
+
for (const statement of statements) {
|
|
63
|
+
if (ts.isImportDeclaration(statement))
|
|
64
|
+
continue;
|
|
65
|
+
const key = satisfactoryGroup(statementName(statement));
|
|
66
|
+
const entries = groups.get(key) ?? [];
|
|
67
|
+
entries.push(statement.getText(source));
|
|
68
|
+
groups.set(key, entries);
|
|
69
|
+
}
|
|
70
|
+
return [...groups.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([key, entries]) => {
|
|
71
|
+
const path = `satisfactory/${key}.d.ts`;
|
|
72
|
+
writeDeclaration(typesDirectory, path, ambientModule(SATISFACTORY_MODULE, [...imports, ...entries]));
|
|
73
|
+
return path;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function writeClassPathGroups(typesDirectory, declaration, source) {
|
|
77
|
+
const pathMap = moduleStatements(declaration).find((statement) => ts.isInterfaceDeclaration(statement) && statement.name.text === "NativeClassPathMap");
|
|
78
|
+
if (pathMap === undefined)
|
|
79
|
+
return [];
|
|
80
|
+
const groups = new Map();
|
|
81
|
+
for (const member of pathMap.members) {
|
|
82
|
+
const key = classPathGroup(propertyPath(member));
|
|
83
|
+
const entries = groups.get(key) ?? [];
|
|
84
|
+
entries.push(member);
|
|
85
|
+
groups.set(key, entries);
|
|
86
|
+
}
|
|
87
|
+
return [...groups.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([key, members]) => {
|
|
88
|
+
const body = members.map((member) => qualifiedTypeReference(member.getText(source))).join("\n");
|
|
89
|
+
const path = `paths/${key}.d.ts`;
|
|
90
|
+
writeDeclaration(typesDirectory, path, `import type * as Satisfactory from "satisfactory";\n\ndeclare module "${RUNTIME_MODULE}" {\n interface NativeClassPathMap {\n${indent(body, 4)}\n }\n}`);
|
|
91
|
+
return path;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function updateGeneratedConfigs(typesDirectory) {
|
|
95
|
+
updateJson(join(typesDirectory, "tsconfig.json"), (value) => {
|
|
96
|
+
const compilerOptions = recordOrEmpty(value.compilerOptions);
|
|
97
|
+
value.compilerOptions = compilerOptions;
|
|
98
|
+
compilerOptions.paths = { [RUNTIME_MODULE]: ["index.d.ts"], [SATISFACTORY_MODULE]: ["index.d.ts"] };
|
|
99
|
+
});
|
|
100
|
+
updateJsonIfPresent(join(typesDirectory, "paths.json"), (value) => {
|
|
101
|
+
value.paths = { [RUNTIME_MODULE]: "index.d.ts", [SATISFACTORY_MODULE]: "index.d.ts" };
|
|
102
|
+
});
|
|
103
|
+
updateJsonIfPresent(join(typesDirectory, "editor.json"), (value) => {
|
|
104
|
+
const typescript = recordOrEmpty(value.typescript);
|
|
105
|
+
value.typescript = typescript;
|
|
106
|
+
typescript.declaration = "index.d.ts";
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function updateJsonIfPresent(path, update) {
|
|
110
|
+
if (existsSync(path))
|
|
111
|
+
updateJson(path, update);
|
|
112
|
+
}
|
|
113
|
+
function updateJson(path, update) {
|
|
114
|
+
const value = record(JSON.parse(readFileSync(path, "utf8")));
|
|
115
|
+
update(value);
|
|
116
|
+
writeFileSync(path, formatGeneratedContent(JSON.stringify(value), path));
|
|
117
|
+
}
|
|
118
|
+
function record(value) {
|
|
119
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
120
|
+
throw new Error("Generated config must be an object.");
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
function recordOrEmpty(value) {
|
|
124
|
+
return value === undefined ? {} : record(value);
|
|
125
|
+
}
|
|
126
|
+
function moduleStatements(declaration) {
|
|
127
|
+
return declaration.body !== undefined && ts.isModuleBlock(declaration.body) ? declaration.body.statements : [];
|
|
128
|
+
}
|
|
129
|
+
function stringModuleName(declaration) {
|
|
130
|
+
return ts.isStringLiteral(declaration.name) ? declaration.name.text : undefined;
|
|
131
|
+
}
|
|
132
|
+
function containsNativeClassPathMap(declaration) {
|
|
133
|
+
return moduleStatements(declaration).some(isNativeClassPathMap);
|
|
134
|
+
}
|
|
135
|
+
function isNativeClassPathMap(statement) {
|
|
136
|
+
return ts.isInterfaceDeclaration(statement) && statement.name.text === "NativeClassPathMap";
|
|
137
|
+
}
|
|
138
|
+
function statementName(statement) {
|
|
139
|
+
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement) ||
|
|
140
|
+
ts.isClassDeclaration(statement) || ts.isFunctionDeclaration(statement))
|
|
141
|
+
return statement.name?.text ?? "misc";
|
|
142
|
+
if (ts.isVariableStatement(statement))
|
|
143
|
+
return statement.declarationList.declarations[0]?.name.getText() ?? "misc";
|
|
144
|
+
return "misc";
|
|
145
|
+
}
|
|
146
|
+
function satisfactoryGroup(name) {
|
|
147
|
+
if (name.startsWith("KdfEnumBrand"))
|
|
148
|
+
return "e";
|
|
149
|
+
const first = name[0]?.toLocaleLowerCase();
|
|
150
|
+
return first !== undefined && /^[a-z0-9]$/u.test(first) ? first : "misc";
|
|
151
|
+
}
|
|
152
|
+
function propertyPath(member) {
|
|
153
|
+
if (!ts.isPropertySignature(member) || member.name === undefined)
|
|
154
|
+
return "misc";
|
|
155
|
+
return ts.isStringLiteral(member.name) ? member.name.text : member.name.getText();
|
|
156
|
+
}
|
|
157
|
+
function classPathGroup(path) {
|
|
158
|
+
const segments = path.split("/").filter(Boolean);
|
|
159
|
+
if (segments[0]?.toLocaleLowerCase() === "script") {
|
|
160
|
+
return slug(["script", segments[1]?.split(".")[0] ?? "misc"]);
|
|
161
|
+
}
|
|
162
|
+
const count = Math.max(1, Math.min(3, segments.length - 1));
|
|
163
|
+
return slug(segments.slice(0, count));
|
|
164
|
+
}
|
|
165
|
+
function slug(parts) {
|
|
166
|
+
const value = parts.join("-").toLocaleLowerCase().replace(/[^a-z0-9]+/gu, "-").replace(/^-|-$/gu, "");
|
|
167
|
+
return value === "" ? "misc" : value;
|
|
168
|
+
}
|
|
169
|
+
function qualifiedTypeReference(text) {
|
|
170
|
+
return text.replace(/import\(["']satisfactory["']\)\.([A-Za-z_$][\w$]*)/gu, "Satisfactory.$1");
|
|
171
|
+
}
|
|
172
|
+
function ambientModule(name, statements) {
|
|
173
|
+
return `declare module "${name}" {\n${indent(statements.join("\n"), 2)}\n}`;
|
|
174
|
+
}
|
|
175
|
+
function indent(value, spaces) {
|
|
176
|
+
const prefix = " ".repeat(spaces);
|
|
177
|
+
return value.split("\n").map((line) => line.length === 0 ? line : `${prefix}${line}`).join("\n");
|
|
178
|
+
}
|
|
179
|
+
function writeDeclaration(root, path, body) {
|
|
180
|
+
const target = join(root, path.split("/").join(sep));
|
|
181
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
182
|
+
const content = `// Generated by KDataForgeJS. Do not edit.\n${body.trim()}\n`;
|
|
183
|
+
writeFileSync(target, formatGeneratedContent(content, target));
|
|
184
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sf-kmods/kdataforgejs-cli",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2026.4.19",
|
|
4
4
|
"description": "kdf - create, type-check and validate KDataForge projects with KDataForgeJS (TypeScript) scripts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Kyri123/Kyrium",
|
|
@@ -34,9 +34,11 @@
|
|
|
34
34
|
"node": ">=20"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@biomejs/js-api": "6.1.0",
|
|
38
|
+
"@biomejs/wasm-nodejs": "2.6.3",
|
|
37
39
|
"typescript": ">=5.7.3 <6",
|
|
38
40
|
"yaml": "^2.6.1",
|
|
39
|
-
"@sf-kmods/kdataforgejs": "^
|
|
41
|
+
"@sf-kmods/kdataforgejs": "^2026.4.19"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
44
|
"@types/node": "^22.10.0"
|
|
@@ -2,6 +2,6 @@ import { defineHandler } from "@sf-kmods/kdataforgejs";
|
|
|
2
2
|
import { describeCatalog } from "./lib";
|
|
3
3
|
|
|
4
4
|
export default defineHandler(async (ctx) => {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
console.info(describeCatalog(ctx.catalog().digest));
|
|
6
|
+
await Promise.resolve();
|
|
7
7
|
});
|
|
@@ -5,13 +5,19 @@ KDataForge project with a KDataForgeJS (TypeScript) script, created with `kdf in
|
|
|
5
5
|
| Path | Purpose |
|
|
6
6
|
| --- | --- |
|
|
7
7
|
| `DataForge/` | The pack. Copy it to `FactoryGame/DataForge/<name>/` or ship it with your mod. |
|
|
8
|
-
| `DataForge/pack.yml` | Pack manifest
|
|
9
|
-
| `DataForge/scripts/main.ts` | Canonical entry
|
|
8
|
+
| `DataForge/pack.yml` | Pack manifest with script entry, optional realm policy, imports, and reflected classes. |
|
|
9
|
+
| `DataForge/scripts/main.ts` | Canonical entry with default `defineHandler` and optional capability `meta`. |
|
|
10
10
|
| `package.json`, `tsconfig.json` | Editor tooling only. Never put them inside `DataForge/`. |
|
|
11
11
|
|
|
12
12
|
```sh
|
|
13
13
|
npm install
|
|
14
|
+
npm run refresh # launch game and refresh archive plus generated game types
|
|
15
|
+
npm run sync # generate imports, hashes and reflection classes from source usage
|
|
14
16
|
npm run check # type-check against @sf-kmods/kdataforgejs
|
|
17
|
+
npm run format # format scripts with bundled Biome
|
|
18
|
+
npm run lint # lint scripts with bundled Biome rules
|
|
15
19
|
npm run validate # pack.yml, entry shape, static imports, import digests
|
|
16
20
|
npm run hash # refresh sha256 of declared imports after editing them
|
|
21
|
+
npm run fix # format scripts and synchronize generated manifest fields
|
|
22
|
+
npm run verify # sync, type-check, lint and validate
|
|
17
23
|
```
|
|
@@ -4,10 +4,16 @@
|
|
|
4
4
|
"version": "0.1.0",
|
|
5
5
|
"description": "KDataForge project with KDataForgeJS (TypeScript) scripts. The DataForge folder is the pack; this file is editor tooling only.",
|
|
6
6
|
"scripts": {
|
|
7
|
+
"refresh": "kdf game-data",
|
|
8
|
+
"sync": "kdf sync",
|
|
7
9
|
"check": "kdf check",
|
|
10
|
+
"format": "kdf format",
|
|
11
|
+
"lint": "kdf lint",
|
|
8
12
|
"validate": "kdf validate",
|
|
9
13
|
"hash": "kdf hash",
|
|
10
|
-
"
|
|
14
|
+
"fix": "kdf format && kdf sync",
|
|
15
|
+
"test": "npm run verify",
|
|
16
|
+
"verify": "kdf sync && kdf check && kdf lint && kdf validate"
|
|
11
17
|
},
|
|
12
18
|
"devDependencies": {
|
|
13
19
|
"@sf-kmods/kdataforgejs": "^{{sdkVersion}}",
|