From ec8140e66b50da6a8655a288c2891846874fa64b Mon Sep 17 00:00:00 2001 From: Mayank Rana Date: Wed, 27 Dec 2023 19:14:53 +0530 Subject: [PATCH 1/5] feat: Update report downloader function to download sitemap report --- .../src/components/siteMapReport/index.tsx | 8 +++ .../src/components/utils/reportDownloader.ts | 63 +++++++++++-------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/packages/cli-dashboard/src/components/siteMapReport/index.tsx b/packages/cli-dashboard/src/components/siteMapReport/index.tsx index fbe506fef..5d6225da1 100644 --- a/packages/cli-dashboard/src/components/siteMapReport/index.tsx +++ b/packages/cli-dashboard/src/components/siteMapReport/index.tsx @@ -41,6 +41,7 @@ import SiteMapAffectedCookies from './sitemapAffectedCookies'; import CookiesLandingContainer from '../siteReport/tabs/cookies/cookiesLandingContainer'; import reshapeCookies from '../utils/reshapeCookies'; import sidebarData from './sidebarData'; +import { genereateAndDownloadCSVReports } from '../utils/reportDownloader'; interface SiteMapReportProps { landingPageCookies: CookieFrameStorageType; @@ -134,6 +135,13 @@ const SiteMapReport = ({ tabCookies={reshapedCookies} tabFrames={frames} affectedCookies={affectedCookies} + downloadReport={() => { + if (!Array.isArray(completeJson)) { + return; + } + + genereateAndDownloadCSVReports(completeJson, null, true); + }} /> ); diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader.ts b/packages/cli-dashboard/src/components/utils/reportDownloader.ts index 765891009..7456f0f89 100644 --- a/packages/cli-dashboard/src/components/utils/reportDownloader.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader.ts @@ -30,42 +30,53 @@ import { generateTechnologyCSV, } from './generateReports'; -export const genereateAndDownloadCSVReports = ( +export const genereateAndDownloadCSVReports = async ( JSONReport: CompleteJson[], - selectedPageUrl?: string | null + selectedPageUrl?: string | null, + processAllPages?: boolean ) => { - if (!JSONReport) { + if (!JSONReport.length) { return; } - let siteAnalysisData: CompleteJson = JSONReport[0]; + let siteAnalysisData: CompleteJson[]; if (selectedPageUrl) { - siteAnalysisData = JSONReport.find( - ({ pageUrl }) => pageUrl === selectedPageUrl - ) as CompleteJson; + siteAnalysisData = [ + JSONReport.find(({ pageUrl }) => pageUrl === selectedPageUrl), + ] as CompleteJson[]; + } else if (processAllPages) { + siteAnalysisData = JSONReport; } else { - siteAnalysisData = JSONReport[0]; + siteAnalysisData = [JSONReport[0]]; } - const allCookiesCSV = generateAllCookiesCSV(siteAnalysisData); - let technologyDataCSV = null; - if (siteAnalysisData.technologyData.length > 0) { - technologyDataCSV = generateTechnologyCSV(siteAnalysisData); - } - const affectedCookiesDataCSV = generateAffectedCookiesCSV(siteAnalysisData); - const summaryDataCSV = generateSummaryDataCSV(siteAnalysisData); - const zip = new JSZip(); - zip.file('cookies.csv', allCookiesCSV); - if (technologyDataCSV) { - zip.file('technologies.csv', technologyDataCSV); - } - zip.file('affected-cookies.csv', affectedCookiesDataCSV); - zip.file('report.csv', summaryDataCSV); - zip.file('report.json', JSON.stringify(JSONReport, null, 4)); - zip.generateAsync({ type: 'blob' }).then((content) => { - // see FileSaver.js - saveAs(content, 'report.zip'); + + siteAnalysisData.forEach((data) => { + const allCookiesCSV = generateAllCookiesCSV(data); + let technologyDataCSV = null; + if (data.technologyData.length > 0) { + technologyDataCSV = generateTechnologyCSV(data); + } + const affectedCookiesDataCSV = generateAffectedCookiesCSV(data); + const summaryDataCSV = generateSummaryDataCSV(data); + + let folderName = data.pageUrl + .replace(/^https?:\/\//, '') + .replace(/\/+/g, '-'); + zip.folder(folderName); + folderName = folderName + '/'; + + zip.file(folderName + 'cookies.csv', allCookiesCSV); + if (technologyDataCSV) { + zip.file(folderName + 'technologies.csv', technologyDataCSV); + } + zip.file(folderName + 'affected-cookies.csv', affectedCookiesDataCSV); + zip.file(folderName + 'report.csv', summaryDataCSV); + zip.file(folderName + 'report.json', JSON.stringify(JSONReport, null, 4)); }); + + const content = await zip.generateAsync({ type: 'blob' }); + saveAs(content, 'report.zip'); }; From 94819dd82dbf0d18fe30937f636eb6f444736f68 Mon Sep 17 00:00:00 2001 From: Mayank Rana Date: Thu, 28 Dec 2023 14:09:39 +0530 Subject: [PATCH 2/5] ref: separate the func for site and sitemap and update functions organisation --- .../src/components/siteMapReport/index.tsx | 4 +- .../siteReport/tabs/cookies/index.tsx | 6 +- .../src/components/utils/reportDownloader.ts | 82 ------------------- .../generateAffectedCookiesCSV.ts | 2 +- .../generateReports/generateAllCookiesCSV.ts | 2 +- .../generateReports/generateSummaryDataCSV.ts | 2 +- .../generateReports/generateTechnologyCSV.ts | 2 +- .../generateReports/index.ts | 0 .../generateReports/tests/data.mock.ts | 2 +- .../tests/generateAffectedCookiesCSV.ts | 0 .../tests/generateAllCookiesCSV.ts | 0 .../generateSiteMapReportandDownload.ts | 54 ++++++++++++ .../generateSiteReportandDownload.ts | 60 ++++++++++++++ .../utils/reportDownloader/index.ts | 18 ++++ .../utils/reportDownloader/utils.ts | 65 +++++++++++++++ 15 files changed, 207 insertions(+), 92 deletions(-) delete mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader.ts rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/generateAffectedCookiesCSV.ts (99%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/generateAllCookiesCSV.ts (99%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/generateSummaryDataCSV.ts (99%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/generateTechnologyCSV.ts (96%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/index.ts (100%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/tests/data.mock.ts (98%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/tests/generateAffectedCookiesCSV.ts (100%) rename packages/cli-dashboard/src/components/utils/{ => reportDownloader}/generateReports/tests/generateAllCookiesCSV.ts (100%) create mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts create mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts create mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader/index.ts create mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts diff --git a/packages/cli-dashboard/src/components/siteMapReport/index.tsx b/packages/cli-dashboard/src/components/siteMapReport/index.tsx index 5d6225da1..51969c619 100644 --- a/packages/cli-dashboard/src/components/siteMapReport/index.tsx +++ b/packages/cli-dashboard/src/components/siteMapReport/index.tsx @@ -41,7 +41,7 @@ import SiteMapAffectedCookies from './sitemapAffectedCookies'; import CookiesLandingContainer from '../siteReport/tabs/cookies/cookiesLandingContainer'; import reshapeCookies from '../utils/reshapeCookies'; import sidebarData from './sidebarData'; -import { genereateAndDownloadCSVReports } from '../utils/reportDownloader'; +import { generateSiteMapReportandDownload } from '../utils/reportDownloader'; interface SiteMapReportProps { landingPageCookies: CookieFrameStorageType; @@ -140,7 +140,7 @@ const SiteMapReport = ({ return; } - genereateAndDownloadCSVReports(completeJson, null, true); + generateSiteMapReportandDownload(completeJson); }} /> ); diff --git a/packages/cli-dashboard/src/components/siteReport/tabs/cookies/index.tsx b/packages/cli-dashboard/src/components/siteReport/tabs/cookies/index.tsx index e629e234c..4f70c0156 100644 --- a/packages/cli-dashboard/src/components/siteReport/tabs/cookies/index.tsx +++ b/packages/cli-dashboard/src/components/siteReport/tabs/cookies/index.tsx @@ -21,7 +21,7 @@ import { UNKNOWN_FRAME_KEY, type TabFrames } from '@ps-analysis-tool/common'; */ import CookiesListing from './cookiesListing'; import { useContentStore } from '../../stateProviders/contentStore'; -import { genereateAndDownloadCSVReports } from '../../../utils/reportDownloader'; +import { generateSiteReportandDownload } from '../../../utils/reportDownloader'; import CookiesLandingContainer from './cookiesLandingContainer'; interface CookiesTabProps { @@ -60,9 +60,9 @@ const CookiesTab = ({ selectedFrameUrl, selectedSite }: CookiesTabProps) => { return; } if (Array.isArray(completeJson)) { - genereateAndDownloadCSVReports(completeJson, selectedSite); + generateSiteReportandDownload(completeJson, selectedSite); } else if (!Array.isArray(completeJson)) { - genereateAndDownloadCSVReports([completeJson]); + generateSiteReportandDownload([completeJson]); } }, [completeJson, selectedSite]); diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader.ts b/packages/cli-dashboard/src/components/utils/reportDownloader.ts deleted file mode 100644 index 7456f0f89..000000000 --- a/packages/cli-dashboard/src/components/utils/reportDownloader.ts +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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. - */ -/** - * External dependencies - */ -import JSZip from 'jszip'; -import { saveAs } from 'file-saver'; - -/** - * Internal dependencies - */ -import type { CompleteJson } from '../../types'; -import { - generateAllCookiesCSV, - generateAffectedCookiesCSV, - generateSummaryDataCSV, - generateTechnologyCSV, -} from './generateReports'; - -export const genereateAndDownloadCSVReports = async ( - JSONReport: CompleteJson[], - selectedPageUrl?: string | null, - processAllPages?: boolean -) => { - if (!JSONReport.length) { - return; - } - - let siteAnalysisData: CompleteJson[]; - - if (selectedPageUrl) { - siteAnalysisData = [ - JSONReport.find(({ pageUrl }) => pageUrl === selectedPageUrl), - ] as CompleteJson[]; - } else if (processAllPages) { - siteAnalysisData = JSONReport; - } else { - siteAnalysisData = [JSONReport[0]]; - } - - const zip = new JSZip(); - - siteAnalysisData.forEach((data) => { - const allCookiesCSV = generateAllCookiesCSV(data); - let technologyDataCSV = null; - if (data.technologyData.length > 0) { - technologyDataCSV = generateTechnologyCSV(data); - } - const affectedCookiesDataCSV = generateAffectedCookiesCSV(data); - const summaryDataCSV = generateSummaryDataCSV(data); - - let folderName = data.pageUrl - .replace(/^https?:\/\//, '') - .replace(/\/+/g, '-'); - zip.folder(folderName); - folderName = folderName + '/'; - - zip.file(folderName + 'cookies.csv', allCookiesCSV); - if (technologyDataCSV) { - zip.file(folderName + 'technologies.csv', technologyDataCSV); - } - zip.file(folderName + 'affected-cookies.csv', affectedCookiesDataCSV); - zip.file(folderName + 'report.csv', summaryDataCSV); - zip.file(folderName + 'report.json', JSON.stringify(JSONReport, null, 4)); - }); - - const content = await zip.generateAsync({ type: 'blob' }); - saveAs(content, 'report.zip'); -}; diff --git a/packages/cli-dashboard/src/components/utils/generateReports/generateAffectedCookiesCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateAffectedCookiesCSV.ts similarity index 99% rename from packages/cli-dashboard/src/components/utils/generateReports/generateAffectedCookiesCSV.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateAffectedCookiesCSV.ts index 656f4eeaa..d93b27803 100644 --- a/packages/cli-dashboard/src/components/utils/generateReports/generateAffectedCookiesCSV.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateAffectedCookiesCSV.ts @@ -21,7 +21,7 @@ import { sanitizeCsvRecord } from '@ps-analysis-tool/common'; /** * Internal dependencies */ -import type { CompleteJson, CookieJsonDataType } from '../../../types'; +import type { CompleteJson, CookieJsonDataType } from '../../../../types'; export const AFFECTED_COOKIES_DATA_HEADERS = [ 'Name', diff --git a/packages/cli-dashboard/src/components/utils/generateReports/generateAllCookiesCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateAllCookiesCSV.ts similarity index 99% rename from packages/cli-dashboard/src/components/utils/generateReports/generateAllCookiesCSV.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateAllCookiesCSV.ts index 59647f21c..33d0ec278 100644 --- a/packages/cli-dashboard/src/components/utils/generateReports/generateAllCookiesCSV.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateAllCookiesCSV.ts @@ -21,7 +21,7 @@ import { sanitizeCsvRecord } from '@ps-analysis-tool/common'; /** * Internal dependencies */ -import type { CompleteJson, CookieJsonDataType } from '../../../types'; +import type { CompleteJson, CookieJsonDataType } from '../../../../types'; export const COOKIES_DATA_HEADER = [ 'Name', diff --git a/packages/cli-dashboard/src/components/utils/generateReports/generateSummaryDataCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateSummaryDataCSV.ts similarity index 99% rename from packages/cli-dashboard/src/components/utils/generateReports/generateSummaryDataCSV.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateSummaryDataCSV.ts index 3381b1983..d0c3aad17 100644 --- a/packages/cli-dashboard/src/components/utils/generateReports/generateSummaryDataCSV.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateSummaryDataCSV.ts @@ -17,7 +17,7 @@ /** * Internal dependencies */ -import type { CompleteJson, CookieJsonDataType } from '../../../types'; +import type { CompleteJson, CookieJsonDataType } from '../../../../types'; const generateSummaryDataCSV = (siteAnalysisData: CompleteJson): string => { const frameCookieDataMap = siteAnalysisData.cookieData; diff --git a/packages/cli-dashboard/src/components/utils/generateReports/generateTechnologyCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateTechnologyCSV.ts similarity index 96% rename from packages/cli-dashboard/src/components/utils/generateReports/generateTechnologyCSV.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateTechnologyCSV.ts index 7659e5932..c8e9eb48d 100644 --- a/packages/cli-dashboard/src/components/utils/generateReports/generateTechnologyCSV.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/generateTechnologyCSV.ts @@ -21,7 +21,7 @@ import { sanitizeCsvRecord } from '@ps-analysis-tool/common'; /** * Internal dependencies */ -import type { CompleteJson } from '../../../types'; +import type { CompleteJson } from '../../../../types'; const TECHNOLOGIES_DATA_HEADER = [ 'Name', diff --git a/packages/cli-dashboard/src/components/utils/generateReports/index.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/index.ts similarity index 100% rename from packages/cli-dashboard/src/components/utils/generateReports/index.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/index.ts diff --git a/packages/cli-dashboard/src/components/utils/generateReports/tests/data.mock.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts similarity index 98% rename from packages/cli-dashboard/src/components/utils/generateReports/tests/data.mock.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts index 1b5337093..e82f90583 100644 --- a/packages/cli-dashboard/src/components/utils/generateReports/tests/data.mock.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import type { CompleteJson } from '../../../../types'; +import type { CompleteJson } from '../../../../../types'; export const mockData1: CompleteJson = { pageUrl: 'https://edition.cnn.com/', diff --git a/packages/cli-dashboard/src/components/utils/generateReports/tests/generateAffectedCookiesCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateAffectedCookiesCSV.ts similarity index 100% rename from packages/cli-dashboard/src/components/utils/generateReports/tests/generateAffectedCookiesCSV.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateAffectedCookiesCSV.ts diff --git a/packages/cli-dashboard/src/components/utils/generateReports/tests/generateAllCookiesCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateAllCookiesCSV.ts similarity index 100% rename from packages/cli-dashboard/src/components/utils/generateReports/tests/generateAllCookiesCSV.ts rename to packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateAllCookiesCSV.ts diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts new file mode 100644 index 000000000..1d7c33610 --- /dev/null +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts @@ -0,0 +1,54 @@ +/* + * 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. + */ + +/** + * External dependencies + */ +import JSZip from 'jszip'; +import { saveAs } from 'file-saver'; + +/** + * Internal dependencies + */ +import type { CompleteJson } from '../../../types'; +import { createZip } from './utils'; + +const generateSiteMapReportandDownload = async (JSONReport: CompleteJson[]) => { + if (!JSONReport.length) { + return; + } + + const zip = new JSZip(); + + JSONReport.forEach((data) => { + const folderName = data.pageUrl + .replace(/^https?:\/\//, '') + .replace(/\/+/g, '-'); + + const zipFolder = zip.folder(folderName); + + if (!zipFolder) { + return; + } + + createZip(data, zipFolder); + }); + + const content = await zip.generateAsync({ type: 'blob' }); + saveAs(content, 'report.zip'); +}; + +export default generateSiteMapReportandDownload; diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts new file mode 100644 index 000000000..188b4df8c --- /dev/null +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts @@ -0,0 +1,60 @@ +/* + * 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. + */ +/** + * External dependencies + */ +import JSZip from 'jszip'; +import { saveAs } from 'file-saver'; + +/** + * Internal dependencies + */ +import type { CompleteJson } from '../../../types'; +import { createZip } from './utils'; + +const generateSiteReportandDownload = async ( + JSONReport: CompleteJson[], + selectedPageUrl?: string | null +) => { + if (!JSONReport.length) { + return; + } + + const zip = new JSZip(); + + let siteAnalysisData: CompleteJson; + + if (selectedPageUrl) { + siteAnalysisData = JSONReport.find( + ({ pageUrl }) => pageUrl === selectedPageUrl + ) as CompleteJson; + } else { + siteAnalysisData = JSONReport[0]; + } + + const folderName = siteAnalysisData.pageUrl + .replace(/^https?:\/\//, '') + .replace(/\/+/g, '-'); + + const zipFolder = zip.folder(folderName) as JSZip; + + createZip(siteAnalysisData, zipFolder); + + const content = await zip.generateAsync({ type: 'blob' }); + saveAs(content, 'report.zip'); +}; + +export default generateSiteReportandDownload; diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/index.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/index.ts new file mode 100644 index 000000000..7ac062d10 --- /dev/null +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/index.ts @@ -0,0 +1,18 @@ +/* + * 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. + */ + +export { default as generateSiteMapReportandDownload } from './generateSiteMapReportandDownload'; +export { default as generateSiteReportandDownload } from './generateSiteReportandDownload'; diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts new file mode 100644 index 000000000..2b8058d84 --- /dev/null +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts @@ -0,0 +1,65 @@ +/* + * 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. + */ + +/** + * External dependencies + */ +import type JSZip from 'jszip'; + +/** + * Internal dependencies + */ +import type { CompleteJson } from '../../../types'; +import { + generateAffectedCookiesCSV, + generateAllCookiesCSV, + generateSummaryDataCSV, + generateTechnologyCSV, +} from './generateReports'; + +const generateCSVFiles = (data: CompleteJson) => { + const allCookiesCSV = generateAllCookiesCSV(data); + let technologyDataCSV = null; + if (data.technologyData.length > 0) { + technologyDataCSV = generateTechnologyCSV(data); + } + const affectedCookiesDataCSV = generateAffectedCookiesCSV(data); + const summaryDataCSV = generateSummaryDataCSV(data); + + return { + allCookiesCSV, + technologyDataCSV, + affectedCookiesDataCSV, + summaryDataCSV, + }; +}; + +export const createZip = (analysisData: CompleteJson, zipObject: JSZip) => { + const { + allCookiesCSV, + technologyDataCSV, + affectedCookiesDataCSV, + summaryDataCSV, + } = generateCSVFiles(analysisData); + + zipObject.file('cookies.csv', allCookiesCSV); + if (technologyDataCSV) { + zipObject.file('technologies.csv', technologyDataCSV); + } + zipObject.file('affected-cookies.csv', affectedCookiesDataCSV); + zipObject.file('report.csv', summaryDataCSV); + zipObject.file('report.json', JSON.stringify(analysisData, null, 4)); +}; From fdedb64b6b9665ca5f1a8781bb89c9f5501938f0 Mon Sep 17 00:00:00 2001 From: Mayank Rana Date: Mon, 1 Jan 2024 12:54:05 +0530 Subject: [PATCH 3/5] Move folder creation to util and handle last slash removal case --- .../generateSiteMapReportandDownload.ts | 8 ++------ .../generateSiteReportandDownload.ts | 12 +++++++----- .../src/components/utils/reportDownloader/utils.ts | 11 +++++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts index 1d7c33610..0614ea399 100644 --- a/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteMapReportandDownload.ts @@ -24,7 +24,7 @@ import { saveAs } from 'file-saver'; * Internal dependencies */ import type { CompleteJson } from '../../../types'; -import { createZip } from './utils'; +import { createZip, getFolderName } from './utils'; const generateSiteMapReportandDownload = async (JSONReport: CompleteJson[]) => { if (!JSONReport.length) { @@ -34,11 +34,7 @@ const generateSiteMapReportandDownload = async (JSONReport: CompleteJson[]) => { const zip = new JSZip(); JSONReport.forEach((data) => { - const folderName = data.pageUrl - .replace(/^https?:\/\//, '') - .replace(/\/+/g, '-'); - - const zipFolder = zip.folder(folderName); + const zipFolder: JSZip | null = zip.folder(getFolderName(data.pageUrl)); if (!zipFolder) { return; diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts index 188b4df8c..37dc62e97 100644 --- a/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateSiteReportandDownload.ts @@ -23,7 +23,7 @@ import { saveAs } from 'file-saver'; * Internal dependencies */ import type { CompleteJson } from '../../../types'; -import { createZip } from './utils'; +import { createZip, getFolderName } from './utils'; const generateSiteReportandDownload = async ( JSONReport: CompleteJson[], @@ -45,11 +45,13 @@ const generateSiteReportandDownload = async ( siteAnalysisData = JSONReport[0]; } - const folderName = siteAnalysisData.pageUrl - .replace(/^https?:\/\//, '') - .replace(/\/+/g, '-'); + const zipFolder: JSZip | null = zip.folder( + getFolderName(siteAnalysisData.pageUrl) + ); - const zipFolder = zip.folder(folderName) as JSZip; + if (!zipFolder) { + return; + } createZip(siteAnalysisData, zipFolder); diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts index 2b8058d84..cd7672550 100644 --- a/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts @@ -63,3 +63,14 @@ export const createZip = (analysisData: CompleteJson, zipObject: JSZip) => { zipObject.file('report.csv', summaryDataCSV); zipObject.file('report.json', JSON.stringify(analysisData, null, 4)); }; + +export const getFolderName = (pageUrl: string) => { + let folderName = pageUrl.replace(/^https?:\/\//, '').replace(/\/+/g, '-'); + + const lastDashIndex = folderName.lastIndexOf('-'); + if (lastDashIndex !== -1) { + folderName = folderName.substring(0, lastDashIndex); + } + + return folderName; +}; From c01a557d99619926baa1ad9b9c3041c7d0b79b60 Mon Sep 17 00:00:00 2001 From: Mayank Rana Date: Mon, 1 Jan 2024 18:54:07 +0530 Subject: [PATCH 4/5] fix: trim string and use correct condition for hyphen removal --- .../src/components/utils/reportDownloader/utils.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts index cd7672550..ee57d1e96 100644 --- a/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/utils.ts @@ -65,10 +65,13 @@ export const createZip = (analysisData: CompleteJson, zipObject: JSZip) => { }; export const getFolderName = (pageUrl: string) => { - let folderName = pageUrl.replace(/^https?:\/\//, '').replace(/\/+/g, '-'); + let folderName = pageUrl + .trim() + .replace(/^https?:\/\//, '') + .replace(/\/+/g, '-'); - const lastDashIndex = folderName.lastIndexOf('-'); - if (lastDashIndex !== -1) { + if (folderName.endsWith('-')) { + const lastDashIndex = folderName.lastIndexOf('-'); folderName = folderName.substring(0, lastDashIndex); } From b8a82f836db3cffd5684717bb647e96555617fe2 Mon Sep 17 00:00:00 2001 From: Mayank Rana Date: Mon, 1 Jan 2024 19:13:33 +0530 Subject: [PATCH 5/5] Update testcases --- .../generateReports/tests/data.mock.ts | 61 ++++++++++++++++++- .../tests/generateSummaryData.ts | 39 ++++++++++++ .../tests/generateTechnologyCSV.ts | 29 +++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateSummaryData.ts create mode 100644 packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateTechnologyCSV.ts diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts index e82f90583..1659c278e 100644 --- a/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/data.mock.ts @@ -18,7 +18,66 @@ import type { CompleteJson } from '../../../../../types'; export const mockData1: CompleteJson = { pageUrl: 'https://edition.cnn.com/', - technologyData: [], + technologyData: [ + { + slug: 'bootstrap', + name: 'Bootstrap', + description: + 'Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains CSS and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components.', + confidence: 100, + version: null, + icon: 'Bootstrap.svg', + website: 'https://getbootstrap.com', + cpe: 'cpe:2.3:a:getbootstrap:bootstrap:*:*:*:*:*:*:*:*', + categories: [ + { + id: 66, + slug: 'ui-frameworks', + name: 'UI frameworks', + }, + ], + }, + { + slug: 'varnish', + name: 'Varnish', + description: 'Varnish is a reverse caching proxy.', + confidence: 100, + version: null, + icon: 'Varnish.svg', + website: 'https://www.varnish-cache.org', + cpe: 'cpe:2.3:a:varnish-software:varnish_cache:*:*:*:*:*:*:*:*', + categories: [ + { + id: 23, + slug: 'caching', + name: 'Caching', + }, + ], + }, + { + slug: 'styled-components', + name: 'styled-components', + description: + 'Styled components is a CSS-in-JS styling framework that uses tagged template literals in JavaScript.', + confidence: 100, + version: '4.3.2', + icon: 'styled-components.svg', + website: 'https://styled-components.com', + cpe: null, + categories: [ + { + id: 12, + slug: 'javascript-frameworks', + name: 'JavaScript frameworks', + }, + { + id: 47, + slug: 'development', + name: 'Development', + }, + ], + }, + ], cookieData: { 'https://edition.cnn.com': { cookiesCount: 2, diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateSummaryData.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateSummaryData.ts new file mode 100644 index 000000000..8c49c3356 --- /dev/null +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateSummaryData.ts @@ -0,0 +1,39 @@ +/* + * 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. + */ + +/** + * Internal dependencies + */ +import generateSummaryDataCSV from '../generateSummaryDataCSV'; +import { mockData1 } from './data.mock'; + +describe('generateSummaryDataCSV', () => { + it('should create CSV string for summary data', () => { + const CSVString = generateSummaryDataCSV(mockData1); + + expect(CSVString.split('\r\n').filter((str) => str).length).toBe(12); + }); + + it('shoulde have 3 cookies', () => { + const CSVString = generateSummaryDataCSV(mockData1); + + expect( + CSVString.split('\r\n').filter( + (str) => str.split(',')[0] === 'Total Cookies' + ) + ).toStrictEqual(['Total Cookies, 3']); + }); +}); diff --git a/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateTechnologyCSV.ts b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateTechnologyCSV.ts new file mode 100644 index 000000000..3020751e4 --- /dev/null +++ b/packages/cli-dashboard/src/components/utils/reportDownloader/generateReports/tests/generateTechnologyCSV.ts @@ -0,0 +1,29 @@ +/* + * 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. + */ + +/** + * Internal dependencies + */ +import generateTechnologyCSV from '../generateTechnologyCSV'; +import { mockData1 } from './data.mock'; + +describe('generateTechnologyCSV', () => { + it('should create CSV string for technology data', () => { + const CSVString = generateTechnologyCSV(mockData1); + + expect(CSVString.split('\r\n').filter((str) => str).length).toBe(4); + }); +});