@matra/cli 0.0.0-stage → 0.3.2-alpha.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/LICENSE +21 -0
- package/package.json +34 -3
- package/src/cli.mjs +70 -0
- package/src/dev.mjs +116 -0
- package/src/project.mjs +67 -0
- package/templates/basic/gitignore +1 -0
- package/templates/basic/index.md +60 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Matra contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matra/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.2-alpha.0",
|
|
4
|
+
"description": "Matra project initialization and HTML build tools",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"matra": "src/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"templates"
|
|
12
|
+
],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@matra/core": "0.3.1-site.0",
|
|
15
|
+
"@matra/html": "0.3.1-site.0",
|
|
16
|
+
"@matra/site-generation": "0.1.0-site.2",
|
|
17
|
+
"@matra/styles": "0.1.0"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": "^22.0.0 || ^24.0.0"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/matralang/matra.git",
|
|
25
|
+
"directory": "packages/cli"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"registry": "https://registry.npmjs.org"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node --check src/cli.mjs && node --check src/project.mjs && node --check src/dev.mjs && node --check scripts/build-distribution.mjs && node --check scripts/verify-distribution.mjs && node --check scripts/distribution-platform.mjs && node --check scripts/verify-release.mjs",
|
|
34
|
+
"check": "pnpm run build",
|
|
35
|
+
"test": "node --test tests/*.test.mjs"
|
|
36
|
+
}
|
|
6
37
|
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFile } from "node:fs/promises"
|
|
3
|
+
import { buildSite } from "@matra/site-generation"
|
|
4
|
+
import { startDev } from "./dev.mjs"
|
|
5
|
+
import { buildProject, initProject } from "./project.mjs"
|
|
6
|
+
|
|
7
|
+
const usage = `Usage: matra init DIRECTORY
|
|
8
|
+
matra build
|
|
9
|
+
matra site build
|
|
10
|
+
matra dev [--port PORT]
|
|
11
|
+
matra --version
|
|
12
|
+
|
|
13
|
+
init Create a new project directory.
|
|
14
|
+
build Build index.md and matra.css into dist/ in the current directory.
|
|
15
|
+
site Build the configured multi-page site into dist/.
|
|
16
|
+
dev Serve on 127.0.0.1:3000 and rebuild on changes. Refresh the browser to view changes.
|
|
17
|
+
|
|
18
|
+
matra build executes selected JavaScript in index.md.
|
|
19
|
+
matra site build executes only explicitly named .matra.md pages and layouts.
|
|
20
|
+
Only build trusted programs; execution has no OS sandbox or timeout.`
|
|
21
|
+
|
|
22
|
+
const args = process.argv.slice(2)
|
|
23
|
+
if (args[0] === "--") args.shift()
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
if (args.length === 1 && (args[0] === "--help" || args[0] === "-h")) {
|
|
27
|
+
console.log(usage)
|
|
28
|
+
} else if (args.length === 1 && (args[0] === "--version" || args[0] === "-v")) {
|
|
29
|
+
const { version } = JSON.parse(
|
|
30
|
+
await readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
31
|
+
)
|
|
32
|
+
console.log(version)
|
|
33
|
+
} else if (args[0] === "init" && args.length === 2 && !args[1].startsWith("-")) {
|
|
34
|
+
const root = await initProject(args[1])
|
|
35
|
+
console.log(`Created: ${root}\nNext: cd ${JSON.stringify(args[1])}\n matra build`)
|
|
36
|
+
} else if (args[0] === "build" && args.length === 1) {
|
|
37
|
+
const output = await buildProject(process.cwd())
|
|
38
|
+
console.log(`Generated: ${output}`)
|
|
39
|
+
} else if (args[0] === "site" && args[1] === "build" && args.length === 2) {
|
|
40
|
+
const outputs = await buildSite(process.cwd())
|
|
41
|
+
console.log(`Generated ${outputs.length} site file(s)`)
|
|
42
|
+
} else if (
|
|
43
|
+
args[0] === "dev" &&
|
|
44
|
+
(args.length === 1 || (args.length === 3 && args[1] === "--port" && /^[0-9]+$/.test(args[2])))
|
|
45
|
+
) {
|
|
46
|
+
const port = args.length === 1 ? 3000 : Number(args[2])
|
|
47
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
48
|
+
throw new Error("Port must be an integer between 1 and 65535")
|
|
49
|
+
}
|
|
50
|
+
const dev = await startDev(process.cwd(), { port })
|
|
51
|
+
console.log(`Serving: ${dev.url}\nWatching for changes. Refresh the browser. Ctrl+C to stop.`)
|
|
52
|
+
let stop
|
|
53
|
+
try {
|
|
54
|
+
await new Promise(resolve => {
|
|
55
|
+
stop = resolve
|
|
56
|
+
process.once("SIGINT", stop)
|
|
57
|
+
process.once("SIGTERM", stop)
|
|
58
|
+
})
|
|
59
|
+
} finally {
|
|
60
|
+
process.removeListener("SIGINT", stop)
|
|
61
|
+
process.removeListener("SIGTERM", stop)
|
|
62
|
+
await dev.close()
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
throw new Error(usage)
|
|
66
|
+
}
|
|
67
|
+
} catch (error) {
|
|
68
|
+
console.error(`${error.code ? `${error.code}: ` : ""}${error.message}`)
|
|
69
|
+
process.exitCode = 1
|
|
70
|
+
}
|
package/src/dev.mjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { unwatchFile, watchFile } from "node:fs"
|
|
2
|
+
import { readFile } from "node:fs/promises"
|
|
3
|
+
import { createServer } from "node:http"
|
|
4
|
+
import { join, resolve } from "node:path"
|
|
5
|
+
import { buildProject } from "./project.mjs"
|
|
6
|
+
|
|
7
|
+
const assets = [
|
|
8
|
+
["index.html", "text/html; charset=utf-8"],
|
|
9
|
+
["matra.css", "text/css; charset=utf-8"],
|
|
10
|
+
["matra.css.LICENSE", "text/plain; charset=utf-8"],
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
export async function startDev(
|
|
14
|
+
directory,
|
|
15
|
+
{ port = 3000, onBuild = console.log, onError = console.error } = {},
|
|
16
|
+
) {
|
|
17
|
+
const root = resolve(directory)
|
|
18
|
+
let snapshot
|
|
19
|
+
let failure
|
|
20
|
+
let timer
|
|
21
|
+
let building
|
|
22
|
+
let pending = false
|
|
23
|
+
let closed = false
|
|
24
|
+
|
|
25
|
+
async function refresh() {
|
|
26
|
+
const output = await buildProject(root)
|
|
27
|
+
const entries = await Promise.all(
|
|
28
|
+
assets.map(async ([name, type]) => [
|
|
29
|
+
`/${name}`,
|
|
30
|
+
{ type, body: await readFile(join(root, "dist", name)) },
|
|
31
|
+
]),
|
|
32
|
+
)
|
|
33
|
+
snapshot = new Map(entries)
|
|
34
|
+
failure = undefined
|
|
35
|
+
onBuild(`Generated: ${output}`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function rebuild() {
|
|
39
|
+
pending = false
|
|
40
|
+
try {
|
|
41
|
+
await refresh()
|
|
42
|
+
} catch (error) {
|
|
43
|
+
failure = `${error.code ? `${error.code}: ` : ""}${error.message}`
|
|
44
|
+
onError(`Build failed: ${failure}\nWaiting for changes.`)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function schedule() {
|
|
49
|
+
if (closed) return
|
|
50
|
+
pending = true
|
|
51
|
+
clearTimeout(timer)
|
|
52
|
+
timer = setTimeout(() => {
|
|
53
|
+
if (building) return
|
|
54
|
+
building = rebuild().finally(() => {
|
|
55
|
+
building = undefined
|
|
56
|
+
if (pending && !closed) schedule()
|
|
57
|
+
})
|
|
58
|
+
}, 100)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const server = createServer((request, response) => {
|
|
62
|
+
response.setHeader("Cache-Control", "no-store")
|
|
63
|
+
response.setHeader("X-Content-Type-Options", "nosniff")
|
|
64
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
65
|
+
response.writeHead(405, { Allow: "GET, HEAD" })
|
|
66
|
+
response.end()
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
const path = request.url.split("?")[0]
|
|
70
|
+
const asset = snapshot?.get(path === "/" ? "/index.html" : path)
|
|
71
|
+
const status = !asset ? 404 : failure ? 503 : 200
|
|
72
|
+
const body = !asset ? "Not found\n" : failure ? `Build failed: ${failure}\n` : asset.body
|
|
73
|
+
response.writeHead(status, {
|
|
74
|
+
"Content-Type": status === 200 ? asset.type : "text/plain; charset=utf-8",
|
|
75
|
+
})
|
|
76
|
+
response.end(request.method === "HEAD" ? undefined : body)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const watched = ["index.md", "matra.css", "matra.css.LICENSE"].map(name => join(root, name))
|
|
80
|
+
for (const path of watched) watchFile(path, { interval: 200 }, schedule)
|
|
81
|
+
|
|
82
|
+
async function close() {
|
|
83
|
+
closed = true
|
|
84
|
+
clearTimeout(timer)
|
|
85
|
+
for (const path of watched) unwatchFile(path, schedule)
|
|
86
|
+
if (building) await building
|
|
87
|
+
if (server.listening) {
|
|
88
|
+
await new Promise((resolveClose, reject) => {
|
|
89
|
+
server.close(error => (error ? reject(error) : resolveClose()))
|
|
90
|
+
server.closeAllConnections()
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
building = refresh()
|
|
97
|
+
try {
|
|
98
|
+
await building
|
|
99
|
+
} finally {
|
|
100
|
+
building = undefined
|
|
101
|
+
}
|
|
102
|
+
if (pending) schedule()
|
|
103
|
+
await new Promise((resolveListen, reject) => {
|
|
104
|
+
server.once("error", reject)
|
|
105
|
+
server.listen(port, "127.0.0.1", () => {
|
|
106
|
+
server.removeListener("error", reject)
|
|
107
|
+
resolveListen()
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
} catch (error) {
|
|
111
|
+
await close()
|
|
112
|
+
throw error
|
|
113
|
+
}
|
|
114
|
+
server.on("error", onError)
|
|
115
|
+
return { url: `http://127.0.0.1:${server.address().port}/`, close }
|
|
116
|
+
}
|
package/src/project.mjs
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { copyFile, lstat, mkdir, readFile, writeFile } from "node:fs/promises"
|
|
2
|
+
import { join, resolve } from "node:path"
|
|
3
|
+
import { toHTML } from "@matra/html"
|
|
4
|
+
import { renderLiterateDocument } from "@matra/site-generation"
|
|
5
|
+
|
|
6
|
+
const template = new URL("../templates/basic/", import.meta.url)
|
|
7
|
+
const stylesheet = new URL(import.meta.resolve("@matra/styles/matra.css"))
|
|
8
|
+
|
|
9
|
+
export async function initProject(directory) {
|
|
10
|
+
const root = resolve(directory)
|
|
11
|
+
const files = [
|
|
12
|
+
["index.md", await readFile(new URL("index.md", template))],
|
|
13
|
+
[".gitignore", await readFile(new URL("gitignore", template))],
|
|
14
|
+
["matra.css", await readFile(stylesheet)],
|
|
15
|
+
["matra.css.LICENSE", await readFile(new URL("LICENSE", stylesheet))],
|
|
16
|
+
]
|
|
17
|
+
await mkdir(root)
|
|
18
|
+
for (const [name, content] of files) {
|
|
19
|
+
await writeFile(join(root, name), content, { flag: "wx" })
|
|
20
|
+
}
|
|
21
|
+
return root
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function regularFile(path) {
|
|
25
|
+
if (!(await lstat(path)).isFile()) {
|
|
26
|
+
throw new Error(`Expected a regular file, not a directory or symlink: ${path}`)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function outputFile(path) {
|
|
31
|
+
try {
|
|
32
|
+
await regularFile(path)
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error.code !== "ENOENT") throw error
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function buildProject(directory) {
|
|
39
|
+
const root = resolve(directory)
|
|
40
|
+
const source = join(root, "index.md")
|
|
41
|
+
const css = join(root, "matra.css")
|
|
42
|
+
const license = join(root, "matra.css.LICENSE")
|
|
43
|
+
await regularFile(source)
|
|
44
|
+
await regularFile(css)
|
|
45
|
+
await regularFile(license)
|
|
46
|
+
const ast = await renderLiterateDocument(await readFile(source, "utf8"), {
|
|
47
|
+
document: "index.md",
|
|
48
|
+
})
|
|
49
|
+
const html = `<!doctype html>\n${toHTML(ast)}\n`
|
|
50
|
+
const output = join(root, "dist")
|
|
51
|
+
try {
|
|
52
|
+
await mkdir(output)
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error.code !== "EEXIST") throw error
|
|
55
|
+
if (!(await lstat(output)).isDirectory()) {
|
|
56
|
+
throw new Error(`Expected an output directory, not a file or symlink: ${output}`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const htmlOutput = join(output, "index.html")
|
|
60
|
+
const cssOutput = join(output, "matra.css")
|
|
61
|
+
const licenseOutput = join(output, "matra.css.LICENSE")
|
|
62
|
+
for (const path of [htmlOutput, cssOutput, licenseOutput]) await outputFile(path)
|
|
63
|
+
await writeFile(htmlOutput, html, "utf8")
|
|
64
|
+
await copyFile(css, cssOutput)
|
|
65
|
+
await copyFile(license, licenseOutput)
|
|
66
|
+
return htmlOutput
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/dist/
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
matra:
|
|
3
|
+
entry: main.matra
|
|
4
|
+
values:
|
|
5
|
+
- content.js.part
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Hello, Matra
|
|
9
|
+
|
|
10
|
+
Edit this document, then run `matra build`.
|
|
11
|
+
Open `dist/index.html` in a browser. No network connection is needed to view it.
|
|
12
|
+
For a local preview, run `matra dev` and open the URL printed in the terminal.
|
|
13
|
+
Changes rebuild automatically; refresh the browser to see them. Press Ctrl+C to stop.
|
|
14
|
+
|
|
15
|
+
## Page structure
|
|
16
|
+
|
|
17
|
+
```main.matra
|
|
18
|
+
html(lang="en") {
|
|
19
|
+
head {
|
|
20
|
+
meta(charset="utf-8") {}
|
|
21
|
+
meta(name="viewport" content="width=device-width, initial-scale=1") {}
|
|
22
|
+
title { content.title }
|
|
23
|
+
link(rel="stylesheet" href="./matra.css") {}
|
|
24
|
+
}
|
|
25
|
+
body {
|
|
26
|
+
header {
|
|
27
|
+
h1 { content.title }
|
|
28
|
+
}
|
|
29
|
+
main {
|
|
30
|
+
p { content.message }
|
|
31
|
+
h2 { "Get started" }
|
|
32
|
+
ol {
|
|
33
|
+
...for (step in content.steps) {
|
|
34
|
+
li { step }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
footer {
|
|
39
|
+
p { "Built with Matra." }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Page content
|
|
46
|
+
|
|
47
|
+
The selected JavaScript expression below supplies the page data.
|
|
48
|
+
Only build documents you trust: their JavaScript is executed during the build.
|
|
49
|
+
|
|
50
|
+
```content.js.part
|
|
51
|
+
({
|
|
52
|
+
title: "Hello, Matra!",
|
|
53
|
+
message: "Your first Matra page is ready.",
|
|
54
|
+
steps: [
|
|
55
|
+
"Edit index.md to change the structure and content.",
|
|
56
|
+
"Edit matra.css to customize the appearance.",
|
|
57
|
+
"Run matra build and open dist/index.html."
|
|
58
|
+
]
|
|
59
|
+
})
|
|
60
|
+
```
|
package/README.md
DELETED