Skip to content

Commit

Permalink
feat: add api docs generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nmerget committed Sep 30, 2024
1 parent a6b4d35 commit 891bbb5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 40 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm run generate:api-docs
npm run lint
npm run build
npm run test
30 changes: 1 addition & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,4 @@ Tool for using [GodotJS](https://github.com/godotjs/javascript) with TypeScript.

## API

### Help

```shell
@godot-js/godot-ts --help
```

### Init

Creates a new GodotJS project with TypeScript support

```shell
@godot-js/godot-ts init
```

### Build

Build `*.ts` files once and minified

```shell
@godot-js/godot-ts build
```

### Watch

Watching for changes in `*.ts` files and rebuilding on need

```shell
@godot-js/godot-ts watch
```
Look at the [docs](./docs/API.md).
36 changes: 36 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# API - @godot-js/ts

CLI for using GodotJS with TypeScript

## init

Creates a new GodotJS project with TypeScript support

| long | short | description | required | defaultValue |
| :-------------- | :---: | :----------------------------------------------------- | :------: | :----------- |
| `--name` | `-n` | The name of your project | `` | `MyGame` |
| `--out` | `-o` | Relative path where project is written | `` | `.` |
| `--forceDelete` | `-f` | Removes project dir if it's already there | `` | `false` |
| `--dry` | `-d` | Do a dry run with this command - prints/returns output | `` | `false` |

## build

Build typescript files

| long | short | description | required | defaultValue |
| :---------------- | :---: | :----------------------------------------------------- | :------: | :----------------------- |
| `--src` | `-s` | Relative path where *.ts files located | `` | `./src` |
| `--out` | `-o` | Relative path where *.ts files are written | `` | `./scripts/js/generated` |
| `--dry` | `-d` | Do a dry run with this command - prints/returns output | `` | `false` |
| `--minifyClasses` | `-mc` | Minifies GodotJS classes | `` | `true` |

## watch

Watch typescript files

| long | short | description | required | defaultValue |
| :------ | :---: | :----------------------------------------------------- | :------: | :----------------------- |
| `--src` | `-s` | Relative path where *.ts files located | `` | `./src` |
| `--out` | `-o` | Relative path where *.ts files are written | `` | `./scripts/js/generated` |
| `--dry` | `-d` | Do a dry run with this command - prints/returns output | `` | `false` |

11 changes: 11 additions & 0 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
"test"
],
"scripts": {
"prebuild": "tsx src/utils/prepare.ts",
"pretest": "tsx src/utils/prepare.ts",
"build": "node esbuild.js",
"generate:api-docs": "tsx src/api-docs.ts",
"lint": "eslint *.ts",
"prepare": "husky",
"postinstall": "tsx src/utils/prepare.ts",
"prebuild": "tsx src/utils/prepare.ts",
"prepare": "husky",
"pretest": "tsx src/utils/prepare.ts",
"test": "vitest run --no-file-parallelism"
},
"dependencies": {
Expand All @@ -44,6 +45,7 @@
"find-versions-cli": "^5.0.0",
"globals": "^15.9.0",
"husky": "^9.0.11",
"markdown-table": "^3.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"tsx": "^4.19.1",
Expand Down
44 changes: 44 additions & 0 deletions src/api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getProgram } from "./program";
import { programDescription, programName } from "./data";
import { markdownTable } from "markdown-table";
import { writeFileSync } from "node:fs";

const getBooleanValue = (value: unknown): string => {
if (String(value) === "true") {
return "✅";
} else if (String(value) === "false") {
return "❌";
}

return String(value);
};

export const generateApiDocs = (name: string, description: string) => {
const mProgram = getProgram(name, description);
let result = `# API - ${mProgram.name()}\n\n`;
result += `${mProgram.description()}\n\n`;

mProgram.commands.forEach((command) => {
result += `## ${command.name()}\n\n`;
result += `${command.description()}\n\n`;
const mTable: string[][] = [
["long", "short", "description", "required", "defaultValue"],
];
command.options.forEach(
({ description, required, short, long, defaultValue }) => {
mTable.push([
`\`${long}\``,
`\`${short}\``,
description,
`\`${getBooleanValue(required)}\``,
`\`${defaultValue}\``,
]);
},
);
result += `${markdownTable(mTable, { align: ["l", "c", "l", "c", "l"] })}\n\n`;
});

writeFileSync("./docs/API.md", result);
};

generateApiDocs(programName, programDescription);
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import startProgram from "./program";
import { startProgram } from "./program";
import {programDescription, programName} from "./data";

startProgram("@godot-js/ts", "CLI for using GodotJS with TypeScript");
startProgram(programName, programDescription);
3 changes: 3 additions & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { buildAction } from "./commands/build";
import { initOptions } from "./commands/init/data";
import { initAction } from "./commands/init";

export const programName = "@godot-js/ts";
export const programDescription = "CLI for using GodotJS with TypeScript";

export type Command = {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
14 changes: 8 additions & 6 deletions src/program.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { program, Command } from "commander";
import { Command, program } from "commander";
import { godotTS } from "./data";

const startProgram = (name: string, description: string) => {
program.name(name).description(description);
export const getProgram = (name: string, description: string) => {
const mProgram = program.name(name).description(description);

for (const command of godotTS) {
const pCommand = new Command(command.name);
Expand Down Expand Up @@ -37,10 +37,12 @@ const startProgram = (name: string, description: string) => {
}

pCommand.action(command.action);
program.addCommand(pCommand);
mProgram.addCommand(pCommand);
}

program.parse();
return mProgram;
};

export default startProgram;
export const startProgram = (name: string, description: string) => {
getProgram(name, description).parse();
};

0 comments on commit 891bbb5

Please sign in to comment.