Skip to content

Commit

Permalink
feat: Integrate framework detection in build info (#4876)
Browse files Browse the repository at this point in the history
* feat: integrate framework detection inside build-info

* chore: add rest of package managers + logos

* chore: fix gatsby5 + order frameworks correctly and make workspace detection working

* chore: updates

* chore: updates

* chore: update

* chore: updates

* chore: sort detection based on accuracy and type

* chore: drop less accurate detection results

* chore: get all possible commands for nx

* chore: add nxignore

* chore: updates

* chore: remove logos as it's probably good to have them in a follow up PR

* chore: windows path fixes

* chore: full compatibility

* chore: fix e2e

* chore: update

* chore: update

* chore: pr feedback from daniel

* chore: update

* chore: updates

* chore: fix windows tests
  • Loading branch information
Lukas Holzer authored Mar 28, 2023
1 parent 7081ca9 commit 0df38c0
Show file tree
Hide file tree
Showing 117 changed files with 4,121 additions and 305 deletions.
2 changes: 2 additions & 0 deletions .nxignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this contains fixtures like an nx workspace that would infer with the root one.
packages/build-info/tests
6 changes: 2 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"lerna": "^5.5.2",
"lint-staged": "^13.0.3",
"nx": "^14.7.13",
"prettier": "^2.7.1"
"prettier": "^2.8.4"
},
"engines": {
"node": "^14.16.0 || >=16.0.0"
Expand Down
12 changes: 12 additions & 0 deletions packages/build-info/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ node_modules/
/test-results/
/playwright-report/
/playwright/.cache/

tsconfig*.tsbuildinfo

dist

# Local Netlify folder
.netlify

# These are mock node_modules folder for testing purposes
!tests/fixtures/simple/node_modules
!tests/fixtures/multiple/node_modules
!tests/fixtures/monorepos/node_modules
2 changes: 1 addition & 1 deletion packages/build-info/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

// This is a workaround for npm issue: https://github.com/npm/cli/issues/2632

import './lib/node/bin.js'
import './lib/src/node/bin.js'
16 changes: 11 additions & 5 deletions packages/build-info/e2e/browser-compatibility.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { test, expect } from '@playwright/test'

import type { FileSystem } from '../src/file-system'
import type { detectPackageManager } from '../src/package-managers/detect-package-manager'
import type { Project } from '../src/project'
import type { detectWorkspaces } from '../src/workspaces/detect-workspace'
import type { FileSystem } from '../src/file-system.js'
import type { detectPackageManager } from '../src/package-managers/detect-package-manager.js'
import type { Project } from '../src/project.js'
import type { detectWorkspaces } from '../src/workspaces/detect-workspace.js'

declare const window: Window & {
fs: FileSystem
Expand Down Expand Up @@ -59,7 +59,13 @@ test.beforeEach(async ({ page }) => {
})

test('Should detect nx on the root', async ({ page }) => {
expect(await page.evaluate(() => new window.project(window.fs, '/').detectBuildSystem())).toMatchObject([
await page.pause()
expect(
await page.evaluate(async () => {
console.log('here', window)
return new window.project(window.fs, '/').detectBuildSystem()
}),
).toMatchObject([
{
id: 'nx',
name: 'Nx',
Expand Down
8 changes: 8 additions & 0 deletions packages/build-info/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Build Info</title>
</head>
<body></body>
</html>
13 changes: 7 additions & 6 deletions packages/build-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "Build info utility",
"type": "module",
"exports": {
".": "./lib/index.js"
".": "./lib/src/index.js",
"./node": "./lib/src/node/index.js"
},
"browser": "./lib/index.js",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"browser": "./lib/src/index.js",
"main": "./lib/src/index.js",
"types": "./lib/src/index.d.ts",
"bin": {
"build-info": "./bin.js"
},
Expand All @@ -18,7 +19,7 @@
],
"scripts": {
"prebuild": "rm -rf lib",
"build": "tsc",
"build": "tsc --project tsconfig.build.json",
"e2e": "playwright test",
"test": "vitest run",
"test:dev": "vitest --ui",
Expand All @@ -41,6 +42,7 @@
"find-up": "^6.3.0",
"minimatch": "^6.2.0",
"read-pkg": "^7.1.0",
"semver": "^7.3.8",
"yaml": "^2.1.3",
"yargs": "^17.6.0"
},
Expand All @@ -54,7 +56,6 @@
"execa": "^6.0.0",
"memfs": "^3.4.7",
"node-fetch": "^3.3.0",
"semver": "^7.3.8",
"typescript": "^4.8.4",
"unionfs": "^4.4.0",
"vite": "^4.1.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/build-info/src/browser/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DirType, FileSystem } from '../file-system.js'
import { DirType, Environment, FileSystem } from '../file-system.js'

/** A sample implementation of a GitHub provider */
export class GithubProvider {
Expand Down Expand Up @@ -40,6 +40,10 @@ export class WebFS extends FileSystem {
super()
}

getEnvironment() {
return Environment.Browser
}

isAbsolute(path: string): boolean {
return path.startsWith('/')
}
Expand Down
9 changes: 5 additions & 4 deletions packages/build-info/src/build-systems/bazel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Project } from '../project.js'

import { BaseBuildTool } from './build-system.js'

export class Bazel extends BaseBuildTool {
id = 'bazel'
name = 'Bazel'
configFiles = ['.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'BUILD.bazel']

async detect(project: Project) {
const config = await project.fs.findUp(this.configFiles, { cwd: project.baseDirectory, stopAt: project.root })
async detect() {
const config = await this.project.fs.findUp(this.configFiles, {
cwd: this.project.baseDirectory,
stopAt: this.project.root,
})

if (config) {
return this
Expand Down
9 changes: 5 additions & 4 deletions packages/build-info/src/build-systems/buck.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Project } from '../project.js'

import { BaseBuildTool } from './build-system.js'

export class Buck extends BaseBuildTool {
id = 'buck'
name = 'Buck'
configFiles = ['.buckconfig', 'BUCK']

async detect(project: Project) {
const config = await project.fs.findUp(this.configFiles, { cwd: project.baseDirectory, stopAt: project.root })
async detect() {
const config = await this.project.fs.findUp(this.configFiles, {
cwd: this.project.baseDirectory,
stopAt: this.project.root,
})

if (config) {
return this
Expand Down
2 changes: 1 addition & 1 deletion packages/build-info/src/build-systems/build-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ test('invalid package json handled gracefully', async ({ fs }) => {
const detected = await new Project(fs, join(cwd, 'packages/website')).detectBuildSystem()
expect(detected[0].name).toBe('MoonRepo')
expect(detected[0].version).toBeUndefined()
expect(logSpy).toHaveBeenCalledOnce()
expect(logSpy).toHaveBeenCalled()
})
41 changes: 35 additions & 6 deletions packages/build-info/src/build-systems/build-system.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
import { PollingStrategy } from '../frameworks/framework.js'
import { Project } from '../project.js'

export type Command = {
type: 'build' | 'dev' | 'unknown'
command: string
}

export interface BuildSystem {
id: string
name: string
project: Project
version?: string

detect(project: Project): Promise<BuildSystem | undefined>
build?: {
command: string
directory?: string
}

dev?: {
command: string
port?: number
pollingStrategies?: PollingStrategy[]
}

getCommands?(path: string): Promise<Command[]>

detect(): Promise<BuildSystem | undefined>
}

export abstract class BaseBuildTool implements BuildSystem {
export abstract class BaseBuildTool {
id: string
name: string
version?: string
configFiles: string[] = []

async detect(project: Project) {
const config = await project.fs.findUp(this.configFiles, { cwd: project.baseDirectory, stopAt: project.root })
constructor(public project: Project) {}

async detect(): Promise<this | undefined> {
const config = await this.project.fs.findUp(this.configFiles, {
cwd: this.project.baseDirectory,
stopAt: this.project.root,
})

if (config) {
const pkgJson = await project.getPackageJSON(project.fs.dirname(config))
const pkgJson = await this.project.getPackageJSON(this.project.fs.dirname(config))
this.version = pkgJson.devDependencies?.[this.id]
return this
}
}

/** Gets a JSON from the class information */
toJSON() {
return { id: this.id, name: this.name, version: this.version }
return {
id: this.id,
name: this.name,
version: this.version,
}
}
}
9 changes: 5 additions & 4 deletions packages/build-info/src/build-systems/gradle.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Project } from '../project.js'

import { BaseBuildTool } from './build-system.js'

export class Gradle extends BaseBuildTool {
id = 'gradle'
name = 'Gradle'
configFiles = ['build.gradle']

async detect(project: Project) {
const config = await project.fs.findUp(this.configFiles, { cwd: project.baseDirectory, stopAt: project.root })
async detect() {
const config = await this.project.fs.findUp(this.configFiles, {
cwd: this.project.baseDirectory,
stopAt: this.project.root,
})

if (config) {
return this
Expand Down
2 changes: 1 addition & 1 deletion packages/build-info/src/build-systems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ import { Pants } from './pants.js'
import { Rush } from './rush.js'
import { Turbo } from './turbo.js'

export const buildSystems = [Bazel, Buck, Gradle, Lage, Lerna, Moon, Nix, Nx, Pants, Rush, Turbo]
export const buildSystems = [Bazel, Buck, Gradle, Lage, Lerna, Moon, Nix, Nx, Pants, Rush, Turbo] as const
12 changes: 5 additions & 7 deletions packages/build-info/src/build-systems/moon.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { Project } from '../project.js'

import { BaseBuildTool } from './build-system.js'

export class Moon extends BaseBuildTool {
id = 'moon'
name = 'MoonRepo'
configFiles = ['.moon']

async detect(project: Project) {
const config = await project.fs.findUp(this.configFiles, {
cwd: project.baseDirectory,
async detect() {
const config = await this.project.fs.findUp(this.configFiles, {
cwd: this.project.baseDirectory,
type: 'directory',
stopAt: project.root,
stopAt: this.project.root,
})

if (config) {
const pkgJson = await project.getPackageJSON(config)
const pkgJson = await this.project.getPackageJSON(config)
this.version = pkgJson.devDependencies?.[this.id]
return this
}
Expand Down
9 changes: 5 additions & 4 deletions packages/build-info/src/build-systems/nix.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Project } from '../project.js'

import { BaseBuildTool } from './build-system.js'

export class Nix extends BaseBuildTool {
id = 'nix'
name = 'Nix'
configFiles = ['default.nix', 'shell.nix', 'release.nix']

async detect(project: Project) {
const config = await project.fs.findUp(this.configFiles, { cwd: project.baseDirectory, stopAt: project.root })
async detect() {
const config = await this.project.fs.findUp(this.configFiles, {
cwd: this.project.baseDirectory,
stopAt: this.project.root,
})

if (config) {
return this
Expand Down
Loading

0 comments on commit 0df38c0

Please sign in to comment.