From e385b60418c4c4b5b0316073b350b36460d43fc5 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Sun, 9 Oct 2022 16:29:53 +0800 Subject: [PATCH] refactor(cli): use cli project root as alteration scripts directory (#2075) --- packages/cli/package.json | 1 + .../cli/src/commands/database/alteration.ts | 32 ++++++++++++++++--- pnpm-lock.yaml | 8 ++--- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 3ff2ba1a4..5caa4f503 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -40,6 +40,7 @@ "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 2e34892a7..1b8f5bf80 100644 --- a/packages/cli/src/commands/database/alteration.ts +++ b/packages/cli/src/commands/database/alteration.ts @@ -1,8 +1,9 @@ import path from 'path'; import { AlterationScript } from '@logto/schemas/lib/types/alteration'; -import { conditionalString } from '@silverhand/essentials'; +import { conditional, 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'; @@ -38,9 +39,32 @@ type AlterationFile = { path: string; filename: string }; export const getAlterationFiles = async (): Promise => { const alterationDirectory = getPathInModule('@logto/schemas', 'alterations'); - // Until we migrate to ESM - // eslint-disable-next-line unicorn/prefer-module - const localAlterationDirectory = path.resolve(__dirname, './alteration-scripts'); + + /** + * We copy all alteration scripts to the CLI package root directory, + * 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 localAlterationDirectory = path.resolve( + // Until we migrate to ESM + // eslint-disable-next-line unicorn/prefer-module + packageDirectory ?? __dirname, + 'alteration-scripts' + ); if (!existsSync(alterationDirectory)) { return []; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 22d7c22bf..c2259e103 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,6 +37,7 @@ 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 @@ -63,6 +64,7 @@ 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 @@ -7666,7 +7668,6 @@ packages: dependencies: locate-path: 6.0.0 path-exists: 4.0.0 - dev: true /flat-cache/3.0.4: resolution: {integrity: sha512-dm9s5Pw7Jc0GvMYbshN6zchCA9RgQlzzEZX3vylR9IqFfS8XciblUXOKfW6SiuJ0e13eDYZoZV5wdrev7P3Nwg==} @@ -10526,7 +10527,6 @@ packages: engines: {node: '>=10'} dependencies: p-locate: 5.0.0 - dev: true /lodash._reinterpolate/3.0.0: resolution: {integrity: sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=} @@ -12173,7 +12173,6 @@ packages: engines: {node: '>=10'} dependencies: yocto-queue: 0.1.0 - dev: true /p-locate/2.0.0: resolution: {integrity: sha512-nQja7m7gSKuewoVRen45CtVfODR3crN3goVQ0DDZ9N3yHxgpkuBhZqsaiotSQRrADUrne346peY7kT3TSACykg==} @@ -12194,7 +12193,6 @@ packages: engines: {node: '>=10'} dependencies: p-limit: 3.1.0 - dev: true /p-map-series/2.1.0: resolution: {integrity: sha512-RpYIIK1zXSNEOdwxcfe7FdvGcs7+y5n8rifMhMNWvaxRNMPINJHF5GDeuVxWqnfrcHPSCnp7Oo5yNXHId9Av2Q==} @@ -12459,7 +12457,6 @@ packages: /path-exists/4.0.0: resolution: {integrity: sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==} engines: {node: '>=8'} - dev: true /path-is-absolute/1.0.1: resolution: {integrity: sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==} @@ -16065,7 +16062,6 @@ packages: /yocto-queue/0.1.0: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} - dev: true /zod/3.18.0: resolution: {integrity: sha512-gwTm8RfUCe8l9rDwN5r2A17DkAa8Ez4Yl4yXqc5VqeGaXaJahzYYXbTwvhroZi0SNBqTwh/bKm2N0mpCzuw4bA==}