Skip to content

Commit

Permalink
added extra clean option for zip option
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Oct 9, 2024
1 parent 05c5cd3 commit 15e23f3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

* 2.9.5
- added extra `clean` option for `zip` option

* 2.9.4
- fixed merging entry for custom zip path

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1060,13 +1060,14 @@ see example [merge.js](https://github.com/cenfun/monocart-reporter-examples/blob

### Using `merge` CLI
```sh
npx monocart merge <glob-patterns>
# NOTE: The asterisk(*) is a special character which is interpreted by some operating systems (Mac and Linux), please put it in quotes
npx monocart merge '<glob-patterns>'
# -o --outputFile
npx monocart merge path-to/shard*/index.json -o merged-reports/index.html
npx monocart merge 'path-to/shard*/index.json' -o merged-reports/index.html
# -c --config
npx monocart merge path-to/shard*/index.json -c mr.config.js
npx monocart merge 'path-to/shard*/my-report.zip' -c mr.config.js
```
The default config files (In order of priority)
- mr.config.js
Expand Down
72 changes: 59 additions & 13 deletions lib/generate-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,80 @@ const generateJson = (outputDir, filename, reportData, options) => {
reportData.jsonPath = Util.relativePath(jsonPath);
};

const generateZip = (outputDir, filename, reportData, options) => {

if (!options.zip) {
return;
const initZipOptions = (outputDir, filename, zipOptions) => {
let clean = false;
let outputFile;
if (typeof zipOptions === 'string') {
outputFile = zipOptions;
} else if (typeof zipOptions === 'object') {
if (zipOptions.outputFile) {
outputFile = zipOptions.outputFile;
}
if (zipOptions.clean) {
clean = true;
}
}

let zipPath = path.resolve(outputDir, `${filename}.zip`);
if (typeof options.zip === 'string') {
zipPath = options.zip;
if (!zipPath.endsWith('.zip')) {
zipPath += '.zip';
if (outputFile) {
if (!outputFile.endsWith('.zip')) {
outputFile += '.zip';
}
const zipDir = path.dirname(zipPath);
const zipDir = path.dirname(outputFile);
if (!fs.existsSync(zipDir)) {
fs.mkdirSync(zipDir, {
recursive: true
});
}

} else {
outputFile = path.resolve(outputDir, `${filename}.zip`);
}


return {
outputFile,
clean
};
};

const generateZip = (outputDir, filename, reportData, options) => {

const zipOptions = options.zip;
if (!zipOptions) {
return;
}

const { outputFile, clean } = initZipOptions(outputDir, filename, zipOptions);

const reportFiles = [];
return new Promise((resolve) => {
const zipFile = new ZipFile();
zipFile.outputStream.pipe(fs.createWriteStream(zipPath)).on('close', function() {
zipFile.outputStream.pipe(fs.createWriteStream(outputFile)).on('close', function() {

// remove files after zip
reportData.reportFiles = reportFiles;
reportData.zipPath = Util.relativePath(zipPath);

// whether to clean the files after zipped
if (clean) {
// console.log('clean', reportFiles);
const dirSet = new Set();
reportFiles.forEach((f) => {
const fp = path.resolve(outputDir, f);
dirSet.add(path.dirname(fp));
Util.rmSync(fp);
});

// clean empty dirs
const dirList = Array.from(dirSet).reverse();
dirList.forEach((dir) => {
const files = fs.readdirSync(dir);
if (files.length === 0) {
Util.rmSync(dir);
}
});

}

reportData.zipPath = Util.relativePath(outputFile);

resolve();
});
Expand Down
7 changes: 6 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export type MonocartReporterOptions = {
* {boolean} using default path
* {string} zip file path
*/
zip?: boolean | string;
zip?: boolean | string | {
/** the zip file path */
outputFile?: string;
/** whether to clean the files after zipped */
clean?: boolean;
};

/**
* whether to copy attachments to the reporter output dir, defaults to true
Expand Down
18 changes: 14 additions & 4 deletions lib/merge-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,30 @@ const unzipDataFile = async (item, num, options) => {
// Do not forget to close the file once you're done
await zip.close();

let filename = path.basename(item, '.zip');
const filename = path.basename(item, '.zip');
let jsonPath = path.resolve(unzipDir, `${filename}.json`);
if (fs.existsSync(jsonPath)) {
return jsonPath;
}

// filename for both html and json
const htmlMap = {};
const jsonMap = {};
Util.forEachFile(unzipDir, (name, dir) => {
if (name.endsWith('.html')) {
filename = path.basename(name, '.html');
return 'break';
htmlMap[path.basename(name, '.html')] = true;
} else if (name.endsWith('.json')) {
jsonMap[path.basename(name, '.json')] = true;
}
}, true);

jsonPath = path.resolve(unzipDir, `${filename}.json`);
const keys = Object.keys(htmlMap);
for (const fn of keys) {
if (jsonMap[fn]) {
jsonPath = path.resolve(unzipDir, `${fn}.json`);
break;
}
}

return jsonPath;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"devDependencies": {
"@babel/code-frame": "^7.25.7",
"@playwright/test": "^1.47.2",
"@playwright/test": "^1.48.0",
"ansi-to-html": "^0.7.2",
"async-tick": "^1.0.0",
"autolinker": "^4.0.0",
Expand Down

0 comments on commit 15e23f3

Please sign in to comment.