Skip to content

Commit

Permalink
feat: Added ability to import code from ee folder (#18841)
Browse files Browse the repository at this point in the history
We want to experiment with some conditional licensing of frontend code via the ee folder.

---------

Co-authored-by: Paul D'Ambra <[email protected]>
  • Loading branch information
benjackwhite and pauldambra authored Nov 23, 2023
1 parent 98c71d4 commit 4ad3928
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 2 deletions.
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

- 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 = {
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

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

0 comments on commit 4ad3928

Please sign in to comment.