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