diff --git a/packages/cli/package.json b/packages/cli/package.json index dd3042fa1..62d37af0e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -41,7 +41,6 @@ "chalk": "^4.1.2", "decamelize": "^5.0.0", "dotenv": "^16.0.0", - "find-up": "^5.0.0", "fs-extra": "^10.1.0", "got": "^11.8.2", "hpagent": "^1.0.0", diff --git a/packages/cli/src/commands/database/alteration.ts b/packages/cli/src/commands/database/alteration.ts index 4ec9a73b3..437822e9f 100644 --- a/packages/cli/src/commands/database/alteration.ts +++ b/packages/cli/src/commands/database/alteration.ts @@ -1,9 +1,9 @@ import path from 'path'; import { AlterationScript } from '@logto/schemas/lib/types/alteration'; -import { conditional, conditionalString } from '@silverhand/essentials'; +import { findPackage } from '@logto/shared'; +import { conditionalString } from '@silverhand/essentials'; import chalk from 'chalk'; -import findUp, { exists } from 'find-up'; import { copy, existsSync, remove, readdir } from 'fs-extra'; import { DatabasePool } from 'slonik'; import { CommandModule } from 'yargs'; @@ -45,18 +45,10 @@ export const getAlterationFiles = async (): Promise => { * since they need a proper context that includes required dependencies (such as slonik) in `node_modules/`. * While the original `@logto/schemas` may remove them in production. */ - const packageDirectory = await findUp( - async (directory) => { - const hasPackageJson = await exists(path.join(directory, 'package.json')); - - return conditional(hasPackageJson && directory); - }, - { - // Until we migrate to ESM - // eslint-disable-next-line unicorn/prefer-module - cwd: __dirname, - type: 'directory', - } + const packageDirectory = await findPackage( + // Until we migrate to ESM + // eslint-disable-next-line unicorn/prefer-module + __dirname ); const localAlterationDirectory = path.resolve( diff --git a/packages/core/src/connectors/index.ts b/packages/core/src/connectors/index.ts index a6d7c1437..1e4c5317a 100644 --- a/packages/core/src/connectors/index.ts +++ b/packages/core/src/connectors/index.ts @@ -4,6 +4,7 @@ import path from 'path'; import { connectorDirectory } from '@logto/cli/lib/constants'; import { AllConnector, CreateConnector, validateConfig } from '@logto/connector-kit'; +import { findPackage } from '@logto/shared'; import chalk from 'chalk'; import RequestError from '@/errors/RequestError'; @@ -21,16 +22,21 @@ const loadConnectors = async () => { return cachedConnectors; } - if (!existsSync(connectorDirectory)) { + // Until we migrate to ESM + // eslint-disable-next-line unicorn/prefer-module + const coreDirectory = await findPackage(__dirname); + const directory = coreDirectory && path.join(coreDirectory, connectorDirectory); + + if (!directory || !existsSync(directory)) { return []; } - const connectorFolders = await readdir(connectorDirectory); + const connectorFolders = await readdir(directory); const connectors = await Promise.all( connectorFolders.map(async (folder) => { try { - const packagePath = path.join(connectorDirectory, folder); + const packagePath = path.join(directory, folder); // eslint-disable-next-line no-restricted-syntax const { default: createConnector } = (await import(packagePath)) as { default: CreateConnector; diff --git a/packages/shared/package.json b/packages/shared/package.json index 764e542d6..9d5c18846 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -43,6 +43,7 @@ "@logto/schemas": "^1.0.0-beta.10", "@silverhand/essentials": "^1.3.0", "dayjs": "^1.10.5", + "find-up": "^5.0.0", "nanoid": "^3.3.4", "slonik": "^30.0.0" } diff --git a/packages/shared/src/utils/find-package.ts b/packages/shared/src/utils/find-package.ts new file mode 100644 index 000000000..1b5f92ce1 --- /dev/null +++ b/packages/shared/src/utils/find-package.ts @@ -0,0 +1,18 @@ +import path from 'path'; + +import { conditional } from '@silverhand/essentials'; +import findUp, { exists } from 'find-up'; + +const findPackage = async (cwd: string) => + findUp( + async (directory) => { + const hasPackageJson = await exists(path.join(directory, 'package.json')); + + return conditional(hasPackageJson && directory); + }, + { + cwd, + type: 'directory', + } + ); +export default findPackage; diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 116cb6c06..357301f3a 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -1 +1,2 @@ export * from './id'; +export { default as findPackage } from './find-package'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5158ce6c4..2bb9b1d99 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,7 +37,6 @@ importers: decamelize: ^5.0.0 dotenv: ^16.0.0 eslint: ^8.21.0 - find-up: ^5.0.0 fs-extra: ^10.1.0 got: ^11.8.2 hpagent: ^1.0.0 @@ -66,7 +65,6 @@ importers: chalk: 4.1.2 decamelize: 5.0.1 dotenv: 16.0.0 - find-up: 5.0.0 fs-extra: 10.1.0 got: 11.8.3 hpagent: 1.0.0 @@ -621,6 +619,7 @@ importers: '@types/jest': ^28.1.6 dayjs: ^1.10.5 eslint: ^8.21.0 + find-up: ^5.0.0 jest: ^28.1.3 lint-staged: ^13.0.0 nanoid: ^3.3.4 @@ -631,6 +630,7 @@ importers: '@logto/schemas': link:../schemas '@silverhand/essentials': 1.3.0 dayjs: 1.10.7 + find-up: 5.0.0 nanoid: 3.3.4 slonik: 30.1.2 devDependencies: @@ -4035,7 +4035,7 @@ packages: '@jest/types': 28.1.3 deepmerge: 4.2.2 identity-obj-proxy: 3.0.0 - jest: 28.1.3_@types+node@16.11.12 + jest: 28.1.3_k5ytkvaprncdyzidqqws5bqksq jest-matcher-specific-error: 1.0.0 jest-transform-stub: 2.0.0 ts-jest: 28.0.7_lhw3xkmzugq5tscs3x2ndm4sby @@ -15215,7 +15215,7 @@ packages: '@jest/types': 28.1.3 bs-logger: 0.2.6 fast-json-stable-stringify: 2.1.0 - jest: 28.1.3_@types+node@16.11.12 + jest: 28.1.3_k5ytkvaprncdyzidqqws5bqksq jest-util: 28.1.3 json5: 2.2.1 lodash.memoize: 4.1.2