Skip to content

Commit

Permalink
Merge branch 'release/v0.11.0' of github.com:GoogleChromeLabs/ps-anal…
Browse files Browse the repository at this point in the history
…ysis-tool into ref/gdpr-banner-acceptance
  • Loading branch information
amovar18 committed Sep 6, 2024
2 parents b29ee38 + 43671b7 commit 69c6992
Show file tree
Hide file tree
Showing 60 changed files with 2,503 additions and 1,490 deletions.
711 changes: 709 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions packages/cli-dashboard/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
* External dependencies
*/
import React, { useEffect, useState } from 'react';
import type {
CompleteJson,
CookieFrameStorageType,
LibraryData,
TechnologyData,
} from '@google-psat/common';
import { I18n } from '@google-psat/i18n';
import {
type CompleteJson,
type CookieFrameStorageType,
type LibraryData,
type TechnologyData,
extractReportData,
extractCookies,
SiteMapReport,
SiteReport,
} from '@google-psat/report';
} from '@google-psat/common';
import { I18n } from '@google-psat/i18n';
import { SiteMapReport, SiteReport } from '@google-psat/report';

/**
* Internal dependencies
Expand Down
1 change: 1 addition & 0 deletions packages/cli-dashboard/src/dummyData/PSAT_DATA.js
Original file line number Diff line number Diff line change
Expand Up @@ -11739,4 +11739,5 @@ export default {
},
},
dateTime: '24 July, 2024, 12:12:04pm Asia/Calcutta',
psatVersion: '0.10.1-1',
};
3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,21 @@
"@google-psat/common": "*",
"@google-psat/i18n": "*",
"@google-psat/library-detection": "*",
"boxen": "^8.0.1",
"chalk": "^5.3.0",
"commander": "^10.0.1",
"fs-extra": "^11.1.1",
"promptly": "^3.2.0",
"sitemapper": "^3.1.8",
"spinnies": "^0.5.1",
"update-notifier": "^7.3.0",
"xml2js": "^0.6.2"
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/node": "^20.2.3",
"@types/promptly": "^3.0.2",
"@types/update-notifier": "^6.0.8",
"@types/xml2js": "^0.4.14",
"coffee": "^5.5.1",
"tsc-watch": "^6.0.4",
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import {
import { redLogger } from './utils/coloredLoggers';
import saveResultsAsHTML from './utils/saveResultAsHTML';
import getSelectorsFromPath from './utils/getSelectorsFromPath';
import checkLatestVersion from './utils/checkLatestVersion';
import packageJson from '../package.json';

events.EventEmitter.defaultMaxListeners = 15;

Expand All @@ -68,7 +70,7 @@ const isFromNPMRegistry = !existsSync(

program
.name(isFromNPMRegistry ? 'psat' : 'npm run cli')
.version('0.10.1')
.version(packageJson.version)
.usage(
isFromNPMRegistry ? '[website-url] [options]' : '[website-url] -- [options]'
)
Expand Down Expand Up @@ -165,6 +167,8 @@ program.parse();
const waitTime = program.opts().wait;
const selectorFilePath = program.opts().buttonSelectors;

await checkLatestVersion();

const numArgs: number = [
Boolean(url),
Boolean(sitemapUrl),
Expand Down Expand Up @@ -297,6 +301,7 @@ program.parse();
};
return {
pageUrl: _url,
psatVersion: packageJson.version, // For adding in downloaded JSON file.
technologyData: technologyAnalysisData ? technologyAnalysisData[ind] : [],
cookieData: cookieAnalysisAndFetchedResourceData[ind].cookieData,
libraryMatches: detectedMatchingSignatures ?? [],
Expand Down
65 changes: 65 additions & 0 deletions packages/cli/src/utils/checkLatestVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 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 updateNotifier from 'update-notifier';
import boxen from 'boxen';
import chalk from 'chalk';

/**
* Internal dependencies.
*/
import packageJson from '../../package.json';

/**
* Check latest version and show message.
*/
const checkLatestVersion = async () => {
// The update-notifier package doesn't display an update message on the first run, which isn't what we want.
// However, we wanted to leverage its other functionalities, so we check for updates manually.
const notifier = updateNotifier({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 * 365 * 50, // Large number to prevent check.
shouldNotifyInNpmScript: true,
});

// @todo Cache result somehow to prevent it running everytime.
const info = await notifier.fetchInfo();

const message =
'Update available ' +
chalk.dim(info.current) +
chalk.reset(' → ') +
chalk.green(info.latest) +
' \nRun ' +
chalk.cyan('npm i -g @google-psat/cli') +
' to update';

if (info.type !== 'latest') {
console.log(
boxen(message, {
padding: 1,
margin: 1,
textAlignment: 'center',
borderColor: 'yellow',
borderStyle: 'round',
})
);
}
};

export default checkLatestVersion;
10 changes: 9 additions & 1 deletion packages/cli/src/utils/saveReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
/**
* External dependencies.
*/
import { type CompleteJson } from '@google-psat/common';
import {
generateRootSummaryDataCSV,
type CompleteJson,
} from '@google-psat/common';
import { ensureFile, writeFile } from 'fs-extra';

/**
* Internal dependencies.
*/
Expand Down Expand Up @@ -54,6 +58,10 @@ const saveReports = async (
'report.html',
sitemapUrl
);

const rootSummaryData = generateRootSummaryDataCSV(result);
await ensureFile(path.join(outDir, 'report.csv'));
await writeFile(path.join(outDir, 'report.csv'), rootSummaryData);
// Sitemap report
await Promise.all(
result.map(async (siteReport) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/utils/saveResultAsHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import fs from 'fs';
* Internal dependencies
*/
import getOutputFilePath from './getOutputFilePath';
import packageJson from '../../package.json';

const isProduction = process.env.NODE_ENV === 'production';
/**
Expand Down Expand Up @@ -105,6 +106,7 @@ const saveResultsAsHTML = async (
translations: messages,
dateTime,
siteMapUrl: isSiteMap ? sitemapUrl : '',
psatVersion: packageJson.version,
})}</script>` +
htmlText.substring(htmlText.indexOf('</head>'));

Expand Down
94 changes: 94 additions & 0 deletions packages/cli/src/utils/tests/checkLatestVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2024 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 updateNotifier from 'update-notifier';
import boxen from 'boxen';
import chalk from 'chalk';
import { noop } from '@google-psat/common';

/**
* Internal dependencies.
*/
import checkLatestVersion from '../checkLatestVersion';

jest.mock('update-notifier', () => jest.fn());
jest.mock('boxen', () => jest.fn());
jest.mock('chalk', () => ({
dim: jest.fn((text) => text),
reset: jest.fn((text) => text),
green: jest.fn((text) => text),
cyan: jest.fn((text) => text),
}));

describe('checkLatestVersion', () => {
let fetchInfoMock;

beforeEach(() => {
fetchInfoMock = jest.fn();

updateNotifier.mockReturnValue({
fetchInfo: fetchInfoMock,
});

jest.spyOn(console, 'log').mockImplementation(noop);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should not show a message if the current version is the latest', async () => {
// Arrange
fetchInfoMock.mockResolvedValue({
current: '1.0.0',
latest: '1.0.0',
type: 'latest',
});

await checkLatestVersion();
expect(boxen).not.toHaveBeenCalled();
expect(console.log).not.toHaveBeenCalled();
});

it('should show a message if an update is available', async () => {
fetchInfoMock.mockResolvedValue({
current: '1.0.0',
latest: '2.0.0',
type: 'major',
});

const expectedMessage =
'Update available ' +
chalk.dim('1.0.0') +
chalk.reset(' → ') +
chalk.green('2.0.0') +
' \nRun ' +
chalk.cyan('npm i -g @google-psat/cli') +
' to update';

await checkLatestVersion();
expect(boxen).toHaveBeenCalledWith(expectedMessage, {
padding: 1,
margin: 1,
textAlignment: 'center',
borderColor: 'yellow',
borderStyle: 'round',
});
expect(console.log).toHaveBeenCalled();
});
});
3 changes: 2 additions & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"moduleResolution": "node",
"outDir": "dist",
"declarationDir": "dist-types",
"composite": true
"composite": true,
"resolveJsonModule": true
},
"exclude": ["**/tests/**/*.ts", "dist/**", "dist-types/**"],
"references": [
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/cookies.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ export interface TabCookies {
[key: string]: CookieTableData;
}

export type FrameType = 'outermost_frame' | 'fenced_frame' | 'sub_frame';
export interface TabFrames {
[key: string]: {
frameIds: string[];
frameType?: 'outermost_frame' | 'fenced_frame' | 'sub_frame';
frameType?: FrameType;
};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export { default as getCurrentDateAndTime } from './utils/getCurrentDateAndTime'
export { default as resolveWithTimeout } from './utils/resolveWithTimeout';
export { default as deriveBlockingStatus } from './utils/deriveBlockingStatus';
export { default as getLegendDescription } from './utils/getLegendDescription';
export { default as extractCookies } from './utils/extractCookies';
export { default as extractReportData } from './utils/extractReportData';
export { default as reshapeCookies } from './utils/reshapeCookies';
export * from './worker/enums';
export * from './utils/generateReports';
export * from './cookies.types';
Expand Down
Loading

0 comments on commit 69c6992

Please sign in to comment.