Skip to content

Commit cf347b1

Browse files
committed
fix: improve file handling and error logging in linting process
1 parent b5022d6 commit cf347b1

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/builder/src/builder.spec.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { Architect } from '@angular-devkit/architect';
55
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
66
import { logging, schema } from '@angular-devkit/core';
77
import * as path from 'path';
8+
import * as fs from 'fs/promises';
89

910
describe('Lint', () => {
1011
let architect: Architect;
1112
let architectHost: TestingArchitectHost;
1213
let logger: logging.Logger;
1314

1415
beforeEach(async () => {
15-
const fs = await import('fs/promises');
1616
await fs.copyFile('test/src/autofixable.ts.bak', 'test/src/autofixable.ts');
1717
await fs.copyFile('test/src/file.ts.bak', 'test/src/file.ts');
1818
await fs.copyFile('test/src/file.css.bak', 'test/src/file.css');
@@ -34,12 +34,12 @@ describe('Lint', () => {
3434
});
3535

3636
afterEach(async () => {
37-
const fs = await import('fs/promises');
3837
// Remove test files but keep the .bak files
3938
await Promise.all([
4039
fs.rm('test/src/autofixable.ts', { force: true }),
4140
fs.rm('test/src/file.ts', { force: true }),
4241
fs.rm('test/src/file.css', { force: true }),
42+
fs.rm('test/lint-report.txt', { force: true }),
4343
]);
4444
});
4545

@@ -243,4 +243,39 @@ describe('Lint', () => {
243243
expect(output).not.toContain('file.ts');
244244
expect(output).not.toContain('file.css');
245245
});
246+
247+
it('writes JSON output to file with fix enabled', async () => {
248+
const outputFile = 'lint-report.txt';
249+
250+
const run = await architect.scheduleBuilder(
251+
'@krema/angular-eslint-stylelint-builder:lint',
252+
{
253+
eslintFilePatterns: ['src/**/*.ts'],
254+
stylelintFilePatterns: ['src/**/*.css'],
255+
fix: true,
256+
outputFile,
257+
format: 'json',
258+
eslintConfig: 'eslint.config.js',
259+
stylelintConfig: 'stylelint.config.js',
260+
},
261+
{ logger }
262+
);
263+
await run.result;
264+
await run.stop();
265+
logger.complete();
266+
267+
// Check that the output file exists and contains valid JSON
268+
let exists = false;
269+
try {
270+
await fs.access(path.join('test', outputFile));
271+
exists = true;
272+
} catch {
273+
exists = false;
274+
}
275+
expect(exists).toBe(true);
276+
if (exists) {
277+
const content = await fs.readFile(path.join('test', outputFile), 'utf8');
278+
expect(() => JSON.parse(content)).not.toThrow();
279+
}
280+
});
246281
});

lib/builder/src/eslint/reporter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export async function report(
4242
if (options.outputFile) {
4343
const pathToOutputFile = join(workspaceRoot, options.outputFile);
4444
createDirectory(dirname(pathToOutputFile));
45-
writeFileSync(pathToOutputFile, formattedResults);
45+
try {
46+
writeFileSync(pathToOutputFile, formattedResults || '');
47+
} catch (err) {
48+
context.logger.error(`[DEBUG] Failed to write output file: ${err}`);
49+
}
4650
} else {
4751
context.logger.info(formattedResults);
4852
}

lib/builder/src/utils/create-directory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { mkdirSync, statSync } from 'fs';
33

44
export function createDirectory(directoryPath: string): void {
55
const parentPath = dirname(directoryPath);
6-
if (!directoryExists(parentPath)) {
6+
if (!directoryExists(parentPath) && parentPath !== directoryPath) {
77
createDirectory(parentPath);
88
}
99
if (!directoryExists(directoryPath)) {
10-
mkdirSync(directoryPath);
10+
mkdirSync(directoryPath, { recursive: true });
1111
}
1212
}
1313

0 commit comments

Comments
 (0)