@renr/parcel-reporter-docgen 0.0.1-alpha.3 → 0.0.1-alpha.4
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.
|
@@ -6,7 +6,42 @@ import { glob } from "tinyglobby";
|
|
|
6
6
|
//#region src/generate-props.ts
|
|
7
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
8
|
path.dirname(__filename);
|
|
9
|
+
const fileMtimeCache = /* @__PURE__ */ new Map();
|
|
9
10
|
async function generateProps(pagesPattern, rootDir, outputPath, log = console.log) {
|
|
11
|
+
const files = await glob(pagesPattern, { cwd: rootDir });
|
|
12
|
+
if (files.length === 0) {
|
|
13
|
+
log(`No files found for pattern: ${pagesPattern}`);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const resolvedOutputPath = path.resolve(rootDir, outputPath);
|
|
17
|
+
await fs.mkdir(resolvedOutputPath, { recursive: true });
|
|
18
|
+
const filesToProcess = [];
|
|
19
|
+
for (const file of files) {
|
|
20
|
+
const filePath = path.resolve(rootDir, file);
|
|
21
|
+
const componentName = path.basename(file, path.extname(file));
|
|
22
|
+
const outputFilePath = path.join(resolvedOutputPath, `${componentName}.json`);
|
|
23
|
+
try {
|
|
24
|
+
const sourceStat = await fs.stat(filePath);
|
|
25
|
+
const cachedMtime = fileMtimeCache.get(filePath);
|
|
26
|
+
let isUpToDate = false;
|
|
27
|
+
try {
|
|
28
|
+
const outStat = await fs.stat(outputFilePath);
|
|
29
|
+
if (cachedMtime !== void 0 && cachedMtime === sourceStat.mtimeMs || outStat.mtimeMs >= sourceStat.mtimeMs) {
|
|
30
|
+
isUpToDate = true;
|
|
31
|
+
fileMtimeCache.set(filePath, sourceStat.mtimeMs);
|
|
32
|
+
}
|
|
33
|
+
} catch (_unused) {}
|
|
34
|
+
if (isUpToDate) continue;
|
|
35
|
+
filesToProcess.push({
|
|
36
|
+
relFile: file,
|
|
37
|
+
absPath: filePath,
|
|
38
|
+
componentName
|
|
39
|
+
});
|
|
40
|
+
fileMtimeCache.set(filePath, sourceStat.mtimeMs);
|
|
41
|
+
} catch (_unused2) {}
|
|
42
|
+
}
|
|
43
|
+
if (filesToProcess.length === 0) return;
|
|
44
|
+
log(`Found ${filesToProcess.length} changed file(s) for pattern: ${pagesPattern}`);
|
|
10
45
|
const docgenParser = withCustomConfig(path.resolve(rootDir, "./tsconfig.json"), {
|
|
11
46
|
shouldExtractLiteralValuesFromEnum: true,
|
|
12
47
|
savePropValueAsString: true,
|
|
@@ -19,20 +54,20 @@ async function generateProps(pagesPattern, rootDir, outputPath, log = console.lo
|
|
|
19
54
|
return true;
|
|
20
55
|
}
|
|
21
56
|
});
|
|
22
|
-
const files = await glob(pagesPattern, { cwd: rootDir });
|
|
23
|
-
if (files.length === 0) {
|
|
24
|
-
log(`No files found for pattern: ${pagesPattern}`);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
log(`Found ${files.length} files for pattern: ${pagesPattern}`);
|
|
28
|
-
await fs.mkdir(path.resolve(rootDir, outputPath), { recursive: true });
|
|
29
57
|
try {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
58
|
+
const absPaths = filesToProcess.map((f) => f.absPath);
|
|
59
|
+
const allDocs = docgenParser.parse(absPaths);
|
|
60
|
+
const docsByFilePath = /* @__PURE__ */ new Map();
|
|
61
|
+
for (const doc of allDocs) if (doc.filePath) {
|
|
62
|
+
const key = path.resolve(doc.filePath);
|
|
63
|
+
const list = docsByFilePath.get(key) || [];
|
|
64
|
+
list.push(doc);
|
|
65
|
+
docsByFilePath.set(key, list);
|
|
66
|
+
}
|
|
67
|
+
await Promise.all(filesToProcess.map(async ({ relFile, absPath, componentName }) => {
|
|
68
|
+
log(`parsing file: : ${relFile}`);
|
|
69
|
+
const fileDocs = docsByFilePath.get(path.resolve(absPath));
|
|
70
|
+
const props = fileDocs && fileDocs[0] ? fileDocs[0].props : {};
|
|
36
71
|
for (const propName in props) {
|
|
37
72
|
const prop = props[propName];
|
|
38
73
|
if (!prop) throw new Error(`No prop found for ${propName} in ${componentName}`);
|
|
@@ -45,14 +80,14 @@ async function generateProps(pagesPattern, rootDir, outputPath, log = console.lo
|
|
|
45
80
|
}
|
|
46
81
|
const componentJSON = JSON.stringify({
|
|
47
82
|
name: componentName,
|
|
48
|
-
path:
|
|
49
|
-
fileName:
|
|
83
|
+
path: relFile,
|
|
84
|
+
fileName: relFile.split("/").pop(),
|
|
50
85
|
props
|
|
51
86
|
}, null, 2);
|
|
52
|
-
const outputFilePath = path.join(
|
|
87
|
+
const outputFilePath = path.join(resolvedOutputPath, `${componentName}.json`);
|
|
53
88
|
await fs.writeFile(outputFilePath, componentJSON + "\n", "utf-8");
|
|
54
89
|
log(`Generated props for ${componentName}`);
|
|
55
|
-
}
|
|
90
|
+
}));
|
|
56
91
|
} catch (err) {
|
|
57
92
|
log(`Error generating props: ${err.message}`);
|
|
58
93
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate-props.mjs","names":[],"sources":["../../src/generate-props.ts"],"sourcesContent":["import fs from \"fs/promises\";\nimport path from \"path\";\nimport { PropItem, withCustomConfig } from \"react-docgen-typescript\";\nimport { glob } from \"tinyglobby\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nexport async function generateProps(\n pagesPattern: string,\n rootDir: string,\n outputPath: string,\n log = console.log\n) {\n // TODO - should this be the root tsconfig, the parcel project tsconfig, or the ui library tsconfig?\n // TODO - make this configurable\n const tsconfigPath = path.resolve(rootDir, \"./tsconfig.json\");\n\n const docgenParser = withCustomConfig(tsconfigPath, {\n shouldExtractLiteralValuesFromEnum: true,\n savePropValueAsString: true,\n propFilter: (prop) => {\n if (Array.isArray(prop.declarations) && prop.declarations[0]) {\n const declaration = prop.declarations[0];\n\n if (declaration.fileName.includes(\"node_modules/@types/react\")) {\n if (prop.name !== \"ref\" && prop.name !== \"className\") {\n return false;\n }\n }\n }\n return true;\n },\n });\n\n const
|
|
1
|
+
{"version":3,"file":"generate-props.mjs","names":[],"sources":["../../src/generate-props.ts"],"sourcesContent":["import fs from \"fs/promises\";\nimport path from \"path\";\nimport { PropItem, withCustomConfig } from \"react-docgen-typescript\";\nimport { glob } from \"tinyglobby\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\n// cache for watch mode across buildStart events\nconst fileMtimeCache = new Map<string, number>();\n\nexport async function generateProps(\n pagesPattern: string,\n rootDir: string,\n outputPath: string,\n log = console.log\n) {\n const files = await glob(pagesPattern, {\n cwd: rootDir,\n });\n\n if (files.length === 0) {\n log(`No files found for pattern: ${pagesPattern}`);\n return;\n }\n\n const resolvedOutputPath = path.resolve(rootDir, outputPath);\n await fs.mkdir(resolvedOutputPath, { recursive: true });\n\n const filesToProcess: {\n relFile: string;\n absPath: string;\n componentName: string;\n }[] = [];\n\n for (const file of files) {\n const filePath = path.resolve(rootDir, file);\n const componentName = path.basename(file, path.extname(file));\n const outputFilePath = path.join(\n resolvedOutputPath,\n `${componentName}.json`\n );\n\n try {\n const sourceStat = await fs.stat(filePath);\n const cachedMtime = fileMtimeCache.get(filePath);\n\n let isUpToDate = false;\n try {\n const outStat = await fs.stat(outputFilePath);\n if (\n (cachedMtime !== undefined && cachedMtime === sourceStat.mtimeMs) ||\n outStat.mtimeMs >= sourceStat.mtimeMs\n ) {\n isUpToDate = true;\n fileMtimeCache.set(filePath, sourceStat.mtimeMs);\n }\n } catch {\n // output file does not exist\n }\n\n if (isUpToDate) {\n continue;\n }\n\n filesToProcess.push({\n relFile: file,\n absPath: filePath,\n componentName,\n });\n fileMtimeCache.set(filePath, sourceStat.mtimeMs);\n } catch {\n // source file not accessible\n }\n }\n\n if (filesToProcess.length === 0) {\n return;\n }\n\n log(\n `Found ${filesToProcess.length} changed file(s) for pattern: ${pagesPattern}`\n );\n\n // TODO - should this be the root tsconfig, the parcel project tsconfig, or the ui library tsconfig?\n // TODO - make this configurable\n const tsconfigPath = path.resolve(rootDir, \"./tsconfig.json\");\n\n const docgenParser = withCustomConfig(tsconfigPath, {\n shouldExtractLiteralValuesFromEnum: true,\n savePropValueAsString: true,\n propFilter: (prop) => {\n if (Array.isArray(prop.declarations) && prop.declarations[0]) {\n const declaration = prop.declarations[0];\n\n if (declaration.fileName.includes(\"node_modules/@types/react\")) {\n if (prop.name !== \"ref\" && prop.name !== \"className\") {\n return false;\n }\n }\n }\n return true;\n },\n });\n\n try {\n const absPaths = filesToProcess.map((f) => f.absPath);\n const allDocs = docgenParser.parse(absPaths);\n\n const docsByFilePath = new Map<string, typeof allDocs>();\n for (const doc of allDocs) {\n if (doc.filePath) {\n const key = path.resolve(doc.filePath);\n const list = docsByFilePath.get(key) || [];\n list.push(doc);\n docsByFilePath.set(key, list);\n }\n }\n\n await Promise.all(\n filesToProcess.map(async ({ relFile, absPath, componentName }) => {\n log(`parsing file: : ${relFile}`);\n\n const fileDocs = docsByFilePath.get(path.resolve(absPath));\n const props = fileDocs && fileDocs[0] ? fileDocs[0].props : {};\n\n for (const propName in props) {\n const prop = props[propName] as\n | (PropItem & {\n shortPropTypeName: string | null;\n })\n | undefined;\n\n if (!prop) {\n throw new Error(\n `No prop found for ${propName} in ${componentName}`\n );\n }\n\n if (prop.type && prop.type.name) {\n const { type: shortPropTypeName, detailedType } = getShortPropType(\n propName,\n prop.type.name\n );\n\n const hasExpandedType = Boolean(detailedType);\n\n prop.type.name =\n hasExpandedType && prop.type.name.split(\"|\").length > 3\n ? prop.type.name\n .split(\"|\")\n .map((line) => `| ${line}\\n`)\n .join(\"\")\n : prop.type.name;\n\n prop.shortPropTypeName = hasExpandedType ? shortPropTypeName : null;\n }\n }\n\n const componentJSON = JSON.stringify(\n {\n name: componentName,\n path: relFile,\n fileName: relFile.split(\"/\").pop(),\n props,\n },\n null,\n 2\n );\n\n const outputFilePath = path.join(\n resolvedOutputPath,\n `${componentName}.json`\n );\n await fs.writeFile(outputFilePath, componentJSON + \"\\n\", \"utf-8\");\n\n log(`Generated props for ${componentName}`);\n })\n );\n } catch (err) {\n log(`Error generating props: ${(err as Error).message}`);\n }\n}\n\n// based on https://github.com/mui/base-ui/blob/master/docs/src/components/ReferenceTable/PropsReferenceAccordion.tsx\nfunction getShortPropType(name: string, type: string) {\n if (/^(on|get)[A-Z].*/.test(name)) {\n return { type: \"function\", detailedType: true };\n }\n\n if (type === undefined || type === null) {\n return { type: String(type), detailedType: false };\n }\n\n if (name === \"className\") {\n return { type: \"string | function\", detailedType: true };\n }\n\n if (name === \"render\") {\n return { type: \"ReactElement | function\", detailedType: true };\n }\n\n if (\n name.endsWith(\"Ref\") ||\n name === \"children\" ||\n type === \"boolean\" ||\n type === \"string\" ||\n type === \"number\" ||\n type.indexOf(\" | \") === -1 ||\n (type.split(\"|\").length < 3 && type.length < 30)\n ) {\n return { type, detailedType: false };\n }\n\n return { type: \"Union\", detailedType: true };\n}\n"],"mappings":";;;;;;AAMA,MAAM,aAAa,cAAc,OAAO,KAAK,IAAI;AAC/B,KAAK,QAAQ,WAAW;AAG1C,MAAM,iCAAiB,IAAI,KAAqB;AAEhD,eAAsB,cACpB,cACA,SACA,YACA,MAAM,QAAQ,KACd;CACA,MAAM,QAAQ,MAAM,KAAK,cAAc,EACrC,KAAK,SACN,CAAC;CAEF,IAAI,MAAM,WAAW,GAAG;EACtB,IAAI,+BAA+B,eAAe;EAClD;;CAGF,MAAM,qBAAqB,KAAK,QAAQ,SAAS,WAAW;CAC5D,MAAM,GAAG,MAAM,oBAAoB,EAAE,WAAW,MAAM,CAAC;CAEvD,MAAM,iBAIA,EAAE;CAER,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,WAAW,KAAK,QAAQ,SAAS,KAAK;EAC5C,MAAM,gBAAgB,KAAK,SAAS,MAAM,KAAK,QAAQ,KAAK,CAAC;EAC7D,MAAM,iBAAi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