UNPKG

@yolkai/nx-insights

Version:

Nx Insights plugin for Nx

155 lines (154 loc) 5.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const rxjs_1 = require("rxjs"); const default_tasks_runner_1 = require("@yolkai/nx-workspace/src/tasks-runner/default-tasks-runner"); const fs = require("fs"); const axios = require('axios'); const insightsTaskRunner = (tasks, options, context) => { const res = new rxjs_1.Subject(); const notifier = createNotifier(options, context); let commandResult = true; notifier.startCommand(tasks).then(() => { default_tasks_runner_1.defaultTasksRunner(tasks, options).subscribe({ next: (t) => { commandResult = commandResult && t.success; res.next(t); notifier.endTask(t.task.id, stringifyStatus(t.success), ''); }, complete: () => { notifier.endCommand(stringifyStatus(commandResult)).then(() => { printNxInsightsStatus(notifier, commandResult); res.complete(); }); } }); }); return res; }; function createNotifier(options, context) { if (!process.env.NX_INSIGHTS_AUTH_TOKEN) { reportSetupError(`NX_INSIGHTS_AUTH_TOKEN env variable is not set.`); return new EmptyNotifier(); } if (process.env.NX_INSIGHTS_AUTH_TOKEN && !process.env.NX_INSIGHTS_BRANCH_ID) { reportSetupError(`NX_INSIGHTS_BRANCH_ID env variable is not set.`); return new EmptyNotifier(); } if (process.env.NX_INSIGHTS_AUTH_TOKEN && !process.env.NX_INSIGHTS_RUN_ID) { reportSetupError(`NX_INSIGHTS_RUN_ID env variable is not set.`); return new EmptyNotifier(); } return new InsightsNotifier(options, context); } function reportSetupError(reason) { console.error(`WARNING: Nx won't send data to Nx Insights.`); console.error(`Reason: ${reason}`); } function printNxInsightsStatus(notifier, commandResult) { if (notifier.errors.length > 0) { if (commandResult) { console.error(`The command succeeded, but we were unable to send the data to Nx Insights:`); } else { console.error(`We were unable to send the data to Nx Insights.`); } console.error(`Errors:`); console.error(notifier.errors[0]); } } function stringifyStatus(s) { return s ? 'success' : 'failure'; } class EmptyNotifier { constructor() { this.errors = []; } startCommand(tasks) { return Promise.resolve(); } endCommand(result) { return Promise.resolve(); } endTask(taskId, result, log) { } } class InsightsNotifier { constructor(options, context) { this.options = options; this.context = context; this.errors = []; this.endTaskNotifications = []; this.commandId = this.generateCommandId(); this.axiosInstance = axios.create({ baseURL: options.insightsUrl || 'https://nrwl.api.io', timeout: 30000, headers: { authorization: `auth ${this.envOptions.authToken}` } }); } startCommand(tasks) { const files = this.collectGlobalFiles(); const p = this.makePostRequest('nx-insights-record-start-command', { workspaceId: '---', runContext: '---', branchId: this.envOptions.branchId, runId: this.envOptions.runId, commandId: this.commandId, target: this.context.target, packageJson: files.packageJson, workspaceJson: files.workspaceJson, nxJson: files.nxJson, projectGraph: JSON.stringify(this.context.projectGraph), startTime: new Date().toISOString() }); return p.then(() => tasks.map(t => this.startTask(t))); } endCommand(result) { return Promise.all(this.endTaskNotifications).then(() => { return this.makePostRequest('nx-insights-record-end-command', { commandId: this.commandId, endTime: new Date().toISOString(), result }); }); } endTask(taskId, result, log) { this.endTaskNotifications.push(this.makePostRequest('nx-insights-record-end-task', { taskId, endTime: new Date().toISOString(), result, log })); } get envOptions() { return { branchId: process.env.NX_INSIGHTS_BRANCH_ID, runId: process.env.NX_INSIGHTS_RUN_ID, authToken: process.env.NX_INSIGHTS_AUTH_TOKEN }; } startTask(task) { return this.makePostRequest('nx-insights-record-start-task', { commandId: this.commandId, taskId: task.id, startTime: new Date().toISOString(), target: task.target.target, projectName: task.target.project }); } collectGlobalFiles() { return { nxJson: fs.readFileSync('nx.json').toString(), workspaceJson: fs.readFileSync('workspace.json').toString(), packageJson: fs.readFileSync('package.json').toString() }; } generateCommandId() { return `${this.envOptions.runId}-${this.context.target}-${Math.floor(Math.random() * 100000)}`; } makePostRequest(endpoint, post) { return this.axiosInstance.post(endpoint, post).catch(e => { this.errors.push(e.message); }); } } exports.default = insightsTaskRunner;