Skip to content

Commit

Permalink
add tsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
catuhana committed Aug 23, 2023
1 parent 525c42f commit 3160e4b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ You can pass folder name or configuration file name by passing its path as an ar

## Exit Codes

| 0 | 1 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 255 |
| --------------------------- | -------------------------- | -------------------------------- | ----------------------------------- | -------------------------------------------------- | ---------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------- | ---------------------------------- | -------------------------- |
| Hooks created successfully. | An unknown error occurred. | Configuration file is not found. | Could not parse configuration file. | `githooks` field is missing in configuration file. | `githooks` field is not an Object. | No Git hook created. | Current folder is not a Git repository. (To specify a folder to create hooks in, `-g` flag can be used.) | Entered file is not a config file. | An unknown error occurred. |
| 0 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 255 |
| --------------------------- | -------------------------------- | ----------------------------------- | -------------------------------------------------- | ---------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------- | ---------------------------------- | ----------------------------- |
| Hooks created successfully. | Configuration file is not found. | Could not parse configuration file. | `githooks` field is missing in configuration file. | `githooks` field is not an Object. | No Git hook created. | Current folder is not a Git repository. (To specify a folder to create hooks in, `-g` flag can be used.) | Entered file is not a config file. | An unexpected error occurred. |
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ export const HOOKS = [
'post-index-change',
] as const;

/**
* Denoh version.
*/
export const VERSION = '3.0.0';

/**
* Help text for denoh CLI.
*/
export const HELP_TEXT = `
denoh - Generate Git hook by extending Deno's Configuration file.
Expand Down
3 changes: 3 additions & 0 deletions src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Exit codes enum when an error occurs.
*/
export enum ExitCodes {
Success = 0,
NotFound = 243,
Expand Down
12 changes: 10 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { ExitCodes } from './enums.ts';
import { error } from './logger.ts';
import { ExitCodes } from './enums.ts';

/**
* Custom error class for creating errors
* and logging and exiting with {@link DenohError.logAndExit} function.
*/
export class DenohError extends Error {
constructor(message: string, public exitCode?: ExitCodes) {
super(message);

this.name = 'DenohError';
}

/**
* Logs the error to the console and exists with
* specified exit code.
*/
logAndExit() {
error(this.message);
return Deno.exit(this.exitCode ?? 1);
return Deno.exit(this.exitCode ?? ExitCodes.UnknownError);
}
}
20 changes: 20 additions & 0 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const operatorsRegex = new RegExp(
`^(${Object.values(Operators).join('|')})$`,
);

/**
* Create hooks from {@link https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type | Record}<{@link GitHooks}, string[]>.
* @example
* const hooks = createHooks(config.gitHooks)
*/
export const createHooks = (
gitHooks: Awaited<ReturnType<typeof readConfig>>['gitHooks'],
) => {
Expand Down Expand Up @@ -48,6 +53,12 @@ export const createHooks = (
return createdHooks ?? null;
};

/**
* Writes hooks created by {@link createHooks} to the `hooksPath` path.
* @param hooksPath - Where to write hooks. Defaults to `configPath/hooksPath` is `-g` flag is not present.
* @param configPath - Where to write hooks in. Defaults to current folder.
* @example writeHooks(hooks)
*/
export const writeHooks = async (
hooks: ReturnType<typeof createHooks>,
hooksPath = '.git/hooks',
Expand Down Expand Up @@ -75,6 +86,10 @@ export const writeHooks = async (
return createdGitHooks;
};

/**
* Generates Git hook scripts from an array of strings.
* @example generateGitHookScript(["cleanup", "lint && fmt"])
*/
export const generateGitHookScript = (commands: string[]) => {
const script = ['#!/bin/sh'];

Expand All @@ -93,6 +108,11 @@ export const generateGitHookScript = (commands: string[]) => {
return script.join('\n');
};

/**
* Reads config and returns its `githooks` field.
* @param configPath - From where/which file to read the config. Defaults to current directory.
* @example const config = readConfig()
*/
export const readConfig = async (configPath = '.') => {
const fileExtensions = ['json', 'jsonc'];

Expand Down
16 changes: 16 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
/**
* Log levels enum with their background colurs.
*/
const LogLevels = {
error: 'red',
warn: 'orange',
info: 'green',
} as const;

/**
* Logs to console as an error to stderr, with red background.
*/
export const error = (...data: unknown[]) => {
logMessage('error', ...data);
};

/**
* Logs to console as a warning, with orange background.
*/
export const warn = (...data: unknown[]) => {
logMessage('warn', ...data);
};

/**
* Logs to console as an information, with green background.
*/
export const info = (...data: unknown[]) => {
logMessage('info', ...data);
};

/**
* A private function for styling the output and handling logging.
* @param level {@link LogLevels}
*/
function logMessage(level: keyof typeof LogLevels, ...data: unknown[]) {
console[level](
`%cdenoh%c ::%c`,
Expand Down
6 changes: 6 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { HOOKS } from './constants.ts';

import type { JsonValue } from 'std/jsonc/parse.ts';

/**
* Git hooks as an union type.
*/
export type GitHooks = typeof HOOKS[number];

/**
* A JSON value, with `githooks` field included.
*/
export type DenoConfig = JsonValue & {
githooks: Record<GitHooks, string[]>;
};

0 comments on commit 3160e4b

Please sign in to comment.