data-preheating-astro 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -16,13 +16,14 @@
16
16
 
17
17
  所有环境变量均可不设置,项目会使用默认值;但当存在启用 i18n 的模块时,建议明确设置语言列表。
18
18
 
19
- - `EXPORT_BASE_URL`:Strapi 基础地址,例如 `https://example.com`
19
+ - `PUBLIC_API_URL`:Strapi 基础地址,例如 `https://example.com`
20
20
  - `EXPORT_API_TOKEN`:可选,若接口需要鉴权则使用 `Authorization: Bearer ...`
21
21
  - `EXPORT_MAX_CONCURRENCY`:最大并发数,默认 `50`
22
22
  - `EXPORT_FAIL_STRATEGY`:失败策略,默认 `fail_fast`;可选 `continue`
23
23
  - `EXPORT_LANGS`:多语言列表,例如 `en,zh-Hans`(仅在模块启用 i18n 时使用)
24
24
  - `EXPORT_DEFAULT_LANG`:默认语言,默认 `en`
25
25
  - `EXPORT_OUTPUT_DIR`:输出目录,默认 `data`
26
+ - `ITALKIN_API`:Italkin 基础地址,例如 `https://example.com`(用于前置表单接口)
26
27
 
27
28
  ## 运行
28
29
 
@@ -34,6 +35,14 @@ npm run build
34
35
  npm run start
35
36
  ```
36
37
 
38
+ ## 在其它项目中使用(作为 CLI 依赖)
39
+
40
+ 当本工具作为依赖安装到其它项目中并通过命令 `strapi-content-exporter` 执行时:
41
+
42
+ - 运行时会以“目标项目根目录”为当前工作目录(`process.cwd()`)。
43
+ - 会自动尝试读取目标项目根目录下的 `.env` 并注入到环境变量(仅补齐未设置的变量,不覆盖已有值)。
44
+ - 因此建议把 `PUBLIC_API_URL` 等配置写在“目标项目根目录”的 `.env` 中。
45
+
37
46
  ## 模块配置
38
47
 
39
48
  在 `src/config/modules/` 下为每个 Strapi 接口创建一个模块配置文件,并在 `src/config/app.config.ts` 中引入。
package/dist/cli.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  * - 纯工具函数(如 slug/url_slug 规范化):放在 `src/transform/`
18
18
  * - 本文件只做导出流程编排与串联,尽量避免写具体业务协议细节
19
19
  */
20
+ const node_fs_1 = __importDefault(require("node:fs"));
20
21
  const node_path_1 = __importDefault(require("node:path"));
21
22
  const app_config_1 = require("./config/app.config");
22
23
  const keywords_1 = require("./config/keywords");
@@ -53,7 +54,16 @@ let httpTrace = null;
53
54
  * - 只在“到达阈值”时打印,避免分页过多导致刷屏。
54
55
  */
55
56
  async function main() {
56
- const cfg = (0, app_config_1.loadAppConfig)();
57
+ loadDotEnvFromCwd();
58
+ let cfg;
59
+ try {
60
+ cfg = (0, app_config_1.loadAppConfig)();
61
+ }
62
+ catch (err) {
63
+ const msg = err instanceof Error ? err.message : String(err);
64
+ console.error(`配置错误:${msg}`);
65
+ throw err;
66
+ }
57
67
  // 一键监控开关:EXPORT_MONITOR=0 时关闭过程打印/HTTP trace/统计 CSV。
58
68
  const monitor = (0, progress_1.createProgressMonitor)({ enabled: cfg.monitorEnabled });
59
69
  const workDirAbs = process.cwd();
@@ -449,6 +459,50 @@ async function fetchPage(combo, start, limit, cfg) {
449
459
  }
450
460
  return { items, total };
451
461
  }
462
+ /**
463
+ * 从当前工作目录读取 .env 并注入到 process.env(最小实现)。
464
+ *
465
+ * 说明:
466
+ * - 只处理 KEY=VALUE 形式;忽略空行与以 # 开头的注释行
467
+ * - 支持 `export KEY=VALUE` 形式(兼容部分 shell 风格)
468
+ * - 不覆盖已存在的环境变量(保持调用方显式传参优先)
469
+ */
470
+ function loadDotEnvFromCwd() {
471
+ const envPath = node_path_1.default.join(process.cwd(), ".env");
472
+ try {
473
+ if (!node_fs_1.default.existsSync(envPath))
474
+ return;
475
+ const text = node_fs_1.default.readFileSync(envPath, "utf8");
476
+ const lines = text.split(/\r?\n/);
477
+ for (const raw of lines) {
478
+ const line = raw.trim();
479
+ if (!line)
480
+ continue;
481
+ if (line.startsWith("#"))
482
+ continue;
483
+ const noExport = line.startsWith("export ") ? line.slice("export ".length).trim() : line;
484
+ const idx = noExport.indexOf("=");
485
+ if (idx <= 0)
486
+ continue;
487
+ const key = noExport.slice(0, idx).trim();
488
+ if (!key)
489
+ continue;
490
+ // 不覆盖调用方已设置的值
491
+ if (typeof process.env[key] !== "undefined")
492
+ continue;
493
+ let value = noExport.slice(idx + 1).trim();
494
+ // 去掉包裹引号(最小处理,保持简单)
495
+ if ((value.startsWith('"') && value.endsWith('"') && value.length >= 2) ||
496
+ (value.startsWith("'") && value.endsWith("'") && value.length >= 2)) {
497
+ value = value.slice(1, -1);
498
+ }
499
+ process.env[key] = value;
500
+ }
501
+ }
502
+ catch {
503
+ // 读取 .env 失败不应阻断主流程:保持行为简单、可预测
504
+ }
505
+ }
452
506
  main()
453
507
  .then(() => {
454
508
  // 成功结束已由 monitor.done() 打印,这里无需重复输出。
@@ -100,9 +100,9 @@ function loadModulesAuto() {
100
100
  * 读取环境变量并构建配置。
101
101
  */
102
102
  function loadAppConfig() {
103
- const baseUrl = (process.env.EXPORT_BASE_URL || "").trim();
103
+ const baseUrl = (process.env.PUBLIC_API_URL || "").trim();
104
104
  if (!baseUrl) {
105
- throw new Error("缺少 EXPORT_BASE_URL(Strapi 基础地址)");
105
+ throw new Error("缺少 PUBLIC_API_URL(Strapi 基础地址)");
106
106
  }
107
107
  const apiToken = (process.env.EXPORT_API_TOKEN || "").trim() || undefined;
108
108
  // 监控开关:默认开启;设置 EXPORT_MONITOR=0 可一键关闭监控相关输出与落盘日志。
@@ -28,10 +28,10 @@ const output_1 = require("../write/output");
28
28
  * - 缺少 companyId/siteId(companies/websites 落盘缺失)属于依赖错误:必须抛错阻断
29
29
  */
30
30
  async function fetchAndWriteItalkinForm(params) {
31
- const baseUrl = (process.env.EXPORT_ITALKIN_API || process.env.ITALKIN_API || "").trim();
31
+ const baseUrl = (process.env.ITALKIN_API || "").trim();
32
32
  // 默认启用:严格校验环境变量,缺失应阻断(构建期关键数据)。
33
33
  if (!baseUrl) {
34
- throw new Error("缺少 Italkin 接口地址:请设置 EXPORT_ITALKIN_API 或 ITALKIN_API");
34
+ throw new Error("缺少 Italkin 接口地址:请设置 ITALKIN_API");
35
35
  }
36
36
  const derived = await resolveItalkinCompanyAndSiteId({ outputDirAbs: params.outputDirAbs });
37
37
  if (!derived.companyId || !derived.siteId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-preheating-astro",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "description": "从 Strapi 并发拉取数据并落盘到 data 目录。",
6
6
  "license": "UNLICENSED",