/
lighthouse-ci.ts
111 lines (89 loc) · 4.21 KB
/
lighthouse-ci.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import tasklib = require('azure-pipelines-task-lib/task');
import toolrunner = require('azure-pipelines-task-lib/toolrunner');
import * as path from 'path';
import url = require('url');
import * as fs from "fs";
import * as os from "os";
import { throws } from 'assert';
import { LightHouseCIBuildContext } from './lighthouse-ci-build-context';
var uuidV4 = require('uuid/v4');
export class lighthouseCI {
private command: string;
private configFilePath: string;
private parameters: string;
private targetArtifact: string;
private failOnStderr: boolean;
constructor() {
this.command = tasklib.getInput('command');
this.configFilePath = tasklib.filePathSupplied('configFilePath') ?
`--config ${tasklib.getPathInput('configFilePath', false, true)}` : "";
this.parameters = tasklib.getInput('parameters', false) || "";
this.failOnStderr = tasklib.getBoolInput('failOnStderr',false) || false;
this.targetArtifact = tasklib.filePathSupplied('targetArtifactPath') ?
`${tasklib.getPathInput('targetArtifactPath', false, true)}` : "";
}
public async run() {
try {
if (await this.installLightHouse() && await this.setBuildContext()) {
let lighthouse = tasklib.tool('lhci');
lighthouse
.line(`${this.command} ${this.configFilePath} ${this.parameters}`)
.exec(<toolrunner.IExecOptions>{
failOnStdErr: this.failOnStderr,
cwd: path.dirname(tasklib.getPathInput('configFilePath'))
})
.then(() => {
},
(error) => {
tasklib.setResult(tasklib.TaskResult.Failed, error);
}
)
}
}
catch (error) {
tasklib.setResult(tasklib.TaskResult.Failed, error.message);
}
}
private async setBuildContext(): Promise<boolean> {
if (this.command == 'autorun' || this.command == 'upload' || this.command == 'healthcheck') {
tasklib.debug('--------------------------- Setting Up Build Context for LightHouse CI--------------------------------')
let LHCIbuildContext = new LightHouseCIBuildContext(this.targetArtifact);
LHCIbuildContext.setBuildContext();
tasklib.debug('---------------------------Build Context Successfully Set-Up----------------------------- ');
}
return true;
}
private async installLightHouse(): Promise<boolean> {
let lhci:string = tasklib.which('lhci', false);
if (!lhci) {
tasklib.debug('-------------------------Lighthouse CI not found. Installing NPM Package --------------------------------')
let tempDirectory = tasklib.getVariable('agent.tempDirectory');
tasklib.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.sh');
if (os.platform() === "win32") {
fs.writeFileSync(filePath, 'export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true && npm install -g @lhci/cli puppeteer', { encoding: 'utf8' });
}
else {
fs.writeFileSync(filePath, 'sudo PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm install -g @lhci/cli puppeteer', { encoding: 'utf8' });
}
var lighthouseInstallResult = await tasklib.tool(tasklib.which('bash', true))
.arg('--noprofile')
.arg(`--norc`)
.arg(filePath)
.exec();
if (lighthouseInstallResult !== 0) {
throw 'Failed to install Lighthouse CI and Puppeteer.';
}
else {
tasklib.debug('------------------------------------ Successfully Installed Lighthouse CI and Puppeteer -----------------------------');
return true;
}
}
else {
tasklib.debug(`LightHouse CI installation found at ${lhci}.`);
}
return true;
}
}
var exe = new lighthouseCI();
exe.run().catch((reason) => tasklib.setResult(tasklib.TaskResult.Failed, reason));