rbxts-transformer-commit-migrations 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 fome
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/init.js ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { readFileSync, writeFileSync, existsSync } from "fs";
4
+ import { resolve } from "path";
5
+
6
+ // find package.json
7
+ let dir = process.cwd();
8
+ while (!existsSync(resolve(dir, "package.json"))) dir = resolve(dir, "..");
9
+
10
+ const huskyDir = resolve(dir, ".husky");
11
+ const hookFile = resolve(huskyDir, "pre-commit");
12
+
13
+ if (!existsSync(huskyDir)) {
14
+ console.error("No .husky folder found. Run `npx husky init` first.");
15
+ process.exit(1);
16
+ }
17
+
18
+ const setupResolve = `npx resolve-commit-migrations\n`;
19
+
20
+ if (existsSync(hookFile)) {
21
+ const existing = readFileSync(hookFile, "utf-8");
22
+ if (!existing.includes(setupResolve)) {
23
+ writeFileSync(hookFile, existing + setupResolve);
24
+ }
25
+ } else {
26
+ writeFileSync(hookFile, `#!/bin/sh\n${setupResolve}`);
27
+ }
28
+
29
+ console.log("Added rbxts-macro stamp to .husky/pre-commit");
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare function $migrate(order: number): never;
2
+ declare function $migrate(order: "__resolved"): number;
3
+ declare function $migrate(order: number | "__resolved"): number?;
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env node
2
+ import ts from "typescript";
3
+ import { readFileSync, writeFileSync, existsSync } from "fs";
4
+ import { glob } from "glob";
5
+ import loadMigrationEntries from "../util/loadMigrationEntries.js";
6
+ import { CONFIG, MACRO, MACRO_CALL_REGEX, MACRO_REGEX } from "../config.js";
7
+ function findEntryName(node) {
8
+ let current = node.parent;
9
+ while (current) {
10
+ if (ts.isPropertyAssignment(current) || ts.isShorthandPropertyAssignment(current)) {
11
+ const name = ts.isIdentifier(current.name) ? current.name.text : null;
12
+ return name;
13
+ }
14
+ if (ts.isCallExpression(current) || ts.isPropertyAccessExpression(current)) {
15
+ current = current.parent;
16
+ }
17
+ else {
18
+ break;
19
+ }
20
+ }
21
+ return null;
22
+ }
23
+ const files = glob.sync("src/**/*.ts");
24
+ const migrations = [];
25
+ const migrationData = {};
26
+ const usedOrders = new Set();
27
+ function walk(node, ctx) {
28
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && MACRO_REGEX.test(node.expression.text)) {
29
+ const entry = findEntryName(node);
30
+ const orderArg = node.arguments[0];
31
+ const order = orderArg ? ts.isNumericLiteral(orderArg) ? parseInt(orderArg.getText()) : null : 0;
32
+ if (order === null) {
33
+ console.error("Recieved null order when resolving migration macros.");
34
+ process.exit(1);
35
+ }
36
+ if (usedOrders.has(order)) {
37
+ console.error("Multiple of the same migration orders found. Every migration has to have a unique order.");
38
+ process.exit(1);
39
+ }
40
+ if (!entry) {
41
+ console.error("Invalid migration entry, maybe you called a $migrate outside a table?");
42
+ process.exit(1);
43
+ }
44
+ if (!orderArg?.getText().includes("__resolved")) {
45
+ usedOrders.add(order);
46
+ migrationData[entry] = { timestamp: Date.now(), order };
47
+ ctx.changed = true;
48
+ }
49
+ migrations.push(entry);
50
+ }
51
+ ts.forEachChild(node, (node) => {
52
+ walk(node, ctx);
53
+ });
54
+ }
55
+ for (const file of files) {
56
+ const src = readFileSync(file, "utf-8");
57
+ const sourceFile = ts.createSourceFile(file, src, ts.ScriptTarget.Latest, true);
58
+ const ctx = { changed: false };
59
+ walk(sourceFile, ctx);
60
+ if (ctx.changed) {
61
+ writeFileSync(file, src.replaceAll(MACRO_CALL_REGEX, `${MACRO}("__resolved")`));
62
+ }
63
+ }
64
+ const [previousMigrations] = loadMigrationEntries(CONFIG);
65
+ const removedMigrations = previousMigrations.filter((v) => !migrations.includes(v));
66
+ if (removedMigrations.length > 0) {
67
+ console.error(`Removed migrations: ${removedMigrations}. Migrations should never be removed!`);
68
+ process.exit(1);
69
+ }
70
+ const newMigrations = migrations.filter((v) => !previousMigrations.includes(v));
71
+ if (newMigrations.length === 0)
72
+ process.exit(0);
73
+ const newLines = newMigrations
74
+ .map((e) => `\t${e}: { timestamp: ${migrationData[e].timestamp}, order: ${migrationData[e].order} },`)
75
+ .join("\n");
76
+ const migratedEntries = migrations.map((e) => `"${e}"`).join(" | ");
77
+ if (!existsSync(CONFIG)) {
78
+ writeFileSync(CONFIG, `type MigratedEntries = ${migratedEntries}\n\ninterface MigrationData {\n\ttimestamp: number\n\torder: number\n}\n\n// AUTO-GENERATED MIGRATION CONFIG - DO NOT EDIT\nexport const MIGRATED_ENTRIES: Record<MigratedEntries, MigrationData> = {\n${newLines}\n};\n`);
79
+ }
80
+ else {
81
+ const lines = readFileSync(CONFIG, "utf-8").split("\n");
82
+ const typeIndex = lines.findIndex((l) => l.startsWith("type MigratedEntries"));
83
+ if (typeIndex !== -1) {
84
+ lines[typeIndex] = `type MigratedEntries = ${migratedEntries}`;
85
+ }
86
+ const updated = lines.join("\n").replace(/\};\s*$/, `${newLines}\n};`);
87
+ writeFileSync(CONFIG, updated);
88
+ }
89
+ console.log(`Successfuly resolved ${newMigrations.length} new migrations. [${newMigrations}]`);
package/out/config.js ADDED
@@ -0,0 +1,7 @@
1
+ function escapeRegExp(str) {
2
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3
+ }
4
+ export const CONFIG = "migrations.config.ts";
5
+ export const MACRO = "$migrate";
6
+ export const MACRO_REGEX = new RegExp(escapeRegExp(MACRO));
7
+ export const MACRO_CALL_REGEX = new RegExp(escapeRegExp(MACRO) + "\\([^)]*\\)", "g");
package/out/index.js ADDED
@@ -0,0 +1,28 @@
1
+ import ts from "typescript";
2
+ import { findMacroCall, replaceMacroCall } from "./macro.js";
3
+ import loadMigrationEntries from "./util/loadMigrationEntries.js";
4
+ import { CONFIG } from "./config.js";
5
+ export default function (_) {
6
+ return (ctx) => {
7
+ return (file) => {
8
+ const [entryArr, entries] = loadMigrationEntries(CONFIG);
9
+ const orderedEntries = entryArr
10
+ .map((i) => ({ i, timestamp: entries[i].timestamp, order: entries[i].order }))
11
+ .toSorted((a, b) => a.timestamp - b.timestamp || a.order - b.order)
12
+ .map(({ i }) => i);
13
+ console.log(orderedEntries);
14
+ function visit(node) {
15
+ if (!ts.isPropertyAssignment(node) || !findMacroCall(node.initializer))
16
+ return ts.visitEachChild(node, visit, ctx);
17
+ const entry = node.name.getText(file);
18
+ const replacement = orderedEntries.indexOf(entry);
19
+ if (replacement !== -1) {
20
+ const newInit = replaceMacroCall(node.initializer, replacement);
21
+ return ts.factory.createPropertyAssignment(node.name, newInit);
22
+ }
23
+ return ts.visitEachChild(node, visit, ctx);
24
+ }
25
+ return ts.visitNode(file, visit);
26
+ };
27
+ };
28
+ }
package/out/macro.js ADDED
@@ -0,0 +1,17 @@
1
+ import ts from "typescript";
2
+ import { MACRO_REGEX } from "./config.js";
3
+ export function findMacroCall(node) {
4
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && MACRO_REGEX.test(node.expression.text)) {
5
+ return node;
6
+ }
7
+ let found;
8
+ ts.forEachChild(node, (child) => { if (found)
9
+ return; found = findMacroCall(child); });
10
+ return found;
11
+ }
12
+ export function replaceMacroCall(node, replacement) {
13
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && MACRO_REGEX.test(node.expression.text)) {
14
+ return ts.factory.createNumericLiteral(replacement);
15
+ }
16
+ return ts.visitEachChild(node, (child) => replaceMacroCall(child, replacement), undefined);
17
+ }
@@ -0,0 +1,38 @@
1
+ import ts from "typescript";
2
+ import fs from "fs";
3
+ import path from "path";
4
+ const VAR_NAME = "MIGRATED_ENTRIES";
5
+ function reconstructProperties(properties) {
6
+ const obj = {};
7
+ for (const prop of properties) {
8
+ if (!ts.isPropertyAssignment(prop) || !ts.isNumericLiteral(prop.initializer))
9
+ continue;
10
+ const name = ts.isIdentifier(prop.name) ? prop.name.text : prop.name.getText();
11
+ obj[name] = Number(prop.initializer.text);
12
+ }
13
+ return obj;
14
+ }
15
+ export default function (config) {
16
+ const configPath = path.join(process.cwd(), config);
17
+ if (!fs.existsSync(configPath))
18
+ return [[], {}];
19
+ const sourceFile = ts.createSourceFile(config, fs.readFileSync(configPath, "utf-8"), ts.ScriptTarget.Latest);
20
+ for (const statement of sourceFile.statements) {
21
+ if (!ts.isVariableStatement(statement) || !statement.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword))
22
+ continue;
23
+ const declaration = statement.declarationList.declarations[0];
24
+ if (!ts.isIdentifier(declaration.name) || declaration.name.text !== VAR_NAME || !declaration.initializer || !ts.isObjectLiteralExpression(declaration.initializer))
25
+ continue;
26
+ const result = [];
27
+ const object = {};
28
+ for (const prop of declaration.initializer.properties) {
29
+ if (!ts.isPropertyAssignment(prop) || !ts.isIdentifier(prop.name) || !ts.isObjectLiteralExpression(prop.initializer))
30
+ continue;
31
+ result.push(prop.name.text);
32
+ object[prop.name.text] = reconstructProperties(prop.initializer.properties);
33
+ console.log(reconstructProperties(prop.initializer.properties));
34
+ }
35
+ return [result, object];
36
+ }
37
+ return [[], {}];
38
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "rbxts-transformer-commit-migrations",
3
+ "version": "1.0.0",
4
+ "description": "rbxts transformer used in user data migration builder chains",
5
+ "main": "out/index.js",
6
+ "typegis": "index.d.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "build": "tsc"
10
+ },
11
+ "bin": {
12
+ "commit-migrations": "bin/init.js",
13
+ "resolve-commit-migrations": "out/cli/resolveMacros.js"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/fomecrazy/rbxts-transformer-commit-migrations.git"
18
+ },
19
+ "keywords": [
20
+ "transformer"
21
+ ],
22
+ "author": "fome",
23
+ "license": "MIT",
24
+ "homepage": "https://github.com/fomecrazy/rbxts-transformer-commit-migrations#readme",
25
+ "bugs": {
26
+ "url": "https://github.com/fomecrazy/rbxts-transformer-commit-migrations/issues"
27
+ },
28
+ "peerDependencies": {
29
+ "husky": "*",
30
+ "jiti": "*",
31
+ "typescript": "*"
32
+ },
33
+ "files": [
34
+ "out",
35
+ "index.d.ts"
36
+ ],
37
+ "devDependencies": {
38
+ "@types/node": "^26.5.1",
39
+ "eslint": "^10.10.0",
40
+ "eslint-config-prettier": "^10.1.8",
41
+ "eslint-plugin-prettier": "^5.5.6",
42
+ "prettier": "^3.9.6",
43
+ "tsx": "^4.23.13",
44
+ "typescript-eslint": "^8.70.0"
45
+ },
46
+ "dependencies": {
47
+ "glob": "^13.0.6",
48
+ "typescript": "^5.5.3"
49
+ }
50
+ }