diff --git a/src/index.test.ts b/src/index.test.ts index 30ba981..0595b94 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -75,7 +75,7 @@ describe('main module', () => { expect(getCombinedBaseUrlSpy.calledOnceWithExactly(rootPath, args.baseUrl)).toBeTruthy(); expect(buildTreeSpy.calledOnceWithExactly(rootPath, barrelName, signale)).toBeTruthy(); expect(getDestinationsSpy.calledOnceWithExactly(builtTree, args.location, barrelName, signale)).toBeTruthy(); - expect(purgeSpy.calledOnceWithExactly(builtTree, args.delete, barrelName, signale)).toBeTruthy(); + expect(purgeSpy.calledOnceWithExactly(builtTree, args.delete, args.noHeader, barrelName, signale)).toBeTruthy(); expect(buildBarrelsSpy).toHaveBeenCalledWith({ addHeader: true, destinations, diff --git a/src/index.ts b/src/index.ts index 736dca2..8df6b51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,7 +46,7 @@ export function Barrelsby(args: Arguments) { logger.debug('Destinations', destinations); // Potentially there are some existing barrels that need removing. - purge(rootTree, args.delete ?? false, barrelName, logger); + purge(rootTree, args.delete ?? false, args.noHeader ?? false, barrelName, logger); // Create the barrels. const quoteCharacter = getQuoteCharacter(args.singleQuotes as boolean); diff --git a/src/purge.test.ts b/src/purge.test.ts index e0a5e72..1f84f4c 100644 --- a/src/purge.test.ts +++ b/src/purge.test.ts @@ -13,6 +13,7 @@ describe('purge module has a', () => { let logger: Logger; let loggerSpy: jest.SpyInstance; const barrelName = 'barrel.ts'; + const barrelNoHeaderName = 'index.ts'; beforeEach(() => { MockFs(TestUtilities.mockFsConfiguration()); directory = TestUtilities.mockDirectoryTree(); @@ -24,7 +25,7 @@ describe('purge module has a', () => { MockFs.restore(); }); it('should delete existing barrels if the delete flag is enabled', () => { - Purge.purge(directory, true, barrelName, logger); + Purge.purge(directory, true, false, barrelName, logger); // Check directory has been manipulated. expect(directory.files.length).toBe(2); @@ -34,7 +35,7 @@ describe('purge module has a', () => { expect(fs.existsSync('directory1/barrel.ts')).toBeFalsy(); }); it('should do nothing if the delete flag is disabled', () => { - Purge.purge(directory, false, barrelName, logger); + Purge.purge(directory, false, false, barrelName, logger); // Check directory has not been manipulated. expect(directory.files.length).toBe(3); @@ -44,9 +45,19 @@ describe('purge module has a', () => { expect(fs.existsSync('directory1/barrel.ts')).toBeTruthy(); }); it('should log useful information to the logger', () => { - Purge.purge(directory, true, barrelName, logger); + Purge.purge(directory, true, false, barrelName, logger); expect(loggerSpy).toHaveBeenCalledTimes(2); }); + it('should delete files without header if noHeader flag is enabled', () => { + Purge.purge(directory, true, true, barrelNoHeaderName, logger); + + // Check directory has been manipulated. + expect(directory.files.length).toBe(2); + expect(directory.files.filter(file => file.name === 'index.ts').length).toBe(0); + + // Check FS has been manipulated. + expect(fs.existsSync('directory/index.ts')).toBeFalsy(); + }); }); }); diff --git a/src/purge.ts b/src/purge.ts index 3f6a6d3..359168d 100644 --- a/src/purge.ts +++ b/src/purge.ts @@ -5,7 +5,7 @@ import { Logger } from './options/logger'; import { Directory } from './interfaces/directory.interface'; import { FileTreeLocation } from './interfaces/location.interface'; -export function purge(rootTree: Directory, shouldPurge: boolean, barrelName: string, logger: Logger) { +export function purge(rootTree: Directory, shouldPurge: boolean, noHeader: boolean, barrelName: string, logger: Logger) { // Delete any existing barrels. if (shouldPurge) { logger.debug(`Purging barrels for ${rootTree}`); @@ -15,8 +15,10 @@ export function purge(rootTree: Directory, shouldPurge: boolean, barrelName: str return file.name === barrelName; }) .filter(file => { - const isGenerated = fs.readFileSync(file.path).includes('@file Automatically generated by barrelsby.'); - return file.name === barrelName && isGenerated; + if(noHeader) { + return true; + } + return fs.readFileSync(file.path).includes('@file Automatically generated by barrelsby.'); }) .forEach((file: FileTreeLocation) => { logger.debug(`Deleting existing barrel @ ${file.path}`);