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