langium-tools 0.1.2 → 0.2.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/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:
@@ -346,9 +345,14 @@ Adding a Target
346
345
  manager.addTarget({
347
346
  name: "CUSTOM_TARGET",
348
347
  overwrite: false,
348
+ clean: true
349
349
  });
350
350
  ```
351
351
 
352
+ - `overwrite`: Default value for `overwrite` option for `createFile`.
353
+ - `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.
354
+
355
+
352
356
  ##### Using Targets
353
357
 
354
358
  Specify the target when creating files:
@@ -1173,6 +1177,5 @@ Project Link: [https://github.com/borisbrodski/langium-tools](https://github.com
1173
1177
  [issues-url]: https://github.com/borisbrodski/langium-tools/issues
1174
1178
  [license-shield]: https://img.shields.io/github/license/borisbrodski/langium-tools.svg?style=for-the-badge
1175
1179
  [license-url]: https://github.com/borisbrodski/langium-tools/blob/master/LICENSE.txt
1176
- [product-screenshot]: images/screenshot.png
1177
1180
  [Typescript]: https://shields.io/badge/TypeScript-3178C6?logo=TypeScript&logoColor=FFF&style=flat-square
1178
1181
  [Typescript-url]: https://www.typescriptlang.org/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langium-tools",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
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"
@@ -26,14 +26,18 @@ export type GeneratedContent = Map<
26
26
  export interface GeneratorTarget {
27
27
  /** The name of the target. */
28
28
  name: string;
29
+
29
30
  /** Default value for the `overwrite` flag for the target. */
30
31
  overwrite: boolean;
32
+
33
+ /** Clean directory before generating. All files besides generated ones will be removed. */
34
+ clean: boolean;
31
35
  }
32
36
 
33
37
  /**
34
38
  * The default target used when no specific target is provided.
35
39
  */
36
- export const DEFAULT_TARGET: GeneratorTarget = { name: 'DEFAULT', overwrite: true };
40
+ export const DEFAULT_TARGET: GeneratorTarget = { name: 'DEFAULT', overwrite: true, clean: false };
37
41
 
38
42
  /**
39
43
  * Options for creating a file.
@@ -300,12 +304,39 @@ export class GeneratedContentManager {
300
304
  }
301
305
 
302
306
  /**
307
+ * Recursively lists all files in the given directory.
308
+ *
309
+ * @param dir - The directory to scan for files.
310
+ * @returns A promise resolving to an array of absolute file paths.
311
+ * @private
312
+ */
313
+ private async listAllFiles(dir: string): Promise<string[]> {
314
+ const entries = await fsPromises.readdir(dir, { withFileTypes: true });
315
+ const files = await Promise.all(
316
+ entries.map(async (entry) => {
317
+ const fullPath = path.join(dir, entry.name);
318
+ if (entry.isDirectory()) {
319
+ return this.listAllFiles(fullPath);
320
+ } else if (entry.isFile()) {
321
+ return [fullPath];
322
+ } else {
323
+ return []; // Skip symbolic links, etc.
324
+ }
325
+ })
326
+ );
327
+ return files.flat();
328
+ }
329
+
330
+ /**
303
331
  * Writes the generated content for a target to the file system asynchronously.
304
332
  *
305
333
  * Existing files are only overwritten if the overwrite flag is set (default behavior).
306
334
  * If the file already exists and the content is the same, the file is not overwritten,
307
335
  * preserving the timestamp and not triggering file system file change events.
308
336
  *
337
+ * If the `clean` flag for the target is `true`, files in the output directory that are not
338
+ * part of the generated content will be deleted after writing.
339
+ *
309
340
  * @param outputDir - The output directory where the files will be written.
310
341
  * @param target - The target name whose content should be written.
311
342
  * If not provided, the default target is used.
@@ -314,6 +345,7 @@ export class GeneratedContentManager {
314
345
  */
315
346
  async writeToDisk(outputDir: string, target?: string): Promise<void> {
316
347
  const generatedContent = this.getGeneratedContent(target);
348
+ const targetConfig = this.#getTarget(target);
317
349
 
318
350
  // Ensure the output directory exists
319
351
  try {
@@ -323,6 +355,22 @@ export class GeneratedContentManager {
323
355
  throw error;
324
356
  }
325
357
 
358
+ // Collect existing files if cleaning is enabled
359
+ let existingFiles: Set<string> | undefined;
360
+ if (targetConfig.clean) {
361
+ try {
362
+ const files = await this.listAllFiles(outputDir);
363
+ existingFiles = new Set(files);
364
+ } catch (error) {
365
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
366
+ existingFiles = new Set(); // Directory doesn't exist yet
367
+ } else {
368
+ console.error(`Error listing files in "${outputDir}": ${(error as Error).message}`);
369
+ throw error;
370
+ }
371
+ }
372
+ }
373
+
326
374
  for (const [file, content] of generatedContent) {
327
375
  const absoluteFilePath = path.join(outputDir, file);
328
376
  const fileDir = path.dirname(absoluteFilePath);
@@ -335,6 +383,20 @@ export class GeneratedContentManager {
335
383
  throw error;
336
384
  }
337
385
 
386
+ // If cleaning, check if this generated file exists and remove it from the set
387
+ if (existingFiles) {
388
+ try {
389
+ const realPath = await fsPromises.realpath(absoluteFilePath);
390
+ existingFiles.delete(realPath); // Remove actual OS-reported path
391
+ } catch (error) {
392
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
393
+ console.error(`Error resolving path "${absoluteFilePath}": ${(error as Error).message}`);
394
+ throw error;
395
+ }
396
+ // File doesn't exist yet, will be created below
397
+ }
398
+ }
399
+
338
400
  let fileExists = false;
339
401
  let existingContent = '';
340
402
 
@@ -364,5 +426,17 @@ export class GeneratedContentManager {
364
426
  throw error;
365
427
  }
366
428
  }
429
+
430
+ // Clean up files that weren't generated
431
+ if (targetConfig.clean && existingFiles && existingFiles.size > 0) {
432
+ for (const fileToDelete of existingFiles) {
433
+ try {
434
+ await fsPromises.unlink(fileToDelete);
435
+ } catch (error) {
436
+ console.error(`Error deleting file "${fileToDelete}": ${(error as Error).message}`);
437
+ // Log and continue to avoid stopping on single file failure
438
+ }
439
+ }
440
+ }
367
441
  }
368
442
  }