diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 1ea8bb008..2856b30bf 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -55,6 +55,7 @@ program '-p, --sitemap-path ', 'Path to a sitemap saved in the file system' ) + .option('-po, --port ', 'A port for the CLI dashboard server.') .option('-ul, --url-limit ', 'No of URLs to analyze') .option( '-nh, --no-headless ', @@ -76,19 +77,6 @@ program program.parse(); -const initialize = async () => { - //check if devserver port in already in use - - const portInUse = await checkPortInUse(9000); - - if (portInUse) { - console.error( - 'Error: Report server port (9000) already in use. You might be already running CLI' - ); - process.exit(1); - } -}; - const saveResults = async ( outDir: string, result: CompleteJson | CompleteJson[] @@ -97,24 +85,23 @@ const saveResults = async ( await writeFile(outDir + '/out.json', JSON.stringify(result, null, 4)); }; -const startDashboardServer = async (dir: string) => { - exec('npm run cli-dashboard:dev'); +const startDashboardServer = async (dir: string, port: number) => { + exec(`npm run cli-dashboard:dev -- -- --port ${port}`); await delay(2000); console.log( - `Report is being served at the URL: http://localhost:9000?dir=${dir}` + `Report is being served at the URL: http://localhost:${port}?dir=${dir}` ); }; // eslint-disable-next-line complexity (async () => { - await initialize(); - const url = program.opts().url; const sitemapUrl = program.opts().sitemapUrl; const csvPath = program.opts().csvPath; const sitemapPath = program.opts().sitemapPath; + const port = parseInt(program.opts().port || '9000'); const numberOfUrlsInput = program.opts().urlLimit; const isHeadless = Boolean(program.opts().headless); const shouldSkipPrompts = !program.opts().prompts; @@ -128,9 +115,23 @@ const startDashboardServer = async (dir: string) => { csvPath, sitemapPath, numberOfUrlsInput, - outDir + outDir, + port ); + //check if devserver port in already in use only if the dashboard is goint to be used + + if (!outDir) { + const isPortInUse = await checkPortInUse(port); + + if (isPortInUse) { + console.error( + `Error: Report server port ${port} already in use. You might be already running CLI` + ); + process.exit(1); + } + } + const prefix = url || sitemapUrl ? Utility.generatePrefix(url || sitemapUrl) @@ -235,6 +236,7 @@ const startDashboardServer = async (dir: string) => { startDashboardServer( encodeURIComponent(prefix) + - (sitemapUrl || csvPath || sitemapPath ? '&type=sitemap' : '') + (sitemapUrl || csvPath || sitemapPath ? '&type=sitemap' : ''), + port ); })(); diff --git a/packages/cli/src/utils/tests/validateArgs.ts b/packages/cli/src/utils/tests/validateArgs.ts index 2cf3df480..bcfb0c5f2 100644 --- a/packages/cli/src/utils/tests/validateArgs.ts +++ b/packages/cli/src/utils/tests/validateArgs.ts @@ -44,7 +44,7 @@ describe('validateArgs', () => { return; }); - validateArgs('https://example.com', '', '', '', '', ''); + validateArgs('https://example.com', '', '', '', '', '', 9000); expect(mockExit).toHaveBeenCalledTimes(0); }); @@ -59,7 +59,7 @@ describe('validateArgs', () => { jest.spyOn(fse, 'mkdir').mockImplementation(() => { return; }); - validateArgs('', '', '', '', '', ''); + validateArgs('', '', '', '', '', '', 9000); expect(mockExit).toHaveBeenCalled(); }); @@ -80,7 +80,8 @@ describe('validateArgs', () => { '', '', '', - '' + '', + 9000 ); expect(mockExit).toHaveBeenCalled(); @@ -92,7 +93,7 @@ describe('validateArgs', () => { return false; }); - validateArgs('', '', '', './path/list.xml', '', ''); + validateArgs('', '', '', './path/list.xml', '', '', 9000); expect(mockExit).toHaveBeenCalled(); }); @@ -103,7 +104,7 @@ describe('validateArgs', () => { return true; }); - validateArgs('', '', '', './path/list.xml', 'a', ''); + validateArgs('', '', '', './path/list.xml', 'a', '', 9000); expect(mockExit).toHaveBeenCalled(); }); diff --git a/packages/cli/src/utils/validateArgs.ts b/packages/cli/src/utils/validateArgs.ts index 1b75e1c2e..eb8b807fb 100644 --- a/packages/cli/src/utils/validateArgs.ts +++ b/packages/cli/src/utils/validateArgs.ts @@ -26,6 +26,7 @@ import path from 'path'; * @param {string} sitemapPath File system path to a sitemap xml file. * @param {string} numberOfUrls Url limit argument. * @param {string} outDir File system path to the output directory. + * @param port */ // eslint-disable-next-line complexity const validateArgs = async ( @@ -34,8 +35,14 @@ const validateArgs = async ( csvPath: string, sitemapPath: string, numberOfUrls: string, - outDir: string + outDir: string, + port: number ) => { + if (isNaN(port) || (!isNaN(port) && (port < 0 || port > 65536))) { + console.log(`Invalid port argument. Please porvide a port >=0 and <=65536`); + process.exit(1); + } + const numArgs: number = [ Boolean(url), Boolean(sitemapUrl),