@matra/cli 0.3.2 → 0.3.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.ja.md +145 -0
- package/README.md +147 -0
- package/package.json +4 -3
- package/src/cli.mjs +27 -9
- package/src/dev.mjs +94 -11
- package/src/project.mjs +22 -2
- package/templates/blog/.node-version +1 -0
- package/templates/blog/README.md +21 -0
- package/templates/blog/components/Shell.matra +14 -0
- package/templates/blog/layouts/default.matra.md +10 -0
- package/templates/blog/layouts/post.matra.md +12 -0
- package/templates/blog/matra.site.yaml +8 -0
- package/templates/blog/pages/404.matra.md +11 -0
- package/templates/blog/pages/index.matra.md +18 -0
- package/templates/blog/pages/posts/hello.md +7 -0
- package/templates/blog/wrangler.toml +3 -0
package/README.ja.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Matra CLI
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [日本語](./README.ja.md)
|
|
4
|
+
|
|
5
|
+
`@matra/cli`は`matra` commandを提供します。Matra projectの初期化、
|
|
6
|
+
単一ページ文書と複数ページサイトのbuild、front matterで宣言された
|
|
7
|
+
npm/Python依存関係のinstall、localの開発previewを行えます。
|
|
8
|
+
|
|
9
|
+
## インストール
|
|
10
|
+
|
|
11
|
+
Node.js 22または24が必要です。
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install --global @matra/cli
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## クイックスタート
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
matra init hello
|
|
21
|
+
cd hello
|
|
22
|
+
matra build
|
|
23
|
+
matra dev
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`matra init`は、`index.md`(FrontMatter、Matra構造、JavaScript data)、
|
|
27
|
+
`@matra/styles`由来の編集可能な`matra.css`とそのlicense file、
|
|
28
|
+
`.gitignore`を生成します。出力先directoryは既存であってはなりません。
|
|
29
|
+
|
|
30
|
+
`matra build`はcurrent directoryの`index.md`をcompile・実行し、
|
|
31
|
+
`dist/index.html`、`dist/matra.css`、`dist/matra.css.LICENSE`を生成します。
|
|
32
|
+
生成されたHTMLはbrowserで直接開けます。閲覧にnetworkは不要です。
|
|
33
|
+
|
|
34
|
+
`matra dev`は`127.0.0.1:3000`(`--port`で変更可能)でprojectを配信し、
|
|
35
|
+
変更を検知して再buildします。browserをreloadすると変更を確認できます。
|
|
36
|
+
|
|
37
|
+
## SVG出力
|
|
38
|
+
|
|
39
|
+
Matraの出力に`svg` nodeを含めると、`matra build`は生成HTMLへinline SVGとして出力します。
|
|
40
|
+
SVGの属性は明示し、`viewBox`も指定してください。
|
|
41
|
+
|
|
42
|
+
```matra
|
|
43
|
+
html {
|
|
44
|
+
head { title { "SVG example" } }
|
|
45
|
+
body {
|
|
46
|
+
svg(width=64 height=64 viewBox="0 0 64 64") {
|
|
47
|
+
circle(cx=32 cy=32 r=20 fill="tomato") {}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## `@matra/graphics`でグラフを作る
|
|
54
|
+
|
|
55
|
+
JavaScript packageとして`@matra/graphics`を宣言し、build前に`matra install`を実行します。
|
|
56
|
+
`.js` moduleのnamed exportはMatra nodeとして使えるため、生成HTMLへinline SVGで出力されます。
|
|
57
|
+
|
|
58
|
+
````markdown
|
|
59
|
+
---
|
|
60
|
+
matra:
|
|
61
|
+
entry: main.matra
|
|
62
|
+
values: [charts.js]
|
|
63
|
+
javascript:
|
|
64
|
+
packages: ["@matra/graphics"]
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
```main.matra
|
|
68
|
+
html {
|
|
69
|
+
head { title { "四半期売上" } }
|
|
70
|
+
body {
|
|
71
|
+
main {
|
|
72
|
+
h1 { "四半期売上" }
|
|
73
|
+
charts.chart
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```charts.js
|
|
80
|
+
import { lineChart } from "@matra/graphics"
|
|
81
|
+
|
|
82
|
+
export const chart = lineChart(
|
|
83
|
+
[
|
|
84
|
+
{ x: 1, y: 24 },
|
|
85
|
+
{ x: 2, y: 32 },
|
|
86
|
+
{ x: 3, y: 28 },
|
|
87
|
+
],
|
|
88
|
+
{ title: "四半期売上", xLabel: "四半期", yLabel: "売上" },
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
````
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
matra install
|
|
95
|
+
matra build
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## コマンド
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
matra init DIRECTORY
|
|
102
|
+
matra init --template blog DIRECTORY
|
|
103
|
+
matra build
|
|
104
|
+
matra install [DOCUMENT] [--yes]
|
|
105
|
+
matra deps install [DOCUMENT] [--yes]
|
|
106
|
+
matra deps install (npm|python) DOCUMENT [--yes]
|
|
107
|
+
matra deps update (npm|python) DOCUMENT [--yes]
|
|
108
|
+
matra site install [--yes]
|
|
109
|
+
matra site build
|
|
110
|
+
matra site dev [--port PORT]
|
|
111
|
+
matra dev [--port PORT]
|
|
112
|
+
matra --version
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
外部moduleを使うには、文書のfront matterの`matra.javascript.packages`と
|
|
116
|
+
`matra.python.packages`にpackage名を列挙し、`matra install`
|
|
117
|
+
(alias: `matra deps install`)で両ecosystemを準備します。既定の文書は
|
|
118
|
+
`index.md`です。`matra deps install`はnetworkアクセス前に確認を求め、
|
|
119
|
+
npmのlifecycle scriptは無効化されます。
|
|
120
|
+
|
|
121
|
+
## 複数ページサイト
|
|
122
|
+
|
|
123
|
+
CLIは宣言的なsite生成workflowにも対応します。`matra.site.yaml`、
|
|
124
|
+
`matra.site.json`、または`matra.site.matra`を作成し、`pages/`配下に
|
|
125
|
+
contentを置いて、`matra site install`と`matra site build`を実行すると
|
|
126
|
+
`dist/`配下にrouteが生成されます。設定schemaと制限は
|
|
127
|
+
[site-generation契約](../../spec/site-generation.ja.md)を参照してください。
|
|
128
|
+
|
|
129
|
+
CLI `0.3.3`では`matra init --template blog my-blog`でブログを開始できます。`cd my-blog`して
|
|
130
|
+
`matra site dev`を実行してください。記事一覧・共通レイアウト・記事テンプレート・
|
|
131
|
+
本文専用のCSS・公開ファイル・ルートの`404.html`を含むCloudflare Pages設定がそろいます。
|
|
132
|
+
プレビューは設定、記事、テンプレート、部品、画像などの追加・変更・削除を監視します。
|
|
133
|
+
`basePath`と404を反映し、生成したルートとファイルだけを配信します。
|
|
134
|
+
出力先にある未追跡ファイルは配信しません。編集後はブラウザーを再読み込みしてください。
|
|
135
|
+
CLIはsite-generationとstyles `0.2.0`に依存しますが、unifiedは同梱しません。
|
|
136
|
+
利用する場合だけ**ブログ内**で`npm install @matra/markdown-unified@^0.1.0`を実行し、`site.markdown: unified`を指定します。
|
|
137
|
+
|
|
138
|
+
## セキュリティ上の注意
|
|
139
|
+
|
|
140
|
+
信頼できる文書のみをbuildしてください。build中に選択されたJavaScriptと
|
|
141
|
+
Pythonが実行され、OS sandboxやtimeoutはありません。
|
|
142
|
+
|
|
143
|
+
## ライセンス
|
|
144
|
+
|
|
145
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Matra CLI
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [日本語](./README.ja.md)
|
|
4
|
+
|
|
5
|
+
`@matra/cli` provides the `matra` command for initializing Matra projects,
|
|
6
|
+
building single-page documents and multi-page sites, installing declared
|
|
7
|
+
npm/Python dependencies, and serving a local development preview.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Node.js 22 or 24 is required.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install --global @matra/cli
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
matra init hello
|
|
21
|
+
cd hello
|
|
22
|
+
matra build
|
|
23
|
+
matra dev
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`matra init` creates `index.md` (FrontMatter, Matra structure, and JavaScript
|
|
27
|
+
data), an editable `matra.css` from `@matra/styles`, its license file, and a
|
|
28
|
+
`.gitignore`. The destination directory must not already exist.
|
|
29
|
+
|
|
30
|
+
`matra build` compiles and executes `index.md` in the current directory and
|
|
31
|
+
generates `dist/index.html`, `dist/matra.css`, and `dist/matra.css.LICENSE`.
|
|
32
|
+
Open the HTML directly in a browser; viewing needs no network.
|
|
33
|
+
|
|
34
|
+
`matra dev` serves the project on `127.0.0.1:3000` (configurable with
|
|
35
|
+
`--port`) and rebuilds on changes. Refresh the browser to view changes.
|
|
36
|
+
|
|
37
|
+
## SVG output
|
|
38
|
+
|
|
39
|
+
Include an `svg` node in Matra output and `matra build` emits it as inline SVG
|
|
40
|
+
in the generated HTML. Set SVG attributes explicitly, including `viewBox`.
|
|
41
|
+
|
|
42
|
+
```matra
|
|
43
|
+
html {
|
|
44
|
+
head { title { "SVG example" } }
|
|
45
|
+
body {
|
|
46
|
+
svg(width=64 height=64 viewBox="0 0 64 64") {
|
|
47
|
+
circle(cx=32 cy=32 r=20 fill="tomato") {}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Charts with `@matra/graphics`
|
|
54
|
+
|
|
55
|
+
Declare `@matra/graphics` as a JavaScript package, then run `matra install` before
|
|
56
|
+
building. A named export from a `.js` module can be used as a Matra node, so the
|
|
57
|
+
chart is emitted inline in the generated HTML.
|
|
58
|
+
|
|
59
|
+
````markdown
|
|
60
|
+
---
|
|
61
|
+
matra:
|
|
62
|
+
entry: main.matra
|
|
63
|
+
values: [charts.js]
|
|
64
|
+
javascript:
|
|
65
|
+
packages: ["@matra/graphics"]
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
```main.matra
|
|
69
|
+
html {
|
|
70
|
+
head { title { "Quarterly sales" } }
|
|
71
|
+
body {
|
|
72
|
+
main {
|
|
73
|
+
h1 { "Quarterly sales" }
|
|
74
|
+
charts.chart
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```charts.js
|
|
81
|
+
import { lineChart } from "@matra/graphics"
|
|
82
|
+
|
|
83
|
+
export const chart = lineChart(
|
|
84
|
+
[
|
|
85
|
+
{ x: 1, y: 24 },
|
|
86
|
+
{ x: 2, y: 32 },
|
|
87
|
+
{ x: 3, y: 28 },
|
|
88
|
+
],
|
|
89
|
+
{ title: "Quarterly sales", xLabel: "Quarter", yLabel: "Sales" },
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
````
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
matra install
|
|
96
|
+
matra build
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Commands
|
|
100
|
+
|
|
101
|
+
```text
|
|
102
|
+
matra init DIRECTORY
|
|
103
|
+
matra init --template blog DIRECTORY
|
|
104
|
+
matra build
|
|
105
|
+
matra install [DOCUMENT] [--yes]
|
|
106
|
+
matra deps install [DOCUMENT] [--yes]
|
|
107
|
+
matra deps install (npm|python) DOCUMENT [--yes]
|
|
108
|
+
matra deps update (npm|python) DOCUMENT [--yes]
|
|
109
|
+
matra site install [--yes]
|
|
110
|
+
matra site build
|
|
111
|
+
matra site dev [--port PORT]
|
|
112
|
+
matra dev [--port PORT]
|
|
113
|
+
matra --version
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
For external modules, list package names in `matra.javascript.packages` and
|
|
117
|
+
`matra.python.packages` in the document front matter, then run `matra install`
|
|
118
|
+
(alias: `matra deps install`) to prepare both ecosystems. The default document
|
|
119
|
+
is `index.md`. `matra deps install` prompts before network access; npm
|
|
120
|
+
lifecycle scripts are disabled.
|
|
121
|
+
|
|
122
|
+
## Multi-page sites
|
|
123
|
+
|
|
124
|
+
The CLI also supports a declarative site-generation workflow. Create
|
|
125
|
+
`matra.site.yaml`, `matra.site.json`, or `matra.site.matra`, put content under
|
|
126
|
+
`pages/`, then run `matra site install` and `matra site build` to generate
|
|
127
|
+
routes under `dist/`. See the
|
|
128
|
+
[site-generation contract](../../spec/site-generation.md) for the
|
|
129
|
+
configuration schema and limits.
|
|
130
|
+
|
|
131
|
+
With CLI `0.3.3`, run `matra init --template blog my-blog`, `cd my-blog`, then `matra site dev`.
|
|
132
|
+
The starter includes a post index, shared shell, article template and scoped stylesheet, public
|
|
133
|
+
files, and Cloudflare Pages configuration for `dist/404.html`. Preview watches the configuration
|
|
134
|
+
and its article, template, component and asset directories, including additions and deletions.
|
|
135
|
+
Only generated routes and files are served (including the configured `basePath` and custom 404);
|
|
136
|
+
untracked files in the output directory are not exposed. Refresh the browser after editing.
|
|
137
|
+
The CLI includes site-generation and styles `0.2.0`, but not unified. For the preset,
|
|
138
|
+
run `npm install @matra/markdown-unified@^0.1.0` **inside the blog**, then set `site.markdown: unified`.
|
|
139
|
+
|
|
140
|
+
## Security notes
|
|
141
|
+
|
|
142
|
+
Build only trusted documents: selected JavaScript and Python execute during
|
|
143
|
+
the build, without an OS sandbox or timeout.
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matra/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Matra project initialization and HTML build tools",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -14,8 +14,9 @@
|
|
|
14
14
|
"@matra/command": "0.1.0",
|
|
15
15
|
"@matra/core": "0.3.1",
|
|
16
16
|
"@matra/html": "0.3.1",
|
|
17
|
-
"@matra/site-generation": "0.
|
|
18
|
-
"@matra/styles": "0.
|
|
17
|
+
"@matra/site-generation": "0.2.0",
|
|
18
|
+
"@matra/styles": "0.2.0",
|
|
19
|
+
"mime-types": "^3.0.2"
|
|
19
20
|
},
|
|
20
21
|
"engines": {
|
|
21
22
|
"node": "^22.0.0 || ^24.0.0"
|
package/src/cli.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import { startDev } from "./dev.mjs"
|
|
|
6
6
|
import { buildProject, initProject } from "./project.mjs"
|
|
7
7
|
|
|
8
8
|
const usage = `Usage: matra init DIRECTORY
|
|
9
|
+
matra init --template blog DIRECTORY
|
|
9
10
|
matra build
|
|
10
11
|
matra install [DOCUMENT] [--yes]
|
|
11
12
|
matra deps install [DOCUMENT] [--yes]
|
|
@@ -13,6 +14,7 @@ const usage = `Usage: matra init DIRECTORY
|
|
|
13
14
|
matra deps update (npm|python) DOCUMENT [--yes]
|
|
14
15
|
matra site install [--yes]
|
|
15
16
|
matra site build
|
|
17
|
+
matra site dev [--port PORT]
|
|
16
18
|
matra dev [--port PORT]
|
|
17
19
|
matra --version
|
|
18
20
|
|
|
@@ -20,6 +22,7 @@ init Create a new project directory.
|
|
|
20
22
|
build Build index.md and matra.css into dist/ in the current directory.
|
|
21
23
|
site install Install dependencies for executable site pages and layouts.
|
|
22
24
|
site Build the configured multi-page site into dist/.
|
|
25
|
+
site dev Watch site inputs and preview the generated routes locally.
|
|
23
26
|
dev Serve on 127.0.0.1:3000 and rebuild on changes. Refresh the browser to view changes.
|
|
24
27
|
deps Install or update npm/Python packages declared by the selected document.
|
|
25
28
|
install Install both ecosystems (default document: index.md); alias of deps install.
|
|
@@ -40,9 +43,20 @@ try {
|
|
|
40
43
|
await readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
41
44
|
)
|
|
42
45
|
console.log(version)
|
|
43
|
-
} else if (args[0] === "init"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
} else if (args[0] === "init") {
|
|
47
|
+
let operands = args.slice(1)
|
|
48
|
+
let name = "basic"
|
|
49
|
+
const option = operands.indexOf("--template")
|
|
50
|
+
if (option !== -1) {
|
|
51
|
+
if (operands[option + 1] !== "blog") throw new Error(usage)
|
|
52
|
+
name = "blog"
|
|
53
|
+
operands = operands.toSpliced(option, 2)
|
|
54
|
+
}
|
|
55
|
+
if (operands.length !== 1 || operands[0].startsWith("-")) throw new Error(usage)
|
|
56
|
+
const root = await initProject(operands[0], name)
|
|
57
|
+
console.log(
|
|
58
|
+
`Created: ${root}\nNext: cd ${JSON.stringify(operands[0])}\n matra ${name === "blog" ? "site dev" : "build"}`,
|
|
59
|
+
)
|
|
46
60
|
} else if (args[0] === "build" && args.length === 1) {
|
|
47
61
|
const output = await buildProject(process.cwd())
|
|
48
62
|
console.log(`Generated: ${output}`)
|
|
@@ -82,15 +96,19 @@ try {
|
|
|
82
96
|
} else if (args[0] === "site" && args[1] === "build" && args.length === 2) {
|
|
83
97
|
const outputs = await buildSite(process.cwd())
|
|
84
98
|
console.log(`Generated ${outputs.length} site file(s)`)
|
|
85
|
-
} else if (
|
|
86
|
-
args[0] === "
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
99
|
+
} else if (args[0] === "dev" || (args[0] === "site" && args[1] === "dev")) {
|
|
100
|
+
const site = args[0] === "site"
|
|
101
|
+
const operands = args.slice(site ? 2 : 1)
|
|
102
|
+
if (
|
|
103
|
+
operands.length !== 0 &&
|
|
104
|
+
!(operands.length === 2 && operands[0] === "--port" && /^[0-9]+$/.test(operands[1]))
|
|
105
|
+
)
|
|
106
|
+
throw new Error(usage)
|
|
107
|
+
const port = operands.length === 0 ? 3000 : Number(operands[1])
|
|
90
108
|
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
91
109
|
throw new Error("Port must be an integer between 1 and 65535")
|
|
92
110
|
}
|
|
93
|
-
const dev = await startDev(process.cwd(), { port })
|
|
111
|
+
const dev = await startDev(process.cwd(), { port, site })
|
|
94
112
|
console.log(`Serving: ${dev.url}\nWatching for changes. Refresh the browser. Ctrl+C to stop.`)
|
|
95
113
|
let stop
|
|
96
114
|
try {
|
package/src/dev.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { unwatchFile, watchFile } from "node:fs"
|
|
1
|
+
import { realpathSync, unwatchFile, watch, watchFile } from "node:fs"
|
|
2
2
|
import { readFile } from "node:fs/promises"
|
|
3
3
|
import { createServer } from "node:http"
|
|
4
|
-
import { join, resolve } from "node:path"
|
|
4
|
+
import { join, relative, resolve, sep } from "node:path"
|
|
5
|
+
import { buildSitePreview } from "@matra/site-generation"
|
|
6
|
+
import mime from "mime-types"
|
|
5
7
|
import { buildProject } from "./project.mjs"
|
|
6
8
|
|
|
7
9
|
const assets = [
|
|
@@ -12,17 +14,82 @@ const assets = [
|
|
|
12
14
|
|
|
13
15
|
export async function startDev(
|
|
14
16
|
directory,
|
|
15
|
-
{ port = 3000, onBuild = console.log, onError = console.error } = {},
|
|
17
|
+
{ port = 3000, site = false, onBuild = console.log, onError = console.error } = {},
|
|
16
18
|
) {
|
|
17
19
|
const root = resolve(directory)
|
|
18
20
|
let snapshot
|
|
21
|
+
let basePath = ""
|
|
22
|
+
let fallback
|
|
19
23
|
let failure
|
|
20
24
|
let timer
|
|
21
25
|
let building
|
|
22
26
|
let pending = false
|
|
23
27
|
let closed = false
|
|
28
|
+
const watched = new Set()
|
|
29
|
+
const directories = new Map()
|
|
30
|
+
|
|
31
|
+
function fileChanged(current, previous) {
|
|
32
|
+
if (current.nlink || previous.nlink) schedule()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function watchPath(path) {
|
|
36
|
+
if (watched.has(path)) return
|
|
37
|
+
watchFile(path, { interval: 200 }, fileChanged)
|
|
38
|
+
watched.add(path)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function refreshSite() {
|
|
42
|
+
const filesSeen = new Set()
|
|
43
|
+
const dirsSeen = new Set()
|
|
44
|
+
const result = await buildSitePreview(root, (path, directory) => {
|
|
45
|
+
if (!directory) {
|
|
46
|
+
filesSeen.add(path)
|
|
47
|
+
watchPath(path)
|
|
48
|
+
} else if (!dirsSeen.has(path)) {
|
|
49
|
+
dirsSeen.add(path)
|
|
50
|
+
// Windowsでは8.3形式のパスを渡すと、長い通知パスとの比較でlibuvが異常終了する。
|
|
51
|
+
const directoryPath = process.platform === "win32" ? realpathSync.native(path) : path
|
|
52
|
+
const watcher = watch(directoryPath, schedule)
|
|
53
|
+
watcher.on("error", onError)
|
|
54
|
+
// ディレクトリ自体が置き換わった場合も新しい inode を監視する。
|
|
55
|
+
directories.get(path)?.close()
|
|
56
|
+
directories.set(path, watcher)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
for (const path of watched) {
|
|
60
|
+
if (filesSeen.has(path)) continue
|
|
61
|
+
unwatchFile(path, fileChanged)
|
|
62
|
+
watched.delete(path)
|
|
63
|
+
}
|
|
64
|
+
for (const [path, watcher] of directories) {
|
|
65
|
+
if (dirsSeen.has(path)) continue
|
|
66
|
+
watcher.close()
|
|
67
|
+
directories.delete(path)
|
|
68
|
+
}
|
|
69
|
+
const entries = new Map()
|
|
70
|
+
for (const path of result.files) {
|
|
71
|
+
const url = `${result.basePath}/${relative(result.outputDirectory, path).split(sep).join("/")}`
|
|
72
|
+
const type = mime.lookup(path)
|
|
73
|
+
entries.set(url, {
|
|
74
|
+
type: type ? mime.contentType(type) : "application/octet-stream",
|
|
75
|
+
body: await readFile(path),
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
for (const [route, path] of result.routes) {
|
|
79
|
+
entries.set(
|
|
80
|
+
result.basePath + route,
|
|
81
|
+
entries.get(`${result.basePath}/${path.split(sep).join("/")}`),
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
snapshot = entries
|
|
85
|
+
basePath = result.basePath
|
|
86
|
+
fallback = result.notFound ? entries.get(`${basePath}/404.html`) : undefined
|
|
87
|
+
failure = undefined
|
|
88
|
+
onBuild(`Generated ${result.files.length} site file(s)`)
|
|
89
|
+
}
|
|
24
90
|
|
|
25
91
|
async function refresh() {
|
|
92
|
+
if (site) return refreshSite()
|
|
26
93
|
const output = await buildProject(root)
|
|
27
94
|
const entries = await Promise.all(
|
|
28
95
|
assets.map(async ([name, type]) => [
|
|
@@ -66,24 +133,40 @@ export async function startDev(
|
|
|
66
133
|
response.end()
|
|
67
134
|
return
|
|
68
135
|
}
|
|
69
|
-
const
|
|
70
|
-
|
|
136
|
+
const [url] = request.url.split("?")
|
|
137
|
+
let path
|
|
138
|
+
try {
|
|
139
|
+
path = site ? decodeURIComponent(url) : url
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (!(error instanceof URIError)) throw error
|
|
142
|
+
response.writeHead(400)
|
|
143
|
+
response.end()
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
const asset = snapshot?.get(!site && path === "/" ? "/index.html" : path)
|
|
147
|
+
if (site && !asset && snapshot?.has(`${path}/`)) {
|
|
148
|
+
response.writeHead(308, { Location: `${url}/${request.url.slice(url.length)}` })
|
|
149
|
+
response.end()
|
|
150
|
+
return
|
|
151
|
+
}
|
|
71
152
|
const status = !asset ? 404 : failure ? 503 : 200
|
|
72
|
-
const
|
|
153
|
+
const content = asset ?? fallback
|
|
154
|
+
const body = status === 503 ? `Build failed: ${failure}\n` : (content?.body ?? "Not found\n")
|
|
73
155
|
response.writeHead(status, {
|
|
74
|
-
"Content-Type": status
|
|
156
|
+
"Content-Type": status !== 503 && content ? content.type : "text/plain; charset=utf-8",
|
|
75
157
|
})
|
|
76
158
|
response.end(request.method === "HEAD" ? undefined : body)
|
|
77
159
|
})
|
|
78
160
|
|
|
79
|
-
|
|
80
|
-
|
|
161
|
+
if (!site)
|
|
162
|
+
for (const name of ["index.md", "matra.css", "matra.css.LICENSE"]) watchPath(join(root, name))
|
|
81
163
|
|
|
82
164
|
async function close() {
|
|
83
165
|
closed = true
|
|
84
166
|
clearTimeout(timer)
|
|
85
|
-
for (const path of watched) unwatchFile(path, schedule)
|
|
86
167
|
if (building) await building
|
|
168
|
+
for (const path of watched) unwatchFile(path, fileChanged)
|
|
169
|
+
for (const watcher of directories.values()) watcher.close()
|
|
87
170
|
if (server.listening) {
|
|
88
171
|
await new Promise((resolveClose, reject) => {
|
|
89
172
|
server.close(error => (error ? reject(error) : resolveClose()))
|
|
@@ -112,5 +195,5 @@ export async function startDev(
|
|
|
112
195
|
throw error
|
|
113
196
|
}
|
|
114
197
|
server.on("error", onError)
|
|
115
|
-
return { url: `http://127.0.0.1:${server.address().port}/`, close }
|
|
198
|
+
return { url: `http://127.0.0.1:${server.address().port}${basePath}/`, close }
|
|
116
199
|
}
|
package/src/project.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { copyFile, lstat, mkdir, readFile, writeFile } from "node:fs/promises"
|
|
1
|
+
import { copyFile, cp, lstat, mkdir, readdir, readFile, writeFile } from "node:fs/promises"
|
|
2
2
|
import { join, resolve } from "node:path"
|
|
3
3
|
import { toHTML } from "@matra/html"
|
|
4
4
|
import { renderLiterateDocument } from "@matra/site-generation"
|
|
@@ -6,8 +6,28 @@ import { renderLiterateDocument } from "@matra/site-generation"
|
|
|
6
6
|
const template = new URL("../templates/basic/", import.meta.url)
|
|
7
7
|
const stylesheet = new URL(import.meta.resolve("@matra/styles/matra.css"))
|
|
8
8
|
|
|
9
|
-
export async function initProject(directory) {
|
|
9
|
+
export async function initProject(directory, name = "basic") {
|
|
10
10
|
const root = resolve(directory)
|
|
11
|
+
if (name === "blog") {
|
|
12
|
+
const blog = new URL("../templates/blog/", import.meta.url)
|
|
13
|
+
await mkdir(root)
|
|
14
|
+
for (const file of await readdir(blog)) {
|
|
15
|
+
await cp(new URL(file, blog), join(root, file), {
|
|
16
|
+
recursive: true,
|
|
17
|
+
force: false,
|
|
18
|
+
errorOnExist: true,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
await copyFile(new URL("gitignore", template), join(root, ".gitignore"))
|
|
22
|
+
await mkdir(join(root, "public"))
|
|
23
|
+
await copyFile(
|
|
24
|
+
new URL(import.meta.resolve("@matra/styles/prose.css")),
|
|
25
|
+
join(root, "public/prose.css"),
|
|
26
|
+
)
|
|
27
|
+
await copyFile(new URL("LICENSE", stylesheet), join(root, "public/prose.css.LICENSE"))
|
|
28
|
+
return root
|
|
29
|
+
}
|
|
30
|
+
if (name !== "basic") throw new Error(`Unknown project template: ${name}`)
|
|
11
31
|
const files = [
|
|
12
32
|
["index.md", await readFile(new URL("index.md", template))],
|
|
13
33
|
[".gitignore", await readFile(new URL("gitignore", template))],
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
24
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Matra blog
|
|
2
|
+
|
|
3
|
+
Use Node.js **22 or 24** and the Matra CLI that created this project.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
matra site dev
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Open the printed URL. Add a Markdown file in `pages/posts/` with `title` and
|
|
10
|
+
`site: { layout: post }` in its YAML frontmatter; refresh the browser to see it in the index.
|
|
11
|
+
The article template is `layouts/post.matra.md`, and `components/Shell.matra` is shared by all pages.
|
|
12
|
+
Only article bodies use `public/prose.css`. Files in `public/` retain their paths at the site root.
|
|
13
|
+
|
|
14
|
+
For GFM, math, heading IDs, TOC and excerpts, install `@matra/markdown-unified` in this directory
|
|
15
|
+
and add `markdown: unified` under `site` in `matra.site.yaml`.
|
|
16
|
+
|
|
17
|
+
## Cloudflare Pages
|
|
18
|
+
|
|
19
|
+
Run `matra site build` and publish `dist/`. It includes a root `404.html`.
|
|
20
|
+
For direct upload with Wrangler, choose your project `name` in `wrangler.toml`, then
|
|
21
|
+
run `npx wrangler pages deploy dist`. Local generation and preview do not need Wrangler or a network connection.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
fn Shell(props, children) {
|
|
2
|
+
html(lang="en") {
|
|
3
|
+
head {
|
|
4
|
+
meta(charset="utf-8") {}
|
|
5
|
+
meta(name="viewport" content="width=device-width, initial-scale=1") {}
|
|
6
|
+
title { props.title }
|
|
7
|
+
link(rel="stylesheet" href="/prose.css") {}
|
|
8
|
+
}
|
|
9
|
+
body {
|
|
10
|
+
header { a(href="/") { props.siteTitle } }
|
|
11
|
+
main { ...children }
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
matra:
|
|
3
|
+
entry: main.matra
|
|
4
|
+
site:
|
|
5
|
+
data:
|
|
6
|
+
title: Posts
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
```main.matra
|
|
10
|
+
section {
|
|
11
|
+
h1 { page.data.title }
|
|
12
|
+
ul {
|
|
13
|
+
...for (post in site.documents) {
|
|
14
|
+
li { a(href=post.route) { post.metadata.title } }
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
```
|