Skip to content

Commit bd80cc9

Browse files
committed
feat(core): try to infer system code for metrics attribute
1 parent c9d0453 commit bd80cc9

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

core/cli/src/install.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { findConflicts, withoutConflicts } from '@dotcom-tool-kit/conflict'
1313
import { formatUninstalledHooks } from './messages'
1414
import { importEntryPoint } from './plugin/entry-point'
1515
import { runInit } from './init'
16+
import { guessSystemCode } from './systemCode'
1617
import { TelemetryRecorder } from '@dotcom-tool-kit/telemetry'
1718

1819
// implementation of the Array#every method that supports asynchronous predicates
@@ -131,8 +132,14 @@ export default async function installHooks(logger: Logger, metrics: TelemetryRec
131132

132133
await runInit(logger, config)
133134

135+
const systemCode = await guessSystemCode(config)
136+
let scoped = metrics
137+
if (systemCode) {
138+
scoped = metrics.scoped({ systemCode })
139+
}
140+
134141
const errors: Error[] = []
135-
const hooks = (await loadHookInstallations(logger, metrics, config)).unwrap(
142+
const hooks = (await loadHookInstallations(logger, scoped, config)).unwrap(
136143
'hooks were found to be invalid when installing'
137144
)
138145

core/cli/src/systemCode.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { readFile } from 'node:fs/promises'
2+
3+
import type { ValidConfig } from '@dotcom-tool-kit/config'
4+
import type { RootOptions } from '@dotcom-tool-kit/plugin/src/root-schema'
5+
import type DopplerSchema from '@dotcom-tool-kit/doppler/src/schema'
6+
7+
import type * as z from 'zod'
8+
9+
export async function guessSystemCode(config: ValidConfig): Promise<string | undefined> {
10+
const systemCodeFromRoot = (config.pluginOptions['app root']?.options as RootOptions).systemCode
11+
if (systemCodeFromRoot) {
12+
return systemCodeFromRoot
13+
}
14+
15+
const systemCodeFromDoppler = (
16+
config.pluginOptions['@dotcom-tool-kit/doppler']?.options as z.infer<typeof DopplerSchema>
17+
).project
18+
if (systemCodeFromDoppler && !systemCodeFromDoppler.startsWith('repo_')) {
19+
return systemCodeFromDoppler
20+
}
21+
22+
try {
23+
const packageJson = JSON.parse(await readFile('package.json', 'utf8'))
24+
if (packageJson.name) {
25+
return `npm:${packageJson.name}`
26+
}
27+
} catch {}
28+
}

core/cli/src/tasks.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { styles } from '@dotcom-tool-kit/logger'
1010
import { shouldDisableNativeFetch } from './fetch'
1111
import { runInit } from './init'
1212
import { formatInvalidOption } from './messages'
13+
import { guessSystemCode } from './systemCode'
1314
import { type TaskOptions, TaskSchemas } from '@dotcom-tool-kit/schemas'
1415
import { OptionsForTask } from '@dotcom-tool-kit/plugin'
1516
import type { RootOptions } from '@dotcom-tool-kit/plugin/src/root-schema'
@@ -168,5 +169,11 @@ export async function runCommands(
168169
): Promise<void> {
169170
const config = await loadConfig(logger, { root: process.cwd() })
170171

171-
return runCommandsFromConfig(logger, config, commands, files, metrics)
172+
const systemCode = await guessSystemCode(config)
173+
let scoped = metrics
174+
if (systemCode) {
175+
scoped = metrics.scoped({ systemCode })
176+
}
177+
178+
return runCommandsFromConfig(logger, config, commands, files, scoped)
172179
}

0 commit comments

Comments
 (0)