linopress-cli 0.0.0-stage → 0.1.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/README.md +136 -2
- package/bin/linopress.js +3 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +627 -0
- package/dist/config-B5ZjA9MO.js +143 -0
- package/dist/config.d.ts +68 -0
- package/dist/config.js +1 -0
- package/dist/index.d.ts +261 -0
- package/dist/index.js +3 -0
- package/dist/linear.d.ts +66 -0
- package/dist/linear.js +1 -0
- package/dist/markdown.d.ts +113 -0
- package/dist/markdown.js +1 -0
- package/dist/model-CgSIC_VE.d.ts +42 -0
- package/dist/model-DAbyGE8k.js +185 -0
- package/dist/src-6dU0mCpU.js +227 -0
- package/dist/src-BU0LDvW9.js +593 -0
- package/dist/src-vhdzyr4k.js +160 -0
- package/dist/src-zk0Kzn3v.js +249 -0
- package/dist/target-Cb9cQbDZ.d.ts +48 -0
- package/dist/transforms-BBEyouxz.d.ts +81 -0
- package/dist/typedoc.d.ts +38 -0
- package/dist/typedoc.js +1 -0
- package/package.json +86 -3
package/dist/cli.js
ADDED
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { realpathSync } from "node:fs";
|
|
5
|
+
import { readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
6
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
7
|
+
import { parseArgs } from "node:util";
|
|
8
|
+
import { c as renderDoc, g as collectAssets, i as isEmptyPlan, n as apply, o as pendingUrl, s as plan } from "./src-BU0LDvW9.js";
|
|
9
|
+
import { a as serializeManifest, i as parseManifest, o as layoutPages, r as emptyManifest, s as formatPlan, t as containerKey } from "./model-DAbyGE8k.js";
|
|
10
|
+
import { n as LinearTarget, t as linearTransforms } from "./src-6dU0mCpU.js";
|
|
11
|
+
import { a as applyFiles, i as adoptedPaths, l as planFiles, n as renderMarkdownEdition, o as formatFilePlan, s as isEmptyFilePlan, t as markdownTransforms, u as readOutputState } from "./src-zk0Kzn3v.js";
|
|
12
|
+
import { n as readMarkdownPages, t as generateMarkdown } from "./src-vhdzyr4k.js";
|
|
13
|
+
import { i as overlaps, r as loadConfig } from "./config-B5ZjA9MO.js";
|
|
14
|
+
// src/footer.ts
|
|
15
|
+
const toPosix = (path) => path.split(sep).join("/");
|
|
16
|
+
const code = (text) => `\`${text}\``;
|
|
17
|
+
/**
|
|
18
|
+
* Turns a repository reference into a web URL: an `https://` URL, an SSH remote
|
|
19
|
+
* (`git@github.com:acme/billing.git`, `ssh://git@github.com/acme/billing`), a URL without a scheme
|
|
20
|
+
* (`github.com/acme/billing`) or GitHub shorthand (`acme/billing`). Returns `null` when it isn't
|
|
21
|
+
* recognized, including SSH host aliases (`git@github-work:acme/billing`), whose web host isn't known.
|
|
22
|
+
*/
|
|
23
|
+
function repositoryWebUrl(ref) {
|
|
24
|
+
const trimmed = ref.trim();
|
|
25
|
+
let host;
|
|
26
|
+
let path;
|
|
27
|
+
const scp = /^[\w.-]+@([\w.-]+):(?!\/)(.+)$/.exec(trimmed);
|
|
28
|
+
const bare = /^([\w-]+(?:\.[\w-]+)+(?::\d+)?)\/([^/\s]+\/[^\s]+)$/.exec(trimmed);
|
|
29
|
+
if (scp?.[1] && scp[2]) {
|
|
30
|
+
host = scp[1];
|
|
31
|
+
path = scp[2];
|
|
32
|
+
} else if (/^[\w-]+\/(?!\.)[\w.-]+$/.test(trimmed)) {
|
|
33
|
+
host = "github.com";
|
|
34
|
+
path = trimmed;
|
|
35
|
+
} else if (bare?.[1] && bare[2]) {
|
|
36
|
+
host = bare[1];
|
|
37
|
+
path = bare[2];
|
|
38
|
+
} else {
|
|
39
|
+
let url;
|
|
40
|
+
try {
|
|
41
|
+
url = new URL(trimmed);
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
if (![
|
|
46
|
+
"https:",
|
|
47
|
+
"http:",
|
|
48
|
+
"ssh:",
|
|
49
|
+
"git:",
|
|
50
|
+
"git+ssh:"
|
|
51
|
+
].includes(url.protocol)) return null;
|
|
52
|
+
host = url.protocol.startsWith("http") ? url.host : url.hostname;
|
|
53
|
+
path = url.pathname;
|
|
54
|
+
}
|
|
55
|
+
if (!/^[\w-]+(?:\.[\w-]+)+(?::\d+)?$/.test(host)) return null;
|
|
56
|
+
const clean = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/, "");
|
|
57
|
+
if (!clean || clean.split("/").some((part) => part === "." || part === "..")) return null;
|
|
58
|
+
return `https://${host}/${clean}`;
|
|
59
|
+
}
|
|
60
|
+
/** URL shapes differ per host; anything unknown is assumed to be GitHub-like (GitHub, Gitea, Forgejo). */
|
|
61
|
+
function forgeOf(repositoryUrl) {
|
|
62
|
+
const host = new URL(repositoryUrl).hostname;
|
|
63
|
+
if (host === "bitbucket.org" || host.startsWith("bitbucket.")) return "bitbucket";
|
|
64
|
+
if (host === "gitlab.com" || host.startsWith("gitlab.")) return "gitlab";
|
|
65
|
+
return "github";
|
|
66
|
+
}
|
|
67
|
+
/** Link to the commit `sha` on the repository's web host. */
|
|
68
|
+
function commitUrl(repository, sha) {
|
|
69
|
+
const segment = {
|
|
70
|
+
github: "commit",
|
|
71
|
+
gitlab: "-/commit",
|
|
72
|
+
bitbucket: "commits"
|
|
73
|
+
}[forgeOf(repository.url)];
|
|
74
|
+
return `${repository.url}/${segment}/${sha}`;
|
|
75
|
+
}
|
|
76
|
+
/** Link to `file` at `sha`, or `null` when the file is outside the repository. */
|
|
77
|
+
function sourceUrl(repository, sha, file) {
|
|
78
|
+
const rel = relative(repository.root, file);
|
|
79
|
+
if (!rel || rel.startsWith("..") || isAbsolute(rel)) return null;
|
|
80
|
+
const path = toPosix(rel).split("/").map(encodeURIComponent).join("/");
|
|
81
|
+
const segment = {
|
|
82
|
+
github: "blob",
|
|
83
|
+
gitlab: "-/blob",
|
|
84
|
+
bitbucket: "src"
|
|
85
|
+
}[forgeOf(repository.url)];
|
|
86
|
+
return `${repository.url}/${segment}/${sha}/${path}`;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The note appended to every doc: which source files it was generated from (linked to the code at
|
|
90
|
+
* that commit when the repository is known) and that edits belong in the repo. Pages without known
|
|
91
|
+
* sources name the page and the edition's source instead. Never part of the content hash.
|
|
92
|
+
*/
|
|
93
|
+
function makeFooter(options) {
|
|
94
|
+
const { sha, repository } = options;
|
|
95
|
+
const shortSha = sha?.slice(0, 7);
|
|
96
|
+
const at = shortSha ? ` at ${repository && sha ? `[${code(shortSha)}](${commitUrl(repository, sha)})` : code(shortSha)}` : "";
|
|
97
|
+
return (node) => {
|
|
98
|
+
const files = (node.sources ?? []).map((file) => {
|
|
99
|
+
const shown = code(toPosix(relative(options.displayBase, file)));
|
|
100
|
+
const url = repository && sha ? sourceUrl(repository, sha, file) : null;
|
|
101
|
+
return url ? `[${shown}](${url})` : shown;
|
|
102
|
+
});
|
|
103
|
+
return `---\n\n_Generated by Linopress from ${files.length > 0 ? files.join(", ") : `${code(node.path)} (${code(options.source)})`}${at}. Edit the doc comments in the repo; changes made here will be overwritten._
|
|
104
|
+
`;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The comment put at the top of every generated Markdown file: which source files it comes from and
|
|
109
|
+
* that edits belong there. Unlike the Linear footer it has no commit sha, so a new commit alone
|
|
110
|
+
* doesn't rewrite every file of a committed docs folder.
|
|
111
|
+
*/
|
|
112
|
+
function makeHeader(options) {
|
|
113
|
+
return (node) => {
|
|
114
|
+
const files = (node.sources ?? []).map((file) => toPosix(relative(options.displayBase, file)));
|
|
115
|
+
const from = files.length > 0 ? files.join(", ") : options.source;
|
|
116
|
+
const edit = files.length > 0 ? "Edit the doc comments there" : "Edit the source";
|
|
117
|
+
return `<!-- Generated by Linopress from ${from.replace(/-(?=-)/g, "-\\")}. ${edit}; changes to this file will be overwritten. -->`;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// src/run.ts
|
|
121
|
+
const USAGE = `Usage: linopress <command> [edition | --all] [options]
|
|
122
|
+
|
|
123
|
+
Commands
|
|
124
|
+
plan Show what publishing would change (never writes)
|
|
125
|
+
push Publish an edition to its target: Linear, or Markdown files (--dry-run to only plan)
|
|
126
|
+
print Print the Markdown each page would get, without writing anything
|
|
127
|
+
|
|
128
|
+
Editions come from linopress.config.ts; --all runs the command for every edition in it.
|
|
129
|
+
|
|
130
|
+
Source (replaces the edition's source)
|
|
131
|
+
--typedoc <file> TypeDoc options file to run (typedoc-plugin-markdown)
|
|
132
|
+
--dir <dir> Folder of already-generated Markdown
|
|
133
|
+
|
|
134
|
+
Target (replaces the edition's target)
|
|
135
|
+
--team <id> Linear team to hold the docs
|
|
136
|
+
--project <id> Linear project to hold the docs
|
|
137
|
+
--out <dir> Write Markdown files to this folder instead of Linear
|
|
138
|
+
|
|
139
|
+
Markdown
|
|
140
|
+
--force push: replace files in the output folder that Linopress didn't write
|
|
141
|
+
|
|
142
|
+
Linear
|
|
143
|
+
--flat Top-level docs with title prefixes instead of nesting under the index
|
|
144
|
+
--manifest <file> Sync state file (default .linopress-manifest.json)
|
|
145
|
+
|
|
146
|
+
Other
|
|
147
|
+
--title <title> Index page title / flat prefix (default "API Reference")
|
|
148
|
+
--all Run for every edition in the config
|
|
149
|
+
--config <file> Config file (default linopress.config.{ts,mts,js,mjs})
|
|
150
|
+
--dry-run push: plan only, change nothing
|
|
151
|
+
--verbose plan: also list unchanged docs and files
|
|
152
|
+
-h, --help
|
|
153
|
+
|
|
154
|
+
Environment
|
|
155
|
+
LINEAR_API_KEY Linear personal API key, required to push to Linear`;
|
|
156
|
+
var UsageError = class extends Error {};
|
|
157
|
+
const SOURCE_FLAGS = ["typedoc", "dir"];
|
|
158
|
+
const TARGET_FLAGS = [
|
|
159
|
+
"team",
|
|
160
|
+
"project",
|
|
161
|
+
"out"
|
|
162
|
+
];
|
|
163
|
+
/** Per-edition settings that make no sense applied to every edition at once. */
|
|
164
|
+
const EDITION_FLAGS = [
|
|
165
|
+
"flat",
|
|
166
|
+
"title",
|
|
167
|
+
"manifest"
|
|
168
|
+
];
|
|
169
|
+
function resolveEdition(loaded, editionName, flags, cwd) {
|
|
170
|
+
const config = loaded?.config;
|
|
171
|
+
const configDir = loaded?.dir ?? cwd;
|
|
172
|
+
const names = Object.keys(config?.editions ?? {});
|
|
173
|
+
const name = editionName ?? (names.length === 1 ? names[0] : void 0);
|
|
174
|
+
if (editionName && !config?.editions[editionName]) throw new UsageError(`Unknown edition "${editionName}"${names.length ? ` (have: ${names.join(", ")})` : " (no config file)"}`);
|
|
175
|
+
const fromConfig = name ? config?.editions[name] : void 0;
|
|
176
|
+
const str = (k) => typeof flags[k] === "string" ? flags[k] : void 0;
|
|
177
|
+
const typedoc = str("typedoc");
|
|
178
|
+
const dir = str("dir");
|
|
179
|
+
if (!name && names.length > 1 && !typedoc && !dir) throw new UsageError(`Pick an edition (${names.join(", ")}) or pass --all`);
|
|
180
|
+
const typedocOptions = fromConfig?.typedocOptions && {
|
|
181
|
+
typedocOptions: fromConfig.typedocOptions,
|
|
182
|
+
typedocOptionsBase: configDir
|
|
183
|
+
};
|
|
184
|
+
const source = typedoc ? {
|
|
185
|
+
typedoc: resolve(cwd, typedoc),
|
|
186
|
+
...typedocOptions
|
|
187
|
+
} : dir ? { dir: resolve(cwd, dir) } : {
|
|
188
|
+
...fromConfig?.typedoc && { typedoc: resolve(configDir, fromConfig.typedoc) },
|
|
189
|
+
...typedocOptions,
|
|
190
|
+
...fromConfig?.dir && { dir: resolve(configDir, fromConfig.dir) }
|
|
191
|
+
};
|
|
192
|
+
const team = str("team");
|
|
193
|
+
const project = str("project");
|
|
194
|
+
const out = str("out");
|
|
195
|
+
if ([
|
|
196
|
+
team,
|
|
197
|
+
project,
|
|
198
|
+
out
|
|
199
|
+
].filter(Boolean).length > 1) throw new UsageError("Pass one of --team, --project or --out");
|
|
200
|
+
const configuredLayout = fromConfig?.target === "linear" ? fromConfig.layout : void 0;
|
|
201
|
+
const mode = flags["flat"] ? "flat" : configuredLayout ?? "nested";
|
|
202
|
+
let target;
|
|
203
|
+
if (out) target = {
|
|
204
|
+
kind: "markdown",
|
|
205
|
+
out: resolve(cwd, out)
|
|
206
|
+
};
|
|
207
|
+
else if (team) target = {
|
|
208
|
+
kind: "linear",
|
|
209
|
+
container: {
|
|
210
|
+
type: "team",
|
|
211
|
+
id: team
|
|
212
|
+
},
|
|
213
|
+
mode
|
|
214
|
+
};
|
|
215
|
+
else if (project) target = {
|
|
216
|
+
kind: "linear",
|
|
217
|
+
container: {
|
|
218
|
+
type: "project",
|
|
219
|
+
id: project
|
|
220
|
+
},
|
|
221
|
+
mode
|
|
222
|
+
};
|
|
223
|
+
else if (fromConfig?.target === "markdown") target = {
|
|
224
|
+
kind: "markdown",
|
|
225
|
+
out: resolve(configDir, fromConfig.out)
|
|
226
|
+
};
|
|
227
|
+
else {
|
|
228
|
+
const c = fromConfig;
|
|
229
|
+
target = {
|
|
230
|
+
kind: "linear",
|
|
231
|
+
container: c?.team ? {
|
|
232
|
+
type: "team",
|
|
233
|
+
id: c.team
|
|
234
|
+
} : c?.project ? {
|
|
235
|
+
type: "project",
|
|
236
|
+
id: c.project
|
|
237
|
+
} : null,
|
|
238
|
+
mode
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
if (target.kind === "markdown" && flags["flat"]) throw new UsageError("--flat only applies to Linear; Markdown files keep the source's folders");
|
|
242
|
+
return {
|
|
243
|
+
name,
|
|
244
|
+
source,
|
|
245
|
+
target,
|
|
246
|
+
title: str("title") ?? fromConfig?.title ?? "API Reference",
|
|
247
|
+
manifestPath: str("manifest") ? resolve(cwd, str("manifest")) : resolve(configDir, config?.manifest ?? ".linopress-manifest.json")
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/** Reads edition-relative files from `root`; `undefined` when missing. */
|
|
251
|
+
function readFrom(root) {
|
|
252
|
+
return async (path) => {
|
|
253
|
+
try {
|
|
254
|
+
return await readFile(join(root, ...path.split("/")));
|
|
255
|
+
} catch (error) {
|
|
256
|
+
const code = error.code;
|
|
257
|
+
if (code === "ENOENT" || code === "EISDIR" || code === "ENOTDIR") return void 0;
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
async function loadPages(edition, cwd) {
|
|
263
|
+
if (edition.typedoc) {
|
|
264
|
+
const options = resolve(cwd, edition.typedoc);
|
|
265
|
+
const { out, pages } = await generateMarkdown({
|
|
266
|
+
options,
|
|
267
|
+
overrides: {
|
|
268
|
+
logLevel: "Warn",
|
|
269
|
+
...edition.typedocOptions
|
|
270
|
+
},
|
|
271
|
+
...edition.typedocOptionsBase && { overridesBase: edition.typedocOptionsBase }
|
|
272
|
+
});
|
|
273
|
+
try {
|
|
274
|
+
const assets = await collectAssets(pages, readFrom(out));
|
|
275
|
+
return {
|
|
276
|
+
pages,
|
|
277
|
+
source: relative(cwd, options),
|
|
278
|
+
assets
|
|
279
|
+
};
|
|
280
|
+
} finally {
|
|
281
|
+
await rm(out, {
|
|
282
|
+
recursive: true,
|
|
283
|
+
force: true
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (edition.dir) {
|
|
288
|
+
const dir = resolve(cwd, edition.dir);
|
|
289
|
+
const pages = await readMarkdownPages(dir);
|
|
290
|
+
return {
|
|
291
|
+
pages,
|
|
292
|
+
source: relative(cwd, dir),
|
|
293
|
+
assets: await collectAssets(pages, readFrom(dir))
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
throw new UsageError("No source: pass --typedoc <typedoc.json> or --dir <markdown dir>, or set one in the config");
|
|
297
|
+
}
|
|
298
|
+
async function readManifest(path) {
|
|
299
|
+
let json;
|
|
300
|
+
try {
|
|
301
|
+
json = await readFile(path, "utf8");
|
|
302
|
+
} catch (error) {
|
|
303
|
+
if (error.code === "ENOENT") return emptyManifest();
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
return parseManifest(json);
|
|
307
|
+
}
|
|
308
|
+
function git(cwd, args) {
|
|
309
|
+
try {
|
|
310
|
+
return execFileSync("git", args, {
|
|
311
|
+
cwd,
|
|
312
|
+
stdio: [
|
|
313
|
+
"ignore",
|
|
314
|
+
"pipe",
|
|
315
|
+
"ignore"
|
|
316
|
+
]
|
|
317
|
+
}).toString().trim() || null;
|
|
318
|
+
} catch {
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* The repository footers link to: `repository` from the config, else the `origin` remote (or the
|
|
324
|
+
* only remote). `null` outside a git checkout, when the URL isn't recognized (with a warning), or
|
|
325
|
+
* when `sha` isn't on any remote-tracking branch, since the web host wouldn't have that commit.
|
|
326
|
+
*/
|
|
327
|
+
function findRepository(cwd, configured, sha, warn) {
|
|
328
|
+
const root = git(cwd, ["rev-parse", "--show-toplevel"]);
|
|
329
|
+
if (!root || !sha) return null;
|
|
330
|
+
const remotes = git(cwd, ["remote"])?.split("\n") ?? [];
|
|
331
|
+
const remote = remotes.includes("origin") ? "origin" : remotes.length === 1 ? remotes[0] : null;
|
|
332
|
+
const ref = configured ?? (remote ? git(cwd, [
|
|
333
|
+
"remote",
|
|
334
|
+
"get-url",
|
|
335
|
+
remote
|
|
336
|
+
]) : null);
|
|
337
|
+
if (!ref) return null;
|
|
338
|
+
const url = repositoryWebUrl(ref);
|
|
339
|
+
if (!url) {
|
|
340
|
+
warn(`linopress: footers won't link to the source: ${configured !== void 0 ? "`repository` in the config" : `remote "${remote}"`} (${ref}) isn't a recognized repository URL. Set \`repository\` to its web URL, e.g. https://github.com/acme/billing.
|
|
341
|
+
`);
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
if (!git(cwd, [
|
|
345
|
+
"branch",
|
|
346
|
+
"-r",
|
|
347
|
+
"--contains",
|
|
348
|
+
sha
|
|
349
|
+
])) {
|
|
350
|
+
warn(`linopress: footers won't link to the source: commit ${sha.slice(0, 7)} isn't on any remote branch yet. Push it (or run from CI) to get links.
|
|
351
|
+
`);
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
url,
|
|
356
|
+
root: realpathSync(root)
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
const OPTIONS = {
|
|
360
|
+
typedoc: { type: "string" },
|
|
361
|
+
dir: { type: "string" },
|
|
362
|
+
team: { type: "string" },
|
|
363
|
+
project: { type: "string" },
|
|
364
|
+
out: { type: "string" },
|
|
365
|
+
force: { type: "boolean" },
|
|
366
|
+
flat: { type: "boolean" },
|
|
367
|
+
title: { type: "string" },
|
|
368
|
+
manifest: { type: "string" },
|
|
369
|
+
config: { type: "string" },
|
|
370
|
+
all: { type: "boolean" },
|
|
371
|
+
"dry-run": { type: "boolean" },
|
|
372
|
+
verbose: { type: "boolean" },
|
|
373
|
+
help: {
|
|
374
|
+
type: "boolean",
|
|
375
|
+
short: "h"
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
/**
|
|
379
|
+
* `README.md` (TypeDoc's default entry page), or `index.md` when that's what the source has. Only
|
|
380
|
+
* for the Markdown target: Linear editions keep `README.md`, so existing synced docs don't move.
|
|
381
|
+
*/
|
|
382
|
+
function indexPathOf(pages) {
|
|
383
|
+
const has = (p) => pages.some((page) => page.path === p);
|
|
384
|
+
return !has("README.md") && has("index.md") ? "index.md" : "README.md";
|
|
385
|
+
}
|
|
386
|
+
function listProblems(title, byPage) {
|
|
387
|
+
const entries = Object.entries(byPage);
|
|
388
|
+
if (entries.length === 0) return [];
|
|
389
|
+
return [
|
|
390
|
+
"",
|
|
391
|
+
title,
|
|
392
|
+
...entries.map(([path, targets]) => ` ${path} → ${targets.join(", ")}`)
|
|
393
|
+
];
|
|
394
|
+
}
|
|
395
|
+
/** Runs `shared.command` for one edition. */
|
|
396
|
+
async function runEdition(r, shared, ctx) {
|
|
397
|
+
const { command, flags, config } = shared;
|
|
398
|
+
const { pages, source, assets } = await loadPages(r.source, ctx.cwd);
|
|
399
|
+
const label = r.name ? `Edition "${r.name}": ` : "";
|
|
400
|
+
const displayBase = realpathSync(config?.dir ?? ctx.cwd);
|
|
401
|
+
if (r.target.kind === "markdown") {
|
|
402
|
+
const indexPath = indexPathOf(pages);
|
|
403
|
+
const { out } = r.target;
|
|
404
|
+
if (r.source.dir && overlaps(out, r.source.dir)) throw new UsageError(`The output folder ${relative(ctx.cwd, out) || "."} overlaps the source`);
|
|
405
|
+
const nodes = layoutPages(pages, {
|
|
406
|
+
mode: "nested",
|
|
407
|
+
rootTitle: r.title,
|
|
408
|
+
indexPath
|
|
409
|
+
});
|
|
410
|
+
const edition = renderMarkdownEdition(nodes, assets.assets, {
|
|
411
|
+
transforms: markdownTransforms,
|
|
412
|
+
header: makeHeader({
|
|
413
|
+
source,
|
|
414
|
+
displayBase
|
|
415
|
+
})
|
|
416
|
+
});
|
|
417
|
+
if (command === "print") {
|
|
418
|
+
for (const file of edition.files) {
|
|
419
|
+
if (file.kind !== "page") continue;
|
|
420
|
+
ctx.stdout(`<!-- ${file.path} -->\n${new TextDecoder().decode(file.data).trimEnd()}\n\n`);
|
|
421
|
+
}
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const state = await readOutputState(out, edition.files);
|
|
425
|
+
const p = planFiles(edition.files, state);
|
|
426
|
+
const header = `${label}${pages.length} pages from ${source} → Markdown files in ${relative(ctx.cwd, out) || "."}`;
|
|
427
|
+
if (command === "plan" || flags["dry-run"]) {
|
|
428
|
+
const lines = [
|
|
429
|
+
header,
|
|
430
|
+
formatFilePlan(p, { verbose: flags["verbose"] === true }),
|
|
431
|
+
...listProblems("Unresolved links (left as-is):", edition.unresolvedLinks),
|
|
432
|
+
...listProblems("Missing images and files (left as-is):", edition.missingImages)
|
|
433
|
+
];
|
|
434
|
+
ctx.stdout(`${lines.join("\n")}\n`);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (isEmptyFilePlan(p)) {
|
|
438
|
+
ctx.stdout(`${header}\nUp to date.\n`);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const adopted = adoptedPaths(p);
|
|
442
|
+
if (adopted.length > 0 && !flags["force"]) throw new UsageError(`${relative(ctx.cwd, out) || "."} has files Linopress didn't write: ${adopted.join(", ")}. Move them, or pass --force to replace them (they are then deleted when their pages go away)`);
|
|
443
|
+
ctx.stdout(`${header}\n`);
|
|
444
|
+
await applyFiles({
|
|
445
|
+
out,
|
|
446
|
+
files: edition.files,
|
|
447
|
+
plan: p,
|
|
448
|
+
owned: state.owned,
|
|
449
|
+
force: flags["force"] === true,
|
|
450
|
+
onEvent: (e) => ctx.stdout(` ${e.kind.padEnd(9)} ${e.path}\n`)
|
|
451
|
+
});
|
|
452
|
+
ctx.stdout("Done.\n");
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const { container, mode } = r.target;
|
|
456
|
+
const nodes = layoutPages(pages, {
|
|
457
|
+
mode,
|
|
458
|
+
rootTitle: r.title
|
|
459
|
+
});
|
|
460
|
+
const footer = makeFooter({
|
|
461
|
+
source,
|
|
462
|
+
sha: shared.sha,
|
|
463
|
+
repository: shared.repository(),
|
|
464
|
+
displayBase
|
|
465
|
+
});
|
|
466
|
+
const transforms = linearTransforms;
|
|
467
|
+
if (command === "print") {
|
|
468
|
+
for (const node of nodes) {
|
|
469
|
+
const { content } = renderDoc(node, {
|
|
470
|
+
urlFor: pendingUrl,
|
|
471
|
+
footer,
|
|
472
|
+
transforms
|
|
473
|
+
});
|
|
474
|
+
ctx.stdout(`<!-- ${node.path} · "${node.title}" · parent: ${node.parentPath ?? "(top level)"} -->\n`);
|
|
475
|
+
ctx.stdout(`${content.trimEnd()}\n\n`);
|
|
476
|
+
}
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const manifest = await readManifest(r.manifestPath);
|
|
480
|
+
const key = container ? `${r.name ? `${r.name}@` : ""}${containerKey(container)}` : null;
|
|
481
|
+
const entries = key && manifest.containers[key] || {};
|
|
482
|
+
const syncAssets = {
|
|
483
|
+
files: assets.assets,
|
|
484
|
+
entries: key && manifest.assets[key] || {}
|
|
485
|
+
};
|
|
486
|
+
const header = `${label}${pages.length} pages from ${source}, ${mode} layout → ${container ? containerKey(container) : "no Linear team or project"}`;
|
|
487
|
+
const p = plan(nodes, entries, {
|
|
488
|
+
footer,
|
|
489
|
+
assets: syncAssets,
|
|
490
|
+
transforms
|
|
491
|
+
});
|
|
492
|
+
if (command === "plan" || flags["dry-run"]) {
|
|
493
|
+
ctx.stdout(`${header}\n${formatPlan(p, { verbose: flags["verbose"] === true })}\n`);
|
|
494
|
+
if (!container) ctx.stdout("\nNo --team/--project given: planned against an empty manifest.\n");
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (!container || !key) throw new UsageError(`push needs a Linear team or project: pass --team <id> or --project <id>${r.name ? `, or set team / project on edition "${r.name}"` : ""}`);
|
|
498
|
+
const apiKey = ctx.env["LINEAR_API_KEY"];
|
|
499
|
+
if (!apiKey) throw new UsageError("LINEAR_API_KEY is not set");
|
|
500
|
+
if (isEmptyPlan(p)) {
|
|
501
|
+
ctx.stdout(`${header}\nUp to date.\n`);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const target = ctx.createTarget?.(container, apiKey) ?? new LinearTarget({
|
|
505
|
+
container,
|
|
506
|
+
apiKey
|
|
507
|
+
});
|
|
508
|
+
let current = manifest;
|
|
509
|
+
const save = async (next) => {
|
|
510
|
+
current = next;
|
|
511
|
+
const tmp = `${r.manifestPath}.tmp`;
|
|
512
|
+
await writeFile(tmp, serializeManifest(next));
|
|
513
|
+
await rename(tmp, r.manifestPath);
|
|
514
|
+
};
|
|
515
|
+
ctx.stdout(`${header}\n`);
|
|
516
|
+
await apply({
|
|
517
|
+
nodes,
|
|
518
|
+
entries,
|
|
519
|
+
target,
|
|
520
|
+
footer,
|
|
521
|
+
transforms,
|
|
522
|
+
assets: syncAssets,
|
|
523
|
+
onEntriesChange: (e) => save({
|
|
524
|
+
...current,
|
|
525
|
+
containers: {
|
|
526
|
+
...current.containers,
|
|
527
|
+
[key]: e
|
|
528
|
+
}
|
|
529
|
+
}),
|
|
530
|
+
onAssetEntriesChange: (a) => save({
|
|
531
|
+
...current,
|
|
532
|
+
assets: {
|
|
533
|
+
...current.assets,
|
|
534
|
+
[key]: a
|
|
535
|
+
}
|
|
536
|
+
}),
|
|
537
|
+
onEvent: (e) => {
|
|
538
|
+
if (e.kind !== "unchanged") ctx.stdout(` ${e.kind.padEnd(9)} ${e.path} ${e.url}\n`);
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
ctx.stdout("Done.\n");
|
|
542
|
+
}
|
|
543
|
+
function reportError(error, ctx, prefix = "") {
|
|
544
|
+
if (error instanceof UsageError) {
|
|
545
|
+
ctx.stderr(`linopress: ${prefix}${error.message}\n`);
|
|
546
|
+
return 2;
|
|
547
|
+
}
|
|
548
|
+
const { message, cause } = error;
|
|
549
|
+
ctx.stderr(`linopress: ${prefix}${message}\n`);
|
|
550
|
+
if (cause !== void 0) ctx.stderr(` caused by: ${cause instanceof Error ? cause.message : String(cause)}\n`);
|
|
551
|
+
return 1;
|
|
552
|
+
}
|
|
553
|
+
/** Runs the CLI. Returns the process exit code. */
|
|
554
|
+
async function run(argv, ctx) {
|
|
555
|
+
let parsed;
|
|
556
|
+
try {
|
|
557
|
+
parsed = parseArgs({
|
|
558
|
+
args: [...argv],
|
|
559
|
+
allowPositionals: true,
|
|
560
|
+
options: OPTIONS
|
|
561
|
+
});
|
|
562
|
+
} catch (error) {
|
|
563
|
+
ctx.stderr(`${error.message}\n\n${USAGE}\n`);
|
|
564
|
+
return 2;
|
|
565
|
+
}
|
|
566
|
+
const [command, editionName, ...extra] = parsed.positionals;
|
|
567
|
+
if (parsed.values.help || !command) {
|
|
568
|
+
(parsed.values.help ? ctx.stdout : ctx.stderr)(`${USAGE}\n`);
|
|
569
|
+
return parsed.values.help ? 0 : 2;
|
|
570
|
+
}
|
|
571
|
+
const flags = parsed.values;
|
|
572
|
+
let editions;
|
|
573
|
+
let shared;
|
|
574
|
+
try {
|
|
575
|
+
if (command !== "plan" && command !== "push" && command !== "print") throw new UsageError(`Unknown command "${command}"`);
|
|
576
|
+
if (extra.length > 0) throw new UsageError(`Unexpected arguments: ${extra.join(" ")}`);
|
|
577
|
+
const config = await loadConfig(ctx.cwd, parsed.values.config);
|
|
578
|
+
if (flags["all"]) {
|
|
579
|
+
if (editionName) throw new UsageError(`Pass an edition or --all, not both`);
|
|
580
|
+
const conflicting = [
|
|
581
|
+
...SOURCE_FLAGS,
|
|
582
|
+
...TARGET_FLAGS,
|
|
583
|
+
...EDITION_FLAGS
|
|
584
|
+
].filter((f) => flags[f] !== void 0);
|
|
585
|
+
if (conflicting.length > 0) throw new UsageError(`--all runs each edition as configured; drop ${conflicting.map((f) => `--${f}`).join(", ")}`);
|
|
586
|
+
editions = Object.keys(config?.config.editions ?? {});
|
|
587
|
+
if (editions.length === 0) throw new UsageError("--all needs editions in a config file");
|
|
588
|
+
} else editions = [editionName];
|
|
589
|
+
const sha = git(ctx.cwd, ["rev-parse", "HEAD"]);
|
|
590
|
+
let repository;
|
|
591
|
+
shared = {
|
|
592
|
+
command,
|
|
593
|
+
flags,
|
|
594
|
+
config,
|
|
595
|
+
sha,
|
|
596
|
+
repository: () => {
|
|
597
|
+
repository ??= findRepository(ctx.cwd, config?.config.repository, sha, ctx.stderr);
|
|
598
|
+
return repository;
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
} catch (error) {
|
|
602
|
+
return reportError(error, ctx);
|
|
603
|
+
}
|
|
604
|
+
let code = 0;
|
|
605
|
+
for (const [i, name] of editions.entries()) {
|
|
606
|
+
if (i > 0) ctx.stdout("\n");
|
|
607
|
+
try {
|
|
608
|
+
await runEdition(resolveEdition(shared.config, name, flags, ctx.cwd), shared, ctx);
|
|
609
|
+
} catch (error) {
|
|
610
|
+
const prefix = flags["all"] && name ? `edition "${name}": ` : "";
|
|
611
|
+
code = Math.max(code, reportError(error, ctx, prefix));
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return code;
|
|
615
|
+
}
|
|
616
|
+
// src/cli.ts
|
|
617
|
+
process.stdout.on("error", (error) => {
|
|
618
|
+
if (error.code !== "EPIPE") throw error;
|
|
619
|
+
process.exit(process.exitCode ?? 0);
|
|
620
|
+
});
|
|
621
|
+
process.exitCode = await run(process.argv.slice(2), {
|
|
622
|
+
cwd: process.cwd(),
|
|
623
|
+
env: process.env,
|
|
624
|
+
stdout: (text) => process.stdout.write(text),
|
|
625
|
+
stderr: (text) => process.stderr.write(text)
|
|
626
|
+
});
|
|
627
|
+
export {};
|