Skip to content

Commit

Permalink
feat(config): Add option to remove empty lines from output
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadashy committed Jul 27, 2024
1 parent e76a038 commit ab3687a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Create a `repopack.config.json` file in your project root for custom configurati
|`output.filePath`| The name of the output file | `"repopack-output.txt"` |
|`output.headerText`| Custom text to include in the file header |`null`|
|`output.removeComments`| Whether to remove comments from supported file types | `false` |
|`output.removeEmptyLines`| Whether to remove empty lines from the output | `false` |
|`output.topFilesLength`| Number of top files to display in the summary. If set to 0, no summary will be displayed |`5`|
|`output.showLineNumbers`| Whether to add line numbers to each line in the output |`false`|
|`ignore.useDefaultPatterns`| Whether to use default ignore patterns |`true`|
Expand Down
1 change: 1 addition & 0 deletions src/config/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const defaultConfig: RepopackConfigDefault = {
output: {
filePath: 'repopack-output.txt',
removeComments: false,
removeEmptyLines: false,
topFilesLength: 5,
showLineNumbers: false,
},
Expand Down
6 changes: 5 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface RepopackConfigBase {
filePath?: string;
headerText?: string;
removeComments?: boolean;
removeEmptyLines?: boolean;
topFilesLength?: number;
showLineNumbers?: boolean;
};
Expand All @@ -16,7 +17,8 @@ export type RepopackConfigDefault = RepopackConfigBase & {
output: {
filePath: string;
headerText?: string;
removeComments?: boolean;
removeComments: boolean;
removeEmptyLines: boolean;
topFilesLength: number;
showLineNumbers: boolean;
};
Expand All @@ -31,6 +33,7 @@ export type RepopackConfigFile = RepopackConfigBase & {
filePath?: string;
headerText?: string;
removeComments?: boolean;
removeEmptyLines?: boolean;
topFilesLength?: number;
showLineNumbers?: boolean;
};
Expand All @@ -45,6 +48,7 @@ export type RepopackConfigCli = RepopackConfigBase & {
filePath?: string;
headerText?: string;
removeComments?: boolean;
removeEmptyLines?: boolean;
topFilesLength?: number;
showLineNumbers?: boolean;
};
Expand Down
19 changes: 16 additions & 3 deletions src/utils/fileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function processFile(
const encoding = jschardet.detect(buffer).encoding || 'utf-8';
let content = iconv.decode(buffer, encoding);

content = preprocessContent(content);
content = preprocessContent(content, config);

if (config.output.removeComments) {
const manipulator = getFileManipulator(filePath);
Expand All @@ -40,6 +40,19 @@ export async function processFile(
return content;
}

export function preprocessContent(content: string): string {
return content.trim();
export function preprocessContent(content: string, config: RepopackConfigMerged): string {
content = content.trim();

if (config.output.removeEmptyLines) {
content = removeEmptyLines(content);
}

return content;
}

function removeEmptyLines(content: string): string {
return content
.split('\n')
.filter((line) => line.trim() !== '')
.join('\n');
}
10 changes: 9 additions & 1 deletion tests/core/outputGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ describe('outputGenerator', () => {

test('generateOutput should write correct content to file', async () => {
const mockConfig = createMockConfig({
output: { filePath: 'output.txt', topFilesLength: 2, showLineNumbers: false },
output: {
filePath: 'output.txt',
topFilesLength: 2,
showLineNumbers: false,
removeComments: false,
removeEmptyLines: false,
},
});
const mockPackedFiles = [
{ path: 'file1.txt', content: 'content1' },
Expand All @@ -40,6 +46,8 @@ describe('outputGenerator', () => {
headerText: 'Custom header text',
topFilesLength: 2,
showLineNumbers: false,
removeComments: false,
removeEmptyLines: false,
},
});

Expand Down
55 changes: 54 additions & 1 deletion tests/utils/fileHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,61 @@ describe('fileHandler', () => {

test('preprocessContent should trim content', () => {
const content = ' Some content with whitespace \n';
const result = preprocessContent(content);
const config = createMockConfig({
output: {
filePath: 'output.txt',
topFilesLength: 2,
showLineNumbers: false,
removeComments: false,
removeEmptyLines: false,
},
});
const result = preprocessContent(content, config);

expect(result).toBe('Some content with whitespace');
});

test('preprocessContent should remove empty lines when configured', () => {
const content = `
Some content
with empty lines
in between
`;
const config = createMockConfig({
output: {
filePath: 'output.txt',
topFilesLength: 2,
showLineNumbers: false,
removeComments: false,
removeEmptyLines: true,
},
});
const result = preprocessContent(content, config);

expect(result).toBe('Some content\n with empty lines\n in between');
});

test('preprocessContent should not remove empty lines when not configured', () => {
const content = `
Some content
with empty lines
in between
`;
const config = createMockConfig({
output: {
filePath: 'output.txt',
topFilesLength: 2,
showLineNumbers: false,
removeComments: false,
removeEmptyLines: false,
},
});
const result = preprocessContent(content, config);

expect(result).toBe('Some content\n\n with empty lines\n\n in between');
});
});

0 comments on commit ab3687a

Please sign in to comment.