rbxts-transformer-commit-migrations 1.2.5 → 1.3.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/index.d.ts +5 -3
- package/out/cli/resolveMacros.js +14 -8
- package/out/config.js +6 -3
- package/out/test.js +20 -0
- package/out/util/loadMigrationEntries.js +8 -2
- package/out/util/macro.js +1 -1
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
|
|
1
|
+
export type MigrateOrder = number & { readonly __migrationKind: "migrate" };
|
|
2
|
+
export type TransformOrder = number & { readonly __migrationKind: "transform" };
|
|
3
|
+
|
|
4
|
+
export declare function $migrate(order: number | "__resolved"): MigrateOrder;
|
|
5
|
+
export declare function $transform(order: number | "__resolved"): TransformOrder;
|
package/out/cli/resolveMacros.js
CHANGED
|
@@ -3,7 +3,7 @@ import ts from "typescript";
|
|
|
3
3
|
import { readFileSync, writeFileSync, existsSync } from "fs";
|
|
4
4
|
import { glob } from "glob";
|
|
5
5
|
import loadMigrationEntries from "../util/loadMigrationEntries.js";
|
|
6
|
-
import { CONFIG,
|
|
6
|
+
import { CONFIG, MACRO_CALL_REGEX, MACRO_REGEX, TRANSFORM_MACRO } from "../config.js";
|
|
7
7
|
function getConfigPathFromTransformerConfig(tsconfigPath = "tsconfig.json") {
|
|
8
8
|
const raw = readFileSync(tsconfigPath, "utf-8");
|
|
9
9
|
if (!raw)
|
|
@@ -56,22 +56,23 @@ function walk(node, ctx) {
|
|
|
56
56
|
const entry = path.join("/");
|
|
57
57
|
const orderArg = node.arguments[0];
|
|
58
58
|
const order = orderArg ? ts.isNumericLiteral(orderArg) ? parseInt(orderArg.getText()) : null : 0;
|
|
59
|
+
const isTransform = node.expression.text === TRANSFORM_MACRO;
|
|
59
60
|
if (order !== null && usedOrders.has(order)) {
|
|
60
61
|
console.error("Multiple of the same migration orders found. Every migration has to have a unique order.");
|
|
61
62
|
process.exit(1);
|
|
62
63
|
}
|
|
63
64
|
if (!entry) {
|
|
64
|
-
console.error(
|
|
65
|
+
console.error(`Invalid migration entry, maybe you called a ${node.expression.text} outside a table?`);
|
|
65
66
|
process.exit(1);
|
|
66
67
|
}
|
|
67
68
|
if (!orderArg?.getText().includes("__resolved")) {
|
|
68
69
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
69
70
|
usedOrders.add(order);
|
|
70
71
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
71
|
-
migrationData[entry] = { timestamp: Date.now(), order: order, path };
|
|
72
|
+
migrationData[entry] = { timestamp: Date.now(), order: order, path, isTransform };
|
|
72
73
|
ctx.changed = true;
|
|
73
|
-
migrations.push(entry);
|
|
74
74
|
}
|
|
75
|
+
migrations.push(entry);
|
|
75
76
|
}
|
|
76
77
|
ts.forEachChild(node, (node) => {
|
|
77
78
|
walk(node, ctx);
|
|
@@ -84,7 +85,7 @@ for (const file of files) {
|
|
|
84
85
|
walk(sourceFile, ctx);
|
|
85
86
|
if (ctx.changed) {
|
|
86
87
|
changed.push(file);
|
|
87
|
-
writeFileSync(file, src.replaceAll(MACRO_CALL_REGEX, `$
|
|
88
|
+
writeFileSync(file, src.replaceAll(MACRO_CALL_REGEX, `$1("__resolved")`));
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
91
|
const [previousMigrations] = loadMigrationEntries(config);
|
|
@@ -99,11 +100,13 @@ if (newMigrations.length === 0) {
|
|
|
99
100
|
process.exit(0);
|
|
100
101
|
}
|
|
101
102
|
const newLines = newMigrations
|
|
102
|
-
.map((e) => `\t"${e}": { timestamp: ${migrationData[e].timestamp}, order: ${migrationData[e].order}, path: ${JSON.stringify(migrationData[e].path)} },`)
|
|
103
|
+
.map((e) => `\t"${e}": { timestamp: ${migrationData[e].timestamp}, order: ${migrationData[e].order}, path: ${JSON.stringify(migrationData[e].path)}, isTransform: ${migrationData[e].isTransform} },`)
|
|
103
104
|
.join("\n");
|
|
104
105
|
const migratedEntries = migrations.map((e) => `"${e}"`).join(" | ");
|
|
106
|
+
// isTransform is optional so entries written before it existed still type check
|
|
107
|
+
const migrationDataInterface = `interface MigrationData {\n\tpath: string[];\n\ttimestamp: number;\n\torder: number;\n\tisTransform?: boolean;\n}`;
|
|
105
108
|
if (!existsSync(config)) {
|
|
106
|
-
writeFileSync(config, `type MigratedEntries = ${migratedEntries}\n\
|
|
109
|
+
writeFileSync(config, `type MigratedEntries = ${migratedEntries}\n\n${migrationDataInterface}\n\n// AUTO-GENERATED MIGRATION CONFIG - DO NOT EDIT\nexport const MIGRATED_ENTRIES: Record<MigratedEntries, MigrationData> = {\n${newLines}\n};\n`);
|
|
107
110
|
}
|
|
108
111
|
else {
|
|
109
112
|
const lines = readFileSync(config, "utf-8").split("\n");
|
|
@@ -111,7 +114,10 @@ else {
|
|
|
111
114
|
if (typeIndex !== -1) {
|
|
112
115
|
lines[typeIndex] = `type MigratedEntries = ${migratedEntries}`;
|
|
113
116
|
}
|
|
114
|
-
const updated = lines
|
|
117
|
+
const updated = lines
|
|
118
|
+
.join("\n")
|
|
119
|
+
.replace(/interface MigrationData \{[^}]*\}/, migrationDataInterface)
|
|
120
|
+
.replace(/\};\s*$/, `${newLines}\n};`);
|
|
115
121
|
writeFileSync(config, updated);
|
|
116
122
|
}
|
|
117
123
|
changed.push(config);
|
package/out/config.js
CHANGED
|
@@ -2,6 +2,9 @@ function escapeRegExp(str) {
|
|
|
2
2
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3
3
|
}
|
|
4
4
|
export const CONFIG = "migrations.config.ts";
|
|
5
|
-
export const
|
|
6
|
-
export const
|
|
7
|
-
|
|
5
|
+
export const MIGRATE_MACRO = "$migrate";
|
|
6
|
+
export const TRANSFORM_MACRO = "$transform";
|
|
7
|
+
const MACROS = [MIGRATE_MACRO, TRANSFORM_MACRO].map(escapeRegExp).join("|");
|
|
8
|
+
export const MACRO_REGEX = new RegExp(`^(?:${MACROS})$`);
|
|
9
|
+
// group 1 holds the macro name, so a replace can keep it
|
|
10
|
+
export const MACRO_CALL_REGEX = new RegExp(`(?<![\\w$])(${MACROS})\\([^)]*\\)`, "g");
|
package/out/test.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { $migrate, $transform } from "../index.js";
|
|
2
|
+
function abc(test) {
|
|
3
|
+
// hello
|
|
4
|
+
}
|
|
5
|
+
function addMigration() {
|
|
6
|
+
return {
|
|
7
|
+
test: (order, fn) => undefined
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
abc({
|
|
11
|
+
hello: addMigration().test($migrate(0), () => {
|
|
12
|
+
// abvcdsc
|
|
13
|
+
}),
|
|
14
|
+
test: addMigration().test($migrate(1), () => {
|
|
15
|
+
// abvcdsc
|
|
16
|
+
}),
|
|
17
|
+
transform: addMigration().test($transform(2), () => {
|
|
18
|
+
// abvcdsc
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
@@ -5,10 +5,16 @@ const VAR_NAME = "MIGRATED_ENTRIES";
|
|
|
5
5
|
function reconstructProperties(properties) {
|
|
6
6
|
const obj = {};
|
|
7
7
|
for (const prop of properties) {
|
|
8
|
-
if (!ts.isPropertyAssignment(prop)
|
|
8
|
+
if (!ts.isPropertyAssignment(prop))
|
|
9
9
|
continue;
|
|
10
10
|
const name = ts.isIdentifier(prop.name) ? prop.name.text : prop.name.getText();
|
|
11
|
-
|
|
11
|
+
const init = prop.initializer;
|
|
12
|
+
if (ts.isNumericLiteral(init))
|
|
13
|
+
obj[name] = Number(init.text);
|
|
14
|
+
else if (init.kind === ts.SyntaxKind.TrueKeyword)
|
|
15
|
+
obj[name] = true;
|
|
16
|
+
else if (init.kind === ts.SyntaxKind.FalseKeyword)
|
|
17
|
+
obj[name] = false;
|
|
12
18
|
}
|
|
13
19
|
return obj;
|
|
14
20
|
}
|
package/out/util/macro.js
CHANGED
|
@@ -14,7 +14,7 @@ export function findMacroCall(node) {
|
|
|
14
14
|
return undefined;
|
|
15
15
|
}
|
|
16
16
|
export function replaceMacroCall(node, order, path) {
|
|
17
|
-
if (ts.isCallExpression(node)
|
|
17
|
+
if (ts.isCallExpression(node)) {
|
|
18
18
|
const newArgs = [];
|
|
19
19
|
let changed = false;
|
|
20
20
|
for (const arg of node.arguments) {
|