Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add option to pass a port for development server #579

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'-p, --sitemap-path <value>',
'Path to a sitemap saved in the file system'
)
.option('-po, --port <value>', 'A port for the CLI dashboard server.')
.option('-ul, --url-limit <value>', 'No of URLs to analyze')
.option(
'-nh, --no-headless ',
Expand All @@ -76,19 +77,6 @@

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[]
Expand All @@ -97,24 +85,23 @@
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;
Expand All @@ -128,9 +115,23 @@
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)
Expand Down Expand Up @@ -200,7 +201,7 @@
text: 'Done analyzing cookies.',
});

let technologyAnalysisData: any = null;

Check warning on line 204 in packages/cli/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type

if (!shouldSkipTechnologyAnalysis) {
spinnies.add('technology-spinner', {
Expand Down Expand Up @@ -235,6 +236,7 @@

startDashboardServer(
encodeURIComponent(prefix) +
(sitemapUrl || csvPath || sitemapPath ? '&type=sitemap' : '')
(sitemapUrl || csvPath || sitemapPath ? '&type=sitemap' : ''),
port
);
})();
11 changes: 6 additions & 5 deletions packages/cli/src/utils/tests/validateArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('validateArgs', () => {
return;
});

validateArgs('https://example.com', '', '', '', '', '');
validateArgs('https://example.com', '', '', '', '', '', 9000);

expect(mockExit).toHaveBeenCalledTimes(0);
});
Expand All @@ -59,7 +59,7 @@ describe('validateArgs', () => {
jest.spyOn(fse, 'mkdir').mockImplementation(() => {
return;
});
validateArgs('', '', '', '', '', '');
validateArgs('', '', '', '', '', '', 9000);

expect(mockExit).toHaveBeenCalled();
});
Expand All @@ -80,7 +80,8 @@ describe('validateArgs', () => {
'',
'',
'',
''
'',
9000
);

expect(mockExit).toHaveBeenCalled();
Expand All @@ -92,7 +93,7 @@ describe('validateArgs', () => {
return false;
});

validateArgs('', '', '', './path/list.xml', '', '');
validateArgs('', '', '', './path/list.xml', '', '', 9000);

expect(mockExit).toHaveBeenCalled();
});
Expand All @@ -103,7 +104,7 @@ describe('validateArgs', () => {
return true;
});

validateArgs('', '', '', './path/list.xml', 'a', '');
validateArgs('', '', '', './path/list.xml', 'a', '', 9000);

expect(mockExit).toHaveBeenCalled();
});
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/utils/validateArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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),
Expand Down
Loading