From 4ad3928c748420681557c2a5656590cbf8d109b2 Mon Sep 17 00:00:00 2001 From: Ben White Date: Thu, 23 Nov 2023 17:40:46 +0100 Subject: [PATCH] feat: Added ability to import code from ee folder (#18841) We want to experiment with some conditional licensing of frontend code via the ee folder. --------- Co-authored-by: Paul D'Ambra --- .github/workflows/ci-backend.yml | 2 ++ .github/workflows/ci-frontend.yml | 8 +++++++- ee/frontend/exports.ts | 13 +++++++++++++ frontend/@posthog/ee/exports.ts | 7 +++++++ frontend/@posthog/ee/types.ts | 5 +++++ frontend/src/lib/ee.test.ts | 17 +++++++++++++++++ jest.config.ts | 1 + tsconfig.json | 3 ++- webpack.config.js | 4 ++++ 9 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 ee/frontend/exports.ts create mode 100644 frontend/@posthog/ee/exports.ts create mode 100644 frontend/@posthog/ee/types.ts create mode 100644 frontend/src/lib/ee.test.ts diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml index 4e09f52eda7b6..a346ec59367a7 100644 --- a/.github/workflows/ci-backend.yml +++ b/.github/workflows/ci-backend.yml @@ -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 diff --git a/.github/workflows/ci-frontend.yml b/.github/workflows/ci-frontend.yml index 2ea70218bd608..c586598152fd6 100644 --- a/.github/workflows/ci-frontend.yml +++ b/.github/workflows/ci-frontend.yml @@ -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 @@ -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 diff --git a/ee/frontend/exports.ts b/ee/frontend/exports.ts new file mode 100644 index 0000000000000..29d80016d730c --- /dev/null +++ b/ee/frontend/exports.ts @@ -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 diff --git a/frontend/@posthog/ee/exports.ts b/frontend/@posthog/ee/exports.ts new file mode 100644 index 0000000000000..96d7dc5a5e93d --- /dev/null +++ b/frontend/@posthog/ee/exports.ts @@ -0,0 +1,7 @@ +import { PostHogEE } from './types' + +const posthogEE: PostHogEE = { + enabled: false, +} + +export default posthogEE diff --git a/frontend/@posthog/ee/types.ts b/frontend/@posthog/ee/types.ts new file mode 100644 index 0000000000000..7270631c7c78f --- /dev/null +++ b/frontend/@posthog/ee/types.ts @@ -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 +} diff --git a/frontend/src/lib/ee.test.ts b/frontend/src/lib/ee.test.ts new file mode 100644 index 0000000000000..e5cddf9ec2436 --- /dev/null +++ b/frontend/src/lib/ee.test.ts @@ -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) + }) +}) diff --git a/jest.config.ts b/jest.config.ts index 54aa39875ab00..3ff4eeff1d470 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -89,6 +89,7 @@ const config: Config = { '^~/(.*)$': '/$1', '^@posthog/lemon-ui(|/.*)$': '/../@posthog/lemon-ui/src/$1', '^@posthog/apps-common(|/.*)$': '/../@posthog/apps-common/src/$1', + '^@posthog/ee/exports': ['/../../ee/frontend/exports', '/../@posthog/ee/exports'], '^lib/(.*)$': '/lib/$1', '^scenes/(.*)$': '/scenes/$1', '^antd/es/(.*)$': 'antd/lib/$1', diff --git a/tsconfig.json b/tsconfig.json index d00e7af6a118b..ef6815e5be0d8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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/*"] }, @@ -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": { diff --git a/webpack.config.js b/webpack.config.js index afa086a96c11c..aef12b7ab0c4b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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'),