@jsenv/lighthouse-impact 1.2.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,18 +1,10 @@
1
- # Lighthouse impact
2
-
3
- Report pull request impacts on lighthouse score.
4
-
5
- [![npm package](https://img.shields.io/npm/v/@jsenv/lighthouse-impact.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/lighthouse-impact)
6
- [![github main](https://github.com/jsenv/lighthouse-impact/workflows/main/badge.svg)](https://github.com/jsenv/lighthouse-impact/actions?workflow=main)
7
- [![codecov coverage](https://codecov.io/gh/jsenv/lighthouse-impact/branch/main/graph/badge.svg)](https://codecov.io/gh/jsenv/lighthouse-impact)
8
-
9
- # Presentation
1
+ # Lighthouse impact [![npm package](https://img.shields.io/npm/v/@jsenv/lighthouse-impact.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/lighthouse-impact) [![github main](https://github.com/jsenv/lighthouse-impact/workflows/main/badge.svg)](https://github.com/jsenv/lighthouse-impact/actions?workflow=main) [![codecov coverage](https://codecov.io/gh/jsenv/lighthouse-impact/branch/main/graph/badge.svg)](https://codecov.io/gh/jsenv/lighthouse-impact)
10
2
 
11
3
  `@jsenv/lighthouse-impact` analyses a pull request impact on lighthouse score. This analysis is posted in a comment of the pull request on GitHub.
12
4
 
13
5
  - Helps you to catch lighthouse score impacts before merging a pull request
14
- - Can be added to any workflow like a GitHub workflow
15
6
  - Gives you the ability to get lighthouse report on your machine during dev
7
+ - Can be added to any automated process (GitHub workflow, Jenkins, ...)
16
8
 
17
9
  # Pull request comment
18
10
 
@@ -42,24 +34,23 @@ _generate_lighthouse_report.mjs_
42
34
  */
43
35
  import { createServer } from "node:http"
44
36
  import { readFileSync } from "node:fs"
45
- import { getLighthouseReportUsingHeadlessChrome } from "@jsenv/lighthouse-impact"
37
+ import { generateLighthouseReport } from "@jsenv/lighthouse-impact"
46
38
 
47
39
  const htmlFileUrl = new URL("./index.html", import.meta.url)
48
40
  const html = String(readFileSync(htmlFileUrl))
49
41
 
50
- export const generateLighthouseReport = async () => {
51
- const server = createServer((request, response) => {
52
- response.writeHead(200, {
53
- "content-type": "text/html",
54
- })
55
- response.end(html)
42
+ const server = createServer((request, response) => {
43
+ response.writeHead(200, {
44
+ "content-type": "text/html",
56
45
  })
57
- server.listen(8080)
58
- server.unref()
46
+ response.end(html)
47
+ })
48
+ server.listen(8080)
49
+ server.unref()
59
50
 
60
- const lighthouseReport = await getLighthouseReportUsingHeadlessChrome("http://127.0.0.1:8080")
61
- return lighthouseReport
62
- }
51
+ export const lighthouseReport = await generateLighthouseReport(
52
+ "http://127.0.0.1:8080",
53
+ )
63
54
  ```
64
55
 
65
56
  _index.html_
@@ -78,7 +69,7 @@ _index.html_
78
69
  </html>
79
70
  ```
80
71
 
81
- At this stage, you could generate a lighthouse report on your machine. For an example, see [package.json#L36](./package.json#L36) and [script/lighthouse/generate_lighthouse_report.mjs#L32](./script/lighthouse/generate_lighthouse_report.mjs#L32).
72
+ At this stage, you could generate a lighthouse report on your machine. For an example, see [package.json#L36](./package.json#L36) and [script/lighthouse/generate_lighthouse_report.mjs#L37](./script/lighthouse/generate_lighthouse_report.mjs#L37).
82
73
 
83
74
  Now it's time to configure a workflow to compare lighthouse reports before and after merging a pull request.
84
75
 
@@ -127,11 +118,14 @@ _report_lighthouse_impact.mjs_
127
118
  * See https://github.com/jsenv/lighthouse-impact#how-it-works
128
119
  */
129
120
 
130
- import { reportLighthouseImpact, readGitHubWorkflowEnv } from "@jsenv/lighthouse-impact"
121
+ import {
122
+ reportLighthouseImpact,
123
+ readGitHubWorkflowEnv,
124
+ } from "@jsenv/lighthouse-impact"
131
125
 
132
126
  reportLighthouseImpact({
133
127
  ...readGitHubWorkflowEnv(),
134
- generateLighthouseReportFileRelativeUrl: "./generate_lighthouse_report.mjs",
128
+ lighthouseReportPath: "./generate_lighthouse_report.mjs#lighthouseReport",
135
129
  })
136
130
  ```
137
131
 
@@ -145,8 +139,7 @@ If you want to use an other tool than GitHub worflow to run the pull request com
145
139
 
146
140
  ### 1. Replicate _lighthouse_impact.yml_
147
141
 
148
- Your script must reproduce the state where your git repository has been cloned and you are currently on the pull request branch.
149
- The corresponding commands looks as below:
142
+ Your script must reproduce the state where your git repository has been cloned and you are currently on the pull request branch. Something like the commands below.
150
143
 
151
144
  ```console
152
145
  git init
@@ -174,8 +167,7 @@ reportLighthouseImpact({
174
167
  + repositoryName: process.env.TRAVIS_REPO_SLUG.split("/")[1],
175
168
  + pullRequestNumber: process.env.TRAVIS_PULL_REQUEST,
176
169
  + githubToken: process.env.GITHUB_TOKEN, // see next step
177
-
178
- generateLighthouseReportFileRelativeUrl: "./generate_lighthouse_report.mjs",
170
+ lighthouseReportPath: "./generate_lighthouse_report.mjs#lighthouseReport",
179
171
  })
180
172
  ```
181
173
 
package/main.js CHANGED
@@ -1,5 +1,4 @@
1
- export { readGitHubWorkflowEnv } from "@jsenv/github-pull-request-impact"
1
+ export { generateLighthouseReport } from "./src/generateLighthouseReport.js"
2
2
 
3
- export { getLighthouseReportUsingHeadlessChrome } from "./src/getLighthouseReportUsingHeadlessChrome.js"
4
- export { logLighthouseReport } from "./src/logLighthouseReport.js"
3
+ export { readGitHubWorkflowEnv } from "@jsenv/github-pull-request-impact"
5
4
  export { reportLighthouseImpact } from "./src/reportLighthouseImpact.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/lighthouse-impact",
3
- "version": "1.2.0",
3
+ "version": "2.1.1",
4
4
  "description": "Package description",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/jsenv/lighthouse-impact"
14
14
  },
15
15
  "engines": {
16
- "node": ">=14.17.0"
16
+ "node": ">=16.13.0"
17
17
  },
18
18
  "publishConfig": {
19
19
  "access": "public",
@@ -26,47 +26,43 @@
26
26
  },
27
27
  "./*": "./*"
28
28
  },
29
+ "main": "./main.js",
29
30
  "files": [
30
31
  "/src/",
31
32
  "/main.js"
32
33
  ],
33
34
  "scripts": {
34
- "eslint-check": "node ./node_modules/eslint/bin/eslint.js .",
35
- "generate-importmap": "node ./script/importmap/generate_importmap.mjs",
36
- "measure-performances": "node ./script/performance/generate_performance_report.mjs --local",
37
- "generate-lighthouse-report": "node ./script/lighthouse/generate_lighthouse_report.mjs --local",
35
+ "eslint": "node ./node_modules/eslint/bin/eslint.js . --ext=.js,.mjs",
36
+ "importmap": "node ./script/importmap/importmap.mjs",
37
+ "performance": "node --expose-gc ./script/performance/performance.mjs --log",
38
+ "lighthouse": "node ./script/lighthouse/lighthouse.mjs --local",
38
39
  "generate-comment-snapshot-file": "node ./test/comment/generate_comment_snapshot_file.mjs",
39
40
  "test": "node ./script/test/test.mjs",
40
41
  "test-with-coverage": "npm run test -- --coverage",
41
- "prettier-format": "node ./script/prettier/prettier_format.mjs",
42
- "prettier-format-stage": "npm run prettier-format -- --staged",
43
- "prettier-check": "npm run prettier-format -- --dry-run",
44
- "prepublishOnly": "node ./script/publish/remove_postinstall.mjs",
45
- "postpublish": "node ./script/publish/restore_postinstall.mjs"
42
+ "prettier": "prettier --write ."
46
43
  },
47
44
  "dependencies": {
48
45
  "@jsenv/cancellation": "3.0.0",
46
+ "@jsenv/dynamic-import-worker": "1.0.0",
47
+ "@jsenv/filesystem": "2.3.1",
49
48
  "@jsenv/github-pull-request-impact": "1.6.4",
50
49
  "@jsenv/logger": "4.0.1",
51
- "@jsenv/util": "4.1.1",
52
50
  "chrome-launcher": "0.14.0",
53
51
  "lighthouse": "8.2.0"
54
52
  },
55
53
  "devDependencies": {
56
- "@jsenv/assert": "2.2.6",
57
- "@jsenv/codecov-upload": "3.5.0",
58
- "@jsenv/core": "19.6.2",
59
- "@jsenv/eslint-config": "15.0.2",
54
+ "@jsenv/assert": "2.4.0",
55
+ "@jsenv/core": "24.5.8",
56
+ "@jsenv/eslint-config": "16.0.9",
60
57
  "@jsenv/github-release-package": "1.2.3",
61
- "@jsenv/importmap-eslint-resolver": "5.1.0",
62
- "@jsenv/importmap-node-module": "1.0.1",
58
+ "@jsenv/importmap-eslint-resolver": "5.2.1",
59
+ "@jsenv/importmap-node-module": "2.8.0",
63
60
  "@jsenv/package-publish": "1.6.2",
64
- "@jsenv/performance-impact": "1.6.2",
65
- "@jsenv/prettier-check-project": "5.6.1",
66
- "@jsenv/server": "6.1.1",
67
- "eslint": "7.32.0",
68
- "eslint-plugin-html": "6.1.2",
69
- "eslint-plugin-import": "2.24.0",
70
- "prettier": "2.3.2"
61
+ "@jsenv/performance-impact": "2.2.1",
62
+ "@jsenv/server": "12.2.0",
63
+ "eslint": "8.4.1",
64
+ "eslint-plugin-import": "2.25.3",
65
+ "eslint-plugin-html": "6.2.0",
66
+ "prettier": "2.5.1"
71
67
  }
72
68
  }
@@ -10,23 +10,22 @@ import {
10
10
  createOperation,
11
11
  executeAsyncFunction,
12
12
  } from "@jsenv/cancellation"
13
- import { writeFile, resolveUrl } from "@jsenv/util"
13
+ import {
14
+ writeFile,
15
+ resolveUrl,
16
+ assertAndNormalizeDirectoryUrl,
17
+ } from "@jsenv/filesystem"
14
18
 
15
- const require = createRequire(import.meta.url)
19
+ import { formatLighthouseReportForLog } from "./internal/formatLighthouseReportForLog.js"
16
20
 
17
- const lighthouse = require("lighthouse")
18
- // eslint-disable-next-line import/no-unresolved
19
- const ReportGenerator = require("lighthouse/report/report-generator")
20
- const { computeMedianRun } = require("lighthouse/lighthouse-core/lib/median-run.js")
21
- const chromeLauncher = require("chrome-launcher")
21
+ const require = createRequire(import.meta.url)
22
22
 
23
- export const getLighthouseReportUsingHeadlessChrome = async (
23
+ export const generateLighthouseReport = async (
24
24
  url,
25
25
  {
26
26
  cancellationToken = createCancellationToken(),
27
27
  cancelOnSIGINT = true,
28
28
  logLevel,
29
- projectDirectoryUrl,
30
29
 
31
30
  headless = true,
32
31
  gpu = false,
@@ -38,20 +37,32 @@ export const getLighthouseReportUsingHeadlessChrome = async (
38
37
  runCount = 1,
39
38
  delayBetweenEachRunInSeconds = 1,
40
39
 
41
- jsonFile = Boolean(projectDirectoryUrl),
40
+ projectDirectoryUrl, // required only when jsonFile or htmlFile is passed
41
+ log = false,
42
+ jsonFile = false,
42
43
  jsonFileRelativeUrl = "./lighthouse/lighthouse_report.json",
43
44
  jsonFileLog = true,
44
- htmlFile = Boolean(projectDirectoryUrl),
45
+ htmlFile = false,
45
46
  htmlFileRelativeUrl = "./lighthouse/lighthouse_report.html",
46
47
  htmlFileLog = true,
47
48
  } = {},
48
49
  ) => {
50
+ // eslint-disable-next-line import/no-unresolved
51
+ const ReportGenerator = require("lighthouse/report/report-generator")
52
+ const {
53
+ computeMedianRun,
54
+ } = require("lighthouse/lighthouse-core/lib/median-run.js")
55
+ const chromeLauncher = require("chrome-launcher")
56
+
49
57
  let cleanup = () => {}
50
58
 
51
59
  if (cancelOnSIGINT) {
52
60
  const processCancellationSource = createCancellationSource()
53
61
  const processCancellationToken = processCancellationSource.token
54
- cancellationToken = composeCancellationToken(cancellationToken, processCancellationToken)
62
+ cancellationToken = composeCancellationToken(
63
+ cancellationToken,
64
+ processCancellationToken,
65
+ )
55
66
  const SIGINTCallback = () => {
56
67
  processCancellationSource.cancel("process SIGINT")
57
68
  }
@@ -90,7 +101,9 @@ export const getLighthouseReportUsingHeadlessChrome = async (
90
101
  .reduce(async (previous, _, index) => {
91
102
  await previous
92
103
  if (index > 0 && delayBetweenEachRunInSeconds) {
93
- await new Promise((resolve) => setTimeout(resolve, delayBetweenEachRunInSeconds * 1000))
104
+ await new Promise((resolve) =>
105
+ setTimeout(resolve, delayBetweenEachRunInSeconds * 1000),
106
+ )
94
107
  }
95
108
  const report = await generateOneLighthouseReport(url, {
96
109
  cancellationToken,
@@ -101,13 +114,25 @@ export const getLighthouseReportUsingHeadlessChrome = async (
101
114
  }, Promise.resolve())
102
115
 
103
116
  const lighthouseReport = computeMedianRun(reports)
117
+
118
+ if (log) {
119
+ logger.info(formatLighthouseReportForLog(lighthouseReport))
120
+ }
121
+
104
122
  await chrome.kill()
105
123
 
124
+ if (jsonFile || htmlFile) {
125
+ projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
126
+ }
127
+
106
128
  const promises = []
107
129
  if (jsonFile) {
108
130
  promises.push(
109
131
  (async () => {
110
- const jsonFileUrl = resolveUrl(jsonFileRelativeUrl, projectDirectoryUrl)
132
+ const jsonFileUrl = resolveUrl(
133
+ jsonFileRelativeUrl,
134
+ projectDirectoryUrl,
135
+ )
111
136
  const json = JSON.stringify(lighthouseReport, null, " ")
112
137
  await writeFile(jsonFileUrl, json)
113
138
  if (jsonFileLog) {
@@ -119,7 +144,10 @@ export const getLighthouseReportUsingHeadlessChrome = async (
119
144
  if (htmlFile) {
120
145
  promises.push(
121
146
  (async () => {
122
- const htmlFileUrl = resolveUrl(htmlFileRelativeUrl, projectDirectoryUrl)
147
+ const htmlFileUrl = resolveUrl(
148
+ htmlFileRelativeUrl,
149
+ projectDirectoryUrl,
150
+ )
123
151
  const html = ReportGenerator.generateReportHtml(lighthouseReport)
124
152
  await writeFile(htmlFileUrl, html)
125
153
  if (htmlFileLog) {
@@ -152,6 +180,7 @@ const generateOneLighthouseReport = async (
152
180
  url,
153
181
  { cancellationToken, lighthouseOptions, config },
154
182
  ) => {
183
+ const lighthouse = require("lighthouse")
155
184
  const results = await createOperation({
156
185
  cancellationToken,
157
186
  start: () => lighthouse(url, lighthouseOptions, config),
@@ -38,8 +38,12 @@ export const createLighthouseImpactComment = ({
38
38
  }
39
39
 
40
40
  const bodyLines = [
41
- ...(beforeMergeGist ? [`<!-- before_merge_gist_id=${beforeMergeGist.id} -->`] : []),
42
- ...(afterMergeGist ? [`<!-- after_merge_gist_id=${afterMergeGist.id} -->`] : []),
41
+ ...(beforeMergeGist
42
+ ? [`<!-- before_merge_gist_id=${beforeMergeGist.id} -->`]
43
+ : []),
44
+ ...(afterMergeGist
45
+ ? [`<!-- after_merge_gist_id=${afterMergeGist.id} -->`]
46
+ : []),
43
47
  `<h4>Lighthouse impact</h4>`,
44
48
  ...(impactAnalysisEnabled
45
49
  ? [
@@ -76,19 +80,24 @@ const renderBody = ({
76
80
  pullRequestBase,
77
81
  pullRequestHead,
78
82
  }) => {
79
- return Object.keys(afterMergeLighthouseReport.categories).map((categoryName) => {
80
- return renderCategory(categoryName, {
81
- beforeMergeLighthouseReport,
82
- afterMergeLighthouseReport,
83
- pullRequestBase,
84
- pullRequestHead,
85
- })
86
- }).join(`
83
+ return Object.keys(afterMergeLighthouseReport.categories).map(
84
+ (categoryName) => {
85
+ return renderCategory(categoryName, {
86
+ beforeMergeLighthouseReport,
87
+ afterMergeLighthouseReport,
88
+ pullRequestBase,
89
+ pullRequestHead,
90
+ })
91
+ },
92
+ ).join(`
87
93
 
88
94
  `)
89
95
  }
90
96
 
91
- const renderCategory = (category, { beforeMergeLighthouseReport, afterMergeLighthouseReport }) => {
97
+ const renderCategory = (
98
+ category,
99
+ { beforeMergeLighthouseReport, afterMergeLighthouseReport },
100
+ ) => {
92
101
  const beforeMergeDisplayedScore = scoreToDisplayedScore(
93
102
  beforeMergeLighthouseReport.categories[category].score,
94
103
  )
@@ -114,7 +123,8 @@ const renderCategory = (category, { beforeMergeLighthouseReport, afterMergeLight
114
123
  </details>`
115
124
  }
116
125
 
117
- const scoreToDisplayedScore = (floatingNumber) => Math.round(floatingNumber * 100)
126
+ const scoreToDisplayedScore = (floatingNumber) =>
127
+ Math.round(floatingNumber * 100)
118
128
 
119
129
  const renderCategoryAudits = (
120
130
  category,
@@ -156,7 +166,10 @@ const renderCategoryAudits = (
156
166
  return
157
167
  }
158
168
 
159
- if (typeof beforeMergeAuditOutput === "number" && typeof afterMergeAuditOutput === "number") {
169
+ if (
170
+ typeof beforeMergeAuditOutput === "number" &&
171
+ typeof afterMergeAuditOutput === "number"
172
+ ) {
160
173
  const diff = afterMergeAuditOutput - beforeMergeAuditOutput
161
174
 
162
175
  audits.push([
@@ -170,7 +183,9 @@ const renderCategoryAudits = (
170
183
 
171
184
  audits.push([
172
185
  `<td nowrap>${auditId}</td>`,
173
- `<td nowrap>${beforeMergeAuditOutput === afterMergeAuditOutput ? "none" : "---"}</td>`,
186
+ `<td nowrap>${
187
+ beforeMergeAuditOutput === afterMergeAuditOutput ? "none" : "---"
188
+ }</td>`,
174
189
  `<td nowrap>${beforeMergeAuditOutput}</td>`,
175
190
  `<td nowrap>${afterMergeAuditOutput}</td>`,
176
191
  ])
@@ -237,7 +252,11 @@ const renderAudit = (audit) => {
237
252
  return null
238
253
  }
239
254
 
240
- const renderGistLinks = ({ beforeMergeGist, afterMergeGist, pullRequestBase }) => {
255
+ const renderGistLinks = ({
256
+ beforeMergeGist,
257
+ afterMergeGist,
258
+ pullRequestBase,
259
+ }) => {
241
260
  return `<sub>
242
261
  Impact analyzed comparing <a href="${gistIdToReportUrl(
243
262
  beforeMergeGist.id,
@@ -1,7 +1,7 @@
1
- export const logLighthouseReport = (lighthouseReport) => {
1
+ export const formatLighthouseReportForLog = (lighthouseReport) => {
2
2
  const scores = {}
3
3
  Object.keys(lighthouseReport.categories).forEach((name) => {
4
4
  scores[name] = lighthouseReport.categories[name].score
5
5
  })
6
- console.log(JSON.stringify(scores, null, " "))
6
+ return JSON.stringify(scores, null, " ")
7
7
  }
@@ -1,6 +1,10 @@
1
1
  /* eslint-disable new-cap */
2
2
  import { createDetailedMessage } from "@jsenv/logger"
3
- import { GET, POST, PATCH } from "@jsenv/github-pull-request-impact/src/internal/git_hub_api.js"
3
+ import {
4
+ GET,
5
+ POST,
6
+ PATCH,
7
+ } from "@jsenv/github-pull-request-impact/src/internal/git_hub_api.js"
4
8
 
5
9
  // https://developer.github.com/v3/gists/#create-a-gist
6
10
 
@@ -27,8 +31,10 @@ export const patchOrPostGists = async ({
27
31
  afterMergeGistId = gistIds.afterMergeGistId
28
32
  logger.debug(
29
33
  createDetailedMessage(`gists found in comment body`, {
30
- "before merging gist with lighthouse report": gistIdToUrl(beforeMergeGistId),
31
- "after merging gist with lighthouse report": gistIdToUrl(afterMergeGistId),
34
+ "before merging gist with lighthouse report":
35
+ gistIdToUrl(beforeMergeGistId),
36
+ "after merging gist with lighthouse report":
37
+ gistIdToUrl(afterMergeGistId),
32
38
  }),
33
39
  )
34
40
  } else {
@@ -39,10 +45,16 @@ export const patchOrPostGists = async ({
39
45
  logger.debug(`update or create both gists.`)
40
46
  let [beforeMergeGist, afterMergeGist] = await Promise.all([
41
47
  beforeMergeGistId
42
- ? GET(`https://api.github.com/gists/${beforeMergeGistId}`, { cancellationToken, githubToken })
48
+ ? GET(`https://api.github.com/gists/${beforeMergeGistId}`, {
49
+ cancellationToken,
50
+ githubToken,
51
+ })
43
52
  : null,
44
53
  afterMergeGistId
45
- ? GET(`https://api.github.com/gists/${afterMergeGistId}`, { cancellationToken, githubToken })
54
+ ? GET(`https://api.github.com/gists/${afterMergeGistId}`, {
55
+ cancellationToken,
56
+ githubToken,
57
+ })
46
58
  : null,
47
59
  ])
48
60
 
@@ -65,10 +77,14 @@ export const patchOrPostGists = async ({
65
77
  logger.info(`base gist updated`)
66
78
  } else {
67
79
  logger.info(`creating base gist`)
68
- beforeMergeGist = await POST(`https://api.github.com/gists`, beforeMergeGistBody, {
69
- cancellationToken,
70
- githubToken,
71
- })
80
+ beforeMergeGist = await POST(
81
+ `https://api.github.com/gists`,
82
+ beforeMergeGistBody,
83
+ {
84
+ cancellationToken,
85
+ githubToken,
86
+ },
87
+ )
72
88
  logger.info(`base gist created at ${gistIdToUrl(beforeMergeGist.id)}`)
73
89
  }
74
90
 
@@ -79,7 +95,9 @@ export const patchOrPostGists = async ({
79
95
  beforeMerge: false,
80
96
  })
81
97
  if (afterMergeGist) {
82
- logger.info(`updating after merge gist at ${gistIdToUrl(afterMergeGist.id)}`)
98
+ logger.info(
99
+ `updating after merge gist at ${gistIdToUrl(afterMergeGist.id)}`,
100
+ )
83
101
  afterMergeGist = await PATCH(
84
102
  `https://api.github.com/gists/${afterMergeGist.id}`,
85
103
  afterMergeGistBody,
@@ -91,10 +109,14 @@ export const patchOrPostGists = async ({
91
109
  logger.info(`after merge gist updated`)
92
110
  } else {
93
111
  logger.info(`creating after merge gist`)
94
- afterMergeGist = await POST(`https://api.github.com/gists`, afterMergeGistBody, {
95
- cancellationToken,
96
- githubToken,
97
- })
112
+ afterMergeGist = await POST(
113
+ `https://api.github.com/gists`,
114
+ afterMergeGistBody,
115
+ {
116
+ cancellationToken,
117
+ githubToken,
118
+ },
119
+ )
98
120
  logger.info(`after merge gist created at ${gistIdToUrl(afterMergeGist.id)}`)
99
121
  }
100
122
 
@@ -120,8 +142,12 @@ const createGistBody = (
120
142
  }
121
143
  }
122
144
 
123
- const beforeMergeGistIdRegex = new RegExp("<!-- before_merge_gist_id=([a-zA-Z0-9_]+) -->")
124
- const afterMergeGistIdRegex = new RegExp("<!-- after_merge_gist_id=([a-zA-Z0-9_]+) -->")
145
+ const beforeMergeGistIdRegex = new RegExp(
146
+ "<!-- before_merge_gist_id=([a-zA-Z0-9_]+) -->",
147
+ )
148
+ const afterMergeGistIdRegex = new RegExp(
149
+ "<!-- after_merge_gist_id=([a-zA-Z0-9_]+) -->",
150
+ )
125
151
 
126
152
  const gistIdsFromComment = (comment) => {
127
153
  const beforeMergeGistIdMatch = comment.body.match(beforeMergeGistIdRegex)
@@ -1,5 +1,6 @@
1
1
  import { commentGitHubPullRequestImpact } from "@jsenv/github-pull-request-impact"
2
- import { assertAndNormalizeDirectoryUrl, resolveUrl, readFileSystemNodeStat } from "@jsenv/util"
2
+ import { assertAndNormalizeDirectoryUrl, resolveUrl } from "@jsenv/filesystem"
3
+ import { importOneExportFromFile } from "@jsenv/dynamic-import-worker"
3
4
 
4
5
  import { patchOrPostGists } from "./internal/patchOrPostGists.js"
5
6
  import { createLighthouseImpactComment } from "./internal/createLighthouseImpactComment.js"
@@ -16,7 +17,7 @@ export const reportLighthouseImpact = async ({
16
17
  repositoryName,
17
18
  pullRequestNumber,
18
19
  installCommand = "npm install",
19
- moduleGeneratinLighthouseReportRelativeUrl, // rename this
20
+ lighthouseReportPath,
20
21
 
21
22
  runLink,
22
23
  commitInGeneratedByInfo,
@@ -24,14 +25,14 @@ export const reportLighthouseImpact = async ({
24
25
  skipGistWarning = false,
25
26
  }) => {
26
27
  projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
27
- if (typeof moduleGeneratinLighthouseReportRelativeUrl !== "string") {
28
+ if (typeof lighthouseReportPath !== "string") {
28
29
  throw new TypeError(
29
- `moduleGeneratinLighthouseReportRelativeUrl must be a string but received ${moduleGeneratinLighthouseReportRelativeUrl}`,
30
+ `lighthouseReportPath must be a string but received ${lighthouseReportPath}`,
30
31
  )
31
32
  }
32
33
  projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
33
- const moduleGeneratingLighthouseReportUrl = resolveUrl(
34
- moduleGeneratinLighthouseReportRelativeUrl,
34
+ const lighthouseReportUrl = resolveUrl(
35
+ lighthouseReportPath,
35
36
  projectDirectoryUrl,
36
37
  )
37
38
 
@@ -51,19 +52,9 @@ export const reportLighthouseImpact = async ({
51
52
 
52
53
  collectInfo: async ({ execCommandInProjectDirectory }) => {
53
54
  await execCommandInProjectDirectory(installCommand)
54
- await assertGenerateLighthouseReportFileExists(moduleGeneratingLighthouseReportUrl)
55
-
56
- const { generateLighthouseReport } = await import(
57
- `${moduleGeneratingLighthouseReportUrl}?cache_busting=${Date.now()}`
55
+ const lighthouseReport = await importOneExportFromFile(
56
+ lighthouseReportUrl,
58
57
  )
59
- if (typeof generateLighthouseReport !== "function") {
60
- throw new TypeError(
61
- `generateLighthouseReport export must be a function, got ${generateLighthouseReport}`,
62
- )
63
- }
64
-
65
- const lighthouseReport = await generateLighthouseReport()
66
-
67
58
  return { version: 1, data: lighthouseReport }
68
59
  },
69
60
  commentIdentifier: `<!-- Generated by @jsenv/lighthouse-impact -->`,
@@ -132,15 +123,3 @@ export const reportLighthouseImpact = async ({
132
123
  catchError,
133
124
  })
134
125
  }
135
-
136
- const assertGenerateLighthouseReportFileExists = async (fileUrl) => {
137
- const fileStat = await readFileSystemNodeStat(fileUrl, {
138
- nullIfNotFound: true,
139
- })
140
-
141
- if (!fileStat) {
142
- throw new Error(
143
- `Cannot find the file responsible to export generateLighthouseReport at ${fileUrl}`,
144
- )
145
- }
146
- }