@stephansama/auto-readme 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 +230 -0
- package/cli.mjs +5 -0
- package/config/schema.cjs +135 -0
- package/config/schema.cjs.map +1 -0
- package/config/schema.d.cts +167 -0
- package/config/schema.d.ts +167 -0
- package/config/schema.js +102 -0
- package/config/schema.js.map +1 -0
- package/config/schema.json +1 -0
- package/config/schema.yaml +182 -0
- package/dist/index.cjs +676 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +641 -0
- package/dist/index.js.map +1 -0
- package/package.json +88 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { fromMarkdown as fromMarkdown2 } from "mdast-util-from-markdown";
|
|
3
|
+
import * as cp2 from "child_process";
|
|
4
|
+
import * as fsp3 from "fs/promises";
|
|
5
|
+
import ora from "ora";
|
|
6
|
+
|
|
7
|
+
// src/args.ts
|
|
8
|
+
import yargs from "yargs";
|
|
9
|
+
import { hideBin } from "yargs/helpers";
|
|
10
|
+
import z2 from "zod";
|
|
11
|
+
|
|
12
|
+
// src/log.ts
|
|
13
|
+
import chalk from "chalk";
|
|
14
|
+
var verbosity = 0;
|
|
15
|
+
function ERROR(...rest) {
|
|
16
|
+
const [first, ...remaining] = rest;
|
|
17
|
+
console.error(chalk.red(first), ...remaining);
|
|
18
|
+
}
|
|
19
|
+
function INFO(...rest) {
|
|
20
|
+
if (verbosity < 1) return;
|
|
21
|
+
const [first, ...remaining] = rest;
|
|
22
|
+
console.info(chalk.blue(first), ...remaining);
|
|
23
|
+
}
|
|
24
|
+
function setVerbosity(input) {
|
|
25
|
+
verbosity = input;
|
|
26
|
+
}
|
|
27
|
+
function WARN(...rest) {
|
|
28
|
+
if (verbosity < 1) return;
|
|
29
|
+
const [first, ...remaining] = rest;
|
|
30
|
+
console.warn(chalk.yellow(first), ...remaining);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/schema.js
|
|
34
|
+
import { z } from "zod";
|
|
35
|
+
var actionsSchema = z.enum(["ACTION", "PKG", "USAGE", "WORKSPACE", "ZOD"]).describe("Comment action options");
|
|
36
|
+
var formatsSchema = z.enum(["LIST", "TABLE"]).default("TABLE").optional();
|
|
37
|
+
var languageSchema = z.enum(["JS", "RS"]).optional().default("JS");
|
|
38
|
+
var headingsSchema = z.enum([
|
|
39
|
+
"default",
|
|
40
|
+
"description",
|
|
41
|
+
"devDependency",
|
|
42
|
+
"downloads",
|
|
43
|
+
"name",
|
|
44
|
+
"private",
|
|
45
|
+
"required",
|
|
46
|
+
"version"
|
|
47
|
+
]).describe("Table heading options");
|
|
48
|
+
var tableHeadingsSchema = z.record(actionsSchema, headingsSchema.array().optional()).optional().describe("Table heading action configuration").default({
|
|
49
|
+
ACTION: ["name", "required", "default", "description"],
|
|
50
|
+
PKG: ["name", "version", "devDependency"],
|
|
51
|
+
WORKSPACE: ["name", "version", "downloads", "description"],
|
|
52
|
+
ZOD: []
|
|
53
|
+
});
|
|
54
|
+
var templatesSchema = z.object({
|
|
55
|
+
downloadImage: z.string().optional().default("https://img.shields.io/npm/dw/{{name}}?labelColor=211F1F"),
|
|
56
|
+
emojis: z.record(headingsSchema, z.string()).optional().describe("Table heading emojis used when enabled").default({
|
|
57
|
+
default: "\u2699\uFE0F",
|
|
58
|
+
description: "\u{1F4DD}",
|
|
59
|
+
devDependency: "\u{1F4BB}",
|
|
60
|
+
downloads: "\u{1F4E5}",
|
|
61
|
+
name: "\u{1F3F7}\uFE0F",
|
|
62
|
+
private: "\u{1F512}",
|
|
63
|
+
required: "",
|
|
64
|
+
version: ""
|
|
65
|
+
}),
|
|
66
|
+
registryUrl: z.string().optional().default("https://www.npmjs.com/package/{{name}}"),
|
|
67
|
+
versionImage: z.string().optional().default(
|
|
68
|
+
"https://img.shields.io/npm/v/{{uri_name}}?logo=npm&logoColor=red&color=211F1F&labelColor=211F1F"
|
|
69
|
+
)
|
|
70
|
+
});
|
|
71
|
+
var defaultTemplates = templatesSchema.parse({});
|
|
72
|
+
var defaultTableHeadings = tableHeadingsSchema.parse(void 0);
|
|
73
|
+
var _configSchema = z.object({
|
|
74
|
+
affectedRegexes: z.string().array().optional().default([]),
|
|
75
|
+
defaultLanguage: languageSchema.meta({
|
|
76
|
+
alias: "l",
|
|
77
|
+
description: "Default language to infer projects from"
|
|
78
|
+
}),
|
|
79
|
+
disableEmojis: z.boolean().default(false).meta({
|
|
80
|
+
alias: "e",
|
|
81
|
+
description: "Whether or not to use emojis in markdown table headings"
|
|
82
|
+
}),
|
|
83
|
+
disableMarkdownHeadings: z.boolean().default(false).meta({
|
|
84
|
+
description: "Whether or not to display markdown headings"
|
|
85
|
+
}),
|
|
86
|
+
enableToc: z.boolean().default(false).meta({
|
|
87
|
+
alias: "t",
|
|
88
|
+
description: "generate table of contents for readmes"
|
|
89
|
+
}),
|
|
90
|
+
enableUsage: z.boolean().optional().default(false).meta({
|
|
91
|
+
description: "Whether or not to enable usage plugin"
|
|
92
|
+
}),
|
|
93
|
+
headings: tableHeadingsSchema.optional().default(defaultTableHeadings).describe("List of headings for different table outputs"),
|
|
94
|
+
onlyReadmes: z.boolean().default(true).meta({
|
|
95
|
+
alias: "r",
|
|
96
|
+
description: "Whether or not to only traverse readmes"
|
|
97
|
+
}),
|
|
98
|
+
onlyShowPublicPackages: z.boolean().default(false).meta({
|
|
99
|
+
alias: "p",
|
|
100
|
+
description: "Only show public packages in workspaces"
|
|
101
|
+
}),
|
|
102
|
+
removeScope: z.string().optional().default("").meta({
|
|
103
|
+
description: "Remove common workspace scope"
|
|
104
|
+
}),
|
|
105
|
+
templates: templatesSchema.optional().default(defaultTemplates).describe(
|
|
106
|
+
"Handlebars templates used to fuel list and table generation"
|
|
107
|
+
),
|
|
108
|
+
tocHeading: z.string().optional().default("Table of contents").meta({
|
|
109
|
+
description: "Markdown heading used to generate table of contents"
|
|
110
|
+
}),
|
|
111
|
+
usageFile: z.string().optional().default("").meta({
|
|
112
|
+
description: "Workspace level usage file"
|
|
113
|
+
}),
|
|
114
|
+
usageHeading: z.string().optional().default("Usage").meta({
|
|
115
|
+
description: "Markdown heading used to generate usage example"
|
|
116
|
+
}),
|
|
117
|
+
verbose: z.boolean().default(false).meta({
|
|
118
|
+
alias: "v",
|
|
119
|
+
description: "whether or not to display verbose logging"
|
|
120
|
+
})
|
|
121
|
+
});
|
|
122
|
+
var configSchema = _configSchema.optional();
|
|
123
|
+
|
|
124
|
+
// src/args.ts
|
|
125
|
+
var complexOptions = ["affectedRegexes", "templates", "headings"];
|
|
126
|
+
var args = {
|
|
127
|
+
...zodToYargs(),
|
|
128
|
+
changes: {
|
|
129
|
+
alias: "g",
|
|
130
|
+
default: false,
|
|
131
|
+
description: "Check only changed git files",
|
|
132
|
+
type: "boolean"
|
|
133
|
+
},
|
|
134
|
+
check: {
|
|
135
|
+
alias: "k",
|
|
136
|
+
default: false,
|
|
137
|
+
description: "Do not write to files. Only check for changes",
|
|
138
|
+
type: "boolean"
|
|
139
|
+
},
|
|
140
|
+
config: { alias: "c", description: "Path to config file", type: "string" }
|
|
141
|
+
};
|
|
142
|
+
async function parseArgs() {
|
|
143
|
+
const yargsInstance = yargs(hideBin(process.argv)).options(args).help("h").alias("h", "help").epilogue(`--> @stephansama open-source ${(/* @__PURE__ */ new Date()).getFullYear()}`);
|
|
144
|
+
const parsed = await yargsInstance.wrap(yargsInstance.terminalWidth()).parse();
|
|
145
|
+
if (parsed.verbose) setVerbosity(1);
|
|
146
|
+
return parsed;
|
|
147
|
+
}
|
|
148
|
+
function zodToYargs() {
|
|
149
|
+
const { shape } = configSchema.unwrap();
|
|
150
|
+
const entries = Object.entries(shape).map(([key, value]) => {
|
|
151
|
+
if (complexOptions.includes(key)) return [];
|
|
152
|
+
if (value.def.innerType instanceof z2.ZodObject) return [];
|
|
153
|
+
const meta = value.meta();
|
|
154
|
+
const { innerType } = value.def;
|
|
155
|
+
const isBoolean = innerType instanceof z2.ZodBoolean;
|
|
156
|
+
const isNumber = innerType instanceof z2.ZodNumber;
|
|
157
|
+
const isArray = innerType instanceof z2.ZodArray;
|
|
158
|
+
const yargType = isArray && "array" || isNumber && "number" || isBoolean && "boolean" || "string";
|
|
159
|
+
const options = {
|
|
160
|
+
default: value.def.defaultValue,
|
|
161
|
+
type: yargType
|
|
162
|
+
};
|
|
163
|
+
if (meta?.alias) options.alias = meta.alias;
|
|
164
|
+
if (meta?.description) options.description = meta.description;
|
|
165
|
+
return [key, options];
|
|
166
|
+
});
|
|
167
|
+
return Object.fromEntries(entries);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/comment.ts
|
|
171
|
+
var SEPARATOR = "-";
|
|
172
|
+
function loadAstComments(root) {
|
|
173
|
+
return root.children.map((child) => child.type === "html" && getComment(child.value)).filter((f) => f !== false);
|
|
174
|
+
}
|
|
175
|
+
function parseComment(comment) {
|
|
176
|
+
const input = trimComment(comment);
|
|
177
|
+
const [type, ...parameters] = input.split(" ");
|
|
178
|
+
const [first, second, third] = type.split(SEPARATOR);
|
|
179
|
+
INFO("parsing inputs", { first, second, third });
|
|
180
|
+
const languageInput = third ? first : void 0;
|
|
181
|
+
const actionInput = third ? second : first;
|
|
182
|
+
const formatInput = third ? third : second;
|
|
183
|
+
const language = languageSchema.parse(languageInput);
|
|
184
|
+
const action = actionsSchema.parse(actionInput);
|
|
185
|
+
const format = formatsSchema.parse(formatInput);
|
|
186
|
+
const isStart = comment.includes("start");
|
|
187
|
+
const parsed = { action, format, isStart, language, parameters };
|
|
188
|
+
INFO(`Parsed comment ${comment}`, parsed);
|
|
189
|
+
return parsed;
|
|
190
|
+
}
|
|
191
|
+
var startComment = "<!--";
|
|
192
|
+
var endComment = "-->";
|
|
193
|
+
function trimComment(comment) {
|
|
194
|
+
return comment.replace(startComment, "").replace(/start|end/, "").replace(endComment, "").trim();
|
|
195
|
+
}
|
|
196
|
+
function getComment(comment) {
|
|
197
|
+
return isComment(comment) && parseComment(comment);
|
|
198
|
+
}
|
|
199
|
+
function isComment(comment) {
|
|
200
|
+
return comment.startsWith(startComment) && comment.endsWith(endComment);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/config.ts
|
|
204
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
205
|
+
import deepmerge from "deepmerge";
|
|
206
|
+
async function loadConfig(args2) {
|
|
207
|
+
const opts2 = {};
|
|
208
|
+
if (args2.config) opts2.searchPlaces = [args2.config];
|
|
209
|
+
const explorer = cosmiconfig("autoreadme", opts2);
|
|
210
|
+
const search = await explorer.search();
|
|
211
|
+
if (!search) {
|
|
212
|
+
const location = args2.config ? " at location: " + args2.config : "";
|
|
213
|
+
WARN(`no config file found`, location);
|
|
214
|
+
INFO("using default configuration");
|
|
215
|
+
} else {
|
|
216
|
+
INFO("found configuration file at: ", search.filepath);
|
|
217
|
+
INFO("loaded cosmiconfig", search.config);
|
|
218
|
+
}
|
|
219
|
+
args2 = removeFalsy(args2);
|
|
220
|
+
INFO("merging config with args", args2);
|
|
221
|
+
return configSchema.parse(
|
|
222
|
+
deepmerge(search?.config || {}, args2, {
|
|
223
|
+
arrayMerge: (_, sourceArray) => sourceArray
|
|
224
|
+
})
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
function removeFalsy(obj) {
|
|
228
|
+
return Object.fromEntries(
|
|
229
|
+
Object.entries(obj).map(([k, v]) => !v ? false : [k, v]).filter((e) => Boolean(e))
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/data.ts
|
|
234
|
+
import { getPackages } from "@manypkg/get-packages";
|
|
235
|
+
import * as fs2 from "fs";
|
|
236
|
+
import * as fsp2 from "fs/promises";
|
|
237
|
+
import * as path2 from "path";
|
|
238
|
+
import { readPackageJSON } from "pkg-types";
|
|
239
|
+
import * as yaml from "yaml";
|
|
240
|
+
import { zod2md } from "zod2md";
|
|
241
|
+
|
|
242
|
+
// src/utils.ts
|
|
243
|
+
import glob from "fast-glob";
|
|
244
|
+
import * as cp from "child_process";
|
|
245
|
+
import * as fs from "fs";
|
|
246
|
+
import * as fsp from "fs/promises";
|
|
247
|
+
import * as path from "path";
|
|
248
|
+
var sh = String.raw;
|
|
249
|
+
var opts = { encoding: "utf8" };
|
|
250
|
+
var ignore = ["**/node_modules/**"];
|
|
251
|
+
var matches = [
|
|
252
|
+
/.*README\.md$/gi,
|
|
253
|
+
/.*Cargo\.toml$/gi,
|
|
254
|
+
/.*action\.ya?ml$/gi,
|
|
255
|
+
/.*package\.json$/gi,
|
|
256
|
+
/.*pnpm-workspace\.yaml$/gi
|
|
257
|
+
];
|
|
258
|
+
async function fileExists(file) {
|
|
259
|
+
return await fsp.access(file).then(() => true).catch(() => false);
|
|
260
|
+
}
|
|
261
|
+
function findAffectedMarkdowns(root, config) {
|
|
262
|
+
const affected = cp.execSync(sh`git diff --cached --name-only --diff-filter=MACT`, opts).trim().split("\n").filter(Boolean);
|
|
263
|
+
if (!affected.length) ERROR("no staged files found");
|
|
264
|
+
if (config.affectedRegexes?.length) {
|
|
265
|
+
INFO("adding the following expressions: ", config.affectedRegexes);
|
|
266
|
+
}
|
|
267
|
+
const allMatches = [
|
|
268
|
+
...matches,
|
|
269
|
+
...config.affectedRegexes?.map((r) => new RegExp(r)) || []
|
|
270
|
+
];
|
|
271
|
+
INFO("Checking affected files against regexes", affected, allMatches);
|
|
272
|
+
const eligible = affected.filter((a) => allMatches.some((m) => a.match(m)));
|
|
273
|
+
INFO("Found the following eligible affected files", eligible);
|
|
274
|
+
const md = eligible.map((e) => findNearestReadme(root, path.resolve(e)));
|
|
275
|
+
const rootMd = path.join(root, "README.md");
|
|
276
|
+
const dedupe = [...new Set(md), rootMd].filter(
|
|
277
|
+
(s) => Boolean(s)
|
|
278
|
+
);
|
|
279
|
+
INFO("Found the following readmes", dedupe);
|
|
280
|
+
return dedupe;
|
|
281
|
+
}
|
|
282
|
+
function findNearestReadme(gitRoot, inputFile, maxRotations = 15) {
|
|
283
|
+
let dir = path.dirname(inputFile);
|
|
284
|
+
let rotations = 0;
|
|
285
|
+
while (true) {
|
|
286
|
+
const option = path.join(dir, "README.md");
|
|
287
|
+
if (fs.existsSync(option)) return option;
|
|
288
|
+
const parent = path.dirname(dir);
|
|
289
|
+
if (parent === dir || dir === gitRoot || ++rotations > maxRotations) {
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
dir = parent;
|
|
293
|
+
}
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
function getGitRoot() {
|
|
297
|
+
const root = cp.execSync(sh`git rev-parse --show-toplevel`, opts).trim();
|
|
298
|
+
if (!root) {
|
|
299
|
+
throw new Error("must be ran within a git directory.");
|
|
300
|
+
}
|
|
301
|
+
INFO("found git root at location: ", root);
|
|
302
|
+
return root;
|
|
303
|
+
}
|
|
304
|
+
async function getMarkdownPaths(cwd, config) {
|
|
305
|
+
const pattern = `**/${config?.onlyReadmes ? "README" : "*"}.md`;
|
|
306
|
+
const readmes = await glob(pattern, { cwd, ignore });
|
|
307
|
+
return readmes.map((readme) => path.resolve(cwd, readme));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// src/data.ts
|
|
311
|
+
function createFindParameter(parameterList) {
|
|
312
|
+
return function(parameterName) {
|
|
313
|
+
return parameterList?.find((p) => p.startsWith(parameterName))?.replace(parameterName + "=", "")?.replace(/"/gi, "")?.replace(/_/gi, " ");
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
async function loadActionData(actions, file, root) {
|
|
317
|
+
const startActions = actions.filter((action) => action.isStart);
|
|
318
|
+
return await Promise.all(
|
|
319
|
+
startActions.map(async (action) => {
|
|
320
|
+
const find = createFindParameter(action.parameters);
|
|
321
|
+
switch (action.action) {
|
|
322
|
+
case "ACTION": {
|
|
323
|
+
const baseDir = path2.dirname(file);
|
|
324
|
+
const actionYaml = await loadActionYaml(baseDir);
|
|
325
|
+
return {
|
|
326
|
+
action: action.action,
|
|
327
|
+
actionYaml,
|
|
328
|
+
parameters: action.parameters
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
case "PKG": {
|
|
332
|
+
const inputPath = find("path");
|
|
333
|
+
const filename = inputPath ? path2.resolve(path2.dirname(file), inputPath) : path2.dirname(file);
|
|
334
|
+
const pkgJson = await readPackageJSON(filename);
|
|
335
|
+
return {
|
|
336
|
+
action: action.action,
|
|
337
|
+
parameters: action.parameters,
|
|
338
|
+
pkgJson
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
case "USAGE": {
|
|
342
|
+
return {
|
|
343
|
+
action: action.action,
|
|
344
|
+
parameters: action.parameters
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
case "WORKSPACE": {
|
|
348
|
+
const workspaces = await getPackages(process.cwd());
|
|
349
|
+
const pnpmPath = path2.resolve(root, "pnpm-workspace.yaml");
|
|
350
|
+
const isPnpm = fs2.existsSync(pnpmPath);
|
|
351
|
+
return {
|
|
352
|
+
action: action.action,
|
|
353
|
+
isPnpm,
|
|
354
|
+
parameters: action.parameters,
|
|
355
|
+
root,
|
|
356
|
+
workspaces
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
case "ZOD": {
|
|
360
|
+
if (action.format === "LIST") {
|
|
361
|
+
throw new Error("cannot display zod in list format");
|
|
362
|
+
}
|
|
363
|
+
const inputPath = find("path");
|
|
364
|
+
if (!inputPath) {
|
|
365
|
+
const error = `no path found for zod table at markdown file ${file}`;
|
|
366
|
+
throw new Error(error);
|
|
367
|
+
}
|
|
368
|
+
const body = await zod2md({
|
|
369
|
+
entry: path2.resolve(path2.dirname(file), inputPath),
|
|
370
|
+
title: find("title") || "Zod Schema"
|
|
371
|
+
});
|
|
372
|
+
return {
|
|
373
|
+
action: action.action,
|
|
374
|
+
body,
|
|
375
|
+
parameters: action.parameters
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
default:
|
|
379
|
+
throw new Error("feature not yet implemented");
|
|
380
|
+
}
|
|
381
|
+
})
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
async function loadActionYaml(baseDir) {
|
|
385
|
+
const actionYmlPath = path2.resolve(baseDir, "action.yml");
|
|
386
|
+
const actionYamlPath = path2.resolve(baseDir, "action.yaml");
|
|
387
|
+
const actualPath = await fileExists(actionYamlPath) && actionYamlPath || await fileExists(actionYmlPath) && actionYmlPath;
|
|
388
|
+
if (!actualPath) {
|
|
389
|
+
const locations = [actionYmlPath, actionYamlPath];
|
|
390
|
+
const error = `no yaml file found at locations: ${locations}`;
|
|
391
|
+
throw new Error(error);
|
|
392
|
+
}
|
|
393
|
+
const actionFile = await fsp2.readFile(actualPath, { encoding: "utf8" });
|
|
394
|
+
return yaml.parse(actionFile);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// src/pipeline.ts
|
|
398
|
+
import * as path4 from "path";
|
|
399
|
+
import { remark } from "remark";
|
|
400
|
+
import remarkCollapse from "remark-collapse";
|
|
401
|
+
import remarkToc from "remark-toc";
|
|
402
|
+
import remarkUsage from "remark-usage";
|
|
403
|
+
|
|
404
|
+
// src/plugin.ts
|
|
405
|
+
import Handlebars from "handlebars";
|
|
406
|
+
import { markdownTable } from "markdown-table";
|
|
407
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
408
|
+
import { zone } from "mdast-zone";
|
|
409
|
+
import path3 from "path";
|
|
410
|
+
function createHeading(headings, disableEmojis = false, emojis = defaultTemplates.emojis) {
|
|
411
|
+
return headings.map(
|
|
412
|
+
(h) => `${disableEmojis ? "" : emojis[h] + " "}${h?.at(0)?.toUpperCase() + h?.slice(1)}`
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
function wrapRequired(required, input) {
|
|
416
|
+
if (!required) return input;
|
|
417
|
+
return `<b>*${input}</b>`;
|
|
418
|
+
}
|
|
419
|
+
var autoReadmeRemarkPlugin = (config, data) => (tree) => {
|
|
420
|
+
zone(tree, /.*ZOD.*/gi, function(start, _, end) {
|
|
421
|
+
const zod = data.find((d) => d?.action === "ZOD");
|
|
422
|
+
if (!zod?.body) {
|
|
423
|
+
throw new Error("unable to load zod body");
|
|
424
|
+
}
|
|
425
|
+
const ast = fromMarkdown(zod.body);
|
|
426
|
+
return [start, ast, end];
|
|
427
|
+
});
|
|
428
|
+
zone(tree, /.*ACTION.*/gi, function(start, _, end) {
|
|
429
|
+
const value = start.type === "html" && start.value;
|
|
430
|
+
const options = value && parseComment(value);
|
|
431
|
+
if (!options) throw new Error("not able to parse comment");
|
|
432
|
+
const first = data.find((d) => d?.action === "ACTION");
|
|
433
|
+
const inputs = first?.actionYaml?.inputs || {};
|
|
434
|
+
const heading = `### ${config.disableEmojis ? "" : "\u{1F9F0}"} actions`;
|
|
435
|
+
if (options.format === "LIST") {
|
|
436
|
+
const body2 = `${heading}
|
|
437
|
+
` + Object.entries(inputs).sort((a) => a[1].required ? -1 : 1).map(([key, value2]) => {
|
|
438
|
+
return `- ${wrapRequired(value2.required, key)}: (default: ${value2.default})
|
|
439
|
+
|
|
440
|
+
${value2.description}`;
|
|
441
|
+
}).join("\n");
|
|
442
|
+
const ast2 = fromMarkdown(body2);
|
|
443
|
+
return [start, ast2, end];
|
|
444
|
+
}
|
|
445
|
+
const headings = config.headings?.ACTION?.length && config.headings.ACTION || defaultTableHeadings.ACTION;
|
|
446
|
+
const table = markdownTable([
|
|
447
|
+
createHeading(
|
|
448
|
+
headings,
|
|
449
|
+
config.disableEmojis,
|
|
450
|
+
config.templates?.emojis
|
|
451
|
+
),
|
|
452
|
+
...Object.entries(inputs).map(
|
|
453
|
+
([k, v]) => headings.map((heading2) => v[heading2] || k).map(String)
|
|
454
|
+
)
|
|
455
|
+
]);
|
|
456
|
+
const body = [heading, "", table].join("\n");
|
|
457
|
+
const ast = fromMarkdown(body);
|
|
458
|
+
return [start, ast, end];
|
|
459
|
+
});
|
|
460
|
+
zone(tree, /.*WORKSPACE.*/gi, function(start, _, end) {
|
|
461
|
+
const value = start.type === "html" && start.value;
|
|
462
|
+
const comment = value && parseComment(value);
|
|
463
|
+
const workspace = data.find((d) => d?.action === "WORKSPACE");
|
|
464
|
+
const templates = loadTemplates(config.templates);
|
|
465
|
+
const packages = workspace?.workspaces?.packages || [];
|
|
466
|
+
const headings = config.headings?.WORKSPACE?.length && config.headings?.WORKSPACE || defaultTableHeadings.WORKSPACE;
|
|
467
|
+
if (comment && comment.format === "LIST") {
|
|
468
|
+
}
|
|
469
|
+
const tableHeadings = createHeading(
|
|
470
|
+
headings,
|
|
471
|
+
config.disableEmojis,
|
|
472
|
+
config.templates?.emojis
|
|
473
|
+
);
|
|
474
|
+
const table = markdownTable([
|
|
475
|
+
tableHeadings,
|
|
476
|
+
...packages.filter(
|
|
477
|
+
(pkg) => config.onlyShowPublicPackages ? !pkg.packageJson.private : true
|
|
478
|
+
).map((pkg) => {
|
|
479
|
+
const { name } = pkg.packageJson;
|
|
480
|
+
return headings.map((heading2) => {
|
|
481
|
+
if (heading2 === "name") {
|
|
482
|
+
const scoped = config.removeScope ? name.replace(config.removeScope, "") : name;
|
|
483
|
+
return `[${scoped}](${path3.relative(
|
|
484
|
+
process.cwd(),
|
|
485
|
+
path3.resolve(pkg.dir, "README.md")
|
|
486
|
+
)})`;
|
|
487
|
+
}
|
|
488
|
+
if (heading2 === "version") {
|
|
489
|
+
return ` }
|
|
491
|
+
)})`;
|
|
492
|
+
}
|
|
493
|
+
if (heading2 === "downloads") {
|
|
494
|
+
return `})`;
|
|
497
|
+
}
|
|
498
|
+
if (heading2 === "description") {
|
|
499
|
+
return pkg.packageJson?.description;
|
|
500
|
+
}
|
|
501
|
+
return ``;
|
|
502
|
+
});
|
|
503
|
+
})
|
|
504
|
+
]);
|
|
505
|
+
const heading = `### ${config.disableEmojis ? "" : "\u{1F3ED}"} workspace`;
|
|
506
|
+
const body = [heading, "", table].join("\n");
|
|
507
|
+
const ast = fromMarkdown(body);
|
|
508
|
+
return [start, ast, end];
|
|
509
|
+
});
|
|
510
|
+
zone(tree, /.*PKG.*/gi, function(start, _, end) {
|
|
511
|
+
const value = start.type === "html" && start.value;
|
|
512
|
+
const comment = value && parseComment(value);
|
|
513
|
+
const first = data.find((d) => d?.action === "PKG");
|
|
514
|
+
const templates = loadTemplates(config.templates);
|
|
515
|
+
const headings = config.headings?.PKG?.length && config.headings?.PKG || defaultTableHeadings.PKG;
|
|
516
|
+
if (comment && comment.format === "LIST") {
|
|
517
|
+
const ast = fromMarkdown("");
|
|
518
|
+
return [start, ast, end];
|
|
519
|
+
}
|
|
520
|
+
function mapDependencies(isDev) {
|
|
521
|
+
return function([name, version]) {
|
|
522
|
+
const url = templates.registryUrl({ name });
|
|
523
|
+
return headings.map((key) => {
|
|
524
|
+
if (key === "devDependency") {
|
|
525
|
+
if (config.disableEmojis) {
|
|
526
|
+
return `\`${isDev}\``;
|
|
527
|
+
}
|
|
528
|
+
return `${isDev ? "\u2328\uFE0F" : "\u{1F465}"}`;
|
|
529
|
+
}
|
|
530
|
+
if (key === "name") {
|
|
531
|
+
return `[${name}](${url})`;
|
|
532
|
+
}
|
|
533
|
+
if (key === "version") {
|
|
534
|
+
if (["workspace", "catalog", "*"].some(
|
|
535
|
+
(type) => version.includes(type)
|
|
536
|
+
)) {
|
|
537
|
+
return `\`${version}\``;
|
|
538
|
+
}
|
|
539
|
+
return ` })})`;
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
const { dependencies = {}, devDependencies = {} } = first?.pkgJson || {};
|
|
545
|
+
const table = markdownTable([
|
|
546
|
+
createHeading(
|
|
547
|
+
headings,
|
|
548
|
+
config.disableEmojis,
|
|
549
|
+
config.templates?.emojis
|
|
550
|
+
),
|
|
551
|
+
...Object.entries(devDependencies).map(mapDependencies(true)),
|
|
552
|
+
...Object.entries(dependencies).map(mapDependencies(false))
|
|
553
|
+
]);
|
|
554
|
+
const heading = `### ${config.disableEmojis ? "" : "\u{1F4E6}"} packages`;
|
|
555
|
+
const body = [heading, "", table].join("\n");
|
|
556
|
+
const tableAst = fromMarkdown(body);
|
|
557
|
+
return [start, tableAst, end];
|
|
558
|
+
});
|
|
559
|
+
};
|
|
560
|
+
function loadTemplates(templates) {
|
|
561
|
+
if (!templates) throw new Error("failed to load templates");
|
|
562
|
+
return Object.fromEntries(
|
|
563
|
+
Object.entries(templates).map(([key, value]) => {
|
|
564
|
+
if (typeof value !== "string") return [];
|
|
565
|
+
return [key, Handlebars.compile(value)];
|
|
566
|
+
})
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// src/pipeline.ts
|
|
571
|
+
async function parse2(file, filepath, root, config, data) {
|
|
572
|
+
const pipeline = remark().use(autoReadmeRemarkPlugin, config, data);
|
|
573
|
+
const usage = data.find((d) => d.action === "USAGE");
|
|
574
|
+
if (usage?.action === "USAGE" || config.enableUsage) {
|
|
575
|
+
const find = createFindParameter(usage?.parameters || []);
|
|
576
|
+
const examplePath = find("path");
|
|
577
|
+
const dirname4 = path4.dirname(filepath);
|
|
578
|
+
const resolvePath = examplePath && path4.resolve(dirname4, examplePath);
|
|
579
|
+
const relativeProjectPath = config.usageFile && path4.relative(root, path4.resolve(dirname4, config.usageFile));
|
|
580
|
+
const example = examplePath && resolvePath && path4.relative(root, resolvePath) || relativeProjectPath || void 0;
|
|
581
|
+
if (await fileExists(example || "")) {
|
|
582
|
+
INFO("generating usage section");
|
|
583
|
+
pipeline.use(remarkUsage, {
|
|
584
|
+
example,
|
|
585
|
+
heading: config.usageHeading
|
|
586
|
+
});
|
|
587
|
+
} else {
|
|
588
|
+
WARN("not able to find example file for readme", filepath, example);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (config.enableToc) {
|
|
592
|
+
INFO("generating table of contents section");
|
|
593
|
+
pipeline.use(remarkToc, { heading: config.tocHeading }).use(remarkCollapse, { test: config.tocHeading });
|
|
594
|
+
}
|
|
595
|
+
const vfile = await pipeline.process(file);
|
|
596
|
+
return vfile.toString();
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// src/index.ts
|
|
600
|
+
async function run() {
|
|
601
|
+
const args2 = await parseArgs();
|
|
602
|
+
const config = await loadConfig(args2) || {};
|
|
603
|
+
INFO("Loaded the following configuration:", config);
|
|
604
|
+
const root = getGitRoot();
|
|
605
|
+
const isAffected = args2.changes ? "affected" : "";
|
|
606
|
+
INFO(`Loading ${!isAffected ? "all " : "affected "}files`);
|
|
607
|
+
const paths = isAffected ? findAffectedMarkdowns(root, config) : await getMarkdownPaths(root, config);
|
|
608
|
+
INFO("Loaded the following files:", paths.join("\n"));
|
|
609
|
+
const type = args2.onlyReadmes ? "readmes" : "all markdown files";
|
|
610
|
+
if (!paths.length) {
|
|
611
|
+
return ERROR(`no ${isAffected} readmes found to update`);
|
|
612
|
+
}
|
|
613
|
+
const spinner = !args2.verbose && ora(`Updating ${type}`).start();
|
|
614
|
+
await Promise.all(
|
|
615
|
+
paths.map(async (path5) => {
|
|
616
|
+
const file = await fsp3.readFile(path5, { encoding: "utf8" });
|
|
617
|
+
const actions = (() => {
|
|
618
|
+
const ast = fromMarkdown2(file);
|
|
619
|
+
return loadAstComments(ast);
|
|
620
|
+
})();
|
|
621
|
+
if (!actions.length) {
|
|
622
|
+
WARN(`no action comments found in`, path5);
|
|
623
|
+
if (!config.enableUsage || !config.enableToc) {
|
|
624
|
+
return ERROR("no action or plugins found");
|
|
625
|
+
} else {
|
|
626
|
+
INFO("plugins enabled. continuing parsing", path5);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const data = await loadActionData(actions, path5, root);
|
|
630
|
+
INFO("Loaded comment action data", data);
|
|
631
|
+
const content = await parse2(file, path5, root, config, data);
|
|
632
|
+
await fsp3.writeFile(path5, content);
|
|
633
|
+
})
|
|
634
|
+
);
|
|
635
|
+
if (isAffected) cp2.execFileSync("git", ["add", ...paths]);
|
|
636
|
+
if (spinner) spinner.stop();
|
|
637
|
+
}
|
|
638
|
+
export {
|
|
639
|
+
run
|
|
640
|
+
};
|
|
641
|
+
//# sourceMappingURL=index.js.map
|