UNPKG

@nx-dart/nx-dart

Version:

A Nx plugin, that adds support for developing Dart and Flutter packages in a Nx workspace

107 lines 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path = require("path"); const execute_command_1 = require("../../utils/execute-command"); const fs_1 = require("../../utils/fs"); const package_1 = require("../../utils/package"); const coverageDir = 'coverage'; const coverageLcovFile = path.join(coverageDir, 'lcov.info'); const coverageDartDataDir = path.join(coverageDir, 'dart'); function runExecutor(options, context) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const projectRoot = context.workspace.projects[context.projectName].root; const pubspec = (0, package_1.loadPubspec)(projectRoot); if (!pubspec) { throw new Error(`Could not find pubspec.yaml in ${projectRoot}`); } const tool = (0, package_1.isFlutterPackage)(pubspec) ? 'flutter' : 'dart'; const args = buildTestArguments(tool, options); if (options.coverage) { (0, fs_1.removeFile)(path.join(projectRoot, coverageDir)); } let success = yield (0, execute_command_1.executeCommand)({ executable: tool, arguments: args, cwd: projectRoot, throwOnFailure: false, }); if (success && options.coverage && tool === 'dart') { // The `flutter` tool already outputs coverage data in the form of a .lcov file, // so we only need to convert it when using the `dart` tool. success = yield convertCoverageDataToLcov(projectRoot); } return { success: success, }; }); } exports.default = runExecutor; function buildTestArguments(tool, options) { const command = ['test']; if (!('r' in options) && !('reporter' in options)) { // We default to the expanded reporter, because it plays better with Nx's caching of task // executions. command.push('--reporter', 'expanded'); } for (const [key, value] of Object.entries(options)) { if (key === 'targets') { // We add the targets as the last arguments, because they are not named options. continue; } if (key === 'coverage') { if (value) { switch (tool) { case 'dart': command.push('--coverage', coverageDartDataDir); break; case 'flutter': command.push('--coverage', '--coverage-path', coverageLcovFile); break; } } continue; } command.push(`--${key}`, value.toString()); } if (options.targets) { command.push(...options.targets); } return command; } function convertCoverageDataToLcov(projectRoot) { return tslib_1.__awaiter(this, void 0, void 0, function* () { // Ensure that the `coverage` package is globally installed. let success = yield (0, execute_command_1.executeCommand)({ executable: 'dart', arguments: ['pub', 'global', 'activate', 'coverage'], throwOnFailure: false, }); if (!success) { return false; } // Then run the conversion. success = yield (0, execute_command_1.executeCommand)({ executable: 'dart', arguments: [ 'pub', 'global', 'run', 'coverage:format_coverage', '--lcov', '--in', coverageDartDataDir, '--out', coverageLcovFile, ], cwd: projectRoot, throwOnFailure: false, }); if (!success) { return false; } (0, fs_1.removeFile)(path.join(projectRoot, coverageDartDataDir)); return true; }); } //# sourceMappingURL=executor.js.map