Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added ability to import code from ee folder #18841

Merged
merged 11 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ jobs:
# NOTE: we are at risk of missing a dependency here. We could make
# the dependencies more clear if we separated the backend/frontend
# code completely
# really we should ignore ee/frontend/** but dorny doesn't support that
- 'ee/**/*'
- '!ee/frontend/**'
- 'posthog/**/*'
- 'bin/*.py'
- requirements.txt
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/ci-frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
# NOTE: we are at risk of missing a dependency here.
- 'bin/**'
- 'frontend/**'
- 'ee/frontend/**'
# Make sure we run if someone is explicitly change the workflow
- .github/workflows/ci-frontend.yml
# various JS config files
Expand Down Expand Up @@ -117,18 +118,23 @@ jobs:
jest:
runs-on: ubuntu-latest
needs: changes
name: Jest test (${{ matrix.chunk }})
name: Jest test (${{ matrix.segment }} - ${{ matrix.chunk }})

strategy:
# If one test fails, still run the others
fail-fast: false
matrix:
segment: ['FOSS', 'EE']
chunk: [1, 2, 3]

steps:
# we need at least one thing to run to make sure we include everything for required jobs
- uses: actions/checkout@v3

- name: Remove ee
if: needs.changes.outputs.frontend == 'true' && matrix.segment == 'FOSS'
run: rm -rf ee
benjackwhite marked this conversation as resolved.
Show resolved Hide resolved

- name: Install pnpm
if: needs.changes.outputs.frontend == 'true'
uses: pnpm/action-setup@v2
Expand Down
13 changes: 13 additions & 0 deletions ee/frontend/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PostHogEE } from '@posthog/ee/types'

const myTestCode = (): void => {
// eslint-disable-next-line no-console
console.log('it works!')
}

const postHogEE: PostHogEE = {
enabled: true,
myTestCode,
}

export default postHogEE
7 changes: 7 additions & 0 deletions frontend/@posthog/ee/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PostHogEE } from './types'

const posthogEE: PostHogEE = {
enabled: false,
}

export default posthogEE
5 changes: 5 additions & 0 deletions frontend/@posthog/ee/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// NOTE: All exported items from the EE module _must_ be optionally defined to ensure we work well with FOSS
export type PostHogEE = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really neat though! one or more types in here that define expectation and then a FOSS and EE licensed version.

Is it just typescript magic that ensure the EE version overwrites the FOSS version?

I.e. imagine the case where we want code present in both versions but different behavoiur? Which might simply be something we explicitly say we don't support (or don't support this way...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will cross that bridge when we come to it tbh.
Typescript will import in order of the files mentioned which is how it works. So if the EE folder isn't present it will fallback to the base folder.

enabled: boolean
myTestCode?: () => void
}
17 changes: 17 additions & 0 deletions frontend/src/lib/ee.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'fs'

const eeFolderExists = fs.existsSync('ee/frontend/exports.ts')
export const ifEeIt = eeFolderExists ? it : it.skip
export const ifFossIt = !eeFolderExists ? it : it.skip
benjackwhite marked this conversation as resolved.
Show resolved Hide resolved

import posthogEE from '@posthog/ee/exports'

describe('ee importing', () => {
ifEeIt('should import actual ee code', () => {
expect(posthogEE.enabled).toBe(true)
})

ifFossIt('should import actual ee code', () => {
expect(posthogEE.enabled).toBe(false)
})
})
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const config: Config = {
'^~/(.*)$': '<rootDir>/$1',
'^@posthog/lemon-ui(|/.*)$': '<rootDir>/../@posthog/lemon-ui/src/$1',
'^@posthog/apps-common(|/.*)$': '<rootDir>/../@posthog/apps-common/src/$1',
'^@posthog/ee/exports': ['<rootDir>/../../ee/frontend/exports', '<rootDir>/../@posthog/ee/exports'],
'^lib/(.*)$': '<rootDir>/lib/$1',
'^scenes/(.*)$': '<rootDir>/scenes/$1',
'^antd/es/(.*)$': 'antd/lib/$1',
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@posthog/lemon-ui": ["@posthog/lemon-ui/src/index"],
"@posthog/lemon-ui/*": ["@posthog/lemon-ui/src/*"],
"storybook/*": ["../.storybook/*"],
"@posthog/ee/exports": ["../ee/exports", "@posthog/ee/exports"],
"~/*": ["src/*"],
"public/*": ["public/*"]
},
Expand All @@ -33,7 +34,7 @@
"suppressImplicitAnyIndexErrors": true, // Index objects by number
"lib": ["dom", "es2019"]
},
"include": ["frontend/**/*", ".storybook/**/*"],
"include": ["frontend/**/*", ".storybook/**/*", "ee/frontend/**/*"],
"exclude": ["frontend/dist/**/*"],
"ts-node": {
"compilerOptions": {
Expand Down
4 changes: 4 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function createEntry(entry) {
scenes: path.resolve(__dirname, 'frontend', 'src', 'scenes'),
'@posthog/apps-common': path.resolve(__dirname, 'frontend', '@posthog', 'apps-common', 'src'),
'@posthog/lemon-ui': path.resolve(__dirname, 'frontend', '@posthog', 'lemon-ui', 'src'),
'@posthog/ee/exports': [
path.resolve(__dirname, 'ee', 'frontend', 'exports'),
path.resolve(__dirname, 'frontend', '@posthog', 'ee', 'exports'),
],
storybook: path.resolve(__dirname, '.storybook'),
types: path.resolve(__dirname, 'frontend', 'types'),
public: path.resolve(__dirname, 'frontend', 'public'),
Expand Down
Loading