Skip to content

Commit

Permalink
merge cli (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 1, 2024
1 parent 82d63c5 commit 59f63ec
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 8 deletions.
114 changes: 113 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ const http = require('http');
const https = require('https');
const net = require('net');
const os = require('os');
const { pathToFileURL } = require('url');
const EC = require('eight-colors');
const KSR = require('koa-static-resolver');
const Koa = require('koa');
const CG = require('console-grid');

const { program, open } = require('./packages/monocart-reporter-vendor.js');
const Util = require('./utils/util.js');
const {
program, open, glob, findUpSync, supportsColor
} = require('./packages/monocart-reporter-vendor.js');
const getDefaultOptions = require('./default/options.js');
const merge = require('./merge-data.js');
const version = require('../package.json').version;

// https://github.com/chalk/supports-color
// disabled color if Terminal stdout does not support color
if (!supportsColor.stdout) {
EC.disabled = true;
}


const getInternalIps = () => {
const n = os.networkInterfaces();
// console.log(n);
Expand Down Expand Up @@ -153,6 +165,93 @@ const serveReport = async (p, options) => {

};

const findUpConfig = (customConfigFile) => {
if (customConfigFile) {
if (fs.existsSync(customConfigFile)) {
return customConfigFile;
}
// custom config not found
return;
}

const defaultConfigList = [
'mr.config.js',
'mr.config.cjs',
'mr.config.mjs',
'mr.config.json',
'mr.config.ts'
];

const configPath = findUpSync(defaultConfigList);
if (configPath) {
return configPath;
}

// default config not found
};

const resolveConfigOptions = async (configPath) => {
// json format
const ext = path.extname(configPath);
if (ext === '.json' || configPath.slice(-2) === 'rc') {
return JSON.parse(Util.readFileSync(configPath));
}

let configOptions;
let err;
try {
configOptions = await import(pathToFileURL(configPath));
} catch (ee) {
err = ee;
}

if (err) {
Util.logError(`ERROR: failed to load config "${configPath}": ${err && err.message} `);
return;
}

// could be multiple level default
while (configOptions && configOptions.default) {
configOptions = configOptions.default;
}

return configOptions;
};

const mergeReports = async (str, cliOptions) => {

const customConfig = cliOptions.config;
const configPath = findUpConfig(customConfig);

let configOptions = {};
if (configPath) {
configOptions = await resolveConfigOptions(configPath);
} else {
if (customConfig) {
Util.logError(`ERROR: not found config file: ${customConfig}`);
return;
}
}

const options = {
... getDefaultOptions(),
... configOptions,
... cliOptions
};

Util.initLoggingLevel(options.logging);

const files = await glob(str);
if (!Util.isList(files)) {
Util.logError(`ERROR: no files found with glob: ${str}`);
return;
}
// console.log(files, options);

await merge(files, options);

};

program
.name('monocart')
.description('CLI to serve monocart reporter')
Expand All @@ -177,6 +276,19 @@ program.command('serve-report')
serveReport(str, options);
});

program.command('merge-reports')
.alias('merge')
.description('Merge reports')
.argument('<path>', 'path to report dirs')
.option('-c, --config <path>', 'config file')

.option('-n, --name <name>', 'report name for title')
.option('-o, --outputFile <path>', 'output file')

.action((str, options) => {
mergeReports(str, options);
});

program.addHelpText('after', `
Starts ${EC.cyan('https')} with option --ssl:
npx monocart show-report <path-to-report> ${EC.cyan('--ssl <path-to-key,path-to-cert>')}
Expand Down
35 changes: 30 additions & 5 deletions lib/merge-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,27 @@ const mergeArtifacts = async (artifactsList, options) => {
const outputDir = options.outputDir;

const artifacts = [];
const coverageList = [];
const coverageRawList = [];

artifactsList.forEach((item) => {
const jsonDir = item.jsonDir;
let hasAssets = false;
item.artifacts.forEach((art) => {

// merge global coverage
if (art.global && art.type === 'coverage') {

const rawDir = path.resolve(jsonDir, art.rawDir);
if (fs.existsSync(rawDir)) {
coverageRawList.push(rawDir);
coverageList.push({
jsonDir,
art
});
if (art.rawDir) {
const rawDir = path.resolve(jsonDir, art.rawDir);
if (fs.existsSync(rawDir)) {
coverageRawList.push(rawDir);
}
}
return;

}

const targetDirName = path.dirname(art.path);
Expand All @@ -142,11 +148,30 @@ const mergeArtifacts = async (artifactsList, options) => {

});

// merge global coverage raws
if (coverageRawList.length) {
const coverage = await mergeGlobalCoverageReport(coverageRawList, options);
if (coverage) {
artifacts.push(coverage);
}
} else {

// copy single one art
const item = coverageList.find((it) => it.art.path);
if (item) {
const { art, jsonDir } = item;

const targetDirName = path.dirname(art.path);
// copy all files in art dir, like network, audit
copyTarget(targetDirName, jsonDir, outputDir);

copyTarget('assets', jsonDir, outputDir);

// update path relative to cwd/root
art.path = Util.relativePath(path.resolve(outputDir, art.path));

artifacts.push(art);
}
}

return artifacts;
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ const Util = {
if (!outputFile || typeof outputFile !== 'string') {
outputFile = getDefaultOptions().outputFile;
}

// end with html
if (!outputFile.endsWith('.html')) {
outputFile = path.join(outputFile, 'report.html');
}

return path.resolve(outputFile);
},

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@
"ansi-to-html": "^0.7.2",
"async-tick": "^1.0.0",
"autolinker": "^4.0.0",
"axios": "^1.7.5",
"axios": "^1.7.7",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"eslint": "^9.9.1",
"eslint-config-plus": "^2.0.2",
"eslint-plugin-html": "^8.1.1",
"eslint-plugin-vue": "^9.27.0",
"file-saver": "^2.0.5",
"find-up": "^7.0.0",
"github-markdown-css": "^5.6.1",
"glob": "^11.0.0",
"marked": "^14.1.0",
"mermaid": "^11.0.2",
"mitt": "^3.0.1",
Expand All @@ -76,6 +78,7 @@
"stack-utils": "^2.0.6",
"stylelint": "^16.9.0",
"stylelint-config-plus": "^1.1.2",
"supports-color": "^9.4.0",
"turbogrid": "^3.2.0",
"vine-ui": "^3.1.16",
"ws": "^8.18.0"
Expand Down
8 changes: 7 additions & 1 deletion packages/vendor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import WebSocket, { WebSocketServer } from 'ws';
import sanitize from 'sanitize-filename';
import { program } from 'commander';
import open from 'open';
import { glob } from 'glob';
import { findUpSync } from 'find-up';
import supportsColor from 'supports-color';

export {
StackUtils,
Expand All @@ -12,5 +15,8 @@ export {
WebSocketServer,
sanitize,
program,
open
open,
glob,
findUpSync,
supportsColor
};

0 comments on commit 59f63ec

Please sign in to comment.