Skip to content

Commit

Permalink
Merge pull request #197 from GoogleChromeLabs/fix/cli-std-stream
Browse files Browse the repository at this point in the history
Fix: CLI std output changes
  • Loading branch information
Sayed Taqui authored Oct 24, 2023
2 parents 311aa4a + 775d34c commit dc71de1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
3 changes: 2 additions & 1 deletion packages/cli-dashboard/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ const App = () => {

const [type, path] = useMemo(() => {
const urlParams = new URLSearchParams(window.location.search);
const dir = urlParams.get('dir');

return [
urlParams.get('type') === 'sitemap'
? DisplayType.SITEMAP
: DisplayType.SITE,
urlParams.get('path')?.substring(1) || '',
`/out/${dir}/out.json`,
];
}, []);

Expand Down
62 changes: 24 additions & 38 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { analyzeCookiesUrls } from './procedures/analyzeCookieUrls';
import { analyzeCookiesUrlsInBatches } from './procedures/analyzeCookieUrlsInBatches';
import { analyzeTechnologiesUrlsInBatches } from './procedures/analyzeTechnologiesUrlsInBatches';
import { delay } from './utils';
import { checkPortInUse } from './utils/checkPortInUse';

events.EventEmitter.defaultMaxListeners = 15;

Expand All @@ -54,10 +55,20 @@ program.parse();
const isHeadless = Boolean(program.opts().headless);

export 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 url = program.opts().url;
const sitemapURL = program.opts().sitemapUrl;
const cookieDictionary = await fetchDictionary();

if (url) {
const prefix = Utility.generatePrefix(url ?? 'untitled');
const directory = `./out/${prefix}`;
Expand Down Expand Up @@ -98,27 +109,14 @@ export const initialize = async () => {
await ensureFile(directory + '/out.json');
await writeFile(directory + '/out.json', JSON.stringify(output, null, 4));

let isTerminated = false;

exec('npm run cli-dashboard:dev', (error) => {
if (error) {
isTerminated = true;
return;
}
});
exec('npm run cli-dashboard:dev');

await delay(2000);

//if in 2 seconds dasboard server process is terminated show error
if (isTerminated) {
console.log('Error starting server');
} else {
console.log(
`Report is being served at the URL: http://localhost:9000?path=${encodeURIComponent(
directory + '/out.json'
)}`
);
}
console.log(
`Report is being served at the URL: http://localhost:9000?dir=${encodeURIComponent(
prefix
)}`
);
} else {
const spinnies = new Spinnies();

Expand Down Expand Up @@ -177,27 +175,15 @@ export const initialize = async () => {
await ensureFile(directory + '/out.json');
await writeFile(directory + '/out.json', JSON.stringify(result, null, 4));

let isTerminated = false;

exec('npm run cli-dashboard:dev', (error) => {
if (error) {
isTerminated = true;
return;
}
});
exec('npm run cli-dashboard:dev');

await delay(2000);

//if in 2 seconds dasboard server process is terminated show error
if (isTerminated) {
console.log('Error starting server');
} else {
console.log(
`Report is being served at the URL: http://localhost:9000?path=${encodeURIComponent(
directory + '/out.json'
)}&type=sitemap`
);
}
console.log(
`Report is being served at the URL: http://localhost:9000?dir=${encodeURIComponent(
prefix
)}&type=sitemap`
);
}
};

Expand Down
35 changes: 35 additions & 0 deletions packages/cli/src/utils/checkPortInUse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import http from 'http';

/**
* @param port number Port number to test
* @returns Promise which will resolve in a boolean value
*/
export function checkPortInUse(port: number): Promise<boolean> {
return new Promise((resolve) => {
const server = http
.createServer()
.listen(port, () => {
server.close();
resolve(false);
})
.on('error', () => {
resolve(true);
});
});
}

0 comments on commit dc71de1

Please sign in to comment.