Skip to content

Commit 7f4a2fa

Browse files
jobalaLukas Holzer
andauthored
fix: separate detectBuildsystems and listFrameworks (#4766)
* chore: get buildSystems separately * fix: get build system in different try...catch * chore: gracefully handle invalid package.json * Update packages/build-info/tests/detect-build-systems.test.ts Co-authored-by: Lukas Holzer <[email protected]> * chore: initialize empty package.json Co-authored-by: Lukas Holzer <[email protected]> * chore: fix formatting error * chore: return an empty list if no build system detected * chore: wrap build system detection in a try...catch Co-authored-by: Lukas Holzer <[email protected]>
1 parent 77f8833 commit 7f4a2fa

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

packages/build-info/src/detect-build-system.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,11 @@ const lookFor = (
169169
}
170170

171171
const getPkgJson = (configPath: string): PackageJson => {
172-
return JSON.parse(readFileSync(path.join(path.dirname(configPath), 'package.json'), 'utf-8'))
172+
let pkgJson: PackageJson = {}
173+
try {
174+
pkgJson = JSON.parse(readFileSync(path.join(path.dirname(configPath), 'package.json'), 'utf-8'))
175+
} catch {
176+
// noop
177+
}
178+
return pkgJson
173179
}

packages/build-info/src/get-build-info.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ export type Info = {
1515
export const getBuildInfo = async (opts: ContextOptions) => {
1616
const context = await getContext(opts)
1717
let frameworks: any[] = []
18-
let buildSystems: BuildSystem[] = []
1918

2019
try {
21-
// if the framework or buildSystem detection is crashing we should not crash the build info and package-manager
20+
// if the framework detection is crashing we should not crash the build info and package-manager
2221
// detection
2322
frameworks = await listFrameworks({ projectDir: context.projectDir })
24-
buildSystems = await detectBuildSystems(context.projectDir, context.rootDir)
2523
} catch {
2624
// TODO: build reporting to buildbot see: https://github.com/netlify/pillar-workflow/issues/1001
2725
// noop
2826
}
2927

30-
const info: Info = { frameworks, buildSystems }
28+
const info: Info = { frameworks }
29+
30+
try {
31+
info.buildSystems = await detectBuildSystems(context.projectDir, context.rootDir)
32+
} catch (error) {
33+
// noop
34+
}
3135

3236
// only if we find a root package.json we know this is a javascript workspace
3337
if (Object.keys(context.rootPackageJson).length > 0) {

packages/build-info/tests/detect-build-systems.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,13 @@ test('detects multiple build systems in a monorepo setup', async () => {
125125
const buildSystems = await detectBuildSystems(path.join(cwd, 'packages/website'), cwd)
126126
expect(buildSystems).toEqual([{ name: 'lerna', version: '^2.5.3' }, { name: 'gradle' }])
127127
})
128+
129+
test('invalid package json handled gracefully', async () => {
130+
const cwd = mockFileSystem({
131+
'package.json': "{ 'devDependencies': { moon: '^0.5.1' } }",
132+
'.moon/toolchain.yml': '',
133+
})
134+
135+
const buildSystems = await detectBuildSystems(cwd)
136+
expect(buildSystems[0]).toEqual({ name: 'moon', version: undefined })
137+
})

0 commit comments

Comments
 (0)