@lark-apaas/fullstack-cli 0.1.0-alpha.4 → 0.1.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,9 @@
1
1
  import path from 'node:path';
2
2
  import fs from 'node:fs';
3
+ import { fileURLToPath } from 'node:url';
3
4
  import { spawnSync } from 'node:child_process';
5
+ // 加载 .env 配置
6
+ import { config as loadEnv } from 'dotenv';
4
7
  /**
5
8
  * 生成数据库 schema
6
9
  *
@@ -11,9 +14,16 @@ import { spawnSync } from 'node:child_process';
11
14
  * - DRIZZLE_TABLES_FILTER: 表过滤器,逗号分隔,默认 '*'
12
15
  */
13
16
  export async function run() {
17
+ // 加载用户项目的 .env 文件
18
+ const envPath = path.resolve(process.cwd(), '.env');
19
+ if (fs.existsSync(envPath)) {
20
+ loadEnv({ path: envPath });
21
+ console.log('[gen-db-schema] ✓ Loaded .env file');
22
+ }
14
23
  const databaseUrl = process.env.SUDA_DATABASE_URL;
15
24
  if (!databaseUrl) {
16
25
  console.error('[gen-db-schema] Error: SUDA_DATABASE_URL environment variable is required');
26
+ console.error('[gen-db-schema] Please set it in .env file or pass it as environment variable');
17
27
  process.exit(1);
18
28
  }
19
29
  const outputPath = process.env.DB_SCHEMA_OUTPUT || 'server/database/schema.ts';
@@ -21,6 +31,9 @@ export async function run() {
21
31
  const SCHEMA_FILE = path.resolve(process.cwd(), outputPath);
22
32
  console.log('[gen-db-schema] Starting...');
23
33
  console.log(`[gen-db-schema] Output: ${outputPath}`);
34
+ // 获取当前文件所在目录(ESM 方式)
35
+ const __filename = fileURLToPath(import.meta.url);
36
+ const __dirname = path.dirname(__filename);
24
37
  // 使用 CLI 内部的 drizzle 配置
25
38
  const cliRoot = path.resolve(__dirname, '../..');
26
39
  const configPath = path.join(cliRoot, 'templates', 'drizzle.config.ts');
package/package.json CHANGED
@@ -1,12 +1,10 @@
1
1
  {
2
2
  "name": "@lark-apaas/fullstack-cli",
3
- "version": "0.1.0-alpha.4",
3
+ "version": "0.1.0-alpha.6",
4
4
  "description": "CLI tool for fullstack template management",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "bin": {
8
- "fullstack-cli": "./bin/cli.js"
9
- },
7
+ "bin": "./bin/cli.js",
10
8
  "files": [
11
9
  "dist",
12
10
  "templates",
@@ -31,11 +29,10 @@
31
29
  "access": "public"
32
30
  },
33
31
  "dependencies": {
34
- "@lark-apaas/devtool-kits": "^1.0.0"
32
+ "@lark-apaas/devtool-kits": "^1.0.0",
33
+ "dotenv": "^16.0.0"
35
34
  },
36
35
  "peerDependencies": {
37
- "@nestjs/core": "^10.0.0",
38
- "@lark-apaas/fullstack-nestjs-core": "^1.0.0",
39
36
  "drizzle-kit": "^0.20.0"
40
37
  },
41
38
  "peerDependenciesMeta": {
@@ -1,48 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { spawnSync } from 'node:child_process';
4
- import { postprocessDrizzleSchema } from '@lark-apaas/devtool-kits';
5
-
6
- const OUT_DIR = path.resolve(process.cwd(), 'server/database/.introspect');
7
- const SCHEMA_FILE = path.resolve(process.cwd(), 'server/database/schema.ts');
8
-
9
- function run(): void {
10
- const args = process.argv.slice(2);
11
- const spawnArgs = ['--yes', 'drizzle-kit', 'introspect', '--config', 'drizzle.config.ts', ...args];
12
-
13
- const result = spawnSync('npx', spawnArgs, { stdio: 'inherit' });
14
- if (result.error) {
15
- console.error('[gen-db-schema] Execution failed:', result.error);
16
- process.exit(result.status ?? 1);
17
- }
18
-
19
- if ((result.status ?? 0) !== 0) {
20
- process.exit(result.status ?? 1);
21
- }
22
-
23
- const generatedSchema = path.join(OUT_DIR, 'schema.ts');
24
- if (!fs.existsSync(generatedSchema)) {
25
- console.warn('[gen-db-schema] schema.ts not generated');
26
- process.exit(1);
27
- }
28
-
29
- fs.mkdirSync(path.dirname(SCHEMA_FILE), { recursive: true });
30
- fs.copyFileSync(generatedSchema, SCHEMA_FILE);
31
-
32
- try {
33
- const stats = postprocessDrizzleSchema(SCHEMA_FILE);
34
- if (stats?.unmatchedUnknown?.length) {
35
- console.warn('[gen-db-schema] Unmatched custom types detected');
36
- }
37
- } catch (error) {
38
- console.warn('[gen-db-schema] postprocess failed:', error);
39
- }
40
-
41
- if (fs.existsSync(OUT_DIR)) {
42
- fs.rmSync(OUT_DIR, { recursive: true, force: true });
43
- }
44
- }
45
-
46
- if (require.main === module) {
47
- run();
48
- }