@nocobase/cli 2.0.0-beta.7 → 2.0.0-beta.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/cli",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.0-beta.9",
4
4
  "description": "",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./src/index.js",
@@ -8,7 +8,7 @@
8
8
  "nocobase": "./bin/index.js"
9
9
  },
10
10
  "dependencies": {
11
- "@nocobase/app": "2.0.0-beta.7",
11
+ "@nocobase/app": "2.0.0-beta.9",
12
12
  "@nocobase/license-kit": "^0.3.5",
13
13
  "@types/fs-extra": "^11.0.1",
14
14
  "@umijs/utils": "3.5.20",
@@ -27,12 +27,12 @@
27
27
  "tsx": "^4.19.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@nocobase/devtools": "2.0.0-beta.7"
30
+ "@nocobase/devtools": "2.0.0-beta.9"
31
31
  },
32
32
  "repository": {
33
33
  "type": "git",
34
34
  "url": "git+https://github.com/nocobase/nocobase.git",
35
35
  "directory": "packages/core/cli"
36
36
  },
37
- "gitHead": "17b6e4042a4814e38b256c7b3ae34e6d0b08fd57"
37
+ "gitHead": "36ef50291e22fe1024c819b8748d51a59a5ea4f8"
38
38
  }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ const chalk = require('chalk');
11
+ const { Command } = require('commander');
12
+ const fs = require('fs-extra');
13
+ const { resolve } = require('path');
14
+
15
+ /**
16
+ * 复制主应用客户端文件
17
+ * @param {string} source - 源目录路径
18
+ * @param {string} target - 目标目录路径
19
+ */
20
+ async function copyMainClient(source, target) {
21
+ if (!(await fs.exists(source))) {
22
+ console.log(chalk.yellow(`Source directory does not exist: ${source}`));
23
+ return;
24
+ }
25
+ // 确保目标目录存在且为空
26
+ await fs.ensureDir(target);
27
+ await fs.emptyDir(target);
28
+ await fs.copy(source, target, { recursive: true });
29
+ console.log(chalk.green(`Copied main client files from ${source} to ${target}`));
30
+ }
31
+
32
+ /**
33
+ * 复制插件客户端文件
34
+ * @param {string} pluginsBaseDir - 插件基础目录路径
35
+ * @param {string} namespace - 命名空间(如 '@nocobase' 或 '@nocobase-example')
36
+ * @param {string} target - 目标目录
37
+ */
38
+ async function copyPluginClients(pluginsBaseDir, namespace, target) {
39
+ const pluginsDir = resolve(process.cwd(), pluginsBaseDir, namespace);
40
+ if (await fs.exists(pluginsDir)) {
41
+ const pluginNames = await fs.readdir(pluginsDir);
42
+ for (const pluginName of pluginNames) {
43
+ const pluginPath = resolve(pluginsDir, pluginName);
44
+ const pluginDistClient = resolve(pluginPath, 'dist/client');
45
+ if (await fs.exists(pluginDistClient)) {
46
+ const pluginTarget = resolve(target, 'static/plugins', namespace, pluginName, 'dist/client');
47
+ await fs.mkdir(resolve(pluginTarget, '..'), { recursive: true });
48
+ await fs.copy(pluginDistClient, pluginTarget, { recursive: true });
49
+ console.log(chalk.green(`Copied ${namespace}/${pluginName} client files`));
50
+ }
51
+ }
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 递归上传目录到 OSS
57
+ * @param {Client} client - OSS 客户端实例
58
+ * @param {string} localDir - 本地目录路径
59
+ * @param {string} ossPrefix - OSS 对象前缀(目录路径)
60
+ */
61
+ async function uploadDirectoryToOSS(client, localDir, ossPrefix = '') {
62
+ if (!(await fs.exists(localDir))) {
63
+ console.log(chalk.yellow(`Directory does not exist: ${localDir}`));
64
+ return;
65
+ }
66
+
67
+ const stats = await fs.stat(localDir);
68
+ if (!stats.isDirectory()) {
69
+ throw new Error(`${localDir} is not a directory`);
70
+ }
71
+
72
+ const files = await fs.readdir(localDir);
73
+ let uploadedCount = 0;
74
+
75
+ for (const file of files) {
76
+ const filePath = resolve(localDir, file);
77
+ const fileStats = await fs.stat(filePath);
78
+
79
+ if (fileStats.isDirectory()) {
80
+ // 递归处理子目录
81
+ const subOssPrefix = ossPrefix ? `${ossPrefix}/${file}` : file;
82
+ const subCount = await uploadDirectoryToOSS(client, filePath, subOssPrefix);
83
+ uploadedCount += subCount;
84
+ } else {
85
+ // 上传文件
86
+ const ossKey = ossPrefix ? `${ossPrefix}/${file}` : file;
87
+ try {
88
+ await client.put(ossKey, filePath);
89
+ // console.log(chalk.green(`Uploaded: ${ossKey}`));
90
+ uploadedCount++;
91
+ } catch (error) {
92
+ console.error(chalk.red(`Failed to upload ${ossKey}:`), error.message);
93
+ throw error;
94
+ }
95
+ }
96
+ }
97
+
98
+ return uploadedCount;
99
+ }
100
+
101
+ /**
102
+ *
103
+ * @param {Command} cli
104
+ */
105
+ module.exports = (cli) => {
106
+ cli
107
+ .command('client:extract')
108
+ .allowUnknownOption()
109
+ .action(async () => {
110
+ const version = require('../../package.json').version;
111
+ const target = resolve(process.cwd(), 'storage/dist-client', version);
112
+ const mainClientSource = resolve(process.cwd(), 'node_modules/@nocobase/app/dist/client');
113
+ await copyMainClient(mainClientSource, target);
114
+ await copyPluginClients('packages/plugins', '@nocobase', target);
115
+ await copyPluginClients('packages/plugins', '@nocobase-example', target);
116
+ await copyPluginClients('packages/pro-plugins', '@nocobase', target);
117
+ });
118
+
119
+ cli
120
+ .command('client:upload')
121
+ .allowUnknownOption()
122
+ .action(async () => {
123
+ const version = require('../../package.json').version;
124
+ const target = resolve(process.cwd(), 'storage/dist-client', version);
125
+
126
+ // 检查必要的环境变量
127
+ if (
128
+ !process.env.CDN_ALI_OSS_ACCESS_KEY_ID ||
129
+ !process.env.CDN_ALI_OSS_ACCESS_KEY_SECRET ||
130
+ !process.env.CDN_ALI_OSS_BUCKET ||
131
+ !process.env.CDN_ALI_OSS_REGION
132
+ ) {
133
+ console.error(
134
+ chalk.red(
135
+ 'Missing required environment variables: CDN_ALI_OSS_ACCESS_KEY_ID, CDN_ALI_OSS_ACCESS_KEY_SECRET, CDN_ALI_OSS_BUCKET, CDN_ALI_OSS_REGION',
136
+ ),
137
+ );
138
+ process.exit(1);
139
+ }
140
+
141
+ const Client = require('ali-oss');
142
+
143
+ const client = new Client({
144
+ accessKeyId: process.env.CDN_ALI_OSS_ACCESS_KEY_ID,
145
+ accessKeySecret: process.env.CDN_ALI_OSS_ACCESS_KEY_SECRET,
146
+ bucket: process.env.CDN_ALI_OSS_BUCKET,
147
+ region: process.env.CDN_ALI_OSS_REGION,
148
+ });
149
+
150
+ if (!(await fs.exists(target))) {
151
+ console.error(chalk.red(`Target directory does not exist: ${target}`));
152
+ console.log(chalk.yellow('Please run "client:extract" first to generate the client files.'));
153
+ process.exit(1);
154
+ }
155
+
156
+ console.log(chalk.blue(`Uploading directory ${target} to OSS...`));
157
+ const ossPrefix = `${version}`;
158
+ try {
159
+ const uploadedCount = await uploadDirectoryToOSS(client, target, ossPrefix);
160
+ console.log(chalk.green(`Successfully uploaded ${uploadedCount} files to OSS`));
161
+ } catch (error) {
162
+ console.error(chalk.red('Upload failed:'), error);
163
+ process.exit(1);
164
+ }
165
+ });
166
+ };
@@ -38,6 +38,7 @@ module.exports = (cli) => {
38
38
  require('./pkg')(cli);
39
39
  require('./instance-id')(cli);
40
40
  require('./view-license-key')(cli);
41
+ require('./client')(cli);
41
42
  if (isPackageValid('@umijs/utils')) {
42
43
  require('./create-plugin')(cli);
43
44
  }
package/src/util.js CHANGED
@@ -295,7 +295,8 @@ function buildIndexHtml(force = false) {
295
295
  fs.copyFileSync(file, tpl);
296
296
  }
297
297
  const data = fs.readFileSync(tpl, 'utf-8');
298
- const replacedData = data
298
+ let replacedData = data
299
+ .replace(/\{\{env.CDN_BASE_URL\}\}/g, process.env.CDN_BASE_URL)
299
300
  .replace(/\{\{env.APP_PUBLIC_PATH\}\}/g, process.env.APP_PUBLIC_PATH)
300
301
  .replace(/\{\{env.API_CLIENT_SHARE_TOKEN\}\}/g, process.env.API_CLIENT_SHARE_TOKEN || 'false')
301
302
  .replace(/\{\{env.API_CLIENT_STORAGE_TYPE\}\}/g, process.env.API_CLIENT_STORAGE_TYPE)
@@ -304,6 +305,14 @@ function buildIndexHtml(force = false) {
304
305
  .replace(/\{\{env.WS_URL\}\}/g, process.env.WEBSOCKET_URL || '')
305
306
  .replace(/\{\{env.WS_PATH\}\}/g, process.env.WS_PATH)
306
307
  .replace('src="/umi.', `src="${process.env.APP_PUBLIC_PATH}umi.`);
308
+
309
+ if (process.env.CDN_BASE_URL) {
310
+ const appBaseUrl = process.env.CDN_BASE_URL.replace(/\/+$/, '');
311
+ const appPublicPath = process.env.APP_PUBLIC_PATH.replace(/\/+$/, '');
312
+ const re1 = new RegExp(`src="${appPublicPath}/`, 'g');
313
+ const re2 = new RegExp(`href="${appPublicPath}/`, 'g');
314
+ replacedData = replacedData.replace(re1, `src="${appBaseUrl}/`).replace(re2, `href="${appBaseUrl}/`);
315
+ }
307
316
  fs.writeFileSync(file, replacedData, 'utf-8');
308
317
  }
309
318
 
@@ -385,6 +394,8 @@ exports.initEnv = function initEnv() {
385
394
  PLUGIN_STATICS_PATH: '/static/plugins/',
386
395
  LOGGER_BASE_PATH: 'storage/logs',
387
396
  APP_SERVER_BASE_URL: '',
397
+ APP_BASE_URL: '',
398
+ CDN_BASE_URL: '',
388
399
  APP_PUBLIC_PATH: '/',
389
400
  WATCH_FILE: resolve(process.cwd(), 'storage/app.watch.ts'),
390
401
  };
@@ -440,6 +451,16 @@ exports.initEnv = function initEnv() {
440
451
  process.env.__env_modified__ = true;
441
452
  }
442
453
 
454
+ if (!process.env.CDN_BASE_URL && process.env.APP_PUBLIC_PATH !== '/') {
455
+ process.env.CDN_BASE_URL = process.env.APP_PUBLIC_PATH;
456
+ }
457
+
458
+ if (process.env.CDN_BASE_URL.includes('http') && process.env.CDN_VERSION === 'auto') {
459
+ const version = require('../package.json').version;
460
+ process.env.CDN_BASE_URL = process.env.CDN_BASE_URL.replace(/\/+$/, '') + '/' + version + '/';
461
+ process.env.CDN_VERSION = '';
462
+ }
463
+
443
464
  if (!process.env.TZ) {
444
465
  const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
445
466
  process.env.TZ = getTimezonesByOffset(process.env.DB_TIMEZONE || timeZone);