UNPKG

typescript-assistant

Version:

Combines and integrates professional Typescript tools into your project

103 lines 4.32 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../util"); let delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); exports.createNyc = (dependencies) => { let { taskRunner, logger, bus, git } = dependencies; let runningTask; let coolingDown; let startNyc = () => __awaiter(this, void 0, void 0, function* () { let hasFailingTest = false; let myCoolingDown = delay(100); coolingDown = myCoolingDown; yield (myCoolingDown); if (coolingDown !== myCoolingDown) { return false; } if (runningTask) { logger.log('nyc', 'Aborting previous nyc run'); runningTask.kill(); runningTask = undefined; } else { bus.report({ tool: 'test', status: 'busy' }); bus.report({ tool: 'coverage', status: 'busy' }); } let errorLine = ''; let lastLineWasNotOk = false; let handleOutput = (line) => { if (task === runningTask) { if (lastLineWasNotOk) { errorLine = line; lastLineWasNotOk = false; } // For extra debug info: // if (!/^ok \d+ (.*)/.exec(line)) { logger.log('nyc', ' [ ' + line); } let notOk = /^not ok \d+ (.*)/.exec(line); let contextIt = /^(} )?at Context\.\S+ \(([^)]+)\)/.exec(line); if (notOk) { lastLineWasNotOk = true; logger.log('nyc', `FAILED: ${notOk[1]}`); hasFailingTest = true; } else if (contextIt) { logger.log('nyc', `${util_1.absolutePath(contextIt[2])} ${errorLine}`); errorLine = ''; } } return true; }; let handleError = (line) => { if (task === runningTask) { if (!line.startsWith('ERROR: Coverage for')) { logger.error('nyc', line); } } return true; }; let task = runningTask = taskRunner.runTask('./node_modules/.bin/nyc', ('--check-coverage -- ' + './node_modules/.bin/mocha --require ts-node/register/transpile-only --reporter tap test/**/*-tests.ts*').split(' '), { name: 'nyc', logger, handleOutput, handleError }); return runningTask.result.then(() => { if (task === runningTask) { runningTask = undefined; logger.log('nyc', 'code coverage OK'); bus.report({ tool: 'test', status: 'ready', errors: 0 }); bus.report({ tool: 'coverage', status: 'ready', errors: 0 }); } return true; }).catch(() => __awaiter(this, void 0, void 0, function* () { if (task === runningTask) { runningTask = undefined; logger.log('nyc', 'code coverage FAILED'); bus.report({ tool: 'test', status: 'ready', errors: hasFailingTest ? 1 : 0 }); bus.report({ tool: 'coverage', status: 'ready', errors: 1 }); } let isOnBranch = yield git.isOnBranch(); return isOnBranch && !hasFailingTest; })); }); return { run: () => startNyc().catch(() => false), start: (triggers) => { bus.registerAll(triggers, startNyc); startNyc().catch(() => false); }, stop: () => { bus.unregister(startNyc); } }; }; //# sourceMappingURL=nyc.js.map