Skip to content

Commit

Permalink
feat(@artusx/utils): simplify bootstrap and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Mar 20, 2024
1 parent 4fe3c74 commit 3fb0423
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 72 deletions.
9 changes: 3 additions & 6 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/apps/artusx-koa-bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"@artusx/utils": "workspace:*",
"cron": "^2.2.0",
"dayjs": "^1.11.7",
"dotenv": "^16.0.3",
"fs-extra": "~11.2.0"
},
"devDependencies": {
Expand Down
15 changes: 0 additions & 15 deletions packages/apps/artusx-koa-bench/src/bootstrap.ts

This file was deleted.

7 changes: 5 additions & 2 deletions packages/apps/artusx-koa-bench/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { main } from './bootstrap';
import path from 'path';
import { bootstrap } from '@artusx/utils';

main();
const ROOT_DIR = path.resolve(__dirname);

bootstrap({ root: ROOT_DIR });
7 changes: 5 additions & 2 deletions packages/apps/artusx-koa/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { bootstrap } from '@artusx/utils/bootstrap';
import path from 'path';
import { bootstrap } from '@artusx/utils';

bootstrap();
const ROOT_DIR = path.resolve(__dirname);

bootstrap({ root: ROOT_DIR });
6 changes: 3 additions & 3 deletions packages/apps/artusx-nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"build": "",
"dev": "npx nodemon src/bootstrap.ts"
"dev": "ARTUS_SERVER_ENV=development npx nodemon src/index.ts"
},
"dependencies": {
"@artus/core": "^2.x",
Expand All @@ -15,14 +15,14 @@
"@nestjs/core": "~10.3.0",
"@nestjs/platform-express": "~10.3.0",
"reflect-metadata": "^0.1.13",
"rxjs": "~7.8.1"
"rxjs": "~7.8.1",
"tslib": "~2.6.2"
},
"devDependencies": {
"@artusx/tsconfig": "workspace:*",
"@types/node": "~20.10.6",
"nodemon": "~3.0.2",
"ts-node": "~10.9.2",
"tslib": "~2.6.2",
"typescript": "~5.3.3"
}
}
9 changes: 0 additions & 9 deletions packages/apps/artusx-nest/src/bootstrap.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/apps/artusx-nest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'path';
import { bootstrap } from '@artusx/utils';

const ROOT_DIR = path.resolve(__dirname);

bootstrap({ root: ROOT_DIR });
15 changes: 10 additions & 5 deletions packages/libs/utils/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import path from 'path';
import dotenv from 'dotenv';
import { Application } from './application';

dotenv.config();

export const bootstrap = async (options?: object, configDir?: string) => {
export const bootstrap = async (options: Options) => {
const { root, configDir = 'config', ...rest } = options || {};
const app = await Application.start({
...options,
root: path.resolve(__dirname),
configDir: configDir || 'config',
...rest,
root,
configDir,
});

return app;
};

export type Options = Record<string, any> & {
root: string;
configDir?: string;
};
57 changes: 28 additions & 29 deletions packages/libs/utils/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
export const getEnv = <T>(key: string, type?: string): T => {
const value = process.env[key] || '';

let target: unknown = value;

if (type === 'boolean') {
target = Boolean(value);
}

if (type === 'number') {
target = parseInt(value);
}

return target as T;
};

export const getApiId = () => {
const api_id = process.env.API_ID;
const apiID = getEnv<Number>('API_ID', 'number');

if (!api_id) {
if (!apiID) {
return;
}

return parseInt(api_id);
return apiID;
};

export const getProxy = () => {
const proxy_ip = process.env.PROXY_IP;
const proxy_port = process.env.PROXY_PORT;
const proxy_socket_type = process.env.PROXY_SOCKET_TYPE;
const ip = getEnv<string>('PROXY_IP');
const port = getEnv<Number>('PROXY_PORT', 'number');
const socksType = getEnv<Number>('PROXY_SOCKET_TYPE', 'number');

if (!proxy_ip || !proxy_port || !proxy_socket_type) {
if (!ip || !port || !socksType) {
return;
}

const proxy_protocol = proxy_socket_type === '5' ? 'socks5' : 'socks4';

const proxyString = `${proxy_protocol}://${proxy_ip}:${proxy_port}`;
const protocol = socksType === 5 ? 'socks5' : 'socks4';
const proxyString = `${protocol}://${ip}:${port}`;

return {
ip: proxy_ip,
port: parseInt(proxy_port),
socksType: parseInt(proxy_socket_type),
ip,
port,
socksType,
proxyString,
};
};

export const getEnv = <T>(key: string, type?: string): T => {
const value = process.env[key] || '';

let target: unknown = value;

if (type === 'boolean') {
target = Boolean(value);
}

if (type === 'number') {
target = parseInt(value);
}

return target as T;
};

0 comments on commit 3fb0423

Please sign in to comment.