Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Make barrel header optional #376

Merged
merged 4 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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