Skip to content

Instantly share code, notes, and snippets.

@xn
Created August 11, 2017 19:08
Show Gist options
  • Save xn/d9d71acb3af59eb90d921ecdf8e738e0 to your computer and use it in GitHub Desktop.
Save xn/d9d71acb3af59eb90d921ecdf8e738e0 to your computer and use it in GitHub Desktop.
express app to substitute middleware for ember-cli-code-coverage for use with ember-electron app
'use strict';
/**
* @typedef {Object} Configuration
* @property {String} coverageEnvVar - name of environment variable for coverage
* @property {String} coverageFolder - directory in which to write coverage to
* @property {Array<String>} excludes - list of glob paths to exclude
* @property {Array<String>} reporters - list of reporters
*/
var extend = require('extend');
var fs = require('fs');
var path = require('path');
/**
* Get configuration for a project, falling back to default configuration if
* project does not provide a configuration of its own
* @param {String} configPath - The path for the configuration of the project
* @returns {Configuration} configuration to use for project
*/
function config(configPath) {
var configDirName = path.dirname(configPath);
var configFile = path.resolve(path.join(configDirName, 'coverage.js'));
var defaultConfig = getDefaultConfig();
if (fs.existsSync(configFile)) {
var projectConfig = require(configFile);
return extend({}, defaultConfig, projectConfig);
}
return defaultConfig;
}
/**
* Get default configuration
* @returns {Configuration} default configuration
*/
function getDefaultConfig() {
return {
coverageEnvVar: 'COVERAGE',
coverageFolder: 'coverage',
excludes: [
'*/mirage/**/*'
],
useBabelInstrumenter: false,
// The reasoning behind this default is to match the default language version
// supported by Ember CLI. As of Ember CLI 2.13, it supports ES2017.
babelPlugins: [
'babel-plugin-transform-async-to-generator'
],
reporters: [
'html',
'lcov'
]
};
}
module.exports = config;
const http = require('http'),
path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
cors = require('cors'),
Istanbul = require('istanbul'),
config = require('./config'),
fs = require('fs-extra'),
crypto = require('crypto');
preflight = require('preflight');
// Create global app object
const app = express();
const options = {configPath: '/path/to/ember-electron/app/config', root:'/path/to/ember-electron/app'}
app.use(cors());
app.use(preflight());
// Normal express config defaults
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ limit: '500mb' }));
function logError(err, req, res, next) {
console.error(err.stack);
next(err);
}
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.post('/write-coverage',
(req, res) => {
const collector = new Istanbul.Collector();
const _config = config(options.configPath);
if (_config.parallel) {
_config.coverageFolder = _config.coverageFolder + '_' + crypto.randomBytes(4).toString('hex');
if (_config.reporters.indexOf('json') === -1) {
_config.reporters.push('json');
}
}
const reporter = new Istanbul.Reporter(null, path.join(options.root, _config.coverageFolder));
const sync = true;
collector.add(req.body);
if (_config.reporters.indexOf('json-summary') === -1) {
_config.reporters.push('json-summary');
}
reporter.addAll(_config.reporters);
reporter.write(collector, sync, () => {
const summaryFilePath = path.join(options.root, _config.coverageFolder, 'coverage-summary.json');
const results = fs.readJSONSync(summaryFilePath);
res.send(results);
});
},
logError);
app.listen(8008, () => console.log('Example app listening on port 8008!'));
{
"name": "code-coverage",
"version": "0.0.1",
"description": "code coverage results endpoint",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "boop",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.2",
"cors": "^2.8.4",
"crypto": "0.0.3",
"express": "^4.15.4",
"extend": "^3.0.1",
"fs-extra": "^4.0.1",
"istanbul": "^0.4.5",
"preflight": "^0.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment