Skip to content

Commit dfc141c

Browse files
committed
Interops readmes into registry pages
1 parent b42099d commit dfc141c

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

scripts/docs.ts

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,46 @@ function cleanTargetDir(targetPath: string) {
1818
if (fs.existsSync(targetPath)) fs.emptyDirSync(targetPath);
1919
}
2020

21+
function getReadme(transformId: string) {
22+
const readmeFilePath = path.join(COMMUNITY_PATH, transformId, 'README.md');
23+
24+
const readmeRaw = fs.existsSync(readmeFilePath)
25+
? fs.readFileSync(readmeFilePath, 'utf-8')
26+
: '';
27+
28+
// remove first line
29+
const readme = readmeRaw.trim().split('\n');
30+
readme.shift();
31+
return readme.join('\n').trim();
32+
}
33+
34+
function renderTransform(
35+
id: string,
36+
packageName: string,
37+
type: 'transform' | 'preset',
38+
safePackageName: string,
39+
urlSafePackageName: string,
40+
packageLink: string,
41+
) {
42+
const seperator = type === 'transform' ? '@' : '#';
43+
const readme = getReadme(`${packageName}/${id}`);
44+
const fallback =
45+
type === 'transform'
46+
? `A codemod which facilitates the migration of the ${packageLink} package to version ${id}.`
47+
: '';
48+
49+
return `### ${id}
50+
51+
:::info
52+
[Source](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community/${urlSafePackageName}) | [Report an issue](https://github.com/CodeshiftCommunity/CodeshiftCommunity/issues/new?title=${safePackageName}@${id})
53+
54+
**Usage** \`$ codeshift --packages ${packageName}${seperator}${id} path/to/source\`
55+
:::
56+
57+
${readme ? readme : fallback}
58+
`;
59+
}
60+
2161
interface DocsData {
2262
name: string;
2363
config: CodeshiftConfig;
@@ -58,54 +98,39 @@ slug: /registry/${safeName}
5898
5999
**Target package:** ${packageLink}
60100
61-
**Maintainers:**
101+
${
102+
config.maintainers?.length
103+
? `**Maintainers:**
62104
63105
${config.maintainers!.map(
64106
maintainer => `- [${maintainer}](https://github.com/${maintainer})`,
65107
)}
66108
109+
`
110+
: ''
111+
}
67112
${
68-
config.transforms
113+
config.transforms && Object.keys(config.transforms).length
69114
? `
70115
## Transforms
71116
72117
${Object.keys(config.transforms)
73-
.map(
74-
key => `### ${key}
75-
76-
[Source](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community/${urlSafeName}) | [Report an issue](https://github.com/CodeshiftCommunity/CodeshiftCommunity/issues/new?title=${safeName}@${key})
77-
78-
Migrates ${packageLink} to version ${key}.
79-
80-
#### Usage
81-
82-
\`\`\`
83-
$ codeshift --packages ${name}@${key} path/to/source
84-
\`\`\`
85-
`,
118+
.map(key =>
119+
renderTransform(key, name, 'transform', safeName, urlSafeName, packageLink),
86120
)
87121
.join('')}
88122
`
89123
: ''
90124
}
91125
92126
${
93-
config.presets
127+
config.presets && Object.keys(config.presets).length
94128
? `
95129
## Presets
96130
97131
${Object.keys(config.presets)
98-
.map(
99-
key => `### ${key}
100-
101-
[Source](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community/${urlSafeName}) | [Report an issue](https://github.com/CodeshiftCommunity/CodeshiftCommunity/issues/new?title=${safeName}@${key})
102-
103-
#### Usage
104-
105-
\`\`\`
106-
$ codeshift --packages ${name}#${key} path/to/source
107-
\`\`\`
108-
`,
132+
.map(key =>
133+
renderTransform(key, name, 'preset', safeName, urlSafeName, packageLink),
109134
)
110135
.join('')}
111136
`

0 commit comments

Comments
 (0)