Skip to content

Commit

Permalink
update for assets
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 25, 2024
1 parent 26c0332 commit 763c100
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 158 deletions.
98 changes: 98 additions & 0 deletions lib/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const fs = require('fs');
const path = require('path');
const { deflateSync } = require('lz-utils');
const Util = require('./utils/util.js');
const assetsMap = require('./packages/monocart-reporter-assets.js');

const Assets = {

getFileContent: (id) => {
const content = assetsMap[id];
if (!content) {
Util.logError(`Not found module: ${id}`);
return '';
}
return content;
},

saveHtmlReport: async (options) => {

const {
inline,
reportData,
jsFiles,
assetsPath,
outputDir,
htmlFile,

saveReportPath,

reportDataFile
} = options;

// save path
const htmlPath = path.resolve(outputDir, htmlFile);
const reportPath = Util.relativePath(htmlPath);
if (saveReportPath) {
reportData[saveReportPath] = reportPath;
}

// report data
const reportDataCompressed = deflateSync(JSON.stringify(reportData));
const reportDataStr = `window.reportData = '${reportDataCompressed}';`;

// js libs
const jsList = [];

// deps
jsFiles.forEach((id) => {
jsList.push({
filename: `${id}.js`,
str: Assets.getFileContent(id)
});
});

// html content
let htmlStr = '';
const EOL = Util.getEOL();
if (inline) {
htmlStr = [
'<script>',
reportDataStr,
... jsList.map((it) => it.str),
'</script>'
].join(EOL);
} else {

await Util.writeFile(path.resolve(outputDir, reportDataFile), reportDataStr);

const assetsDir = path.resolve(outputDir, assetsPath);
const relAssetsDir = Util.relativePath(assetsDir, outputDir);

for (const item of jsList) {
const filePath = path.resolve(assetsDir, item.filename);
if (!fs.existsSync(filePath)) {
await Util.writeFile(filePath, item.str);
}
}

htmlStr = [
`<script src="${reportDataFile}"></script>`,
... jsList.map((it) => `<script src="${relAssetsDir}/${it.filename}"></script>`)
].join(EOL);
}

// html
const template = Assets.getFileContent('template');
const html = Util.replace(template, {
title: reportData.title,
content: htmlStr
});

await Util.writeFile(htmlPath, html);

return reportPath;
}
};

module.exports = Assets;
5 changes: 3 additions & 2 deletions lib/generate-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const EC = require('eight-colors');
const nodemailer = require('nodemailer');
const Util = require('./utils/util.js');
const emailPlugin = require('./plugins/email.js');
const Assets = require('./assets.js');

// ===========================================================================

Expand All @@ -17,7 +18,7 @@ const generateJson = (outputDir, htmlFile, reportData) => {
const generateHtml = async (outputDir, htmlFile, reportData, inline) => {

// deps
const jsFiles = [Util.resolvePackage('monocart-reporter-app.js')];
const jsFiles = ['monocart-reporter-app'];

const options = {
inline,
Expand All @@ -30,7 +31,7 @@ const generateHtml = async (outputDir, htmlFile, reportData, inline) => {
reportDataFile: 'report-data.js'
};

const htmlPath = await Util.saveHtmlReport(options);
const htmlPath = await Assets.saveHtmlReport(options);

return htmlPath;
};
Expand Down
5 changes: 3 additions & 2 deletions lib/plugins/network/network.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const Util = require('../../utils/util.js');
const Assets = require('../../assets.js');

const getHarData = (har) => {
if (typeof har === 'string') {
Expand Down Expand Up @@ -99,7 +100,7 @@ const saveNetworkHtmlReport = async (reportData, _options) => {
const { htmlDir, inline } = _options;

// deps
const jsFiles = [Util.resolvePackage('monocart-reporter-network.js')];
const jsFiles = ['monocart-reporter-network'];

const options = {
inline,
Expand All @@ -112,7 +113,7 @@ const saveNetworkHtmlReport = async (reportData, _options) => {
reportDataFile: 'network-data.js'
};

const htmlPath = await Util.saveHtmlReport(options);
const htmlPath = await Assets.saveHtmlReport(options);

return htmlPath;
};
Expand Down
100 changes: 0 additions & 100 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const crypto = require('crypto');
const EC = require('eight-colors');
const CG = require('console-grid');
const Share = require('../platform/share.js');
const { deflateSync } = require('lz-utils');

const getDefaultOptions = require('../default/options.js');

Expand Down Expand Up @@ -160,105 +159,6 @@ const Util = {
Util.initDir(assetsDir);
},

resolveNodeModule: (p) => {

// root
const cwd = path.resolve('node_modules', p);
if (fs.existsSync(cwd)) {
return cwd;
}

// sub node modules
const sub = path.resolve(__dirname, '../../node_modules', p);
if (fs.existsSync(sub)) {
return sub;
}

// same level dep
const dep = path.resolve(__dirname, '../../../', p);
if (fs.existsSync(dep)) {
return dep;
}

Util.logError(`Not found module: ${p}`);

return cwd;
},

resolvePackage: (p) => {
return path.resolve(__dirname, '../packages', p);
},

saveHtmlReport: async (options) => {

const {
inline,
reportData,
jsFiles,
assetsPath,
outputDir,
htmlFile,

reportDataFile
} = options;

// report data
const reportDataCompressed = deflateSync(JSON.stringify(reportData));
const reportDataStr = `window.reportData = '${reportDataCompressed}';`;

// js libs
const jsList = [];
for (const jsFile of jsFiles) {
const jsStr = await Util.readFile(jsFile);
jsList.push({
filename: path.basename(jsFile),
str: jsStr
});
}

// html content
let htmlStr = '';
const EOL = Util.getEOL();
if (inline) {
htmlStr = [
'<script>',
reportDataStr,
... jsList.map((it) => it.str),
'</script>'
].join(EOL);
} else {

await Util.writeFile(path.resolve(outputDir, reportDataFile), reportDataStr);

const assetsDir = path.resolve(outputDir, assetsPath);
const relAssetsDir = Util.relativePath(assetsDir, outputDir);

for (const item of jsList) {
const filePath = path.resolve(assetsDir, item.filename);
if (!fs.existsSync(filePath)) {
await Util.writeFile(filePath, item.str);
}
}

htmlStr = [
`<script src="${reportDataFile}"></script>`,
... jsList.map((it) => `<script src="${relAssetsDir}/${it.filename}"></script>`)
].join(EOL);
}

// html
const htmlPath = path.resolve(outputDir, htmlFile);
const template = Util.getTemplate(path.resolve(__dirname, '../default/template.html'));
const html = Util.replace(template, {
title: reportData.title,
content: htmlStr
});

await Util.writeFile(htmlPath, html);

return Util.relativePath(htmlPath);
},

getEOL: function(content) {
if (!content) {
return os.EOL;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"eight-colors": "^1.3.0",
"koa": "^2.15.3",
"koa-static-resolver": "^1.0.6",
"lz-utils": "^2.0.2",
"lz-utils": "^2.1.0",
"monocart-coverage-reports": "^2.9.3",
"monocart-locator": "^1.0.2",
"nodemailer": "^6.9.14"
Expand Down
12 changes: 0 additions & 12 deletions packages/loader/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions packages/loader/src/index.js

This file was deleted.

Loading

0 comments on commit 763c100

Please sign in to comment.