Skip to content

Commit 0264f71

Browse files
committed
refactor(runner): new default options
1 parent c7df2c6 commit 0264f71

12 files changed

Lines changed: 63 additions & 92 deletions

File tree

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,7 @@ pdfeasy.addFonts([
115115
}
116116
])
117117

118-
pdfeasy.run({
119-
client: {
120-
emit: 'blob'
121-
}
122-
}).then((blob: string) => {
118+
pdfeasy.run().then((blob: string) => {
123119
const iframe = document.querySelector('#pdf') as HTMLIFrameElement
124120

125121
iframe.src = blob
@@ -136,7 +132,7 @@ See [scripts](./packages/pdfeasy/scripts/generate/) for server-side runner.
136132

137133
## Bundles
138134

139-
> Uses standard minifications
135+
> Uses standard minification
140136
141137
`pdfeasy/dist/client.cjs.sj`
142138

packages/pdfeasy-vite-demo/src/main.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ pdfeasy.addFonts([
4343
}
4444
])
4545

46-
pdfeasy.run({
47-
client: {
48-
emit: 'blob'
49-
}
50-
}).then((blob: string) => {
46+
pdfeasy.run().then((blob: string) => {
5147
const iframe = document.querySelector('#pdf') as HTMLIFrameElement
5248

5349
iframe.src = blob
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

packages/pdfeasy/scripts/build.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { dtsPlugin } = require('esbuild-plugin-d.ts')
2-
const resolve = require('esbuild-plugin-resolve')
32
const { build } = require('esbuild')
43
const alias = require('esbuild-plugin-alias')
54
const replace = require('esbuild-plugin-resolve')

packages/pdfeasy/scripts/generate/simple-pdf.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ pdfeasy.add([{ raw: 'A simple pdf!', text: {} }])
1212

1313
pdfeasy
1414
.run({
15-
server: {
16-
path: path.resolve(process.cwd() + '/examples'),
17-
},
15+
type: 'server',
16+
serverPath: path.resolve(process.cwd() + '/examples'),
1817
})
1918
.then(() => {
2019
console.log('simple-pdf.js ready!')

packages/pdfeasy/scripts/generate/using-external-font-pdf.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ pdfeasy.addFonts([
2525

2626
pdfeasy
2727
.run({
28-
server: {
29-
path: path.resolve(process.cwd() + '/examples'),
30-
},
28+
type: 'server',
29+
serverPath: path.resolve(process.cwd() + '/examples'),
3130
})
3231
.then(() => {
3332
console.log('using-external-font-pdf.js ready!')

packages/pdfeasy/src/runner/pdfeasy.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,22 @@ export interface RunnerOptions {
7171
}
7272

7373
export interface RunOptions {
74-
client?: {
75-
emit: PDFRunEmitOption
76-
}
77-
server?: {
78-
path: string
79-
}
74+
/**
75+
* Type runner
76+
* @default 'client'
77+
*/
78+
type?: 'client' | 'server'
79+
/**
80+
* Client type format emitter
81+
* @default 'blob'
82+
*/
83+
clientEmit?: PDFRunEmitOption
84+
/**
85+
* Server file destination
86+
*
87+
* Required in {@link RunOptions} type: server
88+
*/
89+
serverPath?: string
8090
}
8191

8292
/**
@@ -303,8 +313,10 @@ export default class {
303313
*
304314
* @param emit - {@link PDFRunEmitOption}
305315
*/
306-
public run = ({ client, server }: RunOptions): Promise<string> => {
307-
this.optionsRun = { client, server }
316+
public run = (options?: RunOptions): Promise<string> => {
317+
this.optionsRun = options || {}
318+
319+
const runType = options?.type || 'client'
308320

309321
return new Promise(async (res, rej) => {
310322
if (!this.pdfkit) {
@@ -322,11 +334,12 @@ export default class {
322334
this.globals.__NEW_PAGE__ = true
323335
})
324336

325-
if (server) {
337+
if (runType && options?.serverPath) {
326338
this.pdfkit?.pipe(
327339
createWriteStream(
328340
path.resolve(
329-
server.path + `/${this.options?.exports?.name || 'New PDF'}.pdf`
341+
options.serverPath +
342+
`/${this.options?.exports?.name || 'New PDF'}.pdf`
330343
)
331344
)
332345
)
@@ -348,7 +361,7 @@ export default class {
348361
return
349362
}
350363

351-
if (client) {
364+
if (runType === 'client') {
352365
const stream = this.pdfkit.pipe(blobStream())
353366

354367
this.pipeline()
@@ -362,7 +375,7 @@ export default class {
362375
})
363376

364377
stream.on('finish', (): void => {
365-
switch (client?.emit) {
378+
switch (options?.clientEmit || 'blob') {
366379
case 'blob':
367380
res(stream.toBlobURL('application/pdf') as string)
368381
break

packages/pdfeasy/test/contents.test.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ describe('Contents - text', () => {
1010
])
1111

1212
return expect(pdfeasy.run({
13-
client: {
14-
emit: 'none'
15-
}
13+
clientEmit: 'none'
1614
})).resolves.toBeTruthy()
1715
})
1816

@@ -24,9 +22,7 @@ describe('Contents - text', () => {
2422
])
2523

2624
return expect(pdfeasy.run({
27-
client: {
28-
emit: 'none'
29-
}
25+
clientEmit: 'none'
3026
})).resolves.toBeTruthy()
3127
})
3228

@@ -41,9 +37,7 @@ describe('Contents - text', () => {
4137
])
4238

4339
return expect(pdfeasy.run({
44-
client: {
45-
emit: 'none'
46-
}
40+
clientEmit: 'none'
4741
})).resolves.toBeTruthy()
4842
})
4943

@@ -59,9 +53,7 @@ describe('Contents - text', () => {
5953
])
6054

6155
return expect(pdfeasy.run({
62-
client: {
63-
emit: 'none'
64-
}
56+
clientEmit: 'none'
6557
})).resolves.toBeTruthy()
6658
})
6759

@@ -80,9 +72,7 @@ describe('Contents - text', () => {
8072
])
8173

8274
return expect(pdfeasy.run({
83-
client: {
84-
emit: 'none'
85-
}
75+
clientEmit: 'none'
8676
})).resolves.toBeTruthy()
8777
})
8878
})
@@ -98,9 +88,7 @@ describe('Contents - stack', () => {
9888
])
9989

10090
return expect(pdfeasy.run({
101-
client: {
102-
emit: 'none'
103-
}
91+
clientEmit: 'none'
10492
})).resolves.toBeTruthy()
10593
})
10694
})
@@ -114,9 +102,7 @@ describe('Contents - image', () => {
114102
])
115103

116104
return expect(pdfeasy.run({
117-
client: {
118-
emit: 'none'
119-
}
105+
clientEmit: 'none'
120106
})).resolves.toBeTruthy()
121107
})
122108

@@ -128,9 +114,7 @@ describe('Contents - image', () => {
128114
])
129115

130116
return expect(pdfeasy.run({
131-
client: {
132-
emit: 'none'
133-
}
117+
clientEmit: 'none'
134118
})).resolves.toBeTruthy()
135119
})
136120
})

packages/pdfeasy/test/fonts.test.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ describe('Fonts - fontsPurge option', () => {
1515
])
1616

1717
await pdfeasy.run({
18-
client: {
19-
emit: 'none'
20-
}
18+
clientEmit: 'none'
2119
})
2220

2321
expect(pdfeasy.fonts.length).toBe(0)
@@ -33,9 +31,7 @@ describe('Fonts - fontsPurge option', () => {
3331
pdfeasy.add(Utils.content())
3432

3533
await pdfeasy.run({
36-
client: {
37-
emit: 'none'
38-
}
34+
clientEmit: 'none'
3935
})
4036

4137
expect(pdfeasy.fonts.length).toBe(0)
@@ -62,9 +58,7 @@ describe('Fonts - fontsPurge option', () => {
6258
])
6359

6460
await pdfeasy.run({
65-
client: {
66-
emit: 'none'
67-
}
61+
clientEmit: 'none'
6862
})
6963

7064
expect(pdfeasy.fonts.length).toBe(0)
@@ -93,9 +87,7 @@ describe('Fonts - Register', () => {
9387
])
9488

9589
await pdfeasy.run({
96-
client: {
97-
emit: 'none'
98-
}
90+
clientEmit: 'none'
9991
})
10092

10193
expect(pdfeasy.fonts.length).toBe(1)

0 commit comments

Comments
 (0)