Skip to content

Commit

Permalink
Speed up "check:cover" by using async exec (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 2, 2019
1 parent 844aab7 commit acf95e1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
51 changes: 33 additions & 18 deletions resources/check-cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,45 @@
'use strict';

const path = require('path');
const { exec, writeFile, readdirRecursive } = require('./utils');
const { exec, execAsync, writeFile, readdirRecursive } = require('./utils');

const fullCoverage = {};
getFullCoverage()
.then(fullCoverage =>
writeFile(
'./coverage/flow/full-coverage.json',
JSON.stringify(fullCoverage),
),
)
.catch(error => {
console.error(error.stack);
process.exit(1);
});

exec('flow start --quiet');
try {
exec('flow check', { stdio: 'inherit' });
async function getFullCoverage() {
const fullCoverage = {};

// TODO: measure coverage for all files.
// ATM it takes too much time and test files missing types for chai & mocha
for (const filepath of readdirRecursive('./src', { ignoreDir: /^__.*__$/ })) {
if (filepath.endsWith('.js')) {
const fullpath = path.join('src/', filepath);
fullCoverage[fullpath] = getCoverage(fullpath);
}
exec('flow start --quiet');
try {
exec('flow check', { stdio: 'inherit' });

// TODO: measure coverage for all files. ATM missing types for chai & mocha
const files = readdirRecursive('./src', { ignoreDir: /^__.*__$/ })
.filter(filepath => filepath.endsWith('.js'))
.map(filepath => path.join('src/', filepath));

await Promise.all(files.map(getCoverage)).then(covarageObjs => {
for (const coverage of covarageObjs) {
fullCoverage[coverage.path] = coverage;
}
});
} finally {
exec('flow stop --quiet');
}
} finally {
exec('flow stop --quiet');
return fullCoverage;
}

writeFile('./coverage/flow/full-coverage.json', JSON.stringify(fullCoverage));

function getCoverage(filepath) {
const json = exec(`flow coverage --json ${filepath}`);
async function getCoverage(filepath) {
const json = await execAsync(`flow coverage --json ${filepath}`);
const flowExpressions = JSON.parse(json).expressions;

const s = {};
Expand Down
23 changes: 21 additions & 2 deletions resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'use strict';

const fs = require('fs');
const util = require('util');
const path = require('path');
const childProcess = require('child_process');

Expand All @@ -19,8 +20,25 @@ function exec(command, options) {
encoding: 'utf-8',
...options,
});
// Remove trailing new line
return (output || '')
return removeTrailingNewLine(output);
}

const childProcessExec = util.promisify(childProcess.exec);
async function execAsync(command, options) {
const output = await childProcessExec(command, {
maxBuffer: 10 * 1024 * 1024, // 10MB
encoding: 'utf-8',
...options,
});
return removeTrailingNewLine(output.stdout);
}

function removeTrailingNewLine(str) {
if (str == null) {
return str;
}

return str
.split('\n')
.slice(0, -1)
.join('\n');
Expand Down Expand Up @@ -87,6 +105,7 @@ function parseSemver(version) {

module.exports = {
exec,
execAsync,
copyFile,
writeFile,
rmdirRecursive,
Expand Down

0 comments on commit acf95e1

Please sign in to comment.