Skip to content

Commit

Permalink
Refactoring config parsing module and index.ts
Browse files Browse the repository at this point in the history
- clean up whitespace
- move TOML example to examples folder
- store command line arguments in object
- removed version & help switches in example toml
- refactored config parser module
- changed output directory in example toml
- added -c usage to documentation
- removed unnecessary package-lock file
  • Loading branch information
paulkim26 committed Oct 14, 2023
1 parent cb1f734 commit df4dd8b
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 176 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This tool converts [TIL](https://simonwillison.net/2022/Nov/6/what-to-blog-about/) posts written in [Markdown](https://www.markdownguide.org/) (.txt, .md) into static HTML pages.

## Features

- Parses a title from the input files. If the first line is followed by two blank lines, a `<h1>` tag will be generated in the HTML.
- Can define a custom output directory with the `--output`/`-o` argument.
- Program will exit with a non-zero exit code in an error occurred.
- Added support for Markdown horizontal rules.
- Can load arguments from a `.toml` configuration file.

## Setup

### Install Bun
Expand Down Expand Up @@ -36,7 +44,7 @@ bun install
### Run tool

```bash
bun start [-h | --help] [-v | --version] [-o | --output directory] filename | directory
bun start [-h | --help] [-v | --version] [-o | --output directory] [-c | --config filepath] filepath | directory
```

### Compile executable
Expand All @@ -48,7 +56,7 @@ bun run build
### Run tool as executable

```bash
til-to-html [-h | --help] [-v | --version] [-o | --output directory] filename | directory
til-to-html [-h | --help] [-v | --version] [-o | --output directory] [-c | --config filepath] filepath | directory
```

### Command Line Arguments
Expand All @@ -58,6 +66,7 @@ til-to-html [-h | --help] [-v | --version] [-o | --output directory] filename |
| `-v`, `--version` | Display version information. |
| `-h`, `--help` | Display usage message. |
| `-o`, `--output` | Set custom output directory. If omitted, files will be output to a folder named `til`. (optional) |
| `-c`, `--config` | Use a `.toml` configuration file. |

### Examples

Expand All @@ -76,24 +85,23 @@ bun start -h
#### Parse 1 file

```bash
bun start examples/til.txt
bun start ./examples/til.txt
```

#### Parse a folder of files

```bash
bun start examples/dir
bun start ./examples/dir
```

#### Parse a folder of files and output to a new directory

```bash
bun start examples/dir -o output
bun start ./examples/dir -o output
```

## Additional Features
#### Load a `.toml` configuration file

- Parses a title from the input files. If the first line is followed by two blank lines, a `<h1>` tag will be generated in the HTML.
- Can define a custom output directory with the `--output`/`-o` argument.
- Program will exit with a non-zero exit code in an error occurred.
- Add support for Markdown horizontal rules.
```bash
bun start -c ./examples/config.toml
```
7 changes: 7 additions & 0 deletions examples/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TOML sample configuration file

# Set input directory
target = "./examples/dir"

# Output directory where generated files will be saved
output = "./til"
64 changes: 0 additions & 64 deletions package-lock.json

This file was deleted.

16 changes: 0 additions & 16 deletions src/config-example.toml

This file was deleted.

14 changes: 1 addition & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import parseArguments from "~/parseArguments";
import configuration from "./options/configuration";
import configurationParse from "./options/configurationParse";

var args = Bun.argv.slice(2);
try{
if(args[0] === "-c" || args[0] === "--config"){
const configurationPath = args[1];
const configurationArgs = configuration(configurationPath);
args = configurationParse(configurationArgs);
}
} catch (e: any) {
console.error(e.message);
process.exit(1);
}
const args = Bun.argv.slice(2);
try {
await parseArguments(args);
process.exit(0);
Expand Down
20 changes: 0 additions & 20 deletions src/options/configuration.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/options/configurationParse.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/options/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export default async function help() {
message += `Description: ${tool} is a tool that converts TIL Markdown posts into static HTML pages.\n\n`;

message += `Options:\n`;
message += `\t-h, --help\t\tDisplay usage.\n`;
message += `\t-v, --version\t\tDisplay version.\n`;
message += `\t-o, --output DIRECTORY\tIndicate a custom output directory.\n`;
message += `\t-h, --help\t\tdisplay usage\n`;
message += `\t-v, --version\t\tdisplay version\n`;
message += `\t-o, --output DIRECTORY\tindicate custom output directory\n`;
message += `\t-c, --config DIRECTORY\tindicate config TOML file to use\n`;
message += `\n`;

message += `Arguments:\n`;
Expand Down
16 changes: 16 additions & 0 deletions src/options/parseConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import toml from "toml";

export default async function parseConfig(configPath: string) {
const file = Bun.file(configPath);
const fileExists = await file.exists();
if (!fileExists) {
throw new Error(`Configuration file not found at '${configPath}'.`);
}

try {
const configString = await file.text();
return toml.parse(configString);
} catch (err) {
throw new Error(`Error reading or parsing TOML file.`);
}
}
50 changes: 35 additions & 15 deletions src/parseArguments.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import version from "~/options/version";
import help from "~/options/help";
import parseTarget from "~/parseTarget.ts";
import parseConfig from "~/options/parseConfig";

const OUTPUT_DIR_DEFAULT = "til";

export default async function parseArguments(args: string[]) {
let target: string | null = null;
let exit = false;
let outputDir = OUTPUT_DIR_DEFAULT;
let options: any = {};
let configProvided = false;

if (args.length === 0) {
throw new Error(`Add option -h for help.`);
}

for (let i = 0; i < args.length && !exit; i++) {

for (let i = 0; i < args.length && !configProvided; i++) {
const arg = args[i];
const isFinalArg = i === args.length - 1;

switch (arg) {
case "--version":
Expand All @@ -28,24 +28,44 @@ export default async function parseArguments(args: string[]) {
return false;
case "--output":
case "-o":
if (isFinalArg) {
throw new Error(`Option ${arg} is missing an operand.`);
}
options.output = args[++i];
break;
case "--config":
case "-c":
if (isFinalArg) {
throw new Error(`Option ${arg} is missing an operand.`);
}

const finalArg = i === args.length - 1;
configProvided = true;
const configPath = args[++i];
const config = await parseConfig(configPath);

if (finalArg) {
throw new Error(`Option ${arg} is missing an operand.`);
} else {
outputDir = args[i + 1];
i++; // Skip over next argument
options = {};

if ("target" in config) {
options.target = config.target;
}

if ("output" in config) {
options.output = config.output;
}
break;
default:
target = arg;
options.target = arg;
}
}

if (!!target) {
await parseTarget(target, outputDir);
} else {
if (!("target" in options)) {
throw new Error("Missing target files.");
}

// Default output directory if missing
if (!("output" in options)) {
options.output = OUTPUT_DIR_DEFAULT;
}

await parseTarget(options.target, options.output);
}

0 comments on commit df4dd8b

Please sign in to comment.