diff --git a/packages/cli/src/commands/connector/utils.ts b/packages/cli/src/commands/connector/utils.ts index 1d1f2a476..a43290672 100644 --- a/packages/cli/src/commands/connector/utils.ts +++ b/packages/cli/src/commands/connector/utils.ts @@ -1,6 +1,6 @@ import { exec } from 'child_process'; import { existsSync } from 'fs'; -import { readFile, mkdir, unlink, readdir } from 'fs/promises'; +import { readFile, mkdir, unlink } from 'fs/promises'; import path from 'path'; import { promisify } from 'util'; @@ -13,7 +13,7 @@ import tar from 'tar'; import { z } from 'zod'; import { connectorDirectory } from '../../constants'; -import { isTty, log, oraPromise } from '../../utilities'; +import { getConnectorPackagesFromDirectory, isTty, log, oraPromise } from '../../utilities'; import { defaultPath } from '../install/utils'; const coreDirectory = 'packages/core'; @@ -102,40 +102,10 @@ const getConnectorDirectory = (instancePath: string) => export const isOfficialConnector = (packageName: string) => packageName.startsWith('@logto/connector-'); -const getConnectorPackageName = async (directory: string) => { - const filePath = path.join(directory, 'package.json'); - - if (!existsSync(filePath)) { - return; - } - - const json = await readFile(filePath, 'utf8'); - const { name } = z.object({ name: z.string() }).parse(JSON.parse(json)); - - if (name.startsWith('connector-') || Boolean(name.split('/')[1]?.startsWith('connector-'))) { - return name; - } -}; - -export type ConnectorPackage = { - name: string; - path: string; -}; - export const getConnectorPackagesFrom = async (instancePath?: string) => { const directory = getConnectorDirectory(await inquireInstancePath(instancePath)); - const content = await readdir(directory, 'utf8'); - const rawPackages = await Promise.all( - content.map(async (value) => { - const currentDirectory = path.join(directory, value); - return { name: await getConnectorPackageName(currentDirectory), path: currentDirectory }; - }) - ); - - return rawPackages.filter( - (packageInfo): packageInfo is ConnectorPackage => typeof packageInfo.name === 'string' - ); + return getConnectorPackagesFromDirectory(directory); }; export const addConnectors = async (instancePath: string, packageNames: string[]) => { diff --git a/packages/cli/src/utilities.ts b/packages/cli/src/utilities.ts index 14178fd4f..c1d90cd6c 100644 --- a/packages/cli/src/utilities.ts +++ b/packages/cli/src/utilities.ts @@ -1,5 +1,6 @@ import { execSync } from 'child_process'; -import { createWriteStream } from 'fs'; +import { createWriteStream, existsSync } from 'fs'; +import { readdir, readFile } from 'fs/promises'; import path from 'path'; import type { Optional } from '@silverhand/essentials'; @@ -10,6 +11,7 @@ import got from 'got'; import { HttpsProxyAgent } from 'hpagent'; import inquirer from 'inquirer'; import ora from 'ora'; +import { z } from 'zod'; export const safeExecSync = (command: string) => { try { @@ -173,3 +175,38 @@ export function findLastIndex( return -1; } + +const getConnectorPackageName = async (directory: string) => { + const filePath = path.join(directory, 'package.json'); + + if (!existsSync(filePath)) { + return; + } + + const json = await readFile(filePath, 'utf8'); + const { name } = z.object({ name: z.string() }).parse(JSON.parse(json)); + + if (name.startsWith('connector-') || Boolean(name.split('/')[1]?.startsWith('connector-'))) { + return name; + } +}; + +export type ConnectorPackage = { + name: string; + path: string; +}; + +export const getConnectorPackagesFromDirectory = async (directory: string) => { + const content = await readdir(directory, 'utf8'); + const rawPackages = await Promise.all( + content.map(async (value) => { + const currentDirectory = path.join(directory, value); + + return { name: await getConnectorPackageName(currentDirectory), path: currentDirectory }; + }) + ); + + return rawPackages.filter( + (packageInfo): packageInfo is ConnectorPackage => typeof packageInfo.name === 'string' + ); +}; diff --git a/packages/core/src/connectors/index.ts b/packages/core/src/connectors/index.ts index 9ef3237b5..8ae81e02f 100644 --- a/packages/core/src/connectors/index.ts +++ b/packages/core/src/connectors/index.ts @@ -1,8 +1,8 @@ import { existsSync } from 'fs'; -import { readdir } from 'fs/promises'; import path from 'path'; import { connectorDirectory } from '@logto/cli/lib/constants'; +import { getConnectorPackagesFromDirectory } from '@logto/cli/lib/utilities'; import type { AllConnector, CreateConnector } from '@logto/connector-kit'; import { validateConfig } from '@logto/connector-kit'; import { findPackage } from '@logto/shared'; @@ -32,12 +32,11 @@ const loadConnectors = async () => { return []; } - const connectorFolders = await readdir(directory); + const connectorPackages = await getConnectorPackagesFromDirectory(directory); const connectors = await Promise.all( - connectorFolders.map(async (folder) => { + connectorPackages.map(async ({ path: packagePath, name }) => { try { - const packagePath = path.join(directory, folder); // eslint-disable-next-line no-restricted-syntax const { default: createConnector } = (await import(packagePath)) as { default: CreateConnector; @@ -71,7 +70,7 @@ const loadConnectors = async () => { if (error instanceof Error) { console.log( `${chalk.red( - `[load-connector] skip ${chalk.bold(folder)} due to error: ${error.message}` + `[load-connector] skip ${chalk.bold(name)} due to error: ${error.message}` )}` );