Skip to content

Commit d20adae

Browse files
committed
improvement: improve versioning and documentation
- Add a script to generate build metadata including git hash and timestamp to provide detailed versioning information in the CLI output. - Update the README with a comprehensive table of command-line arguments to improve documentation clarity and feature discoverability.
1 parent 2692120 commit d20adae

5 files changed

Lines changed: 51 additions & 7 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ package-lock.json
1717
# Environment variables
1818
.env
1919
.env.local
20+
21+
# Generated files
22+
src/buildInfo.ts

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ Run the script from the command line and pass the appropriate arguments.
4141

4242
### Command-Line Arguments
4343

44-
```bash
45-
docusaurus-docs-to-pdf -h
46-
# or
47-
npx docusaurus-docs-to-pdf -h
48-
```
44+
You can run `docusaurus-docs-to-pdf -h` to see the help message, or refer to the table below:
45+
46+
| Argument | Alias | Description | Required | Default |
47+
| :--- | :--- | :--- | :--- | :--- |
48+
| `--docs-url <url>` | `-u` | The base URL of the Docusaurus documentation (e.g., "http://localhost:3000/docs/introduction") | **Yes** | - |
49+
| `--pdf-path <path>` | `-o` | The output file path for the generated PDF (e.g., "output/my-docs.pdf") | **Yes** | - |
50+
| `--pdf-cover-image <pathOrUrl>` | `-c` | The URL or local file path for the PDF cover image | No | - |
51+
| `--pdf-margin-mm <number>` | `-m` | The margin size in millimeters to apply to all sides of the PDF pages | No | `10` |
52+
| `--page-concurrency <number>` | `-p` | The maximum number of concurrent browser pages to use for content fetching | No | 2x CPU cores |
53+
| `--css <style>` | `-S` | Inject custom CSS styles directly. Can be used multiple times | No | - |
54+
| `--site-rewrite <from=to>` | `-r` | Rewrite site domain in links (e.g., "http://localhost:3000=https://docs.example.com"). Can be used multiple times | No | - |
4955

5056
### Examples
5157

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"typescript": "^5.8.3"
2020
},
2121
"scripts": {
22-
"build": "tsc",
22+
"build": "node scripts/generate-build-info.js && tsc",
2323
"start": "node dist/main.js",
2424
"dev": "yarn build && yarn start"
2525
},

scripts/generate-build-info.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { execSync } = require('child_process');
4+
5+
try {
6+
const commitHash = execSync('git rev-parse --short HEAD').toString().trim();
7+
const buildTime = new Date().toISOString();
8+
const packageJson = require('../package.json');
9+
const version = packageJson.version;
10+
11+
const content = `
12+
export const buildInfo = {
13+
version: '${version}',
14+
commitHash: '${commitHash}',
15+
buildTime: '${buildTime}'
16+
};
17+
`;
18+
19+
const outputPath = path.resolve(__dirname, '../src/buildInfo.ts');
20+
fs.writeFileSync(outputPath, content);
21+
console.log(`Build info generated at ${outputPath}`);
22+
} catch (error) {
23+
console.error('Error generating build info:', error);
24+
// Fallback if git is not available (e.g. inside a tarball without .git)
25+
const content = `
26+
export const buildInfo = {
27+
version: 'unknown',
28+
commitHash: 'unknown',
29+
buildTime: new Date().toISOString()
30+
};
31+
`;
32+
const outputPath = path.resolve(__dirname, '../src/buildInfo.ts');
33+
fs.writeFileSync(outputPath, content);
34+
}

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {launchBrowser, requestForImage} from "./browser";
1717
import consoleStamp from "console-stamp";
1818
import path from "node:path";
1919
import * as fs from "node:fs";
20+
import { buildInfo } from './buildInfo';
2021

2122
import { Command } from 'commander';
2223
import * as os from "node:os";
@@ -561,7 +562,7 @@ async function main() {
561562
console.log("[App Start] Starting PDF generation process...");
562563

563564
program
564-
.version('0.0.3')
565+
.version(`${buildInfo.version} (commit: ${buildInfo.commitHash}, build: ${buildInfo.buildTime})`, '-v, --version')
565566
.description('Converts Docusaurus documentation to a single PDF file.')
566567
.option('-u, --docs-url <url>', 'The base URL of the Docusaurus documentation (e.g., "http://localhost:3000/docs/introduction")')
567568
.option('-o, --pdf-path <path>', 'The output file path for the generated PDF (e.g., "output/my-docs.pdf")')

0 commit comments

Comments
 (0)