Skip to content

Commit

Permalink
✨ Make barrel header optional (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekarel authored Oct 8, 2022
1 parent 14ddc38 commit b8e8b43
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export * from "./barrel";
export { default as barrel } from "./barrel";
```

### `-H` or `--help`
### `-h` or `--help`

Displays help information on the command line arguments that barrelsby accepts.

Expand Down Expand Up @@ -203,6 +203,10 @@ Use 'single quotes' in the generated barrel files instead of the default "double

Omit semicolons from the end of lines in the generated barrel files.

### `-H` or `--noHeader`

Omit adding a header comment to the top of the barrel file.

### `-v` or `--version`

Display the barrelsby version number.
Expand Down
3 changes: 3 additions & 0 deletions src/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('builder/builder module has a', () => {
let builderSpy: Sinon.SinonSpy<
[
{
addHeader: boolean;
directory: Directory;
barrelType: StructureOption;
quoteCharacter: QuoteCharacter;
Expand All @@ -52,6 +53,7 @@ describe('builder/builder module has a', () => {
loggerSpy = spySandbox.spy(logger, 'debug');
builderSpy = spySandbox.spy(BuildBarrelModule, 'buildBarrel');
build({
addHeader: true,
destinations: directory.directories,
quoteCharacter: '"',
semicolonCharacter: ';',
Expand Down Expand Up @@ -139,6 +141,7 @@ describe('builder/builder module has a', () => {
const logger = new Signale();
const runBuilder = () => {
build({
addHeader: true,
destinations: directory.directories,
quoteCharacter: '"',
semicolonCharacter: ';',
Expand Down
2 changes: 2 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Directory } from './interfaces/directory.interface';
import { FileTreeLocation } from './interfaces/location.interface';

export const build = (params: {
addHeader: boolean;
destinations: Directory[];
quoteCharacter: QuoteCharacter;
semicolonCharacter: SemicolonCharacter;
Expand All @@ -26,6 +27,7 @@ export const build = (params: {
// Build the barrels.
params?.destinations?.forEach((destination: Directory) =>
buildBarrel({
addHeader: params.addHeader,
directory: destination,
barrelType: params.structure ?? StructureOption.FLAT,
quoteCharacter: params.quoteCharacter,
Expand Down
2 changes: 2 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('main module', () => {
});
it('should co-ordinate the main stages of the application', () => {
const args: any = {
noHeader: false,
baseUrl: './',
delete: true,
directory: ['testRootPath'],
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('main module', () => {
expect(getDestinationsSpy.calledOnceWithExactly(builtTree, args.location, barrelName, signale)).toBeTruthy();
expect(purgeSpy.calledOnceWithExactly(builtTree, args.delete, barrelName, signale)).toBeTruthy();
expect(buildBarrelsSpy).toHaveBeenCalledWith({
addHeader: true,
destinations,
quoteCharacter,
semicolonCharacter,
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export function Barrelsby(args: Arguments) {
// Create the barrels.
const quoteCharacter = getQuoteCharacter(args.singleQuotes as boolean);
const semicolonCharacter = getSemicolonCharacter(args.noSemicolon as boolean);
// Add header to each barrel if the `noHeader` option is not true
const addHeader = args.noHeader === false;

await build({
addHeader,
destinations,
quoteCharacter,
semicolonCharacter,
Expand Down
7 changes: 7 additions & 0 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Arguments {
delete?: boolean;
exclude?: string[];
exportDefault?: boolean;
noHeader?: boolean;
help?: boolean;
include?: string[];
local?: boolean;
Expand Down Expand Up @@ -66,6 +67,12 @@ export function getOptionsConfig(configParser: any): {
alias: 'exportDefault',
description: 'Also export the default export of the file. Currently works only with the `flat` mode.',
},
H: {
type: 'boolean',
alias: 'noHeader',
description: 'Do not add a header comment to the top of the barrel file.',
default: false,
},
i: {
type: 'array',
alias: 'include',
Expand Down
4 changes: 3 additions & 1 deletion src/tasks/BuildBarrel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { buildFlatBarrel } from '../builders/flat';
import { StructureOption } from '../options/options';

export const buildBarrel = ({
addHeader,
directory,
barrelType,
quoteCharacter,
Expand All @@ -25,6 +26,7 @@ export const buildBarrel = ({
include,
exclude,
}: {
addHeader: boolean;
directory: Directory;
barrelType: StructureOption;
quoteCharacter: QuoteCharacter;
Expand Down Expand Up @@ -68,7 +70,7 @@ export const buildBarrel = ({
return;
}
// Add the header
const contentWithHeader = addHeaderPrefix(content);
const contentWithHeader = addHeader ? addHeaderPrefix(content) : content;
fs.writeFileSync(destination, contentWithHeader);
// Update the file tree model with the new barrel.
if (!directory.files.some(file => file.name === barrelName)) {
Expand Down

0 comments on commit b8e8b43

Please sign in to comment.