Skip to content

Commit 92d9d3e

Browse files
committed
chore: simplify automation to accept document URL/path inline
1 parent d48f7a0 commit 92d9d3e

6 files changed

Lines changed: 27 additions & 95 deletions

File tree

examples/with-playwright-automation/README.md

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,34 @@
22

33
Playwright-based CLI tool for automatically detecting form fields in PDF documents using the SimplePDF editor.
44

5-
## Features
6-
7-
- Automatically detect form fields
8-
- Browser opens for visual inspection after detection
9-
105
## Quick Start
116

127
```bash
138
npm install
14-
npx tsx src/index.ts example.config.json
9+
npx tsx src/index.ts https://example.com/form.pdf
1510
```
1611

1712
## Usage
1813

1914
```bash
20-
npx tsx src/index.ts <config.json> [options]
15+
npx tsx src/index.ts <document> [options]
16+
17+
Arguments:
18+
document URL or local file path to a PDF
2119

2220
Options:
2321
--company-identifier Your SimplePDF company identifier (default: embed)
2422
--help Show help
2523
```
2624

27-
### Using Your Company Identifier
25+
### Examples
2826

2927
```bash
30-
npx tsx src/index.ts config.json --company-identifier mycompany
31-
```
32-
33-
This connects to `https://mycompany.simplepdf.com`.
34-
35-
## Configuration
36-
37-
Create a JSON configuration file:
38-
39-
```json
40-
{
41-
"document": "https://example.com/document.pdf"
42-
}
28+
npx tsx src/index.ts https://example.com/form.pdf
29+
npx tsx src/index.ts ./documents/form.pdf
30+
npx tsx src/index.ts https://example.com/form.pdf --company-identifier mycompany
4331
```
4432

45-
### Document Source
46-
47-
| Format | Example |
48-
|--------|---------|
49-
| URL | `"https://example.com/doc.pdf"` |
50-
| Local file | `"./documents/form.pdf"` |
51-
5233
## How It Works
5334

5435
The tool uses the SimplePDF editor's iframe postMessage API:

examples/with-playwright-automation/example.config.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/with-playwright-automation/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"typecheck": "tsc --noEmit"
1414
},
1515
"dependencies": {
16-
"playwright": "^1.49.0",
17-
"zod": "^4.3.4"
16+
"playwright": "^1.49.0"
1817
},
1918
"devDependencies": {
2019
"@types/node": "^22.10.0",

examples/with-playwright-automation/src/automation.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { chromium, Browser, Page } from 'playwright';
22
import * as fs from 'fs';
33
import * as path from 'path';
4-
import type { AutomationConfig } from './schema';
54

65
type AutomationErrorCode =
76
| 'detect_fields_failed'
@@ -11,11 +10,6 @@ type AutomationResult =
1110
| { success: true; data: null }
1211
| { success: false; error: { code: AutomationErrorCode; message: string } };
1312

14-
type RunAutomationArgs = {
15-
config: AutomationConfig;
16-
baseUrl: string;
17-
};
18-
1913
type IframeEvent = {
2014
type: string;
2115
data?: Record<string, unknown>;
@@ -162,7 +156,7 @@ const setupIframePage = async ({
162156
};
163157
};
164158

165-
const runAutomation = async ({ config, baseUrl }: RunAutomationArgs): Promise<AutomationResult> => {
159+
const runAutomation = async ({ document, baseUrl }: { document: string; baseUrl: string }): Promise<AutomationResult> => {
166160
let browser: Browser | null = null;
167161

168162
try {
@@ -174,7 +168,7 @@ const runAutomation = async ({ config, baseUrl }: RunAutomationArgs): Promise<Au
174168

175169
const page = await context.newPage();
176170

177-
const editorUrl = buildEditorUrl({ document: config.document, baseUrl });
171+
const editorUrl = buildEditorUrl({ document, baseUrl });
178172
const { sendEvent, waitForEvent, waitForDocumentLoaded } = await setupIframePage({
179173
page,
180174
editorUrl,

examples/with-playwright-automation/src/index.ts

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
3-
import { AutomationConfig } from './schema';
41
import { runAutomation } from './automation';
52

63
const EXIT_CODES = {
74
SUCCESS: 0,
85
INVALID_ARGS: 1,
9-
FILE_NOT_FOUND: 2,
10-
INVALID_CONFIG: 3,
11-
AUTOMATION_FAILED: 4,
6+
AUTOMATION_FAILED: 2,
127
} as const;
138

149
const DEFAULT_COMPANY_IDENTIFIER = 'embed';
1510

1611
const printUsage = (): void => {
1712
console.log(`
18-
Usage: npx tsx src/index.ts <config.json> [options]
13+
Usage: npx tsx src/index.ts <document> [options]
1914
2015
Arguments:
21-
config.json Path to JSON configuration file
16+
document URL or local file path to a PDF
2217
2318
Options:
2419
--company-identifier Your SimplePDF company identifier (default: embed)
2520
--help Show this help message
2621
27-
Configuration file format:
28-
{
29-
"document": "https://example.com/document.pdf"
30-
}
31-
3222
Examples:
33-
npx tsx src/index.ts example.config.json
34-
npx tsx src/index.ts example.config.json --company-identifier yourcompany
23+
npx tsx src/index.ts https://example.com/form.pdf
24+
npx tsx src/index.ts ./documents/form.pdf
25+
npx tsx src/index.ts https://example.com/form.pdf --company-identifier yourcompany
3526
`);
3627
};
3728

3829
type ParsedArgs = {
39-
configPath: string | null;
30+
document: string | null;
4031
baseUrl: string;
4132
showHelp: boolean;
4233
};
4334

4435
const parseArgs = (): ParsedArgs => {
4536
const args = process.argv.slice(2);
46-
let configPath: string | null = null;
37+
let document: string | null = null;
4738
let companyIdentifier = DEFAULT_COMPANY_IDENTIFIER;
4839
let baseUrl: string | null = null;
4940
let showHelp = false;
@@ -67,57 +58,35 @@ const parseArgs = (): ParsedArgs => {
6758
}
6859

6960
if (!arg?.startsWith('-')) {
70-
configPath = arg ?? null;
61+
document = arg ?? null;
7162
}
7263
}
7364

7465
const resolvedBaseUrl = baseUrl ?? `https://${companyIdentifier}.simplepdf.com`;
7566

76-
return { configPath, baseUrl: resolvedBaseUrl, showHelp };
77-
};
78-
79-
const loadConfig = ({ configPath }: { configPath: string }): AutomationConfig => {
80-
const absolutePath = path.isAbsolute(configPath) ? configPath : path.resolve(process.cwd(), configPath);
81-
82-
if (!fs.existsSync(absolutePath)) {
83-
throw new Error(`Configuration file not found: ${absolutePath}`);
84-
}
85-
86-
const content = fs.readFileSync(absolutePath, 'utf-8');
87-
const parsed = JSON.parse(content) as unknown;
88-
89-
return parsed as AutomationConfig;
67+
return { document, baseUrl: resolvedBaseUrl, showHelp };
9068
};
9169

9270
const main = async (): Promise<void> => {
93-
const { configPath, baseUrl, showHelp } = parseArgs();
71+
const { document, baseUrl, showHelp } = parseArgs();
9472

9573
if (showHelp) {
9674
printUsage();
9775
process.exit(EXIT_CODES.SUCCESS);
9876
}
9977

100-
if (!configPath) {
101-
console.error('Error: Configuration file path is required');
78+
if (!document) {
79+
console.error('Error: document URL or file path is required');
10280
printUsage();
10381
process.exit(EXIT_CODES.INVALID_ARGS);
10482
}
10583

106-
let config: AutomationConfig;
107-
try {
108-
config = loadConfig({ configPath });
109-
} catch (e) {
110-
const error = e as Error;
111-
console.error(`Error loading configuration: ${error.message}`);
112-
process.exit(EXIT_CODES.FILE_NOT_FOUND);
113-
}
114-
11584
console.log('Starting automation...');
116-
console.log(`Document: ${config.document}`);
85+
console.log(`Document: ${document}`);
11786
console.log(`Editor: ${baseUrl}`);
11887
console.log('');
11988

120-
const result = await runAutomation({ config, baseUrl });
89+
const result = await runAutomation({ document, baseUrl });
12190

12291
if (!result.success) {
12392
console.error(`Automation failed: [${result.error.code}] ${result.error.message}`);

examples/with-playwright-automation/src/schema.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)