Skip to content

Commit 1f22656

Browse files
committed
feat: expose --port cli flag
1 parent 9a7ce95 commit 1f22656

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

cli.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const cliFlags = arg({
3232
'--html-pdf-options': String,
3333
'--pdf-options': String,
3434
'--launch-options': String,
35+
'--port': Number,
3536
'--md-file-encoding': String,
3637
'--stylesheet-encoding': String,
3738
'--as-html': Boolean,
@@ -96,12 +97,12 @@ async function main(args: typeof cliFlags, config: Config) {
9697
}
9798

9899
// serve directory of first file because all files will be in the same dir
99-
const port = await getPort();
100-
const server = await serveDirectory(getDir(mdFiles[0]), port);
100+
config.port = args['--port'] || (await getPort());
101+
const server = await serveDirectory(getDir(mdFiles[0]), config.port);
101102

102103
const getListrTask = (mdFile: string) => ({
103104
title: `generating ${args['--as-html'] ? 'HTML' : 'PDF'} from ${chalk.underline(mdFile)}`,
104-
task: () => convertMdToPdf(mdFile, config, port, args),
105+
task: () => convertMdToPdf(mdFile, config, args),
105106
});
106107

107108
// create list of tasks and run concurrently

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ export const mdToPdf = async (mdFile: string, config: Partial<Config> = {}) => {
1717
throw new TypeError(`mdFile has to be a string, received ${typeof mdFile}`);
1818
}
1919

20-
const port = await getPort();
21-
const server = await serveDirectory(getDir(mdFile), port);
20+
config.port = config.port || (await getPort());
21+
const server = await serveDirectory(getDir(mdFile), config.port);
2222

2323
const mergedConfig: Config = {
2424
...defaultConfig,
2525
...config,
2626
pdf_options: { ...defaultConfig.pdf_options, ...config.pdf_options },
2727
};
2828

29-
const pdf = await convertMdToPdf(mdFile, mergedConfig, port);
29+
const pdf = await convertMdToPdf(mdFile, mergedConfig);
3030

3131
server.close();
3232

lib/help.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const helpText = `
1515
--marked-options ${chalk.dim('.........')} Set custom options for marked (as a JSON string)
1616
--pdf-options ${chalk.dim('............')} Set custom options for the generated PDF (as a JSON string)
1717
--launch-options ${chalk.dim('.........')} Set custom launch options for Puppeteer
18+
--port ${chalk.dim('...................')} Set the port to run the http server on
1819
--md-file-encoding ${chalk.dim('.......')} Set the file encoding for the markdown file
1920
--stylesheet-encoding ${chalk.dim('....')} Set the file encoding for the stylesheet
2021
--as-html ${chalk.dim('................')} Output as HTML instead

lib/md-to-pdf.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getMarginObject } from './helpers';
1010
/**
1111
* Convert a markdown file to pdf.
1212
*/
13-
export const convertMdToPdf = async (mdFile: string, config: Config, port: number, args: any = {}) => {
13+
export const convertMdToPdf = async (mdFile: string, config: Config, args: any = {}) => {
1414
const mdFileContent = await readFile(resolve(mdFile), args['--md-file-encoding'] || config.md_file_encoding);
1515

1616
const { content: md, data: frontMatterConfig } = grayMatter(mdFileContent);
@@ -59,7 +59,7 @@ export const convertMdToPdf = async (mdFile: string, config: Config, port: numbe
5959

6060
const html = getHtml(md, config);
6161

62-
const output = await writeOutput(mdFile, html, { ...config, port });
62+
const output = await writeOutput(mdFile, html, config);
6363

6464
if (!output.filename) {
6565
throw new Error(`Failed to create ${config.as_html ? 'HTML' : 'PDF'}.`);

0 commit comments

Comments
 (0)