-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.ts
More file actions
60 lines (56 loc) · 2.34 KB
/
Copy pathforms.ts
File metadata and controls
60 lines (56 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { promises as fs } from 'fs';
import { Command } from 'commander';
import { type Context } from './types.js';
import { createFormService, defaultFormConfig, parsePdf as parsePdfCore } from '@flexion/forms-core';
import { createFormsRepository } from '@flexion/forms-core/repository';
import { createTestPdfParser } from '@flexion/forms-core/documents/pdf/context';
import { createFilesystemDatabaseContext } from '@flexion/forms-database/context';
export const addFormCommands = (ctx: Context, cli: Command) => {
const cmd = cli
.command('forms')
.description('form management commands')
.option('-d, --database <string>', 'Path to the dev sqlite3 database file. (Postgres currently not wired up.)', async databasePath => {
ctx.db = await createFilesystemDatabaseContext(databasePath);
const repository = createFormsRepository({ db: ctx.db, formConfig: defaultFormConfig });
ctx.forms = createFormService({
repository,
isUserLoggedIn: () => true,
config: defaultFormConfig,
parser: createTestPdfParser(), // Use test parser with filesystem cache for CLI
});
});
cmd
.command('import-pdf')
.description('Intialize a new form by importing a PDF file')
.argument('<string>', 'Source PDF file for form.')
.action(async inputFile => {
// For standalone import-pdf command, use test parser with caching
const parser = createTestPdfParser();
const pdfBytes = await fs.readFile(inputFile);
const maybeForm = await parsePdfCore({ parser, formConfig: defaultFormConfig }, pdfBytes);
if (maybeForm === undefined) {
console.error('Error parsing PDF file:', inputFile);
return;
}
console.log(JSON.stringify(maybeForm, null, 2));
});
cmd
.command('add')
.description('add a form')
.argument('<string>', 'Source JSON file for form.')
.action(async inputFile => {
const fileContents = await fs.readFile(inputFile);
const fileName = inputFile.split(/[\\/]/).pop() ?? inputFile;
const result = await ctx.forms?.initializeForm({
summary: {
title: `Imported Form: ${fileName}`,
description: 'Form imported from PDF',
},
document: {
fileName: inputFile,
data: fileContents.toString('base64')
},
});
console.log(result);
});
};