Skip to content

Commit

Permalink
fix: handle invalid file and log path
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Nov 6, 2023
1 parent f35e92b commit f5a0e62
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/analyser/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ exports[`analyser > should run correctly 1`] = `
"languages": {
"AMPL": 1,
"HCL": 1,
"JSON": 1,
"JSON": 2,
"YAML": 2,
},
"name": "fake",
Expand Down Expand Up @@ -1155,7 +1155,7 @@ exports[`analyser > should run correctly 2`] = `
"Go": 1,
"HCL": 1,
"HTML": 1,
"JSON": 4,
"JSON": 5,
"SCSS": 1,
"TOML": 1,
"YAML": 2,
Expand Down Expand Up @@ -1805,7 +1805,7 @@ exports[`analyser > should run correctly 2`] = `
"Go": 3,
"HCL": 2,
"HTML": 3,
"JSON": 11,
"JSON": 13,
"SCSS": 3,
"TOML": 3,
"YAML": 4,
Expand Down
2 changes: 1 addition & 1 deletion src/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ProviderFile {
export interface BaseProvider {
basePath: string;
listDir: (pathRelative: string) => Promise<ProviderFile[]>;
open: (path: string) => Promise<string>;
open: (path: string) => Promise<string | null>;
}

export const IGNORED_DIVE_PATHS = [
Expand Down
14 changes: 10 additions & 4 deletions src/provider/fs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs/promises';
import path from 'node:path';

import { l } from '../common/log.js';

import type { BaseProvider, ProviderFile } from './base.js';

export interface FSProviderOptions {
Expand Down Expand Up @@ -33,9 +35,13 @@ export class FSProvider implements BaseProvider {
return list;
}

async open(pathRelative: string): Promise<string> {
const content = await fs.readFile(pathRelative);

return content.toString();
async open(pathRelative: string): Promise<string | null> {
try {
const content = await fs.readFile(pathRelative);
return content ? content.toString() : null;
} catch (err) {
l.error('Failed to open file', { pathRelative, err });
return null;
}
}
}
1 change: 0 additions & 1 deletion src/rules/spec/docker/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const detectDockerComponent: ComponentMatcher = async (

const content = await provider.open(file.fp);
if (!content) {
l.warn('Failed to open Docker file', file.fp);
continue;
}

Expand Down
1 change: 0 additions & 1 deletion src/rules/spec/githubActions/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const detectGithubActionsComponent: ComponentMatcher = async (

const content = await provider.open(file.fp);
if (!content) {
l.warn('Failed to open GitHub Actions file', file.fp);
continue;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/__fixtures__/nodejs/invalid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"
}

0 comments on commit f5a0e62

Please sign in to comment.