diff --git a/.scripts/package.sh b/.scripts/package.sh index 296381154..ffa6c0724 100755 --- a/.scripts/package.sh +++ b/.scripts/package.sh @@ -25,7 +25,8 @@ packages/**/*.config.js packages/**/*.config.ts packages/**/tsconfig*.json \ -prune -exec rm -rf {} + # Add official connectors -pnpm cli connector add --official -p . +cloud_option=$( [[ "$IS_CLOUD" =~ ^(1|true|y|yes|yep|yeah)$ ]] && echo "--cloud" || echo "" ) +pnpm cli connector add --official $cloud_option -p . echo Tar cd .. diff --git a/packages/cli/src/commands/connector/add.ts b/packages/cli/src/commands/connector/add.ts index feb2df5e1..9897ca647 100644 --- a/packages/cli/src/commands/connector/add.ts +++ b/packages/cli/src/commands/connector/add.ts @@ -5,7 +5,7 @@ import { addConnectors, addOfficialConnectors, inquireInstancePath } from './uti const add: CommandModule< { path?: string }, - { packages?: string[]; path?: string; official: boolean } + { packages?: string[]; path?: string; official: boolean; cloud: boolean } > = { command: ['add [packages...]', 'a', 'install', 'i'], describe: 'Add specific Logto connectors', @@ -24,12 +24,23 @@ const add: CommandModule< describe: 'Add all official connectors.\n' + "If it's true, the specified package names will be ignored.", + }) + .option('cloud', { + describe: 'Add additional connectors for Logto Cloud', + type: 'boolean', + default: false, + hidden: true, }), - handler: async ({ packages: packageNames, path, official }) => { + handler: async ({ packages: packageNames, path, official, cloud }) => { const instancePath = await inquireInstancePath(path); + if (cloud && !official) { + log.error('--cloud option can only be used with --official option'); + } + if (official) { - await addOfficialConnectors(instancePath); + // Add demo connectors for Logto Cloud only + await addOfficialConnectors(instancePath, cloud); } else { if (!packageNames?.length) { log.error('No connector name provided'); diff --git a/packages/cli/src/commands/connector/utils.ts b/packages/cli/src/commands/connector/utils.ts index 542893c92..e2b37f041 100644 --- a/packages/cli/src/commands/connector/utils.ts +++ b/packages/cli/src/commands/connector/utils.ts @@ -181,7 +181,7 @@ const officialConnectorPrefix = '@logto/connector-'; type PackageMeta = { name: string; scope: string; version: string }; -const fetchOfficialConnectorList = async () => { +const fetchOfficialConnectorList = async (includingCloudConnectors = false) => { // See https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#get-v1search type FetchResult = { objects: Array<{ @@ -211,13 +211,15 @@ const fetchOfficialConnectorList = async () => { // eslint-disable-next-line no-await-in-loop const { objects } = await fetchList(page * 20, 20); + const excludeList = ['mock', 'kit', ...(includingCloudConnectors ? [] : ['logto'])]; + // eslint-disable-next-line @silverhand/fp/no-mutating-methods packages.push( ...objects .filter( ({ package: { name, scope } }) => scope === 'logto' && - ['mock', 'kit'].every( + excludeList.every( (excluded) => !name.slice(officialConnectorPrefix.length).startsWith(excluded) ) ) @@ -232,8 +234,11 @@ const fetchOfficialConnectorList = async () => { return packages; }; -export const addOfficialConnectors = async (instancePath: string) => { - const packages = await oraPromise(fetchOfficialConnectorList(), { +export const addOfficialConnectors = async ( + instancePath: string, + includingCloudConnectors = false +) => { + const packages = await oraPromise(fetchOfficialConnectorList(includingCloudConnectors), { text: 'Fetch official connector list', prefixText: chalk.blue('[info]'), }); diff --git a/packages/core/src/routes/connector.ts b/packages/core/src/routes/connector.ts index 6c011644e..3626bebf3 100644 --- a/packages/core/src/routes/connector.ts +++ b/packages/core/src/routes/connector.ts @@ -2,6 +2,7 @@ import { buildRawConnector } from '@logto/cli/lib/connector/index.js'; import { VerificationCodeType, validateConfig } from '@logto/connector-kit'; import { emailRegEx, phoneRegEx, buildIdGenerator } from '@logto/core-kit'; import { arbitraryObjectGuard, Connectors, ConnectorType } from '@logto/schemas'; +import { demoConnectorIds } from '@logto/shared'; import cleanDeep from 'clean-deep'; import { string, object } from 'zod'; @@ -69,9 +70,9 @@ export default function connectorRoutes( router.get('/connector-factories', async (ctx, next) => { const connectorFactories = await loadConnectorFactories(); - ctx.body = connectorFactories.map((connectorFactory) => - transpileConnectorFactory(connectorFactory) - ); + ctx.body = connectorFactories + .map((connectorFactory) => transpileConnectorFactory(connectorFactory)) + .filter(({ id }) => !demoConnectorIds.includes(id)); return next(); }); diff --git a/packages/shared/src/models/connector.ts b/packages/shared/src/models/connector.ts new file mode 100644 index 000000000..eeb9579cd --- /dev/null +++ b/packages/shared/src/models/connector.ts @@ -0,0 +1 @@ +export const demoConnectorIds = ['logto-email', 'logto-sms']; diff --git a/packages/shared/src/models/index.ts b/packages/shared/src/models/index.ts index 8d4d25673..26edebe36 100644 --- a/packages/shared/src/models/index.ts +++ b/packages/shared/src/models/index.ts @@ -1 +1,2 @@ export * from './tenant.js'; +export * from './connector.js';