Skip to content

Commit ca66176

Browse files
feat: add header to installing extension with the build mode so we know where the installation is coming from (#6478)
* feat: add header to installing extension with the build mode so we know where the installation is coming from * fix: use useragent for extensioninstallsource * fix: log netlify config and version * fix: expose package json as file for netlify config * fix: use other way to get package version for config * fix: use readfile to read package.json --------- Co-authored-by: Karin <=> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 536630e commit ca66176

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/build/src/utils/json.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { readFile } from 'fs/promises'
22
import { fileURLToPath } from 'url'
33

44
import type { PackageJson } from 'read-package-up'
5-
65
// We know how our package.json looks like, so we can be very specific with the type
76
// and only add the properties we want to use
87
export type RootPackageJson = { name: string; version: string }
@@ -14,7 +13,7 @@ const ROOT_PACKAGE_JSON_PATH = fileURLToPath(new URL('../../package.json', impor
1413
export const importJsonFile = async function (filePath: string): Promise<PackageJson> {
1514
const fileContents = await readFile(filePath, 'utf-8')
1615

17-
return JSON.parse(fileContents)
16+
return JSON.parse(fileContents) as PackageJson
1817
}
1918

2019
export const ROOT_PACKAGE_JSON = (await importJsonFile(ROOT_PACKAGE_JSON_PATH)) as RootPackageJson

packages/config/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "23.0.11",
44
"description": "Netlify config module",
55
"type": "module",
6-
"exports": "./lib/index.js",
6+
"exports": {
7+
".": "./lib/index.js"
8+
},
79
"main": "./lib/index.js",
810
"types": "./lib/index.d.ts",
911
"bin": {
@@ -77,6 +79,7 @@
7779
"omit.js": "^2.0.2",
7880
"p-locate": "^6.0.0",
7981
"path-type": "^6.0.0",
82+
"read-package-up": "^11.0.0",
8083
"tomlify-j0.4": "^3.0.0",
8184
"validate-npm-package-name": "^5.0.0",
8285
"yaml": "^2.8.0",

packages/config/src/utils/extensions/auto-install-extensions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export async function handleAutoInstallExtensions({
110110
netlifyToken: token,
111111
slug: ext.slug,
112112
hostSiteUrl: ext.hostSiteUrl,
113+
extensionInstallationSource: mode,
113114
})
114115
}),
115116
)

packages/config/src/utils/extensions/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { EXTENSION_API_BASE_URL } from '../../integrations.js'
2+
import { ModeOption } from '../../types/options.js'
3+
import { ROOT_PACKAGE_JSON } from '../json.js'
24

5+
const configVersion = ROOT_PACKAGE_JSON.version
36
export type InstallExtensionResult =
47
| {
58
slug: string
@@ -18,12 +21,15 @@ export const installExtension = async ({
1821
accountId,
1922
slug,
2023
hostSiteUrl,
24+
extensionInstallationSource,
2125
}: {
2226
netlifyToken: string
2327
accountId: string
2428
slug: string
2529
hostSiteUrl: string
30+
extensionInstallationSource: ModeOption
2631
}): Promise<InstallExtensionResult> => {
32+
const userAgent = `Netlify Config (mode:${extensionInstallationSource}) / ${configVersion}`
2733
const extensionOnInstallUrl = new URL('/.netlify/functions/handler/on-install', hostSiteUrl)
2834
const installedResponse = await fetch(extensionOnInstallUrl, {
2935
method: 'POST',
@@ -32,6 +38,7 @@ export const installExtension = async ({
3238
}),
3339
headers: {
3440
'netlify-token': netlifyToken,
41+
'User-Agent': userAgent,
3542
},
3643
})
3744

packages/config/src/utils/json.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { readFile } from 'fs/promises'
2+
import { fileURLToPath } from 'url'
3+
4+
import type { PackageJson } from 'read-package-up'
5+
// We know how our package.json looks like, so we can be very specific with the type
6+
// and only add the properties we want to use
7+
export type RootPackageJson = { name: string; version: string }
8+
9+
const ROOT_PACKAGE_JSON_PATH = fileURLToPath(new URL('../../package.json', import.meta.url))
10+
11+
// TODO: Replace with dynamic `import()` once it is supported without
12+
// experimental flags
13+
export const importJsonFile = async function (filePath: string): Promise<PackageJson> {
14+
const fileContents = await readFile(filePath, 'utf-8')
15+
16+
return JSON.parse(fileContents) as PackageJson
17+
}
18+
19+
export const ROOT_PACKAGE_JSON = (await importJsonFile(ROOT_PACKAGE_JSON_PATH)) as RootPackageJson

0 commit comments

Comments
 (0)