rbxts-transformer-commit-migrations 1.2.4 → 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 CHANGED
@@ -1,3 +1,5 @@
1
- export declare function $migrate(order: number): never;
2
- export declare function $migrate(order: "__resolved"): number;
3
- export declare function $migrate(order: number | "__resolved"): number | undefined;
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;
@@ -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, MACRO, MACRO_CALL_REGEX, MACRO_REGEX } from "../config.js";
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("Invalid migration entry, maybe you called a $migrate outside a table?");
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, `${MACRO}("__resolved")`));
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\ninterface MigrationData {\n\tpath: string[]\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`);
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.join("\n").replace(/\};\s*$/, `${newLines}\n};`);
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 MACRO = "$migrate";
6
- export const MACRO_REGEX = new RegExp(escapeRegExp(MACRO));
7
- export const MACRO_CALL_REGEX = new RegExp(escapeRegExp(MACRO) + "\\([^)]*\\)", "g");
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/index.js CHANGED
@@ -8,7 +8,7 @@ export default function (_, config) {
8
8
  const configPath = config?.configPath ?? CONFIG;
9
9
  if (!existsSync(configPath)) {
10
10
  console.error(`Invalid migration config path ${configPath}`);
11
- process.exit(1);
11
+ return (file) => file;
12
12
  }
13
13
  const [entryArr, entries] = loadMigrationEntries(configPath);
14
14
  const orderedEntries = entryArr
package/out/test.js CHANGED
@@ -1,7 +1,20 @@
1
- import { $migrate } from "../index.js";
2
- function test(a) {
3
- return 0;
1
+ import { $migrate, $transform } from "../index.js";
2
+ function abc(test) {
3
+ // hello
4
4
  }
5
- const abc = {
6
- test: { bruh: test($migrate(0)) }
7
- };
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) || !ts.isNumericLiteral(prop.initializer))
8
+ if (!ts.isPropertyAssignment(prop))
9
9
  continue;
10
10
  const name = ts.isIdentifier(prop.name) ? prop.name.text : prop.name.getText();
11
- obj[name] = Number(prop.initializer.text);
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) && ts.isIdentifier(node.expression)) {
17
+ if (ts.isCallExpression(node)) {
18
18
  const newArgs = [];
19
19
  let changed = false;
20
20
  for (const arg of node.arguments) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rbxts-transformer-commit-migrations",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "description": "rbxts transformer used in user data migration builder chains",
5
5
  "main": "out/index.js",
6
6
  "type": "module",
package/out/abc.js DELETED
@@ -1,7 +0,0 @@
1
- import { $migrate } from "../index.js";
2
- function test(b) {
3
- return 0;
4
- }
5
- export const abc = {
6
- hello: { abc: test($migrate(0)) }
7
- };
package/out/macro.js DELETED
@@ -1,21 +0,0 @@
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
- if (ts.isCallExpression(node)) {
8
- for (const arg of node.arguments) {
9
- if (ts.isCallExpression(arg) && ts.isIdentifier(arg.expression) && MACRO_REGEX.test(arg.expression.text)) {
10
- return arg;
11
- }
12
- }
13
- }
14
- return undefined;
15
- }
16
- export function replaceMacroCall(node, replacement) {
17
- if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && MACRO_REGEX.test(node.expression.text)) {
18
- return ts.factory.createNumericLiteral(replacement);
19
- }
20
- return ts.visitEachChild(node, (child) => replaceMacroCall(child, replacement), undefined);
21
- }