From 640e63a3cbf373bebb38270a23c1d5ef6a065117 Mon Sep 17 00:00:00 2001 From: Im-Beast Date: Fri, 7 Jun 2024 23:40:13 +0200 Subject: [PATCH] fix: exit code 1 on errors --- LICENSE.md | 2 +- README.md | 53 ++++++++++++++++++++++++++------------------ find-license.sh | 7 +++--- mod.ts | 2 +- src/cli.ts | 12 ++++------ src/license_check.ts | 9 ++++---- src/parse_args.ts | 2 +- 7 files changed, 47 insertions(+), 40 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index b24efb1..c62e8fa 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # The MIT License (MIT) -## Copyright © 2021 Im-Beast +## Copyright © 2021–2024 Im-Beast Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in diff --git a/README.md b/README.md index dd32d9f..ab1f37c 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,52 @@ # 📝 Anzu + Anzu is very light CLI tool for checking whether files have license header ![Anzu help output](./docs/help.png) ![Anzu example output](./docs/example-output.png) ## Installation -Remember you have to have deno path set when installing. Do it using this bash command: -`echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc` -You may want to replace `~/.bashrc` with your shell rc config, eg. `~/.zshrc` for zsh + +Remember you have to have deno path set when installing. Do it using this bash +command: `echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc` You may want +to replace `~/.bashrc` with your shell rc config, eg. `~/.zshrc` for zsh - Without explicit permissions: - - `deno install -n anzu https://deno.land/x/anzu@1.0.0/src/cli.ts` + - `deno install -n anzu https://deno.land/x/anzu@$MODULE_VERSION/src/cli.ts` - With explicit permissions: - - `deno install -n anzu --allow-read --allow-write --allow-net https://deno.land/x/anzu@1.0.0/src/cli.ts` -- You can also do `deno run https://deno.land/x/anzu@1.0.0/src/cli.ts` without actually installing it + - `deno install -n anzu --allow-read --allow-write --allow-net https://deno.land/x/anzu@$MODULE_VERSION/src/cli.ts` +- You can also do `deno run https://deno.land/x/anzu@$MODULE_VERSION/src/cli.ts` + without actually installing it ## 📚 Why not [deno_license_checker](https://github.com/kt3k/deno_license_checker) + I got discouraged from deno_license_checker because of several things, majorly: - - Its size – 177KB - - Performance - - Requirement of config file - - Doesn't ask for permissions – they have to be specified otherwise it'll exit + +- Its size – 177KB +- Performance +- Requirement of config file +- Doesn't ask for permissions – they have to be specified otherwise it'll exit Anzu solves some of these problems: - - It's significantly smaller – 27KB - - In my case its multiple times faster (benchmarks needed) - - Every option has to be set in CLI - - If you want to launch same command using one command – just create bash script that does that (see [here](./find-license.sh)) - - If permissions aren't specified it automatically requests you for them + +- It's significantly smaller – 27KB +- In my case its multiple times faster (benchmarks needed) +- Every option has to be set in CLI +- If you want to launch same command using one command – just create bash script + that does that (see [here](./find-license.sh)) +- If permissions aren't specified it automatically requests you for them Why not Anzu? - - It's not compatible with windows (Im 99.9% sure, but didn't tested it) - - If you prefer having external config file Anzu probably isn't for you + +- It's not compatible with windows (Im 99.9% sure, but didn't tested it) +- If you prefer having external config file Anzu probably isn't for you What can Anzu also do? - - Additionally it can load license template from given URL and Path - - It can search for license using RegExp - - Exclude files and directories using regexp - - You can see full functionality using `-h` option. + +- Additionally it can load license template from given URL and Path +- It can search for license using RegExp +- Exclude files and directories using regexp +- You can see full functionality using `-h` option. ## 🤝 Contributing @@ -46,4 +55,4 @@ pull requests! ## 📝 Licensing -This project is available under MIT License conditions. \ No newline at end of file +This project is available under MIT License conditions. diff --git a/find-license.sh b/find-license.sh index 1d8f13c..578bb9b 100644 --- a/find-license.sh +++ b/find-license.sh @@ -4,9 +4,10 @@ # Look for license with this pattern (when license is regexp it cannot be prepended!) # Prepend license to the top of the file when -deno run ./src/cli.ts \ +deno run $1 ./src/cli.ts \ +$2 \ -i ./ "/.+\.ts/" \ -e "deps.ts" \ --l "// Copyright 2021 Im-Beast. All rights reserved. MIT license." \ --p +-l "// Copyright 2024 Im-Beast. All rights reserved. MIT license." \ + diff --git a/mod.ts b/mod.ts index 203133b..1494bc6 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ -// Copyright 2021 Im-Beast. All rights reserved. MIT license. +// Copyright 2024 Im-Beast. All rights reserved. MIT license. export * from "./src/cli.ts"; export * from "./src/deps.ts"; export * from "./src/license_check.ts"; diff --git a/src/cli.ts b/src/cli.ts index 6b02820..e47606b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,4 +1,4 @@ -// Copyright 2021 Im-Beast. All rights reserved. MIT license. +// Copyright 2024 Im-Beast. All rights reserved. MIT license. import { bold, cyan, @@ -87,7 +87,6 @@ const options: Options = { styleArg("--license") } option`, ); - Deno.exit(1); } let prepend: PrependLicense = PrependLicense.Never; @@ -107,7 +106,6 @@ const options: Options = { } option ${error.message}`, error, ); - Deno.exit(1); } } } @@ -118,12 +116,11 @@ const options: Options = { } const license = await compileLicense( - options.license.value[0], + options.license.value![0], normalizeNewlines, ); if (!license) { cliError(`Given ${styleArg("--license")} argument is invalid!`); - Deno.exit(1); } try { @@ -178,6 +175,7 @@ function cliError(message: string, error?: Error): void { throw error || new Error(message); } else { console.log(`${red("Error")} ${yellow(">")} ${message}`); + Deno.exit(1); } } @@ -239,10 +237,9 @@ if (import.meta.main) { } for (const [name, args] of entries) { - const option = optionLinks.get(name); + const option = optionLinks.get(name)!; if (!option) { cliError(`Option ${cyan(name)} has not been found`); - Deno.exit(1); } if (option.args) { @@ -253,7 +250,6 @@ if (import.meta.main) { cyan(name) } is missing`, ); - Deno.exit(1); } } } diff --git a/src/license_check.ts b/src/license_check.ts index 394ab5f..7b9d0ad 100644 --- a/src/license_check.ts +++ b/src/license_check.ts @@ -1,4 +1,3 @@ -// Copyright 2021 Im-Beast. All rights reserved. MIT license. import { cyan, gray, green, red, white, yellow } from "./deps.ts"; let permissionDelay = 0; @@ -245,9 +244,7 @@ export async function checkForLicense( } } - if ( - prepend !== PrependLicense.Never && license.value instanceof RegExp - ) { + if (prepend !== PrependLicense.Never && license.value instanceof RegExp) { throw new Error( "RegExp license value can only be used to search for a license!", ); @@ -356,4 +353,8 @@ export async function checkDirectoryForLicenses( ${formatIndent(`Checked ${checkedFiles} files`)} (${filesMarker}) ${formatIndent(`It took`)} ${Date.now() - start}ms ${delay}`, ); + + if (status[0] && prepend === PrependLicense.Never) { + throw new Error("Files with no license have been found"); + } } diff --git a/src/parse_args.ts b/src/parse_args.ts index bc58d92..cef3b7f 100644 --- a/src/parse_args.ts +++ b/src/parse_args.ts @@ -1,4 +1,4 @@ -// Copyright 2021 Im-Beast. All rights reserved. MIT license. +// Copyright 2024 Im-Beast. All rights reserved. MIT license. export interface ParsedArgs { [key: string]: string[]; }