rbxts-transform-luau 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/out/index.js +55 -16
  2. package/package.json +1 -1
package/out/index.js CHANGED
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.default = default_1;
30
30
  const typescript_1 = __importDefault(require("typescript"));
31
+ const fs = __importStar(require("fs"));
31
32
  const path = __importStar(require("path"));
32
33
  const format_1 = require("./passes/format");
33
34
  const LUAU_TYPE = {
@@ -170,23 +171,60 @@ function commonRoot(files) {
170
171
  return root.join(path.sep) || undefined;
171
172
  }
172
173
  const pending = new Map();
173
- let finalizeRegistered = false;
174
+ const writingFiles = new Set();
175
+ // dir path → watcher, shared across files in the same dir
176
+ const dirWatchers = new Map();
177
+ function processFile(meta) {
178
+ try {
179
+ (0, format_1.formatFile)(meta.outPath, meta.strict, meta.optimizeLevel, meta.sidecar, meta.annotate ? meta.types : new Map());
180
+ }
181
+ catch {
182
+ // silently skip files that fail — they stay as-is
183
+ }
184
+ }
185
+ function watchDir(dir) {
186
+ if (dirWatchers.has(dir))
187
+ return;
188
+ try {
189
+ const watcher = fs.watch(dir, { persistent: false }, (_event, filename) => {
190
+ if (!filename)
191
+ return;
192
+ const outPath = path.join(dir, filename);
193
+ const meta = pending.get(outPath);
194
+ if (!meta)
195
+ return;
196
+ if (writingFiles.has(outPath))
197
+ return;
198
+ pending.delete(outPath);
199
+ writingFiles.add(outPath);
200
+ try {
201
+ processFile(meta);
202
+ }
203
+ finally {
204
+ setTimeout(() => writingFiles.delete(outPath), 50).unref();
205
+ }
206
+ // clean up watcher if no more pending files in this dir
207
+ const stillPending = [...pending.keys()].some(p => path.dirname(p) === dir);
208
+ if (!stillPending) {
209
+ watcher.close();
210
+ dirWatchers.delete(dir);
211
+ }
212
+ });
213
+ dirWatchers.set(dir, watcher);
214
+ }
215
+ catch {
216
+ // dir may not exist yet — fall back to exit handler
217
+ }
218
+ }
174
219
  function flushPending() {
175
220
  for (const [, meta] of pending) {
176
- try {
177
- (0, format_1.formatFile)(meta.outPath, meta.strict, meta.optimizeLevel, meta.sidecar, meta.annotate ? meta.types : new Map());
178
- }
179
- catch {
180
- // silently skip files that fail — they stay as-is
181
- }
221
+ processFile(meta);
182
222
  }
183
223
  pending.clear();
184
- }
185
- function registerFinalizer() {
186
- if (finalizeRegistered)
187
- return;
188
- finalizeRegistered = true;
189
- process.on("exit", flushPending);
224
+ for (const [dir, watcher] of dirWatchers) {
225
+ watcher.close();
226
+ dirWatchers.delete(dir);
227
+ }
190
228
  }
191
229
  function jsDocText(comment) {
192
230
  if (!comment)
@@ -236,9 +274,9 @@ function collectJsDoc(ts, sourceFile) {
236
274
  function default_1(program, config = {}) {
237
275
  const { strict = true, optimize = false, annotate = true, verbose = false } = config;
238
276
  const optimizeLevel = optimize === false ? false : [0, 1, 2].includes(optimize) ? optimize : 2;
239
- // Watch mode: flush the previous run's pending files before starting this one.
240
- flushPending();
241
- registerFinalizer();
277
+ // Safety net: flush any files that weren't caught by the dir watcher.
278
+ // Also handles the last cycle when the process exits.
279
+ process.on("exit", flushPending);
242
280
  const outDir = program.getCompilerOptions().outDir;
243
281
  const checker = annotate ? program.getTypeChecker() : null;
244
282
  return (_ctx) => (sourceFile) => {
@@ -247,6 +285,7 @@ function default_1(program, config = {}) {
247
285
  const sidecar = collectJsDoc(typescript_1.default, sourceFile);
248
286
  const types = checker ? collectTypes(checker, sourceFile) : new Map();
249
287
  pending.set(outPath, { outPath, strict, optimizeLevel, annotate, verbose, sidecar, types });
288
+ watchDir(path.dirname(outPath));
250
289
  if (verbose) {
251
290
  const rel = outDir ? path.relative(outDir, outPath) : outPath;
252
291
  console.log(`luau: ${rel}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rbxts-transform-luau",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "roblox-ts transformer: idiomatic Luau output — preamble organization, comment cleanup, const promotion, and directive injection",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",