Skip to content

Commit

Permalink
fix: exit code 1 on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-Beast committed Jun 7, 2024
1 parent c8d40f3 commit 640e63a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
53 changes: 31 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/[email protected]/src/cli.ts`
- You can also do `deno run https://deno.land/x/[email protected]/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

Expand All @@ -46,4 +55,4 @@ pull requests!

## 📝 Licensing

This project is available under MIT License conditions.
This project is available under MIT License conditions.
7 changes: 4 additions & 3 deletions find-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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." \


2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
12 changes: 4 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -87,7 +87,6 @@ const options: Options = {
styleArg("--license")
} option`,
);
Deno.exit(1);
}

let prepend: PrependLicense = PrependLicense.Never;
Expand All @@ -107,7 +106,6 @@ const options: Options = {
} option ${error.message}`,
error,
);
Deno.exit(1);
}
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -253,7 +250,6 @@ if (import.meta.main) {
cyan(name)
} is missing`,
);
Deno.exit(1);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/license_check.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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!",
);
Expand Down Expand Up @@ -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");
}
}
2 changes: 1 addition & 1 deletion src/parse_args.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
Expand Down

0 comments on commit 640e63a

Please sign in to comment.