@c-time/frelio-cli 2.0.0 → 2.2.0

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.
@@ -12,3 +12,13 @@ export declare function getBundleVersion(projectDir: string): OperationResult<{
12
12
  version: string | null;
13
13
  installed: boolean;
14
14
  }>;
15
+ /**
16
+ * ディレクトリを丸ごと置き換える(rm → copy)が、その配下にある PROTECTED_CONFIG
17
+ * ファイルは内容を退避 → 復元して保持する(Issue #199)。
18
+ *
19
+ * `update` はバンドル配布のため `workers/` などを毎回全削除して展開し直すが、
20
+ * `workers/contact/wrangler.toml`(D1 の database_id 入り。バンドルに含まれない生成物)や
21
+ * ユーザー編集済みの `workers/file-upload/wrangler.toml` はそれで失われていた。
22
+ * config.json と同じ capture→restore で守る。
23
+ */
24
+ export declare function replaceDirPreservingProtected(projectDir: string, destDir: string, srcDir: string, exclude?: string[]): void;
@@ -6,6 +6,7 @@ import path from 'node:path';
6
6
  import os from 'node:os';
7
7
  import { ok, fail } from './types.js';
8
8
  import { getLatestRelease, getRelease, downloadTarball, extractNpmTarball, } from '../lib/npm-registry.js';
9
+ import { PROTECTED_CONFIG } from './template-refresh.js';
9
10
  // ---------------------------------------------------------------------------
10
11
  // Install (initial)
11
12
  // ---------------------------------------------------------------------------
@@ -54,26 +55,19 @@ export async function updateBundle(projectDir, version) {
54
55
  const release = version ? await getRelease(version) : await getLatestRelease();
55
56
  const tarPath = await downloadTarball(release, tmpDir);
56
57
  const { adminDir: srcAdminDir, adminWorkerDir: srcAdminWorkerDir, workersDir: srcWorkersDir, } = await extractNpmTarball(tarPath, tmpDir);
57
- // admin/ 置き換え
58
+ // admin/ 置き換え(config.json は上で個別に backup/restore。念のため保護一覧も通す)
58
59
  if (fs.existsSync(srcAdminDir)) {
59
- fs.rmSync(adminDir, { recursive: true, force: true });
60
- copyDir(srcAdminDir, adminDir, ['worker']);
60
+ replaceDirPreservingProtected(projectDir, adminDir, srcAdminDir, ['worker']);
61
61
  }
62
62
  // 管理画面 Worker(Issue #184 Phase 2)置き換え: admin-worker/
63
63
  const adminWorkerDestDir = path.join(projectDir, 'admin-worker');
64
64
  if (fs.existsSync(srcAdminWorkerDir)) {
65
- if (fs.existsSync(adminWorkerDestDir)) {
66
- fs.rmSync(adminWorkerDestDir, { recursive: true, force: true });
67
- }
68
- copyDir(srcAdminWorkerDir, adminWorkerDestDir);
65
+ replaceDirPreservingProtected(projectDir, adminWorkerDestDir, srcAdminWorkerDir);
69
66
  }
70
- // workers/ 置き換え
67
+ // workers/ 置き換え(PROTECTED_CONFIG を保持: contact/file-upload の wrangler.toml)
71
68
  const workersDestDir = path.join(projectDir, 'workers');
72
69
  if (fs.existsSync(srcWorkersDir)) {
73
- if (fs.existsSync(workersDestDir)) {
74
- fs.rmSync(workersDestDir, { recursive: true, force: true });
75
- }
76
- copyDir(srcWorkersDir, workersDestDir);
70
+ replaceDirPreservingProtected(projectDir, workersDestDir, srcWorkersDir);
77
71
  }
78
72
  // config.json 復元
79
73
  if (configBackup) {
@@ -107,6 +101,36 @@ export function getBundleVersion(projectDir) {
107
101
  // ---------------------------------------------------------------------------
108
102
  // Shared helper
109
103
  // ---------------------------------------------------------------------------
104
+ /**
105
+ * ディレクトリを丸ごと置き換える(rm → copy)が、その配下にある PROTECTED_CONFIG
106
+ * ファイルは内容を退避 → 復元して保持する(Issue #199)。
107
+ *
108
+ * `update` はバンドル配布のため `workers/` などを毎回全削除して展開し直すが、
109
+ * `workers/contact/wrangler.toml`(D1 の database_id 入り。バンドルに含まれない生成物)や
110
+ * ユーザー編集済みの `workers/file-upload/wrangler.toml` はそれで失われていた。
111
+ * config.json と同じ capture→restore で守る。
112
+ */
113
+ export function replaceDirPreservingProtected(projectDir, destDir, srcDir, exclude = []) {
114
+ // 削除前に、destDir 配下にある保護ファイルの内容を退避する
115
+ const preserved = new Map();
116
+ if (fs.existsSync(destDir)) {
117
+ for (const rel of PROTECTED_CONFIG) {
118
+ const full = path.join(projectDir, rel);
119
+ const relFromDest = path.relative(destDir, full);
120
+ const underDest = relFromDest !== '' && !relFromDest.startsWith('..') && !path.isAbsolute(relFromDest);
121
+ if (underDest && fs.existsSync(full)) {
122
+ preserved.set(full, fs.readFileSync(full, 'utf-8'));
123
+ }
124
+ }
125
+ fs.rmSync(destDir, { recursive: true, force: true });
126
+ }
127
+ copyDir(srcDir, destDir, exclude);
128
+ // テンプレートが同名を置いた場合も、退避した内容で上書きして保持する
129
+ for (const [full, content] of preserved) {
130
+ fs.mkdirSync(path.dirname(full), { recursive: true });
131
+ fs.writeFileSync(full, content, 'utf-8');
132
+ }
133
+ }
110
134
  function copyDir(src, dest, exclude = []) {
111
135
  fs.mkdirSync(dest, { recursive: true });
112
136
  for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
@@ -66,7 +66,12 @@ export function runDoctor(projectDir) {
66
66
  matchedFiles.push(path.relative(projectDir, file).split(path.sep).join('/'));
67
67
  }
68
68
  }
69
- if (matchedFiles.length > 0) {
69
+ // absentFile が指定されている場合、そのファイルが存在するなら要件充足として finding にしない。
70
+ // (files/patterns は「移行済み=Workers 構成」判定に使い、欠落しているべきファイルの
71
+ // 有無だけを警告する用途。例: 404 テンプレ未整備)
72
+ const absentFile = migration.detection.absentFile;
73
+ const requirementSatisfied = absentFile !== undefined && fs.existsSync(path.join(projectDir, absentFile));
74
+ if (matchedFiles.length > 0 && !requirementSatisfied) {
70
75
  matchedFiles.sort();
71
76
  findings.push({ migration, matchedFiles });
72
77
  }
@@ -14,5 +14,15 @@ import type { Migration } from './types.js';
14
14
  export declare const legacyPagesArtifactsIssue184: Migration;
15
15
  /** 旧 Pages 用 root wrangler.toml(pages_build_output_dir を持つ)。 */
16
16
  export declare const legacyPagesWranglerIssue184: Migration;
17
+ /**
18
+ * #189 — Workers 移行済みだが 404 テンプレートが未整備。
19
+ *
20
+ * content Worker の wrangler.content.toml は `not_found_handling="404-page"` を持ち、
21
+ * 未マッチ URL に `public/404.html` を 404 で配信する。しかし `update` は
22
+ * frelio-data/(テンプレート)や recipe を触らないため、404 テンプレートを持たない
23
+ * 世代のリポジトリでは `public/404.html` が生成されず、404 が空ボディになる。
24
+ * デザイン依存のため自動生成はできないので、doctor で整備を促す。
25
+ */
26
+ export declare const missing404TemplateIssue189: Migration;
17
27
  /** #187 で廃止した admin/config.json の Cloudflare デプロイ管理フィールド。 */
18
28
  export declare const deployMgmtConfigFieldsIssue187: Migration;
@@ -19,6 +19,10 @@ export const legacyPagesArtifactsIssue184 = {
19
19
  severity: 'warning',
20
20
  detection: {
21
21
  // 存在すること自体が旧 Pages 資産。catch-all パターンで「存在=検出」とする。
22
+ // root だけでなくアセットディレクトリ(admin/・public/)配下も対象にする。
23
+ // 特に admin/_redirects(`/* /index.html 200`)は Workers の [assets] で
24
+ // "Infinite loop detected"(Cloudflare API code 100324)となり admin Worker の
25
+ // デプロイを止めるため、root と同様に撤去が必要(Issue #189)。
22
26
  files: [
23
27
  '_redirects',
24
28
  '_routes.json',
@@ -26,12 +30,18 @@ export const legacyPagesArtifactsIssue184 = {
26
30
  'functions/_middleware.js',
27
31
  'functions/*',
28
32
  'functions/api/*',
33
+ 'admin/_redirects',
34
+ 'admin/_routes.json',
35
+ 'public/_redirects',
36
+ 'public/_routes.json',
29
37
  ],
30
38
  patterns: ['[\\s\\S]*'],
31
39
  flags: 's',
32
40
  },
33
41
  remediation: '不要な旧 Pages 資産を削除してください: `git rm -r functions/` および ' +
34
- '`git rm -f _redirects _routes.json`(存在するもののみ)。',
42
+ '`git rm -f _redirects _routes.json`(存在するもののみ)。' +
43
+ 'アセットディレクトリ配下にも残ることがあります: ' +
44
+ '`git rm -f admin/_redirects admin/_routes.json public/_redirects public/_routes.json`(存在するもののみ)。',
35
45
  referenceUrl: 'https://github.com/c-time/frelio-develop/issues/189',
36
46
  };
37
47
  /** 旧 Pages 用 root wrangler.toml(pages_build_output_dir を持つ)。 */
@@ -51,6 +61,34 @@ export const legacyPagesWranglerIssue184 = {
51
61
  command: 'npx @c-time/frelio-cli update --templates-only',
52
62
  referenceUrl: 'https://github.com/c-time/frelio-develop/issues/189',
53
63
  };
64
+ /**
65
+ * #189 — Workers 移行済みだが 404 テンプレートが未整備。
66
+ *
67
+ * content Worker の wrangler.content.toml は `not_found_handling="404-page"` を持ち、
68
+ * 未マッチ URL に `public/404.html` を 404 で配信する。しかし `update` は
69
+ * frelio-data/(テンプレート)や recipe を触らないため、404 テンプレートを持たない
70
+ * 世代のリポジトリでは `public/404.html` が生成されず、404 が空ボディになる。
71
+ * デザイン依存のため自動生成はできないので、doctor で整備を促す。
72
+ */
73
+ export const missing404TemplateIssue189 = {
74
+ id: 'missing-404-template-issue189',
75
+ title: '404 テンプレート(templates/404.html)の整備',
76
+ description: 'content Worker は not_found_handling="404-page" で未マッチ URL に public/404.html を配信しますが、' +
77
+ 'frelio-data/site/templates/404.html が見つかりません。このままでは 404 が空ボディになります。',
78
+ severity: 'warning',
79
+ detection: {
80
+ // Workers 構成(wrangler.content.toml が 404-page 配信)であることを判定し、
81
+ files: ['wrangler.content.toml'],
82
+ patterns: ['not_found_handling\\s*=\\s*"404-page"'],
83
+ flags: 'm',
84
+ // 404 テンプレートが存在しないときだけ警告する。
85
+ absentFile: 'frelio-data/site/templates/404.html',
86
+ },
87
+ remediation: 'frelio-data/site/templates/404.html を追加し、build-data-recipe.json の staticPages に ' +
88
+ '{ "type": "static", "outputPath": "404.json", "templatePath": "404.html" } を加えて SSG を再ビルドしてください。' +
89
+ 'テンプレートはサイトのデザイン(_parts / 共通スタイル)に合わせて作成します。',
90
+ referenceUrl: 'https://github.com/c-time/frelio-develop/issues/189',
91
+ };
54
92
  /** #187 で廃止した admin/config.json の Cloudflare デプロイ管理フィールド。 */
55
93
  export const deployMgmtConfigFieldsIssue187 = {
56
94
  id: 'deploy-mgmt-config-fields-issue187',
@@ -1,6 +1,6 @@
1
1
  import { node20ToNode24 } from './node20-to-node24.js';
2
2
  import { deployAdminSubdirIssue127 } from './deploy-admin-subdir-issue127.js';
3
- import { legacyPagesArtifactsIssue184, legacyPagesWranglerIssue184, deployMgmtConfigFieldsIssue187, } from './pages-to-workers-issue184.js';
3
+ import { legacyPagesArtifactsIssue184, legacyPagesWranglerIssue184, missing404TemplateIssue189, deployMgmtConfigFieldsIssue187, } from './pages-to-workers-issue184.js';
4
4
  /**
5
5
  * 既知のマイグレーション一覧(実装順 = 登録順)。
6
6
  * frelio doctor がこの一覧を走査して未適用を検出する。
@@ -10,6 +10,7 @@ export const MIGRATIONS = [
10
10
  deployAdminSubdirIssue127,
11
11
  legacyPagesArtifactsIssue184,
12
12
  legacyPagesWranglerIssue184,
13
+ missing404TemplateIssue189,
13
14
  deployMgmtConfigFieldsIssue187,
14
15
  ];
15
16
  /**
@@ -21,6 +21,13 @@ export type DetectionPattern = {
21
21
  patterns: string[];
22
22
  /** 各正規表現に付与するフラグ(既定 'm') */
23
23
  flags?: string;
24
+ /**
25
+ * 「あるべきファイルが欠けている」ことを条件に加える(任意)。
26
+ * 指定した相対パスのファイルが**存在する**場合は、検出がヒットしても finding にしない
27
+ * (=要件が満たされている)。files/patterns で「移行済み(Workers 構成)である」ことを
28
+ * 判定しつつ、absentFile の欠落だけを警告するために使う(例: 404 テンプレ未整備)。
29
+ */
30
+ absentFile?: string;
24
31
  };
25
32
  /** 1件のマイグレーション(破壊的変更への追従単位) */
26
33
  export type Migration = {
@@ -41,6 +41,14 @@ export type RefreshResult = {
41
41
  /** ユーザー編集を検出したため保持した保護設定ファイル(Issue #150。--force で上書き可) */
42
42
  preserved: string[];
43
43
  };
44
+ /**
45
+ * 保護対象の設定ファイル(Issue #150)。
46
+ * ユーザーが恒常的にカスタムを足す前提のファイルは、既定では上書きせず保持する
47
+ * (config.json と同様)。--force 指定時のみ *.frelio-bak を作って上書きする。
48
+ *
49
+ * ディレクトリ置換(core/bundle の update)でもこの一覧を保持対象にするため export する(Issue #199)。
50
+ */
51
+ export declare const PROTECTED_CONFIG: Set<string>;
44
52
  export type RefreshOptions = {
45
53
  targets?: RefreshTarget[];
46
54
  /**
@@ -42,8 +42,10 @@ const ROOT_DOC_FILES = ['AGENTS.md', 'CLAUDE.md'];
42
42
  * 保護対象の設定ファイル(Issue #150)。
43
43
  * ユーザーが恒常的にカスタムを足す前提のファイルは、既定では上書きせず保持する
44
44
  * (config.json と同様)。--force 指定時のみ *.frelio-bak を作って上書きする。
45
+ *
46
+ * ディレクトリ置換(core/bundle の update)でもこの一覧を保持対象にするため export する(Issue #199)。
45
47
  */
46
- const PROTECTED_CONFIG = new Set([
48
+ export const PROTECTED_CONFIG = new Set([
47
49
  // Issue #184: Workers(Static Assets) の wrangler 設定(content / admin)。ユーザー編集を保持。
48
50
  'wrangler.content.toml',
49
51
  'wrangler.admin.toml',
@@ -25,10 +25,11 @@ export function projectConfigToVars(config) {
25
25
  previewUrl: config.previewUrl,
26
26
  // ビジュアル編集・プレビューのベース URL(Issue #140)。未指定なら previewUrl を既定。
27
27
  templatePreviewBaseUrl: config.templatePreviewBaseUrl || config.previewUrl,
28
- fileUploadUrl: '/api/storage',
29
- // お問い合わせワーカー(frelio-contact)の URL。初期は空(任意機能)。
30
- // 公開後に CMS「基本設定」で設定する。
31
- contactApiUrl: '',
28
+ // 既存 config の値を保持する(未設定時のみ既定)。generateConfigJson と揃える(Issue #198)。
29
+ fileUploadUrl: config.fileUploadUrl || '/api/storage',
30
+ // お問い合わせワーカー(frelio-contact)の URL(任意機能・CMS「基本設定」で設定)。
31
+ // 再生成時に既存値を保持する(Issue #198。未設定時のみ空)。
32
+ contactApiUrl: config.contactApiUrl ?? '',
32
33
  contentWorkerName: config.contentWorkerName,
33
34
  adminWorkerName: config.adminWorkerName,
34
35
  r2BucketName: config.r2BucketName,
@@ -12,6 +12,16 @@ export type ProjectConfig = {
12
12
  * 未指定なら生成時に previewUrl を既定値として出力する。
13
13
  */
14
14
  templatePreviewBaseUrl?: string;
15
+ /**
16
+ * ファイルアップロード API のパス(既定 `/api/storage`)。CMS「基本設定」で編集でき、
17
+ * 再生成(set-mode / set-domain)でも既存値を保持する(Issue #198)。未設定時のみ既定値。
18
+ */
19
+ fileUploadUrl?: string;
20
+ /**
21
+ * お問い合わせワーカー(frelio-contact)の URL(任意機能)。CMS「基本設定」で設定でき、
22
+ * 再生成(set-mode / set-domain)でも既存値を保持する(Issue #198)。未設定時は空。
23
+ */
24
+ contactApiUrl?: string;
15
25
  r2BucketName: string;
16
26
  r2PublicUrl: string;
17
27
  /** コンテンツ配信 Worker 名(旧称 pagesProjectName)。 */
@@ -93,10 +93,11 @@ export function generateConfigJson(config) {
93
93
  return JSON.stringify({
94
94
  contentRepo: config.contentRepo,
95
95
  githubClientId: config.githubClientId,
96
- fileUploadUrl: '/api/storage',
97
- // お問い合わせワーカー(frelio-contact)の URL。初期は空(任意機能)。
98
- // 公開後に CMS「基本設定」で設定する。
99
- contactApiUrl: '',
96
+ // ファイルアップロード API のパス。既存 config の値を保持する(未設定時のみ既定 /api/storage)。
97
+ fileUploadUrl: config.fileUploadUrl || '/api/storage',
98
+ // お問い合わせワーカー(frelio-contact)の URL(任意機能・CMS「基本設定」で設定)。
99
+ // 再生成時に既存値を保持する(Issue #198。未設定時のみ空)。以前はハードコード空で silent data-loss だった。
100
+ contactApiUrl: config.contactApiUrl ?? '',
100
101
  siteTitle: config.siteTitle,
101
102
  productionUrl: config.productionUrl,
102
103
  previewUrl: config.previewUrl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c-time/frelio-cli",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Frelio CMS setup CLI",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,8 +23,8 @@
23
23
  "prepublishOnly": "npm run build"
24
24
  },
25
25
  "dependencies": {
26
- "@c-time/frelio-data-json": "^2.0.0",
27
- "@c-time/frelio-gentl": "^2.0.0",
26
+ "@c-time/frelio-data-json": "^2.2.0",
27
+ "@c-time/frelio-gentl": "^2.2.0",
28
28
  "commander": "^13.0.0",
29
29
  "prompts": "^2.4.2",
30
30
  "tar": "^7.0.0"