0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor: allow start from project root (#630)

This commit is contained in:
Gao Sun 2022-04-24 12:12:48 +08:00 committed by GitHub
parent 96848e6b0f
commit ef60a474f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 20 deletions

View file

@ -9,7 +9,7 @@
"prepare": "if test \"$NODE_ENV\" != \"production\" && test \"$CI\" != \"true\" ; then husky install ; fi",
"prepack": "lerna run --stream prepack",
"dev": "lerna run --stream prepack && lerna --scope=@logto/{core,ui,console} exec -- pnpm dev",
"start": "cd packages/core && node ."
"start": "cd packages/core && node . --from-root"
},
"devDependencies": {
"@commitlint/cli": "^16.0.0",

View file

@ -1,7 +0,0 @@
import { appendFileSync } from 'fs';
const appendDotEnv = (key: string, value: string) => {
appendFileSync('.env', `${key}=${value}\n`);
};
export default appendDotEnv;

View file

@ -3,7 +3,7 @@ import inquirer from 'inquirer';
import { createPool } from 'slonik';
import { createInterceptors } from 'slonik-interceptor-preset';
import appendDotEnv from './append-dot-env';
import { appendDotEnv } from './dot-env';
import { noInquiry } from './parameters';
const createPoolByEnv = async (isTest: boolean) => {

View file

@ -0,0 +1,18 @@
import { appendFileSync } from 'fs';
import dotenv from 'dotenv';
import { fromRoot } from './parameters';
export const appendDotEnv = (key: string, value: string) => {
appendFileSync('.env', `${key}=${value}\n`);
};
export const configDotEnv = () => {
// Started from project root, change working directory
if (fromRoot) {
process.chdir('../..');
}
dotenv.config();
};

View file

@ -6,11 +6,11 @@ import inquirer from 'inquirer';
import { noInquiry } from './parameters';
const readPrivateKey = async (path: string): Promise<string> => {
const readPrivateKey = async (): Promise<string> => {
const privateKeyPath = getEnv('OIDC_PRIVATE_KEY_PATH', 'oidc-private-key.pem');
try {
return readFileSync(path, 'utf-8');
return readFileSync(privateKeyPath, 'utf-8');
} catch (error: unknown) {
if (noInquiry) {
throw error;
@ -44,12 +44,10 @@ const readPrivateKey = async (path: string): Promise<string> => {
};
const loadOidcValues = async (port: number) => {
const privateKeyPath = getEnv('OIDC_PRIVATE_KEY_PATH', 'oidc-private-key.pem');
const privateKey = crypto.createPrivateKey(await readPrivateKey(privateKeyPath));
const privateKey = crypto.createPrivateKey(await readPrivateKey());
const publicKey = crypto.createPublicKey(privateKey);
return Object.freeze({
privateKeyPath,
privateKey,
publicKey,
issuer: getEnv('OIDC_ISSUER', `http://localhost:${port}/oidc`),

View file

@ -1,2 +1,3 @@
const parameters = process.argv.slice(2);
export const noInquiry = parameters.includes('--no-inquiry');
const parameters = new Set(process.argv.slice(2));
export const noInquiry = parameters.has('--no-inquiry');
export const fromRoot = parameters.has('--from-root');

View file

@ -3,7 +3,7 @@ import inquirer from 'inquirer';
import { nanoid } from 'nanoid';
import { number, string } from 'zod';
import appendDotEnv from './append-dot-env';
import { appendDotEnv } from './dot-env';
import { noInquiry } from './parameters';
const loadPeppers = async (isTest: boolean): Promise<string[]> => {

View file

@ -1,9 +1,11 @@
import 'module-alias/register.js';
import dotenv from 'dotenv';
import Koa from 'koa';
dotenv.config();
// eslint-disable-next-line import/order
import { configDotEnv } from './env-set/dot-env';
configDotEnv();
/* eslint-disable import/first */
import initApp from './app/init';

View file

@ -7,6 +7,7 @@ import { IRouterParamContext } from 'koa-router';
import serveStatic from 'koa-static';
import envSet, { MountedApps } from '@/env-set';
import { fromRoot } from '@/env-set/parameters';
export default function koaSpaProxy<StateT, ContextT extends IRouterParamContext, ResponseBodyT>(
packagePath = 'ui',
@ -15,7 +16,8 @@ export default function koaSpaProxy<StateT, ContextT extends IRouterParamContext
): MiddlewareType<StateT, ContextT, ResponseBodyT> {
type Middleware = MiddlewareType<StateT, ContextT, ResponseBodyT>;
const distPath = path.join('..', packagePath, 'dist');
const packagesPath = fromRoot ? 'packages/' : '..';
const distPath = path.join(packagesPath, packagePath, 'dist');
const spaProxy: Middleware = envSet.values.isProduction
? serveStatic(distPath)