langium-tools 0.1.2 → 0.3.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.
package/README.md CHANGED
@@ -30,7 +30,10 @@
30
30
  <br />
31
31
  <a href="#documentation"><strong>Explore the docs »</strong></a>
32
32
  <br />
33
+ <a href="https://borisbrodski.github.io/langium-tools"><strong>Explore the typedocs »</strong></a>
33
34
  <br />
35
+ <br />
36
+
34
37
  <a href="https://github.com/borisbrodski/langium-tools/issues/new?labels=bug&template=bug-report---.md">Report Bug</a>
35
38
  ·
36
39
  <a href="https://github.com/borisbrodski/langium-tools/issues/new?labels=enhancement&template=feature-request---.md">Request Feature</a>
@@ -77,10 +80,6 @@
77
80
 
78
81
  ## About The Project
79
82
 
80
- <!--
81
- [![Product Name Screen Shot][product-screenshot]](https://example.com)
82
- -->
83
-
84
83
  This project is a collection of tools, that should power up DSL development with Langium:
85
84
 
86
85
  Language features:
@@ -349,6 +348,9 @@ manager.addTarget({
349
348
  });
350
349
  ```
351
350
 
351
+ - `overwrite`: Default value for `overwrite` option for `createFile`.
352
+
353
+
352
354
  ##### Using Targets
353
355
 
354
356
  Specify the target when creating files:
@@ -361,14 +363,17 @@ manager.createFile("custom.ts", "// Custom target content", {
361
363
 
362
364
  ##### Writing to Disk
363
365
 
364
- After generating content, use writeToDisk to write all collected files to the filesystem:
366
+ After generating content, use `writeToDisk` or `cleanAndWriteToDisk` to write all collected files to the filesystem:
365
367
 
366
368
  ```typescript
367
- await manager.writeToDisk(outputDir: string, target?: string);
369
+ await manager.cleanAndWriteToDisk(outputDir: string, target?: string);
370
+ await manager.writeToDisk(outputDir: string, target?: string, clean?: boolean);
368
371
  ```
369
372
 
370
373
  - `outputDir`: The directory where files will be written.
371
374
  - `target`: Optional target name. If not provided, the default target is used.
375
+ - `clean`: Remove all files not involved in generation. Files with the same content will not be removed and recreated to pervent file timestamps changes and filesystem events.
376
+
372
377
 
373
378
  #### API Reference
374
379
 
@@ -1173,6 +1178,5 @@ Project Link: [https://github.com/borisbrodski/langium-tools](https://github.com
1173
1178
  [issues-url]: https://github.com/borisbrodski/langium-tools/issues
1174
1179
  [license-shield]: https://img.shields.io/github/license/borisbrodski/langium-tools.svg?style=for-the-badge
1175
1180
  [license-url]: https://github.com/borisbrodski/langium-tools/blob/master/LICENSE.txt
1176
- [product-screenshot]: images/screenshot.png
1177
1181
  [Typescript]: https://shields.io/badge/TypeScript-3178C6?logo=TypeScript&logoColor=FFF&style=flat-square
1178
1182
  [Typescript-url]: https://www.typescriptlang.org/
@@ -191,17 +191,45 @@ export declare class GeneratedContentManager {
191
191
  */
192
192
  getGeneratedContent(targetName?: string): GeneratedContent;
193
193
  /**
194
- * Writes the generated content for a target to the file system asynchronously.
194
+ * Recursively lists all files in the given directory.
195
195
  *
196
- * Existing files are only overwritten if the overwrite flag is set (default behavior).
197
- * If the file already exists and the content is the same, the file is not overwritten,
198
- * preserving the timestamp and not triggering file system file change events.
199
- *
200
- * @param outputDir - The output directory where the files will be written.
201
- * @param target - The target name whose content should be written.
202
- * If not provided, the default target is used.
203
- *
204
- * @throws {Error} If an error occurs during file or directory operations.
196
+ * @param dir - The directory to scan for files.
197
+ * @returns A promise resolving to an array of absolute file paths.
198
+ * @private
205
199
  */
206
- writeToDisk(outputDir: string, target?: string): Promise<void>;
200
+ private listAllFiles;
201
+ /**
202
+ * Writes the generated content for a target to the file system asynchronously.
203
+ *
204
+ * Existing files are only overwritten if the overwrite flag is set (default behavior).
205
+ * If the file already exists and the content is the same, the file is not overwritten,
206
+ * preserving the timestamp and not triggering file system file change events.
207
+ *
208
+ * Files in the output directory that are not
209
+ * part of the generated content will be deleted after writing.
210
+ *
211
+ * Equivalent to `writeToDisk(outputDir, target, true)`
212
+ *
213
+ * @param outputDir - The output directory where the files will be written.
214
+ * @param target - The target name whose content should be written.
215
+ * If not provided, the default target is used.
216
+ * @throws {Error} If an error occurs during file or directory operations.
217
+ */
218
+ cleanAndWriteToDisk(outputDir: string, target?: string): Promise<void>;
219
+ /**
220
+ * Writes the generated content for a target to the file system asynchronously.
221
+ *
222
+ * Existing files are only overwritten if the overwrite flag is set (default behavior).
223
+ * If the file already exists and the content is the same, the file is not overwritten,
224
+ * preserving the timestamp and not triggering file system file change events.
225
+ *
226
+ * @param outputDir - The output directory where the files will be written.
227
+ * @param target - The target name whose content should be written.
228
+ * If not provided, the default target is used.
229
+ * @param clean If `true`, files in the output directory that are not
230
+ * part of the generated content will be deleted after writing.
231
+ *
232
+ * @throws {Error} If an error occurs during file or directory operations.
233
+ */
234
+ writeToDisk(outputDir: string, target?: string, clean?: boolean): Promise<void>;
207
235
  }
@@ -170,19 +170,64 @@ export class GeneratedContentManager {
170
170
  return this.generatedContentMap.get(__classPrivateFieldGet(this, _GeneratedContentManager_instances, "m", _GeneratedContentManager_getTarget).call(this, targetName).name);
171
171
  }
172
172
  /**
173
- * Writes the generated content for a target to the file system asynchronously.
173
+ * Recursively lists all files in the given directory.
174
174
  *
175
- * Existing files are only overwritten if the overwrite flag is set (default behavior).
176
- * If the file already exists and the content is the same, the file is not overwritten,
177
- * preserving the timestamp and not triggering file system file change events.
178
- *
179
- * @param outputDir - The output directory where the files will be written.
180
- * @param target - The target name whose content should be written.
181
- * If not provided, the default target is used.
182
- *
183
- * @throws {Error} If an error occurs during file or directory operations.
175
+ * @param dir - The directory to scan for files.
176
+ * @returns A promise resolving to an array of absolute file paths.
177
+ * @private
184
178
  */
185
- async writeToDisk(outputDir, target) {
179
+ async listAllFiles(dir) {
180
+ const entries = await fsPromises.readdir(dir, { withFileTypes: true });
181
+ const files = await Promise.all(entries.map(async (entry) => {
182
+ const fullPath = path.join(dir, entry.name);
183
+ if (entry.isDirectory()) {
184
+ return this.listAllFiles(fullPath);
185
+ }
186
+ else if (entry.isFile()) {
187
+ return [fullPath];
188
+ }
189
+ else {
190
+ return []; // Skip symbolic links, etc.
191
+ }
192
+ }));
193
+ return files.flat();
194
+ }
195
+ /**
196
+ * Writes the generated content for a target to the file system asynchronously.
197
+ *
198
+ * Existing files are only overwritten if the overwrite flag is set (default behavior).
199
+ * If the file already exists and the content is the same, the file is not overwritten,
200
+ * preserving the timestamp and not triggering file system file change events.
201
+ *
202
+ * Files in the output directory that are not
203
+ * part of the generated content will be deleted after writing.
204
+ *
205
+ * Equivalent to `writeToDisk(outputDir, target, true)`
206
+ *
207
+ * @param outputDir - The output directory where the files will be written.
208
+ * @param target - The target name whose content should be written.
209
+ * If not provided, the default target is used.
210
+ * @throws {Error} If an error occurs during file or directory operations.
211
+ */
212
+ async cleanAndWriteToDisk(outputDir, target) {
213
+ return this.writeToDisk(outputDir, target, true);
214
+ }
215
+ /**
216
+ * Writes the generated content for a target to the file system asynchronously.
217
+ *
218
+ * Existing files are only overwritten if the overwrite flag is set (default behavior).
219
+ * If the file already exists and the content is the same, the file is not overwritten,
220
+ * preserving the timestamp and not triggering file system file change events.
221
+ *
222
+ * @param outputDir - The output directory where the files will be written.
223
+ * @param target - The target name whose content should be written.
224
+ * If not provided, the default target is used.
225
+ * @param clean If `true`, files in the output directory that are not
226
+ * part of the generated content will be deleted after writing.
227
+ *
228
+ * @throws {Error} If an error occurs during file or directory operations.
229
+ */
230
+ async writeToDisk(outputDir, target, clean) {
186
231
  const generatedContent = this.getGeneratedContent(target);
187
232
  // Ensure the output directory exists
188
233
  try {
@@ -192,6 +237,23 @@ export class GeneratedContentManager {
192
237
  console.error(`Error creating directory "${outputDir}": ${error.message}`);
193
238
  throw error;
194
239
  }
240
+ // Collect existing files if cleaning is enabled
241
+ let existingFiles;
242
+ if (clean) {
243
+ try {
244
+ const files = await this.listAllFiles(outputDir);
245
+ existingFiles = new Set(files);
246
+ }
247
+ catch (error) {
248
+ if (error.code === 'ENOENT') {
249
+ existingFiles = new Set(); // Directory doesn't exist yet
250
+ }
251
+ else {
252
+ console.error(`Error listing files in "${outputDir}": ${error.message}`);
253
+ throw error;
254
+ }
255
+ }
256
+ }
195
257
  for (const [file, content] of generatedContent) {
196
258
  const absoluteFilePath = path.join(outputDir, file);
197
259
  const fileDir = path.dirname(absoluteFilePath);
@@ -203,6 +265,20 @@ export class GeneratedContentManager {
203
265
  console.error(`Error creating directory "${fileDir}": ${error.message}`);
204
266
  throw error;
205
267
  }
268
+ // If cleaning, check if this generated file exists and remove it from the set
269
+ if (existingFiles) {
270
+ try {
271
+ const realPath = await fsPromises.realpath(absoluteFilePath);
272
+ existingFiles.delete(realPath); // Remove actual OS-reported path
273
+ }
274
+ catch (error) {
275
+ if (error.code !== 'ENOENT') {
276
+ console.error(`Error resolving path "${absoluteFilePath}": ${error.message}`);
277
+ throw error;
278
+ }
279
+ // File doesn't exist yet, will be created below
280
+ }
281
+ }
206
282
  let fileExists = false;
207
283
  let existingContent = '';
208
284
  // Check if the file exists and read its content
@@ -231,6 +307,18 @@ export class GeneratedContentManager {
231
307
  throw error;
232
308
  }
233
309
  }
310
+ // Clean up files that weren't generated
311
+ if (clean && existingFiles && existingFiles.size > 0) {
312
+ for (const fileToDelete of existingFiles) {
313
+ try {
314
+ await fsPromises.unlink(fileToDelete);
315
+ }
316
+ catch (error) {
317
+ console.error(`Error deleting file "${fileToDelete}": ${error.message}`);
318
+ // Log and continue to avoid stopping on single file failure
319
+ }
320
+ }
321
+ }
234
322
  }
235
323
  }
236
324
  _GeneratedContentManager_instances = new WeakSet(), _GeneratedContentManager_getTarget = function _GeneratedContentManager_getTarget(targetName) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langium-tools",
3
- "version": "0.1.2",
3
+ "version": "0.3.1",
4
4
  "description": "A set of tools for Langium",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -31,7 +31,7 @@
31
31
  "@types/node": "^22.0.0",
32
32
  "change-case": "^5.4.4",
33
33
  "escape-string-regexp": "^5.0.0",
34
- "langium": "^3.2.0"
34
+ "langium": "^3.4.0"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "vitest": "~2.0.5"
@@ -39,9 +39,9 @@
39
39
  "devDependencies": {
40
40
  "@eslint/js": "^9.8.0",
41
41
  "globals": "^15.8.0",
42
- "typedoc": "^0.26.7",
43
- "typedoc-plugin-extras": "^3.1.0",
44
- "typedoc-plugin-markdown": "^4.2.7",
42
+ "typedoc": "^0.27.0",
43
+ "typedoc-plugin-extras": "^4.0.0",
44
+ "typedoc-plugin-markdown": "^3.0.0",
45
45
  "typescript": "^5.5.4",
46
46
  "typescript-eslint": "^7.18.0",
47
47
  "vitest": "~2.0.5"
@@ -300,19 +300,66 @@ export class GeneratedContentManager {
300
300
  }
301
301
 
302
302
  /**
303
+ * Recursively lists all files in the given directory.
304
+ *
305
+ * @param dir - The directory to scan for files.
306
+ * @returns A promise resolving to an array of absolute file paths.
307
+ * @private
308
+ */
309
+ private async listAllFiles(dir: string): Promise<string[]> {
310
+ const entries = await fsPromises.readdir(dir, { withFileTypes: true });
311
+ const files = await Promise.all(
312
+ entries.map(async (entry) => {
313
+ const fullPath = path.join(dir, entry.name);
314
+ if (entry.isDirectory()) {
315
+ return this.listAllFiles(fullPath);
316
+ } else if (entry.isFile()) {
317
+ return [fullPath];
318
+ } else {
319
+ return []; // Skip symbolic links, etc.
320
+ }
321
+ })
322
+ );
323
+ return files.flat();
324
+ }
325
+
326
+ /**
303
327
  * Writes the generated content for a target to the file system asynchronously.
304
328
  *
305
329
  * Existing files are only overwritten if the overwrite flag is set (default behavior).
306
330
  * If the file already exists and the content is the same, the file is not overwritten,
307
331
  * preserving the timestamp and not triggering file system file change events.
308
332
  *
333
+ * Files in the output directory that are not
334
+ * part of the generated content will be deleted after writing.
335
+ *
336
+ * Equivalent to `writeToDisk(outputDir, target, true)`
337
+ *
309
338
  * @param outputDir - The output directory where the files will be written.
310
339
  * @param target - The target name whose content should be written.
311
340
  * If not provided, the default target is used.
341
+ * @throws {Error} If an error occurs during file or directory operations.
342
+ */
343
+ async cleanAndWriteToDisk(outputDir: string, target?: string): Promise<void> {
344
+ return this.writeToDisk(outputDir, target, true);
345
+ }
346
+
347
+ /**
348
+ * Writes the generated content for a target to the file system asynchronously.
349
+ *
350
+ * Existing files are only overwritten if the overwrite flag is set (default behavior).
351
+ * If the file already exists and the content is the same, the file is not overwritten,
352
+ * preserving the timestamp and not triggering file system file change events.
353
+ *
354
+ * @param outputDir - The output directory where the files will be written.
355
+ * @param target - The target name whose content should be written.
356
+ * If not provided, the default target is used.
357
+ * @param clean If `true`, files in the output directory that are not
358
+ * part of the generated content will be deleted after writing.
312
359
  *
313
360
  * @throws {Error} If an error occurs during file or directory operations.
314
361
  */
315
- async writeToDisk(outputDir: string, target?: string): Promise<void> {
362
+ async writeToDisk(outputDir: string, target?: string, clean?: boolean): Promise<void> {
316
363
  const generatedContent = this.getGeneratedContent(target);
317
364
 
318
365
  // Ensure the output directory exists
@@ -323,6 +370,22 @@ export class GeneratedContentManager {
323
370
  throw error;
324
371
  }
325
372
 
373
+ // Collect existing files if cleaning is enabled
374
+ let existingFiles: Set<string> | undefined;
375
+ if (clean) {
376
+ try {
377
+ const files = await this.listAllFiles(outputDir);
378
+ existingFiles = new Set(files);
379
+ } catch (error) {
380
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
381
+ existingFiles = new Set(); // Directory doesn't exist yet
382
+ } else {
383
+ console.error(`Error listing files in "${outputDir}": ${(error as Error).message}`);
384
+ throw error;
385
+ }
386
+ }
387
+ }
388
+
326
389
  for (const [file, content] of generatedContent) {
327
390
  const absoluteFilePath = path.join(outputDir, file);
328
391
  const fileDir = path.dirname(absoluteFilePath);
@@ -335,6 +398,20 @@ export class GeneratedContentManager {
335
398
  throw error;
336
399
  }
337
400
 
401
+ // If cleaning, check if this generated file exists and remove it from the set
402
+ if (existingFiles) {
403
+ try {
404
+ const realPath = await fsPromises.realpath(absoluteFilePath);
405
+ existingFiles.delete(realPath); // Remove actual OS-reported path
406
+ } catch (error) {
407
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
408
+ console.error(`Error resolving path "${absoluteFilePath}": ${(error as Error).message}`);
409
+ throw error;
410
+ }
411
+ // File doesn't exist yet, will be created below
412
+ }
413
+ }
414
+
338
415
  let fileExists = false;
339
416
  let existingContent = '';
340
417
 
@@ -364,5 +441,17 @@ export class GeneratedContentManager {
364
441
  throw error;
365
442
  }
366
443
  }
444
+
445
+ // Clean up files that weren't generated
446
+ if (clean && existingFiles && existingFiles.size > 0) {
447
+ for (const fileToDelete of existingFiles) {
448
+ try {
449
+ await fsPromises.unlink(fileToDelete);
450
+ } catch (error) {
451
+ console.error(`Error deleting file "${fileToDelete}": ${(error as Error).message}`);
452
+ // Log and continue to avoid stopping on single file failure
453
+ }
454
+ }
455
+ }
367
456
  }
368
457
  }