Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/mergeAxeResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { getBrowserToRun, getPlaywrightLaunchOptions } from './constants/common.
import {
getWcagPassPercentage,
getProgressPercentage,
zipResults,
getIssuesPercentage,
getWcagCriteriaMap,
categorizeWcagCriteria,
Expand All @@ -40,6 +39,7 @@ import {
getUserDataTxt,
getVersion,
retryFunction,
zipResults,
} from './utils/index.js';
import { consoleLogger, silentLogger } from './logs.js';
import itemTypeDescription from './constants/itemTypeDescription.js';
Expand Down
54 changes: 0 additions & 54 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import path from 'path';
import fs from 'fs-extra';
import axe, { Rule } from 'axe-core';
import JSZip from 'jszip';
import { createReadStream, createWriteStream } from 'fs';
import constants from './constants/constants.js';
import { consoleLogger, errorsTxtPath } from './logs.js';
import { getAxeConfiguration } from './crawlers/custom/getAxeConfiguration.js';
Expand Down Expand Up @@ -763,58 +761,6 @@ export const getIssuesPercentage = async (
};
};

export const zipResults = async (zipName: string, resultsPath: string): Promise<void> => {
// Resolve and validate the output path
const zipFilePath = path.isAbsolute(zipName) ? zipName : path.join(resultsPath, zipName);

// Ensure parent dir exists
fs.mkdirSync(path.dirname(zipFilePath), { recursive: true });

// Remove any prior file atomically
try {
fs.unlinkSync(zipFilePath);
} catch {
/* ignore if not exists */
}

// CWD must exist and be a directory
const stats = fs.statSync(resultsPath);
if (!stats.isDirectory()) {
throw new Error(`resultsPath is not a directory: ${resultsPath}`);
}
async function addFolderToZip(folderPath: string, zipFolder: JSZip): Promise<void> {
const items = await fs.readdir(folderPath);
for (const item of items) {
const fullPath = path.join(folderPath, item);
const stats = await fs.stat(fullPath);
if (stats.isDirectory()) {
const folder = zipFolder.folder(item);
await addFolderToZip(fullPath, folder);
} else {
// Add file as a stream so that it doesn't load the entire file into memory
zipFolder.file(item, createReadStream(fullPath));
}
}
}

const zip = new JSZip();
await addFolderToZip(resultsPath, zip);

const zipStream = zip.generateNodeStream({
type: 'nodebuffer',
streamFiles: true,
compression: 'DEFLATE',
});

await new Promise((resolve, reject) => {
const outStream = createWriteStream(zipFilePath);
zipStream
.pipe(outStream)
.on('finish', () => resolve(undefined))
.on('error', reject);
});
};

/**
* Determines which WCAG criteria might appear in the "needsReview" category
* based on axe-core's rule configuration.
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { default as getStoragePath } from './getStoragePath.js';
export { default as getPdfStoragePath } from './getPdfStoragePath.js';
export { default as isWhitelistedContentType } from './isWhitelistedContentType.js';
export { default as retryFunction } from './retryFunction.js';
export { default as zipResults } from './zipResults.js';
export { default as randomThreeDigitNumberString } from './randomThreeDigitNumberString.js';
export { areLinksEqual, isFollowStrategy } from './linkStrategy.js';
export { setHeadlessMode, setThresholdLimits } from './runtimeFlags.js';
Expand Down
58 changes: 58 additions & 0 deletions src/utils/zipResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import path from 'path';
import fs from 'fs-extra';
import JSZip from 'jszip';
import { createReadStream, createWriteStream } from 'fs';

const zipResults = async (zipName: string, resultsPath: string): Promise<void> => {
// Resolve and validate the output path
const zipFilePath = path.isAbsolute(zipName) ? zipName : path.join(resultsPath, zipName);

// Ensure parent dir exists
fs.mkdirSync(path.dirname(zipFilePath), { recursive: true });

// Remove any prior file atomically
try {
fs.unlinkSync(zipFilePath);
} catch {
/* ignore if not exists */
}

// CWD must exist and be a directory
const stats = fs.statSync(resultsPath);
if (!stats.isDirectory()) {
throw new Error(`resultsPath is not a directory: ${resultsPath}`);
}
async function addFolderToZip(folderPath: string, zipFolder: JSZip): Promise<void> {
const items = await fs.readdir(folderPath);
for (const item of items) {
const fullPath = path.join(folderPath, item);
const stats = await fs.stat(fullPath);
if (stats.isDirectory()) {
const folder = zipFolder.folder(item);
await addFolderToZip(fullPath, folder);
} else {
// Add file as a stream so that it doesn't load the entire file into memory
zipFolder.file(item, createReadStream(fullPath));
}
}
}

const zip = new JSZip();
await addFolderToZip(resultsPath, zip);

const zipStream = zip.generateNodeStream({
type: 'nodebuffer',
streamFiles: true,
compression: 'DEFLATE',
});

await new Promise((resolve, reject) => {
const outStream = createWriteStream(zipFilePath);
zipStream
.pipe(outStream)
.on('finish', () => resolve(undefined))
.on('error', reject);
});
};

export default zipResults;