Skip to content

Commit

Permalink
fix: Don't detect Redwood App as multiple workspaces (#5271)
Browse files Browse the repository at this point in the history
* fix: redwood workspace detection

* refactor: use mocked redwood repo, instead of local path

* fix: webFS#fileExists needs to await file check
  • Loading branch information
Skn0tt committed Sep 1, 2023
1 parent eb83967 commit 9eaa059
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/build-info/src/browser/file-system.ts
Expand Up @@ -26,7 +26,7 @@ export class GithubProvider {
if (response.headers?.get('Content-Type')?.match(/json/)) {
const json = await response.json()
if (!response.ok) {
Promise.reject(json)
throw new Error(JSON.stringify(json))
}
return json
}
Expand Down Expand Up @@ -55,7 +55,7 @@ export class WebFS extends FileSystem {

async fileExists(path: string): Promise<boolean> {
try {
this.readFile(path)
await this.readFile(path)
return true
} catch {
return false
Expand Down
6 changes: 3 additions & 3 deletions packages/build-info/src/frameworks/qwik.test.ts
Expand Up @@ -8,7 +8,7 @@ beforeEach((ctx) => {
ctx.fs = new NodeFS()
})

test('should detect quick with npm', async ({ fs }) => {
test('should detect qwik with npm', async ({ fs }) => {
const cwd = mockFileSystem({
'package.json': JSON.stringify({ dependencies: { '@builder.io/qwik': '*' } }),
})
Expand All @@ -19,7 +19,7 @@ test('should detect quick with npm', async ({ fs }) => {
expect(detected?.[0].dev?.command).toBe('vite')
})

test('should detect quick with yarn', async ({ fs }) => {
test('should detect qwik with yarn', async ({ fs }) => {
const cwd = mockFileSystem({
'package.json': JSON.stringify({ packageManager: 'yarn@3.2.1', dependencies: { '@builder.io/qwik': '*' } }),
})
Expand All @@ -29,7 +29,7 @@ test('should detect quick with yarn', async ({ fs }) => {
expect(detected?.[0].build.directory).toBe('dist')
})

test('should detect quick', async ({ fs }) => {
test('should detect qwik', async ({ fs }) => {
const cwd = mockFileSystem({
'pnpm-lock.yaml': '',
'package.json': JSON.stringify({ dependencies: { '@builder.io/qwik': '*' } }),
Expand Down
102 changes: 102 additions & 0 deletions packages/build-info/src/frameworks/redwood.test.ts
@@ -0,0 +1,102 @@
import { beforeEach, expect, test } from 'vitest'

import { mockFileSystem } from '../../tests/mock-file-system.js'
import { NodeFS } from '../node/file-system.js'
import { Project } from '../project.js'

beforeEach((ctx) => {
ctx.fs = new NodeFS()
})

const redwoodToml = `
# This file contains the configuration settings for your Redwood app.
# This file is also what makes your Redwood app a Redwood app.
# If you remove it and try to run "yarn rw dev", you'll get an error.
#
# For the full list of options, see the "App Configuration: redwood.toml" doc:
# https://redwoodjs.com/docs/app-configuration-redwood-toml
[web]
title = "Redwood App"
port = 8910
apiUrl = "/.netlify/functions"
includeEnvironmentVariables = [
# Add any ENV vars that should be available to the web side to this array
# See https://redwoodjs.com/docs/environment-variables#web
]
[api]
port = 8911
[browser]
open = true
[notifications]
versionUpdates = ["latest"]
`

test('should detect redwood', async ({ fs }) => {
const cwd = mockFileSystem({
'redwood.toml': redwoodToml,
'package.json': JSON.stringify({
private: true,
workspaces: {
packages: ['api', 'web'],
},
devDependencies: {
'@redwoodjs/core': '6.1.0',
},
eslintConfig: {
extends: '@redwoodjs/eslint-config',
root: true,
},
engines: {
node: '=18.x',
yarn: '>=1.15',
},
prisma: {
seed: 'yarn rw exec seed',
},
packageManager: 'yarn@3.6.1',
}),
'web/package.json': JSON.stringify({
name: 'web',
version: '0.0.0',
private: true,
browserslist: {
development: ['last 1 version'],
production: ['defaults'],
},
dependencies: {
'@redwoodjs/forms': '6.1.0',
'@redwoodjs/router': '6.1.0',
'@redwoodjs/web': '6.1.0',
'humanize-string': '2.1.0',
'prop-types': '15.8.1',
react: '18.2.0',
'react-dom': '18.2.0',
},
devDependencies: {
'@redwoodjs/vite': '6.1.0',
},
}),
'api/package.json': JSON.stringify({
name: 'api',
version: '0.0.0',
private: true,
dependencies: {
'@redwoodjs/api': '6.1.0',
'@redwoodjs/graphql-server': '6.1.0',
},
}),
})

const project = new Project(fs, cwd, cwd)

expect(await project.detectWorkspaces()).toBeNull()
const detected = await project.detectFrameworks()

expect(detected).toHaveLength(1)
expect(detected?.[0].id).toBe('redwoodjs')
expect(detected?.[0].name).toBe('RedwoodJS')
expect(detected?.[0].build.command).toBe('rw deploy netlify')
expect(detected?.[0].build.directory).toBe('web/dist')
expect(detected?.[0].dev?.command).toBe('yarn rw dev')
})
4 changes: 4 additions & 0 deletions packages/build-info/src/project.ts
Expand Up @@ -99,6 +99,10 @@ export class Project {
return null
}

async isRedwoodProject(): Promise<boolean> {
return await this.fs.fileExists(this.fs.resolve(this.fs.cwd, 'redwood.toml'))
}

constructor(public fs: FileSystem, baseDirectory?: string, root?: string) {
this.baseDirectory = fs.resolve(root || '', baseDirectory !== undefined ? baseDirectory : fs.cwd)
this.root = root ? fs.resolve(fs.cwd, root) : undefined
Expand Down
4 changes: 4 additions & 0 deletions packages/build-info/src/workspaces/detect-workspace.ts
Expand Up @@ -78,6 +78,10 @@ export async function detectWorkspaces(project: Project): Promise<WorkspaceInfo
return null
}

if (await project.isRedwoodProject()) {
return null
}

const pkgJSON = await project.getRootPackageJSON()
const workspaceGlobs =
project.packageManager.name === PkgManager.PNPM
Expand Down
17 changes: 6 additions & 11 deletions packages/build-info/tests/helpers.ts
Expand Up @@ -68,16 +68,11 @@ export const createWebFixture = async (fixture: string) => {
const info = await stat(src)
if (info.isDirectory()) {
const entries = await readdir(src, { withFileTypes: true })
return new Response(
JSON.stringify(
entries.map((entry) => ({
path: entry.name,
type: entry.isDirectory() ? 'dir' : 'file',
})),
),
{
headers: { 'Content-Type': 'application/json' },
},
return Response.json(
entries.map((entry) => ({
path: entry.name,
type: entry.isDirectory() ? 'dir' : 'file',
})),
)
} else {
const file = await readFile(src, 'utf-8')
Expand All @@ -87,7 +82,7 @@ export const createWebFixture = async (fixture: string) => {
// noop
}

throw new Error(`404 ${url} not found!`)
return Response.json({ error: 'not found' }, { status: 404 })
})

return { cwd: '/' }
Expand Down

0 comments on commit 9eaa059

Please sign in to comment.