langium-tools 0.2.0 → 0.3.2

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
@@ -345,12 +345,10 @@ Adding a Target
345
345
  manager.addTarget({
346
346
  name: "CUSTOM_TARGET",
347
347
  overwrite: false,
348
- clean: true
349
348
  });
350
349
  ```
351
350
 
352
351
  - `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
352
 
355
353
 
356
354
  ##### Using Targets
@@ -365,14 +363,17 @@ manager.createFile("custom.ts", "// Custom target content", {
365
363
 
366
364
  ##### Writing to Disk
367
365
 
368
- 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:
369
367
 
370
368
  ```typescript
371
- 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);
372
371
  ```
373
372
 
374
373
  - `outputDir`: The directory where files will be written.
375
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
+
376
377
 
377
378
  #### API Reference
378
379
 
@@ -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.2.0",
3
+ "version": "0.3.2",
4
4
  "description": "A set of tools for Langium",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -26,18 +26,14 @@ export type GeneratedContent = Map<
26
26
  export interface GeneratorTarget {
27
27
  /** The name of the target. */
28
28
  name: string;
29
-
30
29
  /** Default value for the `overwrite` flag for the target. */
31
30
  overwrite: boolean;
32
-
33
- /** Clean directory before generating. All files besides generated ones will be removed. */
34
- clean: boolean;
35
31
  }
36
32
 
37
33
  /**
38
34
  * The default target used when no specific target is provided.
39
35
  */
40
- export const DEFAULT_TARGET: GeneratorTarget = { name: 'DEFAULT', overwrite: true, clean: false };
36
+ export const DEFAULT_TARGET: GeneratorTarget = { name: 'DEFAULT', overwrite: true };
41
37
 
42
38
  /**
43
39
  * Options for creating a file.
@@ -318,7 +314,7 @@ export class GeneratedContentManager {
318
314
  if (entry.isDirectory()) {
319
315
  return this.listAllFiles(fullPath);
320
316
  } else if (entry.isFile()) {
321
- return [fullPath];
317
+ return [await fsPromises.realpath(fullPath)];
322
318
  } else {
323
319
  return []; // Skip symbolic links, etc.
324
320
  }
@@ -334,18 +330,37 @@ export class GeneratedContentManager {
334
330
  * If the file already exists and the content is the same, the file is not overwritten,
335
331
  * preserving the timestamp and not triggering file system file change events.
336
332
  *
337
- * If the `clean` flag for the target is `true`, files in the output directory that are not
333
+ * Files in the output directory that are not
338
334
  * part of the generated content will be deleted after writing.
339
335
  *
336
+ * Equivalent to `writeToDisk(outputDir, target, true)`
337
+ *
338
+ * @param outputDir - The output directory where the files will be written.
339
+ * @param target - The target name whose content should be written.
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
+ *
340
354
  * @param outputDir - The output directory where the files will be written.
341
355
  * @param target - The target name whose content should be written.
342
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.
343
359
  *
344
360
  * @throws {Error} If an error occurs during file or directory operations.
345
361
  */
346
- async writeToDisk(outputDir: string, target?: string): Promise<void> {
362
+ async writeToDisk(outputDir: string, target?: string, clean?: boolean): Promise<void> {
347
363
  const generatedContent = this.getGeneratedContent(target);
348
- const targetConfig = this.#getTarget(target);
349
364
 
350
365
  // Ensure the output directory exists
351
366
  try {
@@ -357,7 +372,7 @@ export class GeneratedContentManager {
357
372
 
358
373
  // Collect existing files if cleaning is enabled
359
374
  let existingFiles: Set<string> | undefined;
360
- if (targetConfig.clean) {
375
+ if (clean) {
361
376
  try {
362
377
  const files = await this.listAllFiles(outputDir);
363
378
  existingFiles = new Set(files);
@@ -428,7 +443,7 @@ export class GeneratedContentManager {
428
443
  }
429
444
 
430
445
  // Clean up files that weren't generated
431
- if (targetConfig.clean && existingFiles && existingFiles.size > 0) {
446
+ if (clean && existingFiles && existingFiles.size > 0) {
432
447
  for (const fileToDelete of existingFiles) {
433
448
  try {
434
449
  await fsPromises.unlink(fileToDelete);