@matra/cli 0.3.2-alpha.0 → 0.3.2-alpha.2
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/package.json +5 -4
- package/src/cli.mjs +56 -5
- package/src/deps.mjs +453 -0
- package/src/project.mjs +1 -0
- package/templates/basic/gitignore +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matra/cli",
|
|
3
|
-
"version": "0.3.2-alpha.
|
|
3
|
+
"version": "0.3.2-alpha.2",
|
|
4
4
|
"description": "Matra project initialization and HTML build tools",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -11,9 +11,10 @@
|
|
|
11
11
|
"templates"
|
|
12
12
|
],
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@matra/
|
|
14
|
+
"@matra/command": "0.1.0",
|
|
15
|
+
"@matra/core": "0.3.1-site.3",
|
|
15
16
|
"@matra/html": "0.3.1-site.0",
|
|
16
|
-
"@matra/site-generation": "0.1.0-site.
|
|
17
|
+
"@matra/site-generation": "0.1.0-site.4",
|
|
17
18
|
"@matra/styles": "0.1.0"
|
|
18
19
|
},
|
|
19
20
|
"engines": {
|
|
@@ -30,7 +31,7 @@
|
|
|
30
31
|
},
|
|
31
32
|
"license": "MIT",
|
|
32
33
|
"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
|
+
"build": "node --check src/cli.mjs && node --check src/project.mjs && node --check src/deps.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
35
|
"check": "pnpm run build",
|
|
35
36
|
"test": "node --test tests/*.test.mjs"
|
|
36
37
|
}
|
package/src/cli.mjs
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFile } from "node:fs/promises"
|
|
3
|
-
import { buildSite } from "@matra/site-generation"
|
|
3
|
+
import { buildSite, getSiteProgramDocuments } from "@matra/site-generation"
|
|
4
|
+
import { installDependencies } from "./deps.mjs"
|
|
4
5
|
import { startDev } from "./dev.mjs"
|
|
5
6
|
import { buildProject, initProject } from "./project.mjs"
|
|
6
7
|
|
|
7
8
|
const usage = `Usage: matra init DIRECTORY
|
|
8
9
|
matra build
|
|
10
|
+
matra install [DOCUMENT] [--yes]
|
|
11
|
+
matra deps install [DOCUMENT] [--yes]
|
|
12
|
+
matra deps install (npm|python) DOCUMENT [--yes]
|
|
13
|
+
matra deps update (npm|python) DOCUMENT [--yes]
|
|
14
|
+
matra site install [--yes]
|
|
9
15
|
matra site build
|
|
10
16
|
matra dev [--port PORT]
|
|
11
17
|
matra --version
|
|
12
18
|
|
|
13
19
|
init Create a new project directory.
|
|
14
20
|
build Build index.md and matra.css into dist/ in the current directory.
|
|
21
|
+
site install Install dependencies for executable site pages and layouts.
|
|
15
22
|
site Build the configured multi-page site into dist/.
|
|
16
23
|
dev Serve on 127.0.0.1:3000 and rebuild on changes. Refresh the browser to view changes.
|
|
24
|
+
deps Install or update npm/Python packages declared by the selected document.
|
|
25
|
+
install Install both ecosystems (default document: index.md); alias of deps install.
|
|
17
26
|
|
|
18
|
-
matra build executes selected JavaScript in index.md.
|
|
19
|
-
matra site build executes
|
|
20
|
-
|
|
27
|
+
matra build executes selected JavaScript and Python in index.md.
|
|
28
|
+
matra site build executes .matra.md pages/layouts and explicitly configured .matra components.
|
|
29
|
+
matra deps install prompts before network access; npm lifecycle scripts are disabled.
|
|
30
|
+
Only build trusted programs; JavaScript and Python execution has no OS sandbox or timeout.`
|
|
21
31
|
|
|
22
32
|
const args = process.argv.slice(2)
|
|
23
33
|
if (args[0] === "--") args.shift()
|
|
@@ -36,6 +46,39 @@ try {
|
|
|
36
46
|
} else if (args[0] === "build" && args.length === 1) {
|
|
37
47
|
const output = await buildProject(process.cwd())
|
|
38
48
|
console.log(`Generated: ${output}`)
|
|
49
|
+
} else if (
|
|
50
|
+
args[0] === "site" &&
|
|
51
|
+
args[1] === "install" &&
|
|
52
|
+
(args.length === 2 || (args.length === 3 && args[2] === "--yes"))
|
|
53
|
+
) {
|
|
54
|
+
const yes = args.length === 3
|
|
55
|
+
const documents = await getSiteProgramDocuments(process.cwd())
|
|
56
|
+
if (documents.length === 0) {
|
|
57
|
+
console.log("No executable site pages or layouts found.")
|
|
58
|
+
} else {
|
|
59
|
+
for (const document of documents) {
|
|
60
|
+
console.log(`Installing dependencies for ${document}`)
|
|
61
|
+
await installDependencies(process.cwd(), "all", document, yes)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} else if (
|
|
65
|
+
args[0] === "install" ||
|
|
66
|
+
(args[0] === "deps" && ["install", "update"].includes(args[1]))
|
|
67
|
+
) {
|
|
68
|
+
const update = args[0] === "deps" && args[1] === "update"
|
|
69
|
+
const operands = args.slice(args[0] === "deps" ? 2 : 1)
|
|
70
|
+
const yes = operands.at(-1) === "--yes"
|
|
71
|
+
if (yes) operands.pop()
|
|
72
|
+
const ecosystem = ["npm", "python"].includes(operands[0]) ? operands.shift() : "all"
|
|
73
|
+
if (
|
|
74
|
+
operands.length > 1 ||
|
|
75
|
+
operands.some(value => !value || value.startsWith("-")) ||
|
|
76
|
+
(ecosystem !== "all" && operands.length !== 1) ||
|
|
77
|
+
(update && ecosystem === "all")
|
|
78
|
+
) {
|
|
79
|
+
throw new Error(usage)
|
|
80
|
+
}
|
|
81
|
+
await installDependencies(process.cwd(), ecosystem, operands[0] ?? "index.md", yes, update)
|
|
39
82
|
} else if (args[0] === "site" && args[1] === "build" && args.length === 2) {
|
|
40
83
|
const outputs = await buildSite(process.cwd())
|
|
41
84
|
console.log(`Generated ${outputs.length} site file(s)`)
|
|
@@ -65,6 +108,14 @@ try {
|
|
|
65
108
|
throw new Error(usage)
|
|
66
109
|
}
|
|
67
110
|
} catch (error) {
|
|
68
|
-
|
|
111
|
+
let message = error.message
|
|
112
|
+
const missingDependencies =
|
|
113
|
+
(error.code === "PY_PACKAGE_LOCK" &&
|
|
114
|
+
/Cannot read matra\.python\.lock\.json|Package is not locked/.test(message)) ||
|
|
115
|
+
(error.code === "JS_INITIALIZE" && /Cannot find package/.test(message))
|
|
116
|
+
if (missingDependencies && error.document) {
|
|
117
|
+
message += `\nRun \`matra install ${error.document}\` to prepare its dependencies.`
|
|
118
|
+
}
|
|
119
|
+
console.error(`${error.code ? `${error.code}: ` : ""}${message}`)
|
|
69
120
|
process.exitCode = 1
|
|
70
121
|
}
|
package/src/deps.mjs
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto"
|
|
2
|
+
import { constants } from "node:fs"
|
|
3
|
+
import {
|
|
4
|
+
access,
|
|
5
|
+
copyFile,
|
|
6
|
+
lstat,
|
|
7
|
+
mkdir,
|
|
8
|
+
mkdtemp,
|
|
9
|
+
readdir,
|
|
10
|
+
readFile,
|
|
11
|
+
realpath,
|
|
12
|
+
rename,
|
|
13
|
+
rm,
|
|
14
|
+
writeFile,
|
|
15
|
+
} from "node:fs/promises"
|
|
16
|
+
import { tmpdir } from "node:os"
|
|
17
|
+
import { delimiter, dirname, isAbsolute, join, relative, resolve, sep } from "node:path"
|
|
18
|
+
import { env, execPath, platform, stdin, stdout } from "node:process"
|
|
19
|
+
import { createInterface } from "node:readline/promises"
|
|
20
|
+
import { authorizePlan, executePlan, planCommands } from "@matra/command"
|
|
21
|
+
import {
|
|
22
|
+
getLiterateDependencyDirectory,
|
|
23
|
+
getPythonPackageTarget,
|
|
24
|
+
readLiteratePackages,
|
|
25
|
+
validatePythonWheels,
|
|
26
|
+
} from "@matra/core/experimental/literate"
|
|
27
|
+
|
|
28
|
+
const lockFile = "matra.python.lock.json"
|
|
29
|
+
const wheelDirectory = "wheels"
|
|
30
|
+
|
|
31
|
+
function packageNameKey(name) {
|
|
32
|
+
return name.replaceAll(/[-_.]+/g, "-").toLowerCase()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parsePackageNames(packages, ecosystem, document) {
|
|
36
|
+
const parsed = []
|
|
37
|
+
for (const value of packages) {
|
|
38
|
+
const valid =
|
|
39
|
+
typeof value === "string" &&
|
|
40
|
+
(ecosystem === "javascript"
|
|
41
|
+
? /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/.test(value)
|
|
42
|
+
: /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(value))
|
|
43
|
+
if (!valid) {
|
|
44
|
+
throw new Error(`${document}: ${ecosystem} dependencies must be package names only: ${value}`)
|
|
45
|
+
}
|
|
46
|
+
if (!parsed.includes(value)) parsed.push(value)
|
|
47
|
+
}
|
|
48
|
+
return parsed
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function validWheelPath(file) {
|
|
52
|
+
return (
|
|
53
|
+
typeof file === "string" &&
|
|
54
|
+
file.endsWith(".whl") &&
|
|
55
|
+
!isAbsolute(file) &&
|
|
56
|
+
!file.includes("\\") &&
|
|
57
|
+
file.split("/").every(part => part && part !== "." && part !== "..")
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function inside(root, target) {
|
|
62
|
+
const path = relative(root, target)
|
|
63
|
+
return path !== ".." && !path.startsWith(`..${sep}`) && !isAbsolute(path)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function readExistingLock(root, pyodide) {
|
|
67
|
+
const path = join(root, lockFile)
|
|
68
|
+
let info
|
|
69
|
+
try {
|
|
70
|
+
info = await lstat(path)
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (error.code === "ENOENT") return new Map()
|
|
73
|
+
throw error
|
|
74
|
+
}
|
|
75
|
+
if (!info.isFile() || info.isSymbolicLink()) {
|
|
76
|
+
throw new Error(`${lockFile} must be a regular file`)
|
|
77
|
+
}
|
|
78
|
+
const lock = JSON.parse(await readFile(path, "utf8"))
|
|
79
|
+
if (
|
|
80
|
+
lock?.format !== "matra-python-wheels-1" ||
|
|
81
|
+
lock.pyodide !== pyodide ||
|
|
82
|
+
!Array.isArray(lock.packages)
|
|
83
|
+
) {
|
|
84
|
+
throw new Error(`${lockFile} must use matra-python-wheels-1 for Pyodide ${pyodide}`)
|
|
85
|
+
}
|
|
86
|
+
const rootPath = await realpath(root)
|
|
87
|
+
const packages = new Map()
|
|
88
|
+
const files = new Set()
|
|
89
|
+
for (const entry of lock.packages) {
|
|
90
|
+
const name = packageNameKey(entry?.name ?? "")
|
|
91
|
+
if (
|
|
92
|
+
!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) ||
|
|
93
|
+
typeof entry.version !== "string" ||
|
|
94
|
+
!/^[A-Za-z0-9][A-Za-z0-9.+!_-]*$/.test(entry.version) ||
|
|
95
|
+
!validWheelPath(entry.file) ||
|
|
96
|
+
entry.name !== name ||
|
|
97
|
+
!/^[a-f0-9]{64}$/.test(entry.sha256) ||
|
|
98
|
+
packages.has(name) ||
|
|
99
|
+
files.has(entry.file)
|
|
100
|
+
) {
|
|
101
|
+
throw new Error(`Invalid or duplicate package entry in ${lockFile}`)
|
|
102
|
+
}
|
|
103
|
+
const path = resolve(rootPath, ...entry.file.split("/"))
|
|
104
|
+
const resolvedPath = await realpath(path)
|
|
105
|
+
const wheel = await lstat(resolvedPath)
|
|
106
|
+
if (!inside(rootPath, resolvedPath) || !wheel.isFile()) {
|
|
107
|
+
throw new Error(`Invalid local wheel path in ${lockFile}: ${entry.file}`)
|
|
108
|
+
}
|
|
109
|
+
const bytes = await readFile(resolvedPath)
|
|
110
|
+
if (createHash("sha256").update(bytes).digest("hex") !== entry.sha256) {
|
|
111
|
+
throw new Error(`Wheel hash mismatch in ${lockFile}: ${entry.file}`)
|
|
112
|
+
}
|
|
113
|
+
const filename = entry.file.split("/").at(-1)
|
|
114
|
+
const wheelParts = filename.match(
|
|
115
|
+
/^(.+)-([0-9][A-Za-z0-9.+!_]*)(?:-[0-9][A-Za-z0-9.]*)?-py3-none-any\.whl$/,
|
|
116
|
+
)
|
|
117
|
+
if (!wheelParts || packageNameKey(wheelParts[1]) !== name || wheelParts[2] !== entry.version) {
|
|
118
|
+
throw new Error(`Wheel filename does not match package entry: ${entry.file}`)
|
|
119
|
+
}
|
|
120
|
+
packages.set(name, { ...entry, bytes })
|
|
121
|
+
files.add(entry.file)
|
|
122
|
+
}
|
|
123
|
+
return packages
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function packageFromWheel(file, bytes) {
|
|
127
|
+
const match = file.match(
|
|
128
|
+
/^(.+)-([0-9][A-Za-z0-9.+!_]*)(?:-[0-9][A-Za-z0-9.]*)?-py3-none-any\.whl$/,
|
|
129
|
+
)
|
|
130
|
+
if (!match) throw new Error(`Only py3-none-any wheels are supported: ${file}`)
|
|
131
|
+
const name = packageNameKey(match[1])
|
|
132
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)) {
|
|
133
|
+
throw new Error(`Invalid Python wheel package name: ${file}`)
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
name,
|
|
137
|
+
version: match[2],
|
|
138
|
+
file: `${wheelDirectory}/${file}`,
|
|
139
|
+
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
140
|
+
bytes,
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function executionPlan(specs) {
|
|
145
|
+
const children = specs.map(({ id, program, args, cwd, writes, requires = [] }) => ({
|
|
146
|
+
tag: "command",
|
|
147
|
+
props: {
|
|
148
|
+
id,
|
|
149
|
+
program,
|
|
150
|
+
args,
|
|
151
|
+
cwd,
|
|
152
|
+
produces: writes,
|
|
153
|
+
requires,
|
|
154
|
+
capabilities: {
|
|
155
|
+
read: [cwd],
|
|
156
|
+
write: writes,
|
|
157
|
+
network: true,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
children: [],
|
|
161
|
+
}))
|
|
162
|
+
return planCommands({ tag: "root", props: {}, children: children })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function npmInvocation() {
|
|
166
|
+
if (platform !== "win32") return { program: "npm", args: [] }
|
|
167
|
+
const candidates = [
|
|
168
|
+
...(env.npm_execpath?.endsWith(".js") ? [env.npm_execpath] : []),
|
|
169
|
+
...(env.PATH ?? "")
|
|
170
|
+
.split(delimiter)
|
|
171
|
+
.filter(Boolean)
|
|
172
|
+
.map(directory => join(directory, "node_modules", "npm", "bin", "npm-cli.js")),
|
|
173
|
+
]
|
|
174
|
+
for (const candidate of candidates) {
|
|
175
|
+
try {
|
|
176
|
+
await access(candidate)
|
|
177
|
+
return { program: execPath, args: [candidate] }
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error.code !== "ENOENT") throw error
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
throw new Error("Cannot locate npm-cli.js; install npm and ensure it is available on PATH")
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function confirmPlan(plan, yes) {
|
|
186
|
+
console.log("The following external commands will run with network access:")
|
|
187
|
+
for (const command of plan.commands) {
|
|
188
|
+
console.log(` ${command.program} ${command.args.map(arg => JSON.stringify(arg)).join(" ")}`)
|
|
189
|
+
}
|
|
190
|
+
if (yes) return
|
|
191
|
+
if (!stdin.isTTY || !stdout.isTTY) {
|
|
192
|
+
throw new Error("Cannot prompt for dependency installation; rerun with --yes to confirm")
|
|
193
|
+
}
|
|
194
|
+
const prompt = createInterface({ input: stdin, output: stdout })
|
|
195
|
+
try {
|
|
196
|
+
const answer = await prompt.question("Continue? [y/N] ")
|
|
197
|
+
if (!/^(y|yes)$/i.test(answer.trim())) throw new Error("Dependency installation cancelled")
|
|
198
|
+
} finally {
|
|
199
|
+
prompt.close()
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function ensureDirectory(path) {
|
|
204
|
+
const target = resolve(path)
|
|
205
|
+
const missing = []
|
|
206
|
+
let current = target
|
|
207
|
+
while (true) {
|
|
208
|
+
try {
|
|
209
|
+
const info = await lstat(current)
|
|
210
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
211
|
+
throw new Error(`Expected a regular directory, not a file or symlink: ${current}`)
|
|
212
|
+
}
|
|
213
|
+
break
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error.code !== "ENOENT") throw error
|
|
216
|
+
missing.push(current)
|
|
217
|
+
const parent = dirname(current)
|
|
218
|
+
if (parent === current) throw new Error(`Cannot find a parent directory for ${target}`)
|
|
219
|
+
current = parent
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
for (const directory of missing.reverse()) {
|
|
223
|
+
try {
|
|
224
|
+
await mkdir(directory)
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (error.code !== "EEXIST") throw error
|
|
227
|
+
}
|
|
228
|
+
const info = await lstat(directory)
|
|
229
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
230
|
+
throw new Error(`Expected a regular directory, not a file or symlink: ${directory}`)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function prepareNpmManifest(directory, packages) {
|
|
236
|
+
await ensureDirectory(directory)
|
|
237
|
+
const manifest = join(directory, "package.json")
|
|
238
|
+
try {
|
|
239
|
+
const info = await lstat(manifest)
|
|
240
|
+
if (!info.isFile() || info.isSymbolicLink()) {
|
|
241
|
+
throw new Error("Generated package.json must be a regular file")
|
|
242
|
+
}
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (error.code !== "ENOENT") throw error
|
|
245
|
+
}
|
|
246
|
+
const contents = {
|
|
247
|
+
private: true,
|
|
248
|
+
type: "module",
|
|
249
|
+
dependencies: Object.fromEntries(packages.map(name => [name, "*"])),
|
|
250
|
+
}
|
|
251
|
+
await writeFile(manifest, `${JSON.stringify(contents, null, 2)}\n`)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function installPythonPackages(root, requirements, staging, document) {
|
|
255
|
+
const requested = new Set(requirements.map(packageNameKey))
|
|
256
|
+
const wheels = []
|
|
257
|
+
for (const file of await readdir(staging)) {
|
|
258
|
+
if (!file.endsWith(".whl")) continue
|
|
259
|
+
const bytes = await readFile(join(staging, file))
|
|
260
|
+
wheels.push(packageFromWheel(file, bytes))
|
|
261
|
+
}
|
|
262
|
+
if (wheels.length === 0) throw new Error("pip download completed without producing any wheels")
|
|
263
|
+
const names = new Set()
|
|
264
|
+
for (const wheel of wheels) {
|
|
265
|
+
if (names.has(wheel.name)) throw new Error(`pip produced duplicate wheels for ${wheel.name}`)
|
|
266
|
+
names.add(wheel.name)
|
|
267
|
+
}
|
|
268
|
+
for (const name of requested) {
|
|
269
|
+
if (!names.has(name)) {
|
|
270
|
+
throw new Error(`pip did not produce the requested Python package: ${name}`)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
await validatePythonWheels(wheels, document)
|
|
274
|
+
|
|
275
|
+
const wheelRoot = join(root, wheelDirectory)
|
|
276
|
+
await ensureDirectory(wheelRoot)
|
|
277
|
+
for (const wheel of wheels) {
|
|
278
|
+
const destination = join(wheelRoot, wheel.file.split("/").at(-1))
|
|
279
|
+
try {
|
|
280
|
+
await copyFile(
|
|
281
|
+
join(staging, wheel.file.split("/").at(-1)),
|
|
282
|
+
destination,
|
|
283
|
+
constants.COPYFILE_EXCL,
|
|
284
|
+
)
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (error.code !== "EEXIST") throw error
|
|
287
|
+
const info = await lstat(destination)
|
|
288
|
+
if (!info.isFile() || info.isSymbolicLink()) {
|
|
289
|
+
throw new Error(`Refusing to overwrite a non-regular wheel file: ${wheel.file}`)
|
|
290
|
+
}
|
|
291
|
+
const existing = await readFile(destination)
|
|
292
|
+
if (!existing.equals(wheel.bytes)) {
|
|
293
|
+
throw new Error(`Refusing to overwrite a different wheel file: ${wheel.file}`)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const lock = {
|
|
298
|
+
format: "matra-python-wheels-1",
|
|
299
|
+
pyodide: getPythonPackageTarget().pyodide,
|
|
300
|
+
packages: wheels
|
|
301
|
+
.map(({ bytes: _bytes, ...entry }) => entry)
|
|
302
|
+
.sort((left, right) => left.name.localeCompare(right.name)),
|
|
303
|
+
}
|
|
304
|
+
const temporary = join(root, `.${lockFile}.${randomUUID()}.tmp`)
|
|
305
|
+
try {
|
|
306
|
+
await writeFile(temporary, `${JSON.stringify(lock, null, 2)}\n`, { flag: "wx" })
|
|
307
|
+
await rename(temporary, join(root, lockFile))
|
|
308
|
+
} finally {
|
|
309
|
+
await rm(temporary, { force: true })
|
|
310
|
+
}
|
|
311
|
+
console.log(`Updated: ${lockFile} (${wheels.length} wheel(s) downloaded)`)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export async function installDependencies(
|
|
315
|
+
root,
|
|
316
|
+
ecosystem,
|
|
317
|
+
documentPath,
|
|
318
|
+
yes = false,
|
|
319
|
+
update = false,
|
|
320
|
+
) {
|
|
321
|
+
if (!["all", "npm", "python"].includes(ecosystem)) {
|
|
322
|
+
throw new Error("Specify an ecosystem: all, npm or python")
|
|
323
|
+
}
|
|
324
|
+
root = await realpath(resolve(root))
|
|
325
|
+
if (!documentPath || isAbsolute(documentPath) || documentPath.split(/[\\/]/).includes("..")) {
|
|
326
|
+
throw new Error("Specify a Markdown document path relative to the project root")
|
|
327
|
+
}
|
|
328
|
+
const document = resolve(root, documentPath)
|
|
329
|
+
if (!inside(root, document)) throw new Error("Document path must remain inside the project root")
|
|
330
|
+
const documentInfo = await lstat(document)
|
|
331
|
+
if (!documentInfo.isFile() || documentInfo.isSymbolicLink()) {
|
|
332
|
+
throw new Error("Document path must be a regular file inside the project root")
|
|
333
|
+
}
|
|
334
|
+
const documentName = relative(root, document)
|
|
335
|
+
const markdown = await readFile(document, "utf8")
|
|
336
|
+
const declared = readLiteratePackages(markdown, { document: documentName })
|
|
337
|
+
const npmPackages =
|
|
338
|
+
ecosystem === "python" ? [] : parsePackageNames(declared.javascript, "javascript", documentName)
|
|
339
|
+
const pythonPackages =
|
|
340
|
+
ecosystem === "npm" ? [] : parsePackageNames(declared.python, "python", documentName)
|
|
341
|
+
if (npmPackages.length === 0 && pythonPackages.length === 0) {
|
|
342
|
+
const label = ecosystem === "all" ? "" : `${ecosystem} `
|
|
343
|
+
console.log(`No ${label}dependencies declared in ${documentName}.`)
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const npmRoot = getLiterateDependencyDirectory(root, documentName, "javascript")
|
|
348
|
+
const pythonRoot = getLiterateDependencyDirectory(root, documentName, "python")
|
|
349
|
+
if (npmPackages.length) await ensureDirectory(npmRoot)
|
|
350
|
+
if (pythonPackages.length) await ensureDirectory(pythonRoot)
|
|
351
|
+
const stagingRoot = pythonPackages.length ? await mkdtemp(join(tmpdir(), "matra-python-")) : null
|
|
352
|
+
try {
|
|
353
|
+
const specs = []
|
|
354
|
+
let npmCommand
|
|
355
|
+
let pythonWheels
|
|
356
|
+
let pythonConstraints
|
|
357
|
+
if (npmPackages.length) {
|
|
358
|
+
npmCommand = await npmInvocation()
|
|
359
|
+
specs.push({
|
|
360
|
+
id: update ? "npm-update" : "npm-install",
|
|
361
|
+
program: npmCommand.program,
|
|
362
|
+
args: [
|
|
363
|
+
...npmCommand.args,
|
|
364
|
+
update ? "update" : "install",
|
|
365
|
+
"--prefix",
|
|
366
|
+
npmRoot,
|
|
367
|
+
"--ignore-scripts",
|
|
368
|
+
"--no-audit",
|
|
369
|
+
"--no-fund",
|
|
370
|
+
],
|
|
371
|
+
cwd: root,
|
|
372
|
+
writes: [npmRoot],
|
|
373
|
+
})
|
|
374
|
+
}
|
|
375
|
+
if (pythonPackages.length) {
|
|
376
|
+
const target = getPythonPackageTarget()
|
|
377
|
+
const oldPackages = update ? new Map() : await readExistingLock(pythonRoot, target.pyodide)
|
|
378
|
+
pythonWheels = join(stagingRoot, "wheels")
|
|
379
|
+
await mkdir(pythonWheels)
|
|
380
|
+
const args = [
|
|
381
|
+
"-m",
|
|
382
|
+
"pip",
|
|
383
|
+
"download",
|
|
384
|
+
"--only-binary=:all:",
|
|
385
|
+
"--implementation",
|
|
386
|
+
"py",
|
|
387
|
+
"--python-version",
|
|
388
|
+
target.python.split(".").slice(0, 2).join("."),
|
|
389
|
+
"--abi",
|
|
390
|
+
"none",
|
|
391
|
+
"--platform",
|
|
392
|
+
"any",
|
|
393
|
+
"--dest",
|
|
394
|
+
pythonWheels,
|
|
395
|
+
]
|
|
396
|
+
if (oldPackages.size) {
|
|
397
|
+
pythonConstraints = join(stagingRoot, "constraints.txt")
|
|
398
|
+
await writeFile(
|
|
399
|
+
pythonConstraints,
|
|
400
|
+
[...oldPackages.values()].map(({ name, version }) => `${name}==${version}\n`).join(""),
|
|
401
|
+
)
|
|
402
|
+
args.push("--constraint", pythonConstraints)
|
|
403
|
+
}
|
|
404
|
+
args.push(...pythonPackages)
|
|
405
|
+
const python =
|
|
406
|
+
process.env.MATRA_PYTHON ?? (process.platform === "win32" ? "python" : "python3")
|
|
407
|
+
specs.push({
|
|
408
|
+
id: "python-wheels",
|
|
409
|
+
program: python,
|
|
410
|
+
args,
|
|
411
|
+
cwd: root,
|
|
412
|
+
writes: [stagingRoot, pythonRoot],
|
|
413
|
+
requires: npmPackages.length ? [update ? "npm-update" : "npm-install"] : [],
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
const plan = executionPlan(specs)
|
|
417
|
+
await confirmPlan(plan, yes)
|
|
418
|
+
if (npmPackages.length) await prepareNpmManifest(npmRoot, npmPackages)
|
|
419
|
+
const authorized = authorizePlan(plan, {
|
|
420
|
+
commands: plan.capabilities.commands,
|
|
421
|
+
read: plan.capabilities.read,
|
|
422
|
+
write: plan.capabilities.write,
|
|
423
|
+
network: true,
|
|
424
|
+
})
|
|
425
|
+
const controller = new AbortController()
|
|
426
|
+
const abort = () => controller.abort()
|
|
427
|
+
process.once("SIGINT", abort)
|
|
428
|
+
process.once("SIGTERM", abort)
|
|
429
|
+
let result
|
|
430
|
+
try {
|
|
431
|
+
result = await executePlan(authorized, {
|
|
432
|
+
signal: controller.signal,
|
|
433
|
+
onEvent: event => {
|
|
434
|
+
if (event.type === "finish" && event.result?.status !== "ok") {
|
|
435
|
+
const details = [event.result?.error, event.result?.stderr?.trim()]
|
|
436
|
+
.filter(Boolean)
|
|
437
|
+
.join(": ")
|
|
438
|
+
console.error(`${event.command.id}: ${details || "failed"}`)
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
})
|
|
442
|
+
} finally {
|
|
443
|
+
process.removeListener("SIGINT", abort)
|
|
444
|
+
process.removeListener("SIGTERM", abort)
|
|
445
|
+
}
|
|
446
|
+
if (result.status !== "ok") throw new Error("Dependency command failed")
|
|
447
|
+
if (pythonPackages.length) {
|
|
448
|
+
await installPythonPackages(pythonRoot, pythonPackages, pythonWheels, documentName)
|
|
449
|
+
}
|
|
450
|
+
} finally {
|
|
451
|
+
if (stagingRoot) await rm(stagingRoot, { recursive: true, force: true })
|
|
452
|
+
}
|
|
453
|
+
}
|
package/src/project.mjs
CHANGED
|
@@ -45,6 +45,7 @@ export async function buildProject(directory) {
|
|
|
45
45
|
await regularFile(license)
|
|
46
46
|
const ast = await renderLiterateDocument(await readFile(source, "utf8"), {
|
|
47
47
|
document: "index.md",
|
|
48
|
+
moduleRoot: root,
|
|
48
49
|
})
|
|
49
50
|
const html = `<!doctype html>\n${toHTML(ast)}\n`
|
|
50
51
|
const output = join(root, "dist")
|
|
@@ -1 +1,11 @@
|
|
|
1
1
|
/dist/
|
|
2
|
+
.matra/*
|
|
3
|
+
!.matra/deps/
|
|
4
|
+
.matra/deps/*
|
|
5
|
+
!.matra/deps/*/
|
|
6
|
+
.matra/deps/*/javascript/**
|
|
7
|
+
!.matra/deps/*/javascript/package-lock.json
|
|
8
|
+
.matra/deps/*/python/**
|
|
9
|
+
!.matra/deps/*/python/matra.python.lock.json
|
|
10
|
+
!.matra/deps/*/python/wheels/
|
|
11
|
+
!.matra/deps/*/python/wheels/*.whl
|