From 216fc9ed85bb1393b7f53a599db3c6f6fcccf77c Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Tue, 11 Oct 2022 22:14:09 +0800 Subject: [PATCH] refactor(core): remove parameters --- .gitpod.yml | 2 -- packages/core/.gitignore | 1 - packages/core/package.json | 1 + packages/core/src/env-set/dot-env.ts | 11 +++-------- packages/core/src/env-set/parameters.ts | 7 ------- packages/core/src/index.ts | 9 ++------- pnpm-lock.yaml | 6 ++++-- 7 files changed, 10 insertions(+), 27 deletions(-) delete mode 100644 packages/core/.gitignore diff --git a/.gitpod.yml b/.gitpod.yml index 70a454875..7d5eaf0c7 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -15,8 +15,6 @@ tasks: pnpm cli db seed pnpm lerna --ignore=@logto/integration-test run --parallel dev env: - ALL_YES: 1 - NO_INQUIRY: 0 TRUST_PROXY_HEADER: 1 DB_URL: postgres://postgres:p0stgr3s@127.0.0.1:5432 diff --git a/packages/core/.gitignore b/packages/core/.gitignore deleted file mode 100644 index 83783958b..000000000 --- a/packages/core/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/alterations diff --git a/packages/core/package.json b/packages/core/package.json index 60037212b..72fd84806 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -40,6 +40,7 @@ "deepmerge": "^4.2.2", "dotenv": "^16.0.0", "etag": "^1.8.1", + "find-up": "^5.0.0", "fs-extra": "^10.1.0", "got": "^11.8.2", "hash-wasm": "^4.9.0", diff --git a/packages/core/src/env-set/dot-env.ts b/packages/core/src/env-set/dot-env.ts index 884b7b5e6..564bc3478 100644 --- a/packages/core/src/env-set/dot-env.ts +++ b/packages/core/src/env-set/dot-env.ts @@ -1,17 +1,12 @@ import { appendFileSync } from 'fs'; import dotenv from 'dotenv'; - -import { fromRoot } from './parameters'; +import findUp from 'find-up'; export const appendDotEnv = (key: string, value: string) => { appendFileSync('.env', `${key}=${value}\n`); }; -export const configDotEnv = () => { - if (fromRoot) { - dotenv.config({ path: '../../.env' }); - } else { - dotenv.config(); - } +export const configDotEnv = async () => { + dotenv.config({ path: await findUp('.env', {}) }); }; diff --git a/packages/core/src/env-set/parameters.ts b/packages/core/src/env-set/parameters.ts index ba8c2fa99..bd7157a04 100644 --- a/packages/core/src/env-set/parameters.ts +++ b/packages/core/src/env-set/parameters.ts @@ -1,9 +1,2 @@ -import { getEnv } from '@silverhand/essentials'; - export const isTrue = (value: string) => ['1', 'true', 'y', 'yes', 'yep', 'yeah'].includes(value.toLowerCase()); - -const parameters = new Set(process.argv.slice(2)); -export const noInquiry = parameters.has('--no-inquiry') || isTrue(getEnv('NO_INQUIRY')); -export const fromRoot = parameters.has('--from-root') || isTrue(getEnv('FROM_ROOT')); -export const allYes = parameters.has('--all-yes') || isTrue(getEnv('ALL_YES')); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index db09bb3d5..fbfdc56c0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -2,22 +2,17 @@ import 'module-alias/register'; import Koa from 'koa'; -// eslint-disable-next-line import/order -import { configDotEnv } from './env-set/dot-env'; - -configDotEnv(); - -/* eslint-disable import/first */ import initApp from './app/init'; import { initConnectors } from './connectors'; import envSet from './env-set'; +import { configDotEnv } from './env-set/dot-env'; import initI18n from './i18n/init'; -/* eslint-enable import/first */ // Update after we migrate to ESM // eslint-disable-next-line unicorn/prefer-top-level-await (async () => { try { + await configDotEnv(); await envSet.load(); const app = new Koa({ proxy: envSet.values.trustProxyHeader, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2bb9b1d99..8053bc414 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -275,6 +275,7 @@ importers: dotenv: ^16.0.0 eslint: ^8.21.0 etag: ^1.8.1 + find-up: ^5.0.0 fs-extra: ^10.1.0 got: ^11.8.2 hash-wasm: ^4.9.0 @@ -331,6 +332,7 @@ importers: deepmerge: 4.2.2 dotenv: 16.0.0 etag: 1.8.1 + find-up: 5.0.0 fs-extra: 10.1.0 got: 11.8.3 hash-wasm: 4.9.0 @@ -4035,7 +4037,7 @@ packages: '@jest/types': 28.1.3 deepmerge: 4.2.2 identity-obj-proxy: 3.0.0 - jest: 28.1.3_k5ytkvaprncdyzidqqws5bqksq + jest: 28.1.3_@types+node@16.11.12 jest-matcher-specific-error: 1.0.0 jest-transform-stub: 2.0.0 ts-jest: 28.0.7_lhw3xkmzugq5tscs3x2ndm4sby @@ -15215,7 +15217,7 @@ packages: '@jest/types': 28.1.3 bs-logger: 0.2.6 fast-json-stable-stringify: 2.1.0 - jest: 28.1.3_k5ytkvaprncdyzidqqws5bqksq + jest: 28.1.3_@types+node@16.11.12 jest-util: 28.1.3 json5: 2.2.1 lodash.memoize: 4.1.2