From 612a1d53014ba2b97557d5c4c184ae8f3e1e2fea Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Sun, 2 Apr 2023 14:12:30 +0800 Subject: [PATCH] refactor: use connector link in workflows --- .devcontainer/devcontainer.json | 2 +- .github/CONTRIBUTING.md | 4 ++- .github/workflows/integration-test.yml | 2 +- .gitpod.yml | 3 +- .scripts/package.sh | 2 +- Dockerfile | 2 +- Dockerfile.cloud | 4 +-- package.json | 5 +-- packages/cli/src/commands/connector/link.ts | 37 +++++++++++++++++---- packages/cli/src/connector/loader.ts | 2 +- 10 files changed, 45 insertions(+), 18 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 59d648c36..84182f07b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -3,7 +3,7 @@ "features": { "ghcr.io/devcontainers/features/node:1": {} }, - "updateContentCommand": "npm i -g pnpm && pnpm i && pnpm prepack && pnpm cli connector add --official -p .", + "updateContentCommand": "npm i -g pnpm && pnpm i && pnpm prepack && pnpm connectors:build && pnpm cli connector link -p .", "postStartCommand": "docker run -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=p0stgr3s postgres:14-alpine", "postAttachCommand": "pnpm cli db seed && [[ ! -z $CODESPACES ]] && export ENDPOINT=https://$CODESPACE_NAME-3001.preview.app.github.dev", "containerEnv": { diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index e1d18ba6f..dbc297868 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -101,7 +101,9 @@ If you are developing something with database alterations, see [packages/schemas ### Add connectors (optional) -Run `pnpm cli connector add --official -p .` to add all Logto official connectors. See [Manage connectors](https://docs.logto.io/docs/tutorials/using-cli/manage-connectors) for details about managing connectors via CLI. +Run `logto connector link -p .` to link all local connectors. You can also use `logto connector add -p .` to install connector from NPM. + +See [Manage connectors](https://docs.logto.io/docs/tutorials/using-cli/manage-connectors) for details about managing connectors via CLI. ## Start dev diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index fd99a41d9..e48d5b8d8 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -107,7 +107,7 @@ jobs: working-directory: tests run: | npm run cli connector list -- -p ../logto | grep OFFICIAL - npm run cli connector add @logto/connector-mock-sms @logto/connector-mock-email @logto/connector-mock-standard-email @logto/connector-mock-social -- -p ../logto + npm run cli connector link -- --mock -p ../logto - name: Run Logto working-directory: logto/ diff --git a/.gitpod.yml b/.gitpod.yml index ecff11683..0974dbdc8 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -12,7 +12,8 @@ tasks: cd packages/core pnpm build cd - - pnpm cli connector add --official -p . + pnpm connectors:build + pnpm cli connector link command: | gp ports await 5432 sleep 3 diff --git a/.scripts/package.sh b/.scripts/package.sh index e8f448a96..c7654930a 100755 --- a/.scripts/package.sh +++ b/.scripts/package.sh @@ -26,7 +26,7 @@ packages/**/*.config.js packages/**/*.config.ts packages/**/tsconfig*.json \ # Add official connectors cloud_option=$( [[ "$IS_CLOUD" =~ ^(1|true|y|yes|yep|yeah)$ ]] && echo "--cloud" || echo "" ) -pnpm cli connector add --official $cloud_option -p . +pnpm cli connector link $cloud_option -p . echo Tar cd .. diff --git a/Dockerfile b/Dockerfile index 9dbde839e..4b5385781 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,7 @@ RUN pnpm -r build ### Add official connectors ### ARG additional_connector_args ENV ADDITIONAL_CONNECTOR_ARGS=${additional_connector_args} -RUN pnpm cli connector add --official $ADDITIONAL_CONNECTOR_ARGS -p . +RUN pnpm cli connector link $ADDITIONAL_CONNECTOR_ARGS -p . ### Prune dependencies for production ### RUN rm -rf node_modules packages/**/node_modules diff --git a/Dockerfile.cloud b/Dockerfile.cloud index db459d78c..3d68debc5 100644 --- a/Dockerfile.cloud +++ b/Dockerfile.cloud @@ -24,10 +24,10 @@ ENV IS_CLOUD=1 ARG applicationinsights_connection_string ENV APPLICATIONINSIGHTS_CONNECTION_STRING=${applicationinsights_connection_string} RUN pnpm prepack -RUN pnpm -r --filter @logto/console --filter @logto/cloud build +RUN pnpm connectors:build && pnpm -r --filter @logto/console --filter @logto/cloud build ### Add official connectors ### -RUN pnpm cli connector add --official -p . +RUN pnpm cli connector link -p . ### Prune dependencies for production ### RUN rm -rf node_modules packages/**/node_modules diff --git a/package.json b/package.json index 0e13673ae..66552dcab 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,13 @@ "prepack": "pnpm -r prepack", "dev": "pnpm -r prepack && pnpm start:dev", "dev:cloud": "pnpm -r prepack && pnpm start:dev:cloud", - "start:dev": "pnpm -r --parallel --filter=!@logto/integration-tests --filter=!@logto/cloud dev", - "start:dev:cloud": "CONSOLE_PUBLIC_URL=/ IS_CLOUD=1 pnpm -r --parallel --filter=!@logto/integration-tests dev", + "start:dev": "pnpm -r --parallel --filter=!@logto/integration-tests --filter \"!./packages/connectors/connector-*\" --filter=!@logto/cloud dev", + "start:dev:cloud": "CONSOLE_PUBLIC_URL=/ IS_CLOUD=1 pnpm -r --parallel --filter=!@logto/integration-tests --filter \"!./packages/connectors/connector-*\" dev", "start": "cd packages/core && NODE_ENV=production node .", "start:cloud": "cd packages/cloud && NODE_ENV=production node .", "cli": "logto", "alteration": "logto db alt", + "connectors:build": "pnpm -r --filter \"./packages/connectors/connector-*\" build", "//": "# `changeset version` won't run version lifecycle scripts, see https://github.com/changesets/changesets/issues/860", "ci:version": "changeset version && pnpm -r version", "ci:build": "pnpm -r build", diff --git a/packages/cli/src/commands/connector/link.ts b/packages/cli/src/commands/connector/link.ts index 7d6a86292..8e9232794 100644 --- a/packages/cli/src/commands/connector/link.ts +++ b/packages/cli/src/commands/connector/link.ts @@ -7,22 +7,45 @@ import { log } from '../../utils.js'; import { getConnectorDirectory, getLocalConnectorPackages, inquireInstancePath } from './utils.js'; -const link: CommandModule<{ path?: string }, { path?: string }> = { +const link: CommandModule<{ path?: string }, { path?: string; cloud: boolean; mock: boolean }> = { command: ['link', 'ln'], describe: 'Link all connectors in `packages/connectors`, useful for adding local connectors.', - handler: async ({ path: inputPath }) => { + builder: (yargs) => + yargs + .option('cloud', { + describe: 'Add additional connectors for Logto Cloud', + type: 'boolean', + default: false, + hidden: true, + }) + .option('mock', { + describe: 'Add mock connectors', + type: 'boolean', + default: false, + hidden: true, + }), + handler: async ({ path: inputPath, cloud, mock }) => { const instancePath = await inquireInstancePath(inputPath); const packages = await getLocalConnectorPackages(instancePath); + const connectorDirectory = getConnectorDirectory(instancePath); + + await fs.mkdir(connectorDirectory, { recursive: true }); + await Promise.all( packages.map(async ([packageName, packagePath]) => { + if (!cloud && packageName.startsWith('connector-logto-')) { + return; + } + + if (!mock && packageName.startsWith('connector-mock-')) { + return; + } + try { - const targetPath = path.join( - getConnectorDirectory(instancePath), - '@logto-' + packageName - ); + const targetPath = path.join(connectorDirectory, '@logto-' + packageName); await fs.rm(targetPath, { recursive: true, force: true }); - await fs.symlink(path.resolve(packagePath), targetPath); + await fs.symlink(path.relative(connectorDirectory, packagePath), targetPath); } catch (error) { log.warn(error); diff --git a/packages/cli/src/connector/loader.ts b/packages/cli/src/connector/loader.ts index c87a09971..501aabece 100644 --- a/packages/cli/src/connector/loader.ts +++ b/packages/cli/src/connector/loader.ts @@ -14,7 +14,7 @@ const checkConnectorKitVersion = (dependencies: unknown, ignoreVersionMismatch: const value = dependencies[connectorKit]; if (typeof value === 'string') { - if (satisfies(currentVersion, value)) { + if (value.startsWith('workspace:') || satisfies(currentVersion, value)) { return; }