@c-time/frelio-cli 1.6.0 → 1.8.1
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/dist/commands/bake.d.ts +14 -0
- package/dist/commands/bake.js +59 -0
- package/dist/core/bake.d.ts +39 -0
- package/dist/core/bake.js +104 -0
- package/dist/index.js +8 -0
- package/package.json +3 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* frelio bake - テンプレート焼き込み(issue #168)
|
|
3
|
+
*
|
|
4
|
+
* core/bake の runBake を呼ぶ薄いオーケストレーター。
|
|
5
|
+
* 各ページテンプレを gentl で1パス処理し、include(共通クローム)と repeat(実データ)を
|
|
6
|
+
* 解決した結果をテンプレート自身へ書き戻す。サーバー無し(file://)でも見えるプレビューになる。
|
|
7
|
+
*/
|
|
8
|
+
type BakeCommandOptions = {
|
|
9
|
+
dryRun?: boolean;
|
|
10
|
+
check?: boolean;
|
|
11
|
+
pick?: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function bakeCommand(opts: BakeCommandOptions): Promise<void>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* frelio bake - テンプレート焼き込み(issue #168)
|
|
3
|
+
*
|
|
4
|
+
* core/bake の runBake を呼ぶ薄いオーケストレーター。
|
|
5
|
+
* 各ページテンプレを gentl で1パス処理し、include(共通クローム)と repeat(実データ)を
|
|
6
|
+
* 解決した結果をテンプレート自身へ書き戻す。サーバー無し(file://)でも見えるプレビューになる。
|
|
7
|
+
*/
|
|
8
|
+
import { log, logSuccess, logError } from '../lib/shell.js';
|
|
9
|
+
import { runBake } from '../core/bake.js';
|
|
10
|
+
export async function bakeCommand(opts) {
|
|
11
|
+
log('');
|
|
12
|
+
log('🔥 テンプレート焼き込み(bake)');
|
|
13
|
+
log('');
|
|
14
|
+
const pick = opts.pick === 'first' ? 'first' : 'latest';
|
|
15
|
+
if (opts.pick && opts.pick !== 'first' && opts.pick !== 'latest') {
|
|
16
|
+
logError(`不正な --pick 値: "${opts.pick}"(latest | first)`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
const result = await runBake(process.cwd(), {
|
|
20
|
+
dryRun: opts.dryRun,
|
|
21
|
+
check: opts.check,
|
|
22
|
+
pick: pick,
|
|
23
|
+
});
|
|
24
|
+
if (!result.success) {
|
|
25
|
+
logError(result.error);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
const { written, outputs, drift, errors } = result.data;
|
|
29
|
+
log(` 焼き込み対象テンプレ: ${outputs.length}`);
|
|
30
|
+
for (const o of outputs) {
|
|
31
|
+
log(` - ${o.templatePath} ← 代表1件 / ${o.sourceCount}件`);
|
|
32
|
+
}
|
|
33
|
+
if (errors > 0) {
|
|
34
|
+
log('');
|
|
35
|
+
logError(`レンダリングエラー: ${errors} 件(bake-report 相当はログを参照)`);
|
|
36
|
+
}
|
|
37
|
+
log('');
|
|
38
|
+
if (opts.check) {
|
|
39
|
+
if (drift.length > 0) {
|
|
40
|
+
logError(`焼き込みが最新ではありません(${drift.length}件のテンプレに差分):`);
|
|
41
|
+
for (const p of drift)
|
|
42
|
+
log(` - ${p}`);
|
|
43
|
+
log('');
|
|
44
|
+
log(' `npx @c-time/frelio-cli bake` を実行して差分をコミットしてください。');
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
logSuccess('焼き込みは最新です(差分なし)。');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (opts.dryRun) {
|
|
51
|
+
logSuccess('dry-run 完了(テンプレートへの書き戻しは行っていません)。');
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
logSuccess(`${written} 件のテンプレートを焼き込みました。`);
|
|
55
|
+
log('');
|
|
56
|
+
log(' 焼き込み済みテンプレは file:// で直接開いてクローム+実データを確認できます。');
|
|
57
|
+
log(' (アセットは絶対パス参照のため file:// では無スタイル。スタイル込みは `npm run dev`)');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/bake — テンプレート焼き込み(issue #168)
|
|
3
|
+
*
|
|
4
|
+
* 各ページテンプレを gentl で1パス処理し、`data-gen-include`(共通クローム)と
|
|
5
|
+
* `data-gen-repeat`(実サンプルデータ)を解決した結果をテンプレート自身へ書き戻す。
|
|
6
|
+
* 焼き込み済みテンプレはサーバー無し(file://)でもクローム+実データが見えるプレビューになる。
|
|
7
|
+
*
|
|
8
|
+
* 雛形の `scripts/bake-templates.ts` と同一処理(ロジックは @c-time/frelio-gentl の
|
|
9
|
+
* bakeTemplates に集約)。CLI 版は既存サイト(雛形にスクリプトが無い)向け。
|
|
10
|
+
*
|
|
11
|
+
* gentl 出力は `<template data-gen-*>` ソースを保持するため冪等(再ビルド・再 bake 可能)。
|
|
12
|
+
*/
|
|
13
|
+
import { type OperationResult } from './types.js';
|
|
14
|
+
export type BakePick = 'latest' | 'first';
|
|
15
|
+
export type BakeOptions = {
|
|
16
|
+
/** 書き込まず結果のみ返す */
|
|
17
|
+
dryRun?: boolean;
|
|
18
|
+
/** 冪等性検証。書き込まず、焼き込みが最新でなければ drift を返す(CI 用) */
|
|
19
|
+
check?: boolean;
|
|
20
|
+
/** detail テンプレの代表データ選定(既定 latest) */
|
|
21
|
+
pick?: BakePick;
|
|
22
|
+
};
|
|
23
|
+
export type BakeRunResult = {
|
|
24
|
+
/** 焼き込み(書き戻し)したテンプレ数 */
|
|
25
|
+
written: number;
|
|
26
|
+
/** 焼き込み対象テンプレ(templatePath と代表元件数) */
|
|
27
|
+
outputs: {
|
|
28
|
+
templatePath: string;
|
|
29
|
+
sourceCount: number;
|
|
30
|
+
}[];
|
|
31
|
+
/** --check で検出した差分テンプレ(焼き込みが最新でない) */
|
|
32
|
+
drift: string[];
|
|
33
|
+
/** レンダリングエラー件数 */
|
|
34
|
+
errors: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* テンプレートを焼き込んでプロジェクトに書き戻す。
|
|
38
|
+
*/
|
|
39
|
+
export declare function runBake(projectDir: string, opts?: BakeOptions): Promise<OperationResult<BakeRunResult>>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/bake — テンプレート焼き込み(issue #168)
|
|
3
|
+
*
|
|
4
|
+
* 各ページテンプレを gentl で1パス処理し、`data-gen-include`(共通クローム)と
|
|
5
|
+
* `data-gen-repeat`(実サンプルデータ)を解決した結果をテンプレート自身へ書き戻す。
|
|
6
|
+
* 焼き込み済みテンプレはサーバー無し(file://)でもクローム+実データが見えるプレビューになる。
|
|
7
|
+
*
|
|
8
|
+
* 雛形の `scripts/bake-templates.ts` と同一処理(ロジックは @c-time/frelio-gentl の
|
|
9
|
+
* bakeTemplates に集約)。CLI 版は既存サイト(雛形にスクリプトが無い)向け。
|
|
10
|
+
*
|
|
11
|
+
* gentl 出力は `<template data-gen-*>` ソースを保持するため冪等(再ビルド・再 bake 可能)。
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { bakeTemplates, NodeFileSystem, selectLatestRepresentative, selectFirstRepresentative, } from '@c-time/frelio-gentl';
|
|
16
|
+
import { ok, fail } from './types.js';
|
|
17
|
+
const DATA_JSON_ROOT = 'frelio-data/site/data/data-json';
|
|
18
|
+
const TEMPLATE_ROOT = 'frelio-data/site/templates';
|
|
19
|
+
/** gentl(happy-dom)出力は DOCTYPE を含まないため、file:// 直開き用に再付与する */
|
|
20
|
+
function ensureDoctype(html) {
|
|
21
|
+
return /^\s*<!doctype/i.test(html) ? html : `<!DOCTYPE html>\n${html}`;
|
|
22
|
+
}
|
|
23
|
+
function collectJsonFiles(dir) {
|
|
24
|
+
if (!fs.existsSync(dir))
|
|
25
|
+
return [];
|
|
26
|
+
const files = [];
|
|
27
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
28
|
+
const full = path.join(dir, entry.name);
|
|
29
|
+
if (entry.isDirectory()) {
|
|
30
|
+
files.push(...collectJsonFiles(full));
|
|
31
|
+
}
|
|
32
|
+
else if (path.extname(entry.name) === '.json') {
|
|
33
|
+
files.push(full);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return files;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* テンプレートを焼き込んでプロジェクトに書き戻す。
|
|
40
|
+
*/
|
|
41
|
+
export async function runBake(projectDir, opts = {}) {
|
|
42
|
+
const pick = opts.pick ?? 'latest';
|
|
43
|
+
const templateRoot = path.join(projectDir, TEMPLATE_ROOT);
|
|
44
|
+
const dataJsonRoot = path.join(projectDir, DATA_JSON_ROOT);
|
|
45
|
+
if (!fs.existsSync(templateRoot)) {
|
|
46
|
+
return fail(`テンプレートディレクトリが見つかりません: ${TEMPLATE_ROOT}。Frelio プロジェクトのルートで実行してください。`, 'NOT_FOUND');
|
|
47
|
+
}
|
|
48
|
+
if (!fs.existsSync(dataJsonRoot)) {
|
|
49
|
+
return fail(`data-json が見つかりません: ${DATA_JSON_ROOT}。先に data-json を生成してください(npm run generate)。`, 'NOT_FOUND');
|
|
50
|
+
}
|
|
51
|
+
const jsonFiles = collectJsonFiles(dataJsonRoot);
|
|
52
|
+
if (jsonFiles.length === 0) {
|
|
53
|
+
return fail(`data-json が空です。先に data-json を生成してください(npm run generate)。`, 'NOT_FOUND');
|
|
54
|
+
}
|
|
55
|
+
let dataJsons;
|
|
56
|
+
try {
|
|
57
|
+
dataJsons = jsonFiles.map((f) => JSON.parse(fs.readFileSync(f, 'utf-8')));
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
return fail(`data-json の読み込みに失敗しました: ${e.message}`, 'EXEC_FAILED');
|
|
61
|
+
}
|
|
62
|
+
const selectRepresentative = pick === 'first' ? selectFirstRepresentative : selectLatestRepresentative;
|
|
63
|
+
let result;
|
|
64
|
+
try {
|
|
65
|
+
result = await bakeTemplates({
|
|
66
|
+
dataJsons,
|
|
67
|
+
templateRootPath: templateRoot,
|
|
68
|
+
fileSystem: new NodeFileSystem(),
|
|
69
|
+
options: { logLevel: 'quiet' },
|
|
70
|
+
selectRepresentative,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
return fail(`焼き込みに失敗しました: ${e.message}`, 'EXEC_FAILED');
|
|
75
|
+
}
|
|
76
|
+
const drift = [];
|
|
77
|
+
let written = 0;
|
|
78
|
+
for (const output of result.outputs) {
|
|
79
|
+
const fullPath = path.join(templateRoot, output.templatePath);
|
|
80
|
+
const baked = ensureDoctype(output.html);
|
|
81
|
+
if (opts.check) {
|
|
82
|
+
const current = fs.existsSync(fullPath) ? fs.readFileSync(fullPath, 'utf-8') : '';
|
|
83
|
+
if (current !== baked)
|
|
84
|
+
drift.push(output.templatePath);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (opts.dryRun)
|
|
88
|
+
continue;
|
|
89
|
+
// テンプレートは既存ファイル。ディレクトリは作らない(パス不一致=設定ミスを検出)
|
|
90
|
+
if (!fs.existsSync(fullPath))
|
|
91
|
+
continue;
|
|
92
|
+
fs.writeFileSync(fullPath, baked);
|
|
93
|
+
written++;
|
|
94
|
+
}
|
|
95
|
+
return ok({
|
|
96
|
+
written,
|
|
97
|
+
outputs: result.outputs.map((o) => ({
|
|
98
|
+
templatePath: o.templatePath,
|
|
99
|
+
sourceCount: o.sourceCount,
|
|
100
|
+
})),
|
|
101
|
+
drift,
|
|
102
|
+
errors: result.report.stats.errors,
|
|
103
|
+
});
|
|
104
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import { setDomainCommand } from './commands/set-domain.js';
|
|
|
10
10
|
import { setModeCommand } from './commands/set-mode.js';
|
|
11
11
|
import { doctorCommand } from './commands/doctor.js';
|
|
12
12
|
import { fleetCommand } from './commands/fleet.js';
|
|
13
|
+
import { bakeCommand } from './commands/bake.js';
|
|
13
14
|
import { maybeNotifyUpdate } from './core/version-check.js';
|
|
14
15
|
// バージョンは package.json から読む(ハードコードしない)
|
|
15
16
|
const pkg = JSON.parse(readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf-8'));
|
|
@@ -77,6 +78,13 @@ program
|
|
|
77
78
|
.command('set-mode <mode>')
|
|
78
79
|
.description('Set the production gate mode (live | prelaunch | maintenance | closed) and regenerate config + edge middleware (#93)')
|
|
79
80
|
.action(setModeCommand);
|
|
81
|
+
program
|
|
82
|
+
.command('bake')
|
|
83
|
+
.description('Bake shared chrome (data-gen-include) + real sample data (data-gen-repeat) into page templates so they open standalone in a browser (#168)')
|
|
84
|
+
.option('--dry-run', 'Show results without writing back to templates')
|
|
85
|
+
.option('--pick <strategy>', 'Representative data for detail templates: latest | first', 'latest')
|
|
86
|
+
.option('--check', 'Verify bake is up to date (CI); exit non-zero on drift')
|
|
87
|
+
.action(bakeCommand);
|
|
80
88
|
async function main() {
|
|
81
89
|
await program.parseAsync();
|
|
82
90
|
// コマンド完了後に最新版チェック(best-effort、本処理には影響しない)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c-time/frelio-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Frelio CMS setup CLI",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
"prepublishOnly": "npm run build"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@c-time/frelio-data-json": "*",
|
|
27
|
+
"@c-time/frelio-gentl": "*",
|
|
26
28
|
"commander": "^13.0.0",
|
|
27
29
|
"prompts": "^2.4.2",
|
|
28
30
|
"tar": "^7.0.0"
|