create-szyy-app 1.0.4 → 1.0.5
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 +7 -7
- package/dist/index.js +88 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ pnpm build:prod # 生产构建
|
|
|
49
49
|
|
|
50
50
|
## 版本锁定
|
|
51
51
|
|
|
52
|
-
版本配置默认从模板仓 **`create-app` 分支**
|
|
52
|
+
版本配置默认从模板仓 **`create-app` 分支** 拉取 `versions.json`(内网 Git 走 `git clone`,公网 GitLab 等先尝试 Raw URL);拉取失败时使用 npm 包内置配置兜底。更新模板 tag 或依赖版本时,只需修改模板仓 `create-app` 分支上的 `versions.json`,**无需重新发布 CLI**。
|
|
53
53
|
|
|
54
54
|
生成项目的 `package.json` 会写死精确版本号(无 `^`)。
|
|
55
55
|
|
|
@@ -83,12 +83,12 @@ pnpm publish:pkg --tag=beta
|
|
|
83
83
|
|
|
84
84
|
## 故障排查
|
|
85
85
|
|
|
86
|
-
| 问题 | 处理
|
|
87
|
-
| ---------------- |
|
|
88
|
-
| degit 拉取失败 | 检查内网 Git 连通性,或设置 `SZYY_TEMPLATE_PATH` 使用本地模板
|
|
89
|
-
| 占位符残留报错 | 确认模板 tag 与远程/本地 `versions.json` 中 `template` 版本一致
|
|
90
|
-
| 版本配置拉取失败 |
|
|
91
|
-
| 联调端口冲突 | 创建时指定 `--port`,或修改生成项目 `.env.micro`
|
|
86
|
+
| 问题 | 处理 |
|
|
87
|
+
| ---------------- | ------------------------------------------------------------------------------------------ |
|
|
88
|
+
| degit 拉取失败 | 检查内网 Git 连通性,或设置 `SZYY_TEMPLATE_PATH` 使用本地模板 |
|
|
89
|
+
| 占位符残留报错 | 确认模板 tag 与远程/本地 `versions.json` 中 `template` 版本一致 |
|
|
90
|
+
| 版本配置拉取失败 | 确认模板仓 `create-app` 分支存在 `versions.json` 且本机 Git 已登录;CLI 会自动回退内置配置 |
|
|
91
|
+
| 联调端口冲突 | 创建时指定 `--port`,或修改生成项目 `.env.micro` |
|
|
92
92
|
|
|
93
93
|
## 延伸阅读
|
|
94
94
|
|
package/dist/index.js
CHANGED
|
@@ -355,8 +355,37 @@ function cleanupGeneratedGitignore(targetDir) {
|
|
|
355
355
|
import fs2 from "fs";
|
|
356
356
|
import path3 from "path";
|
|
357
357
|
import os from "os";
|
|
358
|
-
import { spawnSync } from "child_process";
|
|
359
358
|
import degit from "degit";
|
|
359
|
+
|
|
360
|
+
// src/git.ts
|
|
361
|
+
import { spawnSync } from "child_process";
|
|
362
|
+
var PUBLIC_GIT_HOSTS = /* @__PURE__ */ new Set(["github.com", "gitlab.com", "bitbucket.org", "git.sr.ht"]);
|
|
363
|
+
function shouldUseGitCloneDirectly(source) {
|
|
364
|
+
if (source.startsWith("git@") || source.startsWith("file://")) return true;
|
|
365
|
+
if (source.startsWith(".") || source.startsWith("/") || /^[A-Za-z]:[\\/]/.test(source)) return true;
|
|
366
|
+
try {
|
|
367
|
+
const url = new URL(source);
|
|
368
|
+
return !PUBLIC_GIT_HOSTS.has(url.hostname.toLowerCase());
|
|
369
|
+
} catch {
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function shallowCloneBranch(repo, branch, targetDir) {
|
|
374
|
+
const result = spawnSync("git", ["clone", "--depth", "1", "--branch", branch, repo, targetDir], {
|
|
375
|
+
encoding: "utf8"
|
|
376
|
+
});
|
|
377
|
+
if (result.status === 0) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const errorOutput = result.stderr?.trim() || result.stdout?.trim() || "git clone \u6267\u884C\u5931\u8D25";
|
|
381
|
+
throw new Error(`Git \u62C9\u53D6\u5931\u8D25: ${errorOutput}`);
|
|
382
|
+
}
|
|
383
|
+
function shouldFallbackToGitClone(error) {
|
|
384
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
385
|
+
return message.includes("degit supports GitHub, GitLab, Sourcehut and BitBucket");
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// src/template.ts
|
|
360
389
|
async function fetchTemplate(targetDir, versions) {
|
|
361
390
|
const localPath = process.env.SZYY_TEMPLATE_PATH;
|
|
362
391
|
if (localPath) {
|
|
@@ -385,34 +414,15 @@ async function fetchTemplate(targetDir, versions) {
|
|
|
385
414
|
cloneWithGit(gitBase, versions.template, targetDir);
|
|
386
415
|
}
|
|
387
416
|
}
|
|
388
|
-
var DEGIT_HOSTS = /* @__PURE__ */ new Set(["github.com", "gitlab.com", "bitbucket.org", "git.sr.ht"]);
|
|
389
|
-
function shouldUseGitCloneDirectly(source) {
|
|
390
|
-
if (source.startsWith("git@") || source.startsWith("file://")) return true;
|
|
391
|
-
if (source.startsWith(".") || source.startsWith("/") || /^[A-Za-z]:[\\/]/.test(source)) return true;
|
|
392
|
-
try {
|
|
393
|
-
const url = new URL(source);
|
|
394
|
-
return !DEGIT_HOSTS.has(url.hostname.toLowerCase());
|
|
395
|
-
} catch {
|
|
396
|
-
return true;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
function shouldFallbackToGitClone(error) {
|
|
400
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
401
|
-
return message.includes("degit supports GitHub, GitLab, Sourcehut and BitBucket");
|
|
402
|
-
}
|
|
403
417
|
function cloneWithGit(repo, ref, targetDir) {
|
|
404
418
|
const tempDir = fs2.mkdtempSync(path3.join(os.tmpdir(), "create-szyy-app-"));
|
|
405
419
|
try {
|
|
406
420
|
const cloneDir = path3.join(tempDir, "template");
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
const errorOutput = result.stderr?.trim() || result.stdout?.trim() || "git clone \u6267\u884C\u5931\u8D25";
|
|
415
|
-
throw new Error(`\u6A21\u677F\u62C9\u53D6\u5931\u8D25: ${errorOutput}`);
|
|
421
|
+
shallowCloneBranch(repo, ref, cloneDir);
|
|
422
|
+
copyRecursive(cloneDir, targetDir);
|
|
423
|
+
} catch (error) {
|
|
424
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
425
|
+
throw new Error(`\u6A21\u677F\u62C9\u53D6\u5931\u8D25: ${message}`);
|
|
416
426
|
} finally {
|
|
417
427
|
fs2.rmSync(tempDir, { recursive: true, force: true });
|
|
418
428
|
}
|
|
@@ -453,6 +463,7 @@ function removeExcludedTemplateEntries(targetDir) {
|
|
|
453
463
|
|
|
454
464
|
// src/versions.ts
|
|
455
465
|
import fs3 from "fs";
|
|
466
|
+
import os2 from "os";
|
|
456
467
|
import path4 from "path";
|
|
457
468
|
import { fileURLToPath } from "url";
|
|
458
469
|
var __filename2 = fileURLToPath(import.meta.url);
|
|
@@ -496,6 +507,9 @@ function getRepoBase(bundled) {
|
|
|
496
507
|
function getDefaultRemoteVersionsUrl(bundled) {
|
|
497
508
|
return `${getRepoBase(bundled)}/-/raw/${CONFIG_BRANCH}/versions.json`;
|
|
498
509
|
}
|
|
510
|
+
function getGitVersionsLabel(repoBase) {
|
|
511
|
+
return `${repoBase}#${CONFIG_BRANCH}:versions.json`;
|
|
512
|
+
}
|
|
499
513
|
async function fetchVersionsFromUrl(url) {
|
|
500
514
|
const response = await fetch(url, {
|
|
501
515
|
headers: { Accept: "application/json" },
|
|
@@ -506,6 +520,53 @@ async function fetchVersionsFromUrl(url) {
|
|
|
506
520
|
}
|
|
507
521
|
return validateVersionsConfig(await response.json());
|
|
508
522
|
}
|
|
523
|
+
function fetchVersionsFromGit(repo, branch) {
|
|
524
|
+
const tempDir = fs3.mkdtempSync(path4.join(os2.tmpdir(), "create-szyy-app-versions-"));
|
|
525
|
+
try {
|
|
526
|
+
const cloneDir = path4.join(tempDir, "repo");
|
|
527
|
+
shallowCloneBranch(repo, branch, cloneDir);
|
|
528
|
+
const versionsPath = path4.join(cloneDir, "versions.json");
|
|
529
|
+
if (!fs3.existsSync(versionsPath)) {
|
|
530
|
+
throw new Error(`\u6A21\u677F\u4ED3 ${branch} \u5206\u652F\u7F3A\u5C11 versions.json`);
|
|
531
|
+
}
|
|
532
|
+
return validateVersionsConfig(JSON.parse(fs3.readFileSync(versionsPath, "utf-8")));
|
|
533
|
+
} finally {
|
|
534
|
+
fs3.rmSync(tempDir, { recursive: true, force: true });
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
async function fetchRemoteVersions(bundled) {
|
|
538
|
+
const repoBase = getRepoBase(bundled);
|
|
539
|
+
const remoteUrl = process.env.SZYY_VERSIONS_URL || getDefaultRemoteVersionsUrl(bundled);
|
|
540
|
+
if (process.env.SZYY_VERSIONS_URL) {
|
|
541
|
+
return {
|
|
542
|
+
config: await fetchVersionsFromUrl(remoteUrl),
|
|
543
|
+
remoteUrl
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
if (shouldUseGitCloneDirectly(repoBase)) {
|
|
547
|
+
return {
|
|
548
|
+
config: fetchVersionsFromGit(repoBase, CONFIG_BRANCH),
|
|
549
|
+
remoteUrl: getGitVersionsLabel(repoBase)
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
try {
|
|
553
|
+
return {
|
|
554
|
+
config: await fetchVersionsFromUrl(remoteUrl),
|
|
555
|
+
remoteUrl
|
|
556
|
+
};
|
|
557
|
+
} catch (httpError) {
|
|
558
|
+
try {
|
|
559
|
+
return {
|
|
560
|
+
config: fetchVersionsFromGit(repoBase, CONFIG_BRANCH),
|
|
561
|
+
remoteUrl: getGitVersionsLabel(repoBase)
|
|
562
|
+
};
|
|
563
|
+
} catch (gitError) {
|
|
564
|
+
const httpMsg = httpError instanceof Error ? httpError.message : String(httpError);
|
|
565
|
+
const gitMsg = gitError instanceof Error ? gitError.message : String(gitError);
|
|
566
|
+
throw new Error(`${httpMsg}; Git \u56DE\u9000\u4E5F\u5931\u8D25: ${gitMsg}`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
509
570
|
function readVersionsFromPath(filePath) {
|
|
510
571
|
const resolved = path4.resolve(filePath);
|
|
511
572
|
if (!fs3.existsSync(resolved)) {
|
|
@@ -528,15 +589,15 @@ async function resolveVersions(options) {
|
|
|
528
589
|
source: "path"
|
|
529
590
|
};
|
|
530
591
|
}
|
|
531
|
-
const remoteUrl = process.env.SZYY_VERSIONS_URL || getDefaultRemoteVersionsUrl(bundled);
|
|
532
592
|
try {
|
|
533
|
-
const remote = await
|
|
593
|
+
const { config: remote, remoteUrl } = await fetchRemoteVersions(bundled);
|
|
534
594
|
return {
|
|
535
595
|
config: applyOverrides(mergeVersions(bundled, remote), options),
|
|
536
596
|
source: "remote",
|
|
537
597
|
remoteUrl
|
|
538
598
|
};
|
|
539
599
|
} catch (error) {
|
|
600
|
+
const remoteUrl = process.env.SZYY_VERSIONS_URL || getDefaultRemoteVersionsUrl(bundled);
|
|
540
601
|
const fallbackReason = error instanceof Error ? error.message : String(error);
|
|
541
602
|
return {
|
|
542
603
|
config: applyOverrides(bundled, options),
|