Skip to content

Commit

Permalink
feat: tweak options
Browse files Browse the repository at this point in the history
  • Loading branch information
xudafeng committed Apr 25, 2023
1 parent f7ffcb9 commit 5bf3443
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 78 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
node_modules
npm-debug.log
coverage
.nyc_output
test/temp
*.gz
*.sw*
*.un~
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
Install Macaca command-line tool form npm

```bash
$ npm i macaca-cli -g
$ macaca coverage -h
$ npm i macaca-coverage -g
```

```bash
$ macaca-coverage diff --target-branch master --coverage-json-file ./coverage/coverage-final.json --output ./test/temp
```

## Use as Node.js module
Expand Down
98 changes: 46 additions & 52 deletions bin/macaca-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ const path = require('path');
const EOL = require('os').EOL;
const program = require('commander');
const Coverage = require('..');
const getDiff = require('../lib/core/diff');
const summaryTemplate = require('../lib/template/summary-template');
const { incrementalReporter } = require('../lib/core/incremental-coverage');
const _ = require('lodash');
const mkdirp = require('mkdirp').sync;
const { getCurrentBranch, getDiffData } = require('../lib/common/git-diff');
const summaryTemplate = require('../lib/web/template/summary-template');
const { incrementalReporter } = require('../lib/web/incremental-coverage');
const logger = require('../lib/common/logger');

const pkg = require('../package.json');
const cwd = process.cwd();
Expand Down Expand Up @@ -47,62 +50,53 @@ program
});
} else {
program.help();
process.exit(0);
}
});

program
.command('diff')
.description('Get diff')
.option('-c, --currentBranch [String]', 'current branch.', null)
.option('-t, --targetBranch [String]', 'target branch.', 'master')
.option('-o, --output [String]', 'diff save path.', 'coverage-diff.json')
.action(async (option) => {

const output = option.output;
const currentBranch = option.currentBranch;
const targetBranch = option.targetBranch;

const diffMap = await getDiff(cwd, currentBranch, targetBranch);
const newDiffMap = {};
for (const file in diffMap) {
const filePath = path.join(cwd, file);
newDiffMap[filePath] = diffMap[file];
}

console.info(`diff path: ${output}`);

fs.writeFileSync(output, JSON.stringify(newDiffMap, null, 2));
});

program
.command('incremental-reporter')
.description('Get incremental reporter')
.option('-c, --coveragePath [String]', 'coverage path.', 'coverage/coverage-final.json')
.option('-d, --diffPath [String]', 'diff path.', 'coverage-diff.json')
.option('-o, --output [String]', 'reporter path.', 'coverage')
.action(async (option) => {

const output = path.join(cwd, option.output);
const summaryHtmlpath = path.join(output, 'summary.html');
const diffPath = path.join(cwd, option.diffPath);
const coveragePath = path.join(cwd, option.coveragePath);
console.info(`diff path: ${diffPath}`);
console.info(`coverage path: ${coveragePath}`);

const diffMap = require(diffPath);
const coverageMap = require(coveragePath);

const incrementalMap = incrementalReporter(coverageMap, diffMap, {
projectPath: cwd,
needCollectedIncludes: process.env.INCREMENTAL_NEED_COLLECTED_INCLUDES || [],
reporterPath: output,
});
.description('generage diff')
.option('--current-branch [String]', 'current git branch name')
.option('--target-branch [String]', 'target git branch name')
.option('--coverage-json-file [String]', 'existed coverage json file')
.option('--output [String]', 'output directory.')
.action(async (cmdOptions) => {
const defaultOptions = {
currentBranch: getCurrentBranch({ cwd }),
targetBranch: 'master',
coverageJsonFile: path.resolve(cwd, 'coverage/coverage-final.json'),
output: cwd,
cwd,
};

// step1, get options
const options = Object.assign(defaultOptions, _.pick(cmdOptions, [
'currentBranch',
'targetBranch',
'coverageJsonFile',
'output',
]));
options.coverageJsonFile = path.resolve(cwd, options.coverageJsonFile);
options.output = path.resolve(cwd, options.output);
mkdirp(options.output);
options.diffFileName = 'diff-data.json';
options.diffReporterFileName = 'diff-repoter.html';
logger.info('diff command options:\n%j', options);

// step2, generate git diff json
const diffData = getDiffData(options);
const diffDataFilePath = path.resolve(options.output, options.diffFileName);
logger.info('gen diff data: %s', diffDataFilePath);
fs.writeFileSync(diffDataFilePath, JSON.stringify(diffData, null, 2));

// step3, generate diff reporter
const coverageMap = require(options.coverageJsonFile);

const diffReporterFilePath = path.resolve(options.output, options.diffReporterFileName);
const incrementalMap = incrementalReporter(coverageMap, diffData, options);

console.info(incrementalMap);
console.info(`reporter: ${output}`);
console.info(`summary: ${summaryHtmlpath}`);
fs.writeFileSync(summaryHtmlpath, summaryTemplate(incrementalMap.reporterHtml));
fs.writeFileSync(diffReporterFilePath, summaryTemplate(incrementalMap.reporterHtml));
});

program.parse(process.argv);
Expand Down
33 changes: 33 additions & 0 deletions lib/common/git-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const path = require('path');
const LCL = require('last-commit-log');

const getCurrentBranch = ({
cwd: gitDir,
}) => {
const lcl = new LCL(gitDir);
const commit = lcl.getLastCommitSync();
return commit.gitBranch;
};

exports.getCurrentBranch = getCurrentBranch;

exports.getDiffData = ({
cwd,
currentBranch,
targetBranch,
}) => {
const { diff } = LCL;
const diffMap = diff({
targetBranch,
currentBranch,
cwd,
});
const newDiffMap = {};
for (const filePath in diffMap) {
const newFilePath = path.join(cwd, filePath);
newDiffMap[newFilePath] = diffMap[filePath];
}
return newDiffMap;
};
18 changes: 0 additions & 18 deletions lib/core/diff.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const {
} = Coverage({
runtime: 'web',
});
const reporterTemplate = require('../template/reporter-template');

const reporterTemplate = require('./template/reporter-template');

const _incrementalStatements = (coverage = {}, diff = []) => {

Expand Down Expand Up @@ -150,10 +149,14 @@ const incrementalCoverage = (coverageMap = {}, diffMap = [], options = {}) => {
};

const incrementalReporter = (coverageMap = {}, diffMap = [], options = {}) => {
const {
projectPath,
reporterPath,
ossUrl,
} = options;

const incrementalMap = incrementalCoverage(coverageMap, diffMap, options);
const { coverage, summary } = incrementalMap;
const { projectPath, reporterPath, ossUrl } = options;

// create coverage reporter
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const fs = require('fs');
const path = require('path');


const getFilePath = (dir, fileName) => {
const filePath = path.join(dir, fileName);
if (!fs.existsSync(filePath)) {
Expand All @@ -17,7 +16,6 @@ const getFilePath = (dir, fileName) => {
};

const reporterTemplate = (summary, options) => {

const { projectPath, reporterPath, ossUrl } = options;

const rowCoverList = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module.exports = (html) => {
}
</style>
</head>
<body>${html}
<body>
${html}
</body>
</html>`;
};

0 comments on commit 5bf3443

Please sign in to comment.