Skip to content

Commit

Permalink
updated typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
goerwin committed Feb 26, 2022
1 parent de68f83 commit c91e07f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "directory-validator",
"version": "1.5.0",
"version": "1.5.1",
"description": "CLI Tool to validate directory structures.",
"main": "lib/index.js",
"bin": {
Expand All @@ -20,7 +20,7 @@
"@types/node": "^14.14.37",
"jest": "^26.6.3",
"ts-jest": "^26.5.4",
"typescript": "^4.2.3"
"typescript": "^4.5.5"
},
"dependencies": {
"ajv": "^8.0.2",
Expand Down
12 changes: 9 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ export class JsonParseError extends Error {
err: Error;
filePath: string;

constructor(err: Error, filePath: string) {
super(err.message);
this.err = err;
constructor(err: unknown, filePath: string) {
const parsedError = isError(err) ? err : new Error('unknown error');

super(parsedError.message);
this.err = parsedError;
this.filePath = filePath;
}
}
Expand Down Expand Up @@ -46,3 +48,7 @@ export class ValidatorInvalidPathError extends Error {
this.path = path;
}
}

export function isError(err: any): err is Error {
return Boolean(err && err.stack && err.message);
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ if (selectedOptions.init) {
ignoreFilesGlob: selectedOptions.ignoreFiles,
});

console.log('Directory successfully validated!');

if (selectedOptions.print && results.asciiTree) {
console.log(
results.asciiTree
Expand Down Expand Up @@ -114,12 +116,13 @@ if (selectedOptions.init) {
} else if (err instanceof errors.ValidatorInvalidPathError) {
console.error(errorTitle);
console.error('\t', dash, err.path.red, 'was not validated');
} else {
} else if (errors.isError(err)) {
console.error(errorTitle);
console.error('\t', dash, err.message.red);
} else {
console.error('Unknown error');
}

console.error();
process.exit(1);
}
}
2 changes: 1 addition & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getConfig(rulesPath: string): types.Config {

try {
configJson = JSON.parse(fs.readFileSync(rulesPath, 'utf8'));
} catch (err: any) {
} catch (err) {
throw new errors.JsonParseError(err, rulesPath);
}

Expand Down

0 comments on commit c91e07f

Please sign in to comment.