@c-time/frelio-cli 2.4.0 → 2.5.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.
- package/README.md +18 -72
- package/dist/commands/doctor.js +40 -12
- package/dist/commands/init.d.ts +7 -0
- package/dist/commands/init.js +275 -74
- package/dist/commands/set-domain.d.ts +1 -0
- package/dist/commands/set-domain.js +48 -3
- package/dist/commands/validate-templates.d.ts +12 -0
- package/dist/commands/validate-templates.js +44 -0
- package/dist/core/cloudflare.d.ts +16 -0
- package/dist/core/cloudflare.js +35 -0
- package/dist/core/config-doctor.d.ts +28 -0
- package/dist/core/config-doctor.js +82 -0
- package/dist/core/config.js +12 -3
- package/dist/core/domain.d.ts +6 -0
- package/dist/core/domain.js +19 -1
- package/dist/core/file-delivery.d.ts +53 -0
- package/dist/core/file-delivery.js +119 -0
- package/dist/core/template-refresh.js +2 -0
- package/dist/core/types.d.ts +5 -0
- package/dist/core/validate-templates.d.ts +37 -0
- package/dist/core/validate-templates.js +164 -0
- package/dist/index.js +10 -8
- package/dist/lib/template-renderer.js +6 -0
- package/dist/lib/templates.d.ts +92 -1
- package/dist/lib/templates.js +148 -3
- package/package.json +3 -3
- package/dist/commands/add-staging.d.ts +0 -10
- package/dist/commands/add-staging.js +0 -119
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/validate-templates — テンプレートのデータ束縛(data-gen-text / data-gen-html)の検証
|
|
3
|
+
*
|
|
4
|
+
* AI / 人が繰り返しやすい「html フィールドを data-gen-text(エスケープ出力)で束縛してしまう」
|
|
5
|
+
* ミスを機械的に検出する。
|
|
6
|
+
*
|
|
7
|
+
* なぜ問題か:
|
|
8
|
+
* ビジュアルエディタ(TipTap)は本文の段落を必ず <p>…</p> で包んで保存する(正しい HTML 出力)。
|
|
9
|
+
* これをテンプレートで data-gen-html(生出力)ではなく data-gen-text(HTML エスケープ)で書き出すと、
|
|
10
|
+
* <p> がエスケープされて `<p>…</p>` という生タグが**文字として**公開ページに露出する。
|
|
11
|
+
* → 属性を data-gen-html に直すのが修正(フィールドの型は変えない)。
|
|
12
|
+
*
|
|
13
|
+
* 注: content-repo 側のスタンドアロン版 `scripts/validate-templates.ts`(雛形に同梱、`npm run
|
|
14
|
+
* validate:templates` で実行)と同一ロジック。あちらは CMS リポジトリ内で完結させるため
|
|
15
|
+
* @c-time/frelio-types だけに依存する別実装。挙動を変えるときは両方を揃えること。
|
|
16
|
+
*/
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { ok, fail } from './types.js';
|
|
20
|
+
const CONTENT_TYPES_DIR = 'frelio-data/site/content_types';
|
|
21
|
+
const TEMPLATE_ROOT = 'frelio-data/site/templates';
|
|
22
|
+
/** data-gen-html(生出力)が正しい型。 */
|
|
23
|
+
const HTML_TYPE = 'html';
|
|
24
|
+
/** data-gen-text(エスケープ出力)が正しいプレーン系の型。 */
|
|
25
|
+
const TEXT_LIKE_TYPES = new Set(['text', 'markdown', 'number', 'date', 'datetime', 'select']);
|
|
26
|
+
/** data-gen-text / data-gen-html の束縛を拾う。単一・二重引用符どちらも許容。 */
|
|
27
|
+
const BINDING_RE = /data-gen-(text|html)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
28
|
+
/** content_types を読み、フィールドキー → そのキーを定義する型の一覧(html / text 系)を集める。 */
|
|
29
|
+
function loadFieldTypeMap(projectDir) {
|
|
30
|
+
const htmlKeyToTypes = new Map();
|
|
31
|
+
const textKeyToTypes = new Map();
|
|
32
|
+
const dir = path.join(projectDir, CONTENT_TYPES_DIR);
|
|
33
|
+
if (!fs.existsSync(dir))
|
|
34
|
+
return { htmlKeyToTypes, textKeyToTypes };
|
|
35
|
+
for (const name of fs.readdirSync(dir)) {
|
|
36
|
+
if (!name.endsWith('.json'))
|
|
37
|
+
continue;
|
|
38
|
+
let ct;
|
|
39
|
+
try {
|
|
40
|
+
ct = JSON.parse(fs.readFileSync(path.join(dir, name), 'utf-8'));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
continue; // 壊れた JSON は validate:schema の責務なのでここでは無視
|
|
44
|
+
}
|
|
45
|
+
const typeId = ct.id ?? name.replace(/\.json$/, '');
|
|
46
|
+
for (const f of ct.fields ?? []) {
|
|
47
|
+
if (!f.key || !f.type)
|
|
48
|
+
continue;
|
|
49
|
+
if (f.type === HTML_TYPE) {
|
|
50
|
+
htmlKeyToTypes.set(f.key, [...(htmlKeyToTypes.get(f.key) ?? []), typeId]);
|
|
51
|
+
}
|
|
52
|
+
else if (TEXT_LIKE_TYPES.has(f.type)) {
|
|
53
|
+
textKeyToTypes.set(f.key, [...(textKeyToTypes.get(f.key) ?? []), typeId]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { htmlKeyToTypes, textKeyToTypes };
|
|
58
|
+
}
|
|
59
|
+
/** ディレクトリ配下の *.html / *.htm を再帰収集する(絶対パス)。 */
|
|
60
|
+
function collectTemplateFiles(dir) {
|
|
61
|
+
if (!fs.existsSync(dir))
|
|
62
|
+
return [];
|
|
63
|
+
const out = [];
|
|
64
|
+
for (const name of fs.readdirSync(dir)) {
|
|
65
|
+
const p = path.join(dir, name);
|
|
66
|
+
if (fs.statSync(p).isDirectory())
|
|
67
|
+
out.push(...collectTemplateFiles(p));
|
|
68
|
+
else if (name.endsWith('.html') || name.endsWith('.htm'))
|
|
69
|
+
out.push(p);
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
/** 文字列先頭から index までの行番号(1 始まり)。 */
|
|
74
|
+
function lineOf(text, index) {
|
|
75
|
+
let line = 1;
|
|
76
|
+
for (let i = 0; i < index && i < text.length; i++) {
|
|
77
|
+
if (text[i] === '\n')
|
|
78
|
+
line++;
|
|
79
|
+
}
|
|
80
|
+
return line;
|
|
81
|
+
}
|
|
82
|
+
/** 束縛パスのリーフキー(ドット区切りの末尾)。`item.note` → `note`。 */
|
|
83
|
+
function leafKey(bindPath) {
|
|
84
|
+
const parts = bindPath.split('.');
|
|
85
|
+
return (parts[parts.length - 1] ?? '').trim();
|
|
86
|
+
}
|
|
87
|
+
function checkTemplate(file, html, maps, findings) {
|
|
88
|
+
const { htmlKeyToTypes, textKeyToTypes } = maps;
|
|
89
|
+
let m;
|
|
90
|
+
BINDING_RE.lastIndex = 0;
|
|
91
|
+
while ((m = BINDING_RE.exec(html)) !== null) {
|
|
92
|
+
const attr = m[1];
|
|
93
|
+
const bindPath = (m[2] ?? m[3] ?? '').trim();
|
|
94
|
+
const key = leafKey(bindPath);
|
|
95
|
+
if (key === '')
|
|
96
|
+
continue;
|
|
97
|
+
const htmlTypes = htmlKeyToTypes.get(key);
|
|
98
|
+
const textTypes = textKeyToTypes.get(key);
|
|
99
|
+
const line = lineOf(html, m.index);
|
|
100
|
+
if (attr === 'text' && htmlTypes) {
|
|
101
|
+
if (textTypes) {
|
|
102
|
+
// 同名キーが型違いで併存。誤 ERROR を避けて WARN に落とす。
|
|
103
|
+
findings.push({
|
|
104
|
+
level: 'warn',
|
|
105
|
+
file,
|
|
106
|
+
line,
|
|
107
|
+
detail: `"${key}" は html 型(${htmlTypes.join('/')})と text 系(${textTypes.join('/')})に併存。` +
|
|
108
|
+
`束縛先が html なら data-gen-html にすること(data-gen-text は <p> を露出させる)`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
findings.push({
|
|
113
|
+
level: 'error',
|
|
114
|
+
file,
|
|
115
|
+
line,
|
|
116
|
+
detail: `html 型フィールド "${key}"(${htmlTypes.join('/')})を data-gen-text で束縛している。` +
|
|
117
|
+
`<p> 等のタグが文字として露出する。data-gen-html(生出力)を使うこと`,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else if (attr === 'html' && textTypes && !htmlTypes) {
|
|
122
|
+
findings.push({
|
|
123
|
+
level: 'warn',
|
|
124
|
+
file,
|
|
125
|
+
line,
|
|
126
|
+
detail: `text 系フィールド "${key}"(${textTypes.join('/')})を data-gen-html で束縛している。` +
|
|
127
|
+
`プレーン値を生出力している。通常は data-gen-text(エスケープ出力)を使う`,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* テンプレートのデータ束縛を検証する。
|
|
134
|
+
*
|
|
135
|
+
* content_types が無いディレクトリ(Frelio プロジェクトでない)は VALIDATION_ERROR を返す。
|
|
136
|
+
*/
|
|
137
|
+
export function runValidateTemplates(projectDir) {
|
|
138
|
+
const contentTypesDir = path.join(projectDir, CONTENT_TYPES_DIR);
|
|
139
|
+
if (!fs.existsSync(contentTypesDir)) {
|
|
140
|
+
return fail(`${CONTENT_TYPES_DIR} が見つかりません。Frelio プロジェクトのルートで実行してください。`, 'NOT_FOUND');
|
|
141
|
+
}
|
|
142
|
+
const maps = loadFieldTypeMap(projectDir);
|
|
143
|
+
const templateFiles = collectTemplateFiles(path.join(projectDir, TEMPLATE_ROOT));
|
|
144
|
+
const findings = [];
|
|
145
|
+
for (const abs of templateFiles) {
|
|
146
|
+
const rel = path.relative(projectDir, abs);
|
|
147
|
+
let html;
|
|
148
|
+
try {
|
|
149
|
+
html = fs.readFileSync(abs, 'utf-8');
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
findings.push({ level: 'error', file: rel, line: 1, detail: 'テンプレートを読み込めない' });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
checkTemplate(rel, html, maps, findings);
|
|
156
|
+
}
|
|
157
|
+
findings.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line);
|
|
158
|
+
return ok({
|
|
159
|
+
findings,
|
|
160
|
+
scannedTemplateCount: templateFiles.length,
|
|
161
|
+
errorCount: findings.filter((f) => f.level === 'error').length,
|
|
162
|
+
warnCount: findings.filter((f) => f.level === 'warn').length,
|
|
163
|
+
});
|
|
164
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -5,12 +5,12 @@ import { fileURLToPath } from 'node:url';
|
|
|
5
5
|
import { Command } from 'commander';
|
|
6
6
|
import { initCommand } from './commands/init.js';
|
|
7
7
|
import { updateCommand } from './commands/update.js';
|
|
8
|
-
import { addStagingCommand } from './commands/add-staging.js';
|
|
9
8
|
import { setDomainCommand } from './commands/set-domain.js';
|
|
10
9
|
import { setModeCommand } from './commands/set-mode.js';
|
|
11
10
|
import { doctorCommand } from './commands/doctor.js';
|
|
12
11
|
import { fleetCommand } from './commands/fleet.js';
|
|
13
12
|
import { bakeCommand } from './commands/bake.js';
|
|
13
|
+
import { validateTemplatesCommand } from './commands/validate-templates.js';
|
|
14
14
|
import { maybeNotifyUpdate } from './core/version-check.js';
|
|
15
15
|
// バージョンは package.json から読む(ハードコードしない)
|
|
16
16
|
const pkg = JSON.parse(readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf-8'));
|
|
@@ -26,8 +26,9 @@ program
|
|
|
26
26
|
.option('--skip-cloudflare', 'Skip Cloudflare setup (R2 bucket, Worker secrets)')
|
|
27
27
|
.option('--content-repo <repo>', 'Repository name (owner/repo)')
|
|
28
28
|
.option('--site-title <title>', 'Site title')
|
|
29
|
-
.option('--production-url <url>', 'Production URL')
|
|
30
|
-
.option('--staging-domain <domain>', '
|
|
29
|
+
.option('--production-url <url>', 'Production URL(独自ドメイン。任意。通常は set-domain で後付け)')
|
|
30
|
+
.option('--staging-domain <domain>', 'ステージングの独自ドメイン(任意。通常は空=workers.dev プレビューを使う)')
|
|
31
|
+
.option('--admin-host <host>', '管理画面が配信される公開ホスト(例: repo-admin-xxxx.acme.workers.dev / admin.example.com)。指定すると deploy-first の自動検出をスキップし、OAuth コールバック等を実在 URL で確定する(Issue #261)')
|
|
31
32
|
.option('--r2-bucket-name <name>', 'R2 bucket name')
|
|
32
33
|
.option('--content-worker-name <name>', 'コンテンツ配信 Worker 名を明示指定(未指定ならリポジトリ名から導出。サニタイズで別リポジトリ名と衝突する場合の手動制御に使う)')
|
|
33
34
|
.option('--admin-worker-name <name>', '管理画面 Worker 名を明示指定(未指定ならリポジトリ名から導出。サニタイズで別リポジトリ名と衝突する場合の手動制御に使う)')
|
|
@@ -50,11 +51,6 @@ program
|
|
|
50
51
|
.description('生成物を診断し、期限付き廃止や未適用のテンプレ更新を検出する')
|
|
51
52
|
.option('--ci', '対応が必要な指摘があれば非ゼロの終了コードで終了する')
|
|
52
53
|
.action(doctorCommand);
|
|
53
|
-
program
|
|
54
|
-
.command('add-staging')
|
|
55
|
-
.description('Add a staging environment (creates a staging-* branch; deployed via GitHub Actions wrangler --branch)')
|
|
56
|
-
.option('--name <name>', 'Staging name (creates staging-{name} branch)')
|
|
57
|
-
.action(addStagingCommand);
|
|
58
54
|
program
|
|
59
55
|
.command('set-domain')
|
|
60
56
|
.description('Change production / preview / R2 URLs after init and regenerate config files (config.json + wrangler.toml)')
|
|
@@ -62,6 +58,7 @@ program
|
|
|
62
58
|
.option('--preview-url <url>', 'New preview URL (overrides derived staging domain)')
|
|
63
59
|
.option('--staging-domain <domain>', 'New staging domain (verbatim)')
|
|
64
60
|
.option('--r2-public-url <url>', 'New R2 public URL')
|
|
61
|
+
.option('--download-api-url <url>', 'New file-delivery worker URL (config.json downloadApiUrl + wrangler PUBLIC_BASE_URL)')
|
|
65
62
|
.option('--regen-staging-hash', 'Regenerate the staging subdomain hash instead of preserving it')
|
|
66
63
|
.action(setDomainCommand);
|
|
67
64
|
program
|
|
@@ -84,6 +81,11 @@ program
|
|
|
84
81
|
.option('--pick <strategy>', 'Representative data for detail templates: latest | first', 'latest')
|
|
85
82
|
.option('--check', 'Verify bake is up to date (CI); exit non-zero on drift')
|
|
86
83
|
.action(bakeCommand);
|
|
84
|
+
program
|
|
85
|
+
.command('validate-templates')
|
|
86
|
+
.description('Validate template data bindings: flag html fields bound via data-gen-text (escapes HTML, leaking literal <p> tags)')
|
|
87
|
+
.option('--ci', 'Exit non-zero also when only warnings are found')
|
|
88
|
+
.action(validateTemplatesCommand);
|
|
87
89
|
async function main() {
|
|
88
90
|
await program.parseAsync();
|
|
89
91
|
// コマンド完了後に最新版チェック(best-effort、本処理には影響しない)
|
|
@@ -33,8 +33,14 @@ export function projectConfigToVars(config) {
|
|
|
33
33
|
// 予約反映ワーカー(frelio-scheduled-publish)の URL(任意機能・CMS「基本設定」で設定)。
|
|
34
34
|
// 再生成時に既存値を保持する(未設定時のみ空)。
|
|
35
35
|
scheduleApiUrl: config.scheduleApiUrl ?? '',
|
|
36
|
+
// ファイル限定配信ワーカー(frelio-file-delivery)の公開 URL(任意機能・CMS「基本設定」で設定)。
|
|
37
|
+
// 設定の有無が配信フォーム機能の opt-in を兼ねる。再生成時に既存値を保持する(未設定時のみ空)。
|
|
38
|
+
downloadApiUrl: config.downloadApiUrl ?? '',
|
|
36
39
|
contentWorkerName: config.contentWorkerName,
|
|
37
40
|
adminWorkerName: config.adminWorkerName,
|
|
41
|
+
// workers.dev サブドメイン(個人プレビュー URL 導出用)。初回は不明なことが多く任意。
|
|
42
|
+
// 再生成時に既存値を保持する(未設定時のみ空)。generateConfigJson と揃える。
|
|
43
|
+
workersSubdomain: config.workersSubdomain ?? '',
|
|
38
44
|
r2BucketName: config.r2BucketName,
|
|
39
45
|
r2PublicUrl: config.r2PublicUrl,
|
|
40
46
|
ownerUsername: config.ownerUsername,
|
package/dist/lib/templates.d.ts
CHANGED
|
@@ -27,12 +27,24 @@ export type ProjectConfig = {
|
|
|
27
27
|
* 再生成(set-mode / set-domain)でも既存値を保持する。未設定時は空。
|
|
28
28
|
*/
|
|
29
29
|
scheduleApiUrl?: string;
|
|
30
|
+
/**
|
|
31
|
+
* ファイル限定配信ワーカー(frelio-file-delivery)の公開 URL(任意機能)。CMS「基本設定」で設定でき、
|
|
32
|
+
* 再生成(set-mode / set-domain)でも既存値を保持する。未設定時は空。
|
|
33
|
+
* このワーカーの PUBLIC_BASE_URL と同じ値(末尾スラッシュ無し)。
|
|
34
|
+
* 設定の有無がファイル配信フォーム機能の opt-in を兼ねる(contact との両 opt-in が前提)。
|
|
35
|
+
*/
|
|
36
|
+
downloadApiUrl?: string;
|
|
30
37
|
r2BucketName: string;
|
|
31
38
|
r2PublicUrl: string;
|
|
32
39
|
/** コンテンツ配信 Worker 名(旧称 pagesProjectName)。 */
|
|
33
40
|
contentWorkerName: string;
|
|
34
41
|
/** 管理画面 Worker 名(旧称 adminPagesProjectName)。 */
|
|
35
42
|
adminWorkerName: string;
|
|
43
|
+
/**
|
|
44
|
+
* Cloudflare アカウントの workers.dev サブドメイン(個人プレビュー URL の導出に使用)。
|
|
45
|
+
* 初回は不明なことが多いため任意。CMS「基本設定」で後から設定でき、再生成でも既存値を保持する。
|
|
46
|
+
*/
|
|
47
|
+
workersSubdomain?: string;
|
|
36
48
|
ownerUsername: string;
|
|
37
49
|
stagingDomain: string;
|
|
38
50
|
/** 保存タイムゾーン(ISO 8601 オフセット表記、例 "+09:00")。issue #92 */
|
|
@@ -79,6 +91,62 @@ export declare function buildWorkerNames(repoName: string, randomSuffix: boolean
|
|
|
79
91
|
contentWorkerName: string;
|
|
80
92
|
adminWorkerName: string;
|
|
81
93
|
};
|
|
94
|
+
/**
|
|
95
|
+
* workers.dev サブドメイン文字列を正規化する(ラベルだけを取り出す)。
|
|
96
|
+
* `https://acme.workers.dev` / `acme.workers.dev` / `acme` のいずれで渡されても `acme` を返す。
|
|
97
|
+
* 空・空白のみなら空文字。
|
|
98
|
+
*/
|
|
99
|
+
export declare function normalizeWorkersSubdomain(input: string): string;
|
|
100
|
+
/**
|
|
101
|
+
* workers.dev の実在 URL 一式を導出する(Issue #261: workers.dev ファースト)。
|
|
102
|
+
*
|
|
103
|
+
* init 時点で確定できる唯一の値=アカウントの workers.dev サブドメインが分かれば、
|
|
104
|
+
* プレビュー・本番(content)・管理画面・OAuth コールバックの**実在・解決可能な URL** を作れる。
|
|
105
|
+
* preview は CI(build-staging.yml)の `--preview-alias <branch>` が生成する
|
|
106
|
+
* `<branch>-<contentWorker>.<subdomain>.workers.dev` と一致させる(既定ブランチ staging)。
|
|
107
|
+
*
|
|
108
|
+
* サブドメインが空なら null(まだ確定していない=初回デプロイ後に CMS 基本設定で設定)。
|
|
109
|
+
*/
|
|
110
|
+
export declare function buildWorkersDevUrls(workersSubdomain: string, contentWorkerName: string, adminWorkerName: string): {
|
|
111
|
+
contentUrl: string;
|
|
112
|
+
adminUrl: string;
|
|
113
|
+
previewUrl: string;
|
|
114
|
+
adminCallbackUrl: string;
|
|
115
|
+
} | null;
|
|
116
|
+
/**
|
|
117
|
+
* ホスト(`repo-admin-xxxx.acme.workers.dev` / `acme.workers.dev`)から
|
|
118
|
+
* workers.dev の**アカウント部**(`acme`)を取り出す(Issue #261)。
|
|
119
|
+
* `.workers.dev` の直前のラベルがアカウント部。workers.dev でないホスト(独自ドメイン)は空。
|
|
120
|
+
*/
|
|
121
|
+
export declare function extractWorkersSubdomainFromHost(host: string): string;
|
|
122
|
+
export type AdminHostDerivation = {
|
|
123
|
+
/** 正規化したホスト(protocol/末尾スラッシュ除去) */
|
|
124
|
+
adminHost: string;
|
|
125
|
+
adminOrigin: string;
|
|
126
|
+
adminCallbackUrl: string;
|
|
127
|
+
/** workers.dev のときのみアカウント部、独自ドメインなら空 */
|
|
128
|
+
workersSubdomain: string;
|
|
129
|
+
isWorkersDev: boolean;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* 管理画面が配信される公開ホスト(workers.dev か独自ドメイン)から、
|
|
133
|
+
* OAuth コールバック・admin オリジン・(workers.dev なら)アカウント部を導く(Issue #261)。
|
|
134
|
+
*
|
|
135
|
+
* deploy-first の検出結果や `--admin-host` 指定値を受け取り、ニワトリ卵だった
|
|
136
|
+
* OAuth コールバック URL を確定させるために使う。
|
|
137
|
+
*/
|
|
138
|
+
export declare function deriveFromAdminHost(adminHost: string): AdminHostDerivation;
|
|
139
|
+
/**
|
|
140
|
+
* プレビュー URL を導出する(Issue #261)。
|
|
141
|
+
* 独自の staging ドメインがあればそれを優先し(終盤オプショナル工程で設定)、
|
|
142
|
+
* 無ければ workers.dev サブドメインから CI の preview-alias 形式を導く。
|
|
143
|
+
* どちらも無ければ空(=デプロイ後に確定)。
|
|
144
|
+
*/
|
|
145
|
+
export declare function derivePreviewUrl(opts: {
|
|
146
|
+
stagingDomain?: string;
|
|
147
|
+
workersSubdomain?: string;
|
|
148
|
+
contentWorkerName: string;
|
|
149
|
+
}): string;
|
|
82
150
|
/**
|
|
83
151
|
* productionUrl からステージング用の推測困難なカスタムサブドメインを生成する。
|
|
84
152
|
* 例: https://example.com → staging-a3f9c2kp.example.com
|
|
@@ -99,8 +167,11 @@ export declare function generateStagingDomain(productionUrl: string): string;
|
|
|
99
167
|
export declare function rehostStagingDomain(existingStagingDomain: string, newProductionUrl: string): string;
|
|
100
168
|
export declare function generateConfigJson(config: ProjectConfig): string;
|
|
101
169
|
/**
|
|
102
|
-
* CORS 許可オリジンの一覧を生成する(ローカル開発 +
|
|
170
|
+
* CORS 許可オリジンの一覧を生成する(ローカル開発 + 本番/プレビュー + workers.dev)。
|
|
103
171
|
* 管理画面 Worker(/api)と file-upload Worker の双方で使用する。
|
|
172
|
+
*
|
|
173
|
+
* workers.dev サブドメインが分かっていれば、管理画面・content・プレビューの
|
|
174
|
+
* workers.dev オリジンも含める(Issue #261: workers.dev ファースト運用で OAuth/CORS を成立させる)。
|
|
104
175
|
*/
|
|
105
176
|
export declare function buildAllowedOrigins(config: ProjectConfig): string;
|
|
106
177
|
export declare function generateWorkerWranglerToml(config: ProjectConfig): string;
|
|
@@ -140,6 +211,26 @@ export declare function generateContactWranglerToml(config: ProjectConfig, opts:
|
|
|
140
211
|
* 発火用 PAT(GITHUB_DISPATCH_TOKEN, Actions:write + Contents:read)は wrangler secret で投入する。
|
|
141
212
|
*/
|
|
142
213
|
export declare function generateScheduledPublishWranglerToml(config: ProjectConfig): string;
|
|
214
|
+
/** ファイル限定配信ワーカー(workers/file-delivery)の wrangler.toml 生成オプション */
|
|
215
|
+
export type FileDeliveryWorkerOptions = {
|
|
216
|
+
/** `wrangler d1 create frelio_downloads` で取得した database_id */
|
|
217
|
+
databaseId: string;
|
|
218
|
+
/** 限定配信用のプライベート R2 バケット名(公開バケットとは別) */
|
|
219
|
+
bucketName: string;
|
|
220
|
+
/** このワーカー自身の公開ベース URL(CMS の downloadApiUrl と同じ値。末尾スラッシュ無し) */
|
|
221
|
+
publicBaseUrl: string;
|
|
222
|
+
/** 失効/期限切れトークンの保持日数(既定 30、0 で無効) */
|
|
223
|
+
tokenRetentionDays?: string;
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* ファイル限定配信ワーカー(workers/file-delivery)の wrangler.toml を生成する(任意機能)。
|
|
227
|
+
*
|
|
228
|
+
* 専用プライベート R2 バケット(`<project>-downloads`)と D1(`frelio_downloads`)を持つ。
|
|
229
|
+
* database_id / bucket_name / PUBLIC_BASE_URL 等のユーザー設定を含むため init 時に一度だけ生成し、
|
|
230
|
+
* 以降の update では上書きしない(PROTECTED_CONFIG)。トークン発行は contact ワーカーが
|
|
231
|
+
* Service Binding(DeliveryEntrypoint)で呼ぶ(配線は workers/contact/wrangler.toml 側に追加する)。
|
|
232
|
+
*/
|
|
233
|
+
export declare function generateFileDeliveryWranglerToml(config: ProjectConfig, opts: FileDeliveryWorkerOptions): string;
|
|
143
234
|
/**
|
|
144
235
|
* コンテンツ配信 Worker の siteMode 焼き込みモジュール(worker/site-mode.ts)を生成する(Issue #184)。
|
|
145
236
|
*
|
package/dist/lib/templates.js
CHANGED
|
@@ -61,6 +61,90 @@ export function buildWorkerNames(repoName, randomSuffix, overrides) {
|
|
|
61
61
|
adminWorkerName: overrides?.adminWorkerName || derived.adminWorkerName,
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* workers.dev サブドメイン文字列を正規化する(ラベルだけを取り出す)。
|
|
66
|
+
* `https://acme.workers.dev` / `acme.workers.dev` / `acme` のいずれで渡されても `acme` を返す。
|
|
67
|
+
* 空・空白のみなら空文字。
|
|
68
|
+
*/
|
|
69
|
+
export function normalizeWorkersSubdomain(input) {
|
|
70
|
+
return input
|
|
71
|
+
.trim()
|
|
72
|
+
.replace(/^https?:\/\//, '')
|
|
73
|
+
.replace(/\/+$/, '')
|
|
74
|
+
.replace(/\.workers\.dev$/i, '')
|
|
75
|
+
.trim();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* workers.dev の実在 URL 一式を導出する(Issue #261: workers.dev ファースト)。
|
|
79
|
+
*
|
|
80
|
+
* init 時点で確定できる唯一の値=アカウントの workers.dev サブドメインが分かれば、
|
|
81
|
+
* プレビュー・本番(content)・管理画面・OAuth コールバックの**実在・解決可能な URL** を作れる。
|
|
82
|
+
* preview は CI(build-staging.yml)の `--preview-alias <branch>` が生成する
|
|
83
|
+
* `<branch>-<contentWorker>.<subdomain>.workers.dev` と一致させる(既定ブランチ staging)。
|
|
84
|
+
*
|
|
85
|
+
* サブドメインが空なら null(まだ確定していない=初回デプロイ後に CMS 基本設定で設定)。
|
|
86
|
+
*/
|
|
87
|
+
export function buildWorkersDevUrls(workersSubdomain, contentWorkerName, adminWorkerName) {
|
|
88
|
+
const sub = normalizeWorkersSubdomain(workersSubdomain);
|
|
89
|
+
if (!sub)
|
|
90
|
+
return null;
|
|
91
|
+
const adminUrl = `https://${adminWorkerName}.${sub}.workers.dev`;
|
|
92
|
+
return {
|
|
93
|
+
contentUrl: `https://${contentWorkerName}.${sub}.workers.dev`,
|
|
94
|
+
adminUrl,
|
|
95
|
+
previewUrl: `https://staging-${contentWorkerName}.${sub}.workers.dev`,
|
|
96
|
+
adminCallbackUrl: `${adminUrl}/api/auth/callback`,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* ホスト(`repo-admin-xxxx.acme.workers.dev` / `acme.workers.dev`)から
|
|
101
|
+
* workers.dev の**アカウント部**(`acme`)を取り出す(Issue #261)。
|
|
102
|
+
* `.workers.dev` の直前のラベルがアカウント部。workers.dev でないホスト(独自ドメイン)は空。
|
|
103
|
+
*/
|
|
104
|
+
export function extractWorkersSubdomainFromHost(host) {
|
|
105
|
+
const h = host
|
|
106
|
+
.trim()
|
|
107
|
+
.replace(/^https?:\/\//, '')
|
|
108
|
+
.replace(/\/.*$/, '')
|
|
109
|
+
.replace(/:\d+$/, '');
|
|
110
|
+
const m = h.match(/(?:^|\.)([a-z0-9-]+)\.workers\.dev$/i);
|
|
111
|
+
return m ? m[1] : '';
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 管理画面が配信される公開ホスト(workers.dev か独自ドメイン)から、
|
|
115
|
+
* OAuth コールバック・admin オリジン・(workers.dev なら)アカウント部を導く(Issue #261)。
|
|
116
|
+
*
|
|
117
|
+
* deploy-first の検出結果や `--admin-host` 指定値を受け取り、ニワトリ卵だった
|
|
118
|
+
* OAuth コールバック URL を確定させるために使う。
|
|
119
|
+
*/
|
|
120
|
+
export function deriveFromAdminHost(adminHost) {
|
|
121
|
+
const host = adminHost
|
|
122
|
+
.trim()
|
|
123
|
+
.replace(/^https?:\/\//, '')
|
|
124
|
+
.replace(/\/+$/, '');
|
|
125
|
+
const isWorkersDev = /\.workers\.dev$/i.test(host);
|
|
126
|
+
return {
|
|
127
|
+
adminHost: host,
|
|
128
|
+
adminOrigin: `https://${host}`,
|
|
129
|
+
adminCallbackUrl: `https://${host}/api/auth/callback`,
|
|
130
|
+
workersSubdomain: isWorkersDev ? extractWorkersSubdomainFromHost(host) : '',
|
|
131
|
+
isWorkersDev,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* プレビュー URL を導出する(Issue #261)。
|
|
136
|
+
* 独自の staging ドメインがあればそれを優先し(終盤オプショナル工程で設定)、
|
|
137
|
+
* 無ければ workers.dev サブドメインから CI の preview-alias 形式を導く。
|
|
138
|
+
* どちらも無ければ空(=デプロイ後に確定)。
|
|
139
|
+
*/
|
|
140
|
+
export function derivePreviewUrl(opts) {
|
|
141
|
+
if (opts.stagingDomain)
|
|
142
|
+
return `https://${opts.stagingDomain}`;
|
|
143
|
+
const sub = normalizeWorkersSubdomain(opts.workersSubdomain ?? '');
|
|
144
|
+
if (sub)
|
|
145
|
+
return `https://staging-${opts.contentWorkerName}.${sub}.workers.dev`;
|
|
146
|
+
return '';
|
|
147
|
+
}
|
|
64
148
|
/**
|
|
65
149
|
* productionUrl からステージング用の推測困難なカスタムサブドメインを生成する。
|
|
66
150
|
* 例: https://example.com → staging-a3f9c2kp.example.com
|
|
@@ -112,6 +196,9 @@ export function generateConfigJson(config) {
|
|
|
112
196
|
// 予約反映ワーカー(frelio-scheduled-publish)の URL(任意機能・CMS「基本設定」で設定)。
|
|
113
197
|
// 再生成時に既存値を保持する(未設定時のみ空)。
|
|
114
198
|
scheduleApiUrl: config.scheduleApiUrl ?? '',
|
|
199
|
+
// ファイル限定配信ワーカー(frelio-file-delivery)の公開 URL(任意機能・CMS「基本設定」で設定)。
|
|
200
|
+
// 設定の有無が配信フォーム機能の opt-in を兼ねる。再生成時に既存値を保持する(未設定時のみ空)。
|
|
201
|
+
downloadApiUrl: config.downloadApiUrl ?? '',
|
|
115
202
|
siteTitle: config.siteTitle,
|
|
116
203
|
productionUrl: config.productionUrl,
|
|
117
204
|
previewUrl: config.previewUrl,
|
|
@@ -120,6 +207,9 @@ export function generateConfigJson(config) {
|
|
|
120
207
|
templatePreviewBaseUrl: config.templatePreviewBaseUrl || config.previewUrl,
|
|
121
208
|
contentWorkerName: config.contentWorkerName,
|
|
122
209
|
adminWorkerName: config.adminWorkerName,
|
|
210
|
+
// workers.dev サブドメイン(個人プレビュー URL 導出用)。初回は不明なことが多く任意。
|
|
211
|
+
// 再生成時に既存値を保持する(未設定時のみ空)。CMS「基本設定」で後設定可。
|
|
212
|
+
workersSubdomain: config.workersSubdomain ?? '',
|
|
123
213
|
r2BucketName: config.r2BucketName,
|
|
124
214
|
r2PublicUrl: config.r2PublicUrl,
|
|
125
215
|
// allowedOrigins は productionUrl / previewUrl からの派生値(単一ソース / Issue #24)。
|
|
@@ -132,16 +222,24 @@ export function generateConfigJson(config) {
|
|
|
132
222
|
}, null, 2);
|
|
133
223
|
}
|
|
134
224
|
/**
|
|
135
|
-
* CORS 許可オリジンの一覧を生成する(ローカル開発 +
|
|
225
|
+
* CORS 許可オリジンの一覧を生成する(ローカル開発 + 本番/プレビュー + workers.dev)。
|
|
136
226
|
* 管理画面 Worker(/api)と file-upload Worker の双方で使用する。
|
|
227
|
+
*
|
|
228
|
+
* workers.dev サブドメインが分かっていれば、管理画面・content・プレビューの
|
|
229
|
+
* workers.dev オリジンも含める(Issue #261: workers.dev ファースト運用で OAuth/CORS を成立させる)。
|
|
137
230
|
*/
|
|
138
231
|
export function buildAllowedOrigins(config) {
|
|
139
|
-
|
|
232
|
+
const origins = [
|
|
140
233
|
'http://localhost:5173',
|
|
141
234
|
'http://localhost:5174',
|
|
142
235
|
config.productionUrl,
|
|
143
236
|
config.previewUrl,
|
|
144
|
-
]
|
|
237
|
+
];
|
|
238
|
+
const wd = buildWorkersDevUrls(config.workersSubdomain ?? '', config.contentWorkerName, config.adminWorkerName);
|
|
239
|
+
if (wd) {
|
|
240
|
+
origins.push(wd.adminUrl, wd.contentUrl, wd.previewUrl);
|
|
241
|
+
}
|
|
242
|
+
return [...new Set(origins.filter(Boolean))].join(',');
|
|
145
243
|
}
|
|
146
244
|
export function generateWorkerWranglerToml(config) {
|
|
147
245
|
const allowedOrigins = buildAllowedOrigins(config);
|
|
@@ -251,6 +349,53 @@ ALLOWED_ORIGINS = "${allowedOrigins}"
|
|
|
251
349
|
crons = ["0 * * * *"]
|
|
252
350
|
`;
|
|
253
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* ファイル限定配信ワーカー(workers/file-delivery)の wrangler.toml を生成する(任意機能)。
|
|
354
|
+
*
|
|
355
|
+
* 専用プライベート R2 バケット(`<project>-downloads`)と D1(`frelio_downloads`)を持つ。
|
|
356
|
+
* database_id / bucket_name / PUBLIC_BASE_URL 等のユーザー設定を含むため init 時に一度だけ生成し、
|
|
357
|
+
* 以降の update では上書きしない(PROTECTED_CONFIG)。トークン発行は contact ワーカーが
|
|
358
|
+
* Service Binding(DeliveryEntrypoint)で呼ぶ(配線は workers/contact/wrangler.toml 側に追加する)。
|
|
359
|
+
*/
|
|
360
|
+
export function generateFileDeliveryWranglerToml(config, opts) {
|
|
361
|
+
const allowedOrigins = buildAllowedOrigins(config);
|
|
362
|
+
return `name = "frelio-file-delivery"
|
|
363
|
+
main = "src/index.ts"
|
|
364
|
+
compatibility_date = "2024-09-23"
|
|
365
|
+
# WorkerEntrypoint(RPC)を使うため
|
|
366
|
+
compatibility_flags = ["nodejs_compat"]
|
|
367
|
+
|
|
368
|
+
# 限定配信用のプライベート R2 バケット。
|
|
369
|
+
# 重要: このバケットは file-delivery ワーカーだけにバインドする(公開配信 Worker には絶対にバインドしない)。
|
|
370
|
+
[[r2_buckets]]
|
|
371
|
+
binding = "R2"
|
|
372
|
+
bucket_name = "${opts.bucketName}"
|
|
373
|
+
|
|
374
|
+
# ダウンロードトークン台帳
|
|
375
|
+
[[d1_databases]]
|
|
376
|
+
binding = "DB"
|
|
377
|
+
database_name = "frelio_downloads"
|
|
378
|
+
database_id = "${opts.databaseId}"
|
|
379
|
+
migrations_dir = "migrations"
|
|
380
|
+
|
|
381
|
+
[vars]
|
|
382
|
+
# CORS 許可オリジン(CMS 管理画面)。公開ダウンロードゲートは CORS 不要
|
|
383
|
+
ALLOWED_ORIGINS = "${allowedOrigins}"
|
|
384
|
+
# 管理系エンドポイントの認可に使う GitHub リポジトリ(owner/repo)
|
|
385
|
+
CONTENT_REPO = "${config.contentRepo}"
|
|
386
|
+
# users.json のブランチ/パス(canViewDownloads / canEditDownloads をサーバ側で検証。fail-closed)
|
|
387
|
+
ADMIN_DATA_BRANCH = "develop"
|
|
388
|
+
USERS_JSON_PATH = "frelio-data/admin/structure/users.json"
|
|
389
|
+
# このワーカー自身の公開ベース URL。CMS の downloadApiUrl と同じ値(末尾スラッシュ無し)
|
|
390
|
+
PUBLIC_BASE_URL = "${opts.publicBaseUrl}"
|
|
391
|
+
# 失効/期限切れトークンの保持日数(経過後に Cron で削除)。0 で無効
|
|
392
|
+
TOKEN_RETENTION_DAYS = "${opts.tokenRetentionDays ?? '30'}"
|
|
393
|
+
|
|
394
|
+
[triggers]
|
|
395
|
+
# 毎日 03:30 UTC に期限切れ/失効トークンを掃除
|
|
396
|
+
crons = ["30 3 * * *"]
|
|
397
|
+
`;
|
|
398
|
+
}
|
|
254
399
|
/**
|
|
255
400
|
* コンテンツ配信 Worker の siteMode 焼き込みモジュール(worker/site-mode.ts)を生成する(Issue #184)。
|
|
256
401
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c-time/frelio-cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.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.
|
|
27
|
-
"@c-time/frelio-gentl": "^2.
|
|
26
|
+
"@c-time/frelio-data-json": "^2.5.0",
|
|
27
|
+
"@c-time/frelio-gentl": "^2.5.0",
|
|
28
28
|
"commander": "^13.0.0",
|
|
29
29
|
"prompts": "^2.4.2",
|
|
30
30
|
"tar": "^7.0.0"
|