bencoveney / barrelsby

Automatic TypeScript barrels (index.ts files) for your entire code base

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Check file annotation before deleting the index.ts

benjamin658 opened this issue · comments

barrelsby/src/purge.ts

Lines 17 to 23 in 103a1cd

.forEach((file: FileTreeLocation) => {
logger.debug(`Deleting existing barrel @ ${file.path}`);
// Delete barrel file and clean up tree model.
fs.unlinkSync(file.path);
directory.files.splice(directory.files.indexOf(file), 1);
directory.barrel = undefined;
});

Currently, the -d option will delete the index.ts before generating a new one, however, it could delete the user's files that were not created by the barrelsby.

I recommend adding another filter to check if the file that is going to be deleted has the @file Automatically generated by barrelsby. annotation.

//..skip
directory.files
 .filter((file) => {
    const isGenerated = fs.readFileSync(file.path).includes('@file Automatically generated by barrelsby.');
    return file.name === barrelName && isGenerated;
  })