-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdocs.ts
164 lines (135 loc) · 3.67 KB
/
docs.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import fs from 'fs-extra';
import junk from 'junk';
import path from 'path';
import { CodeshiftConfig } from '@hypermod/types';
import { fetchConfig } from '@hypermod/fetcher';
const COMMUNITY_PATH = path.join(__dirname, '..', 'community');
const DOCS_PATH = path.join(
__dirname,
'..',
'website',
'docs',
'registry-generated',
);
function cleanTargetDir(targetPath: string) {
if (fs.existsSync(targetPath)) fs.emptyDirSync(targetPath);
}
function getReadme(transformSourcePath: string) {
const readmeFilePath = path.join(
COMMUNITY_PATH,
transformSourcePath,
'README.md',
);
const readmeRaw = fs.existsSync(readmeFilePath)
? fs.readFileSync(readmeFilePath, 'utf-8')
: '';
// remove first line
const readme = readmeRaw.trim().split('\n');
readme.shift();
return readme.join('\n').trim();
}
function renderTransform(
id: string,
packageName: string,
type: 'transform' | 'preset',
safePackageName: string,
urlSafePackageName: string,
packageLink: string,
) {
const seperator = type === 'transform' ? '@' : '#';
const readme = getReadme(`${packageName}/src/${id}`);
const fallback =
type === 'transform'
? `A codemod which facilitates the migration of the ${packageLink} package to version ${id}.`
: '';
return `### ${id}
:::info
[Source](https://github.com/hypermod-io/hypermod-community/tree/main/community/${urlSafePackageName}) | [Report an issue](https://github.com/hypermod-io/hypermod-community/issues/new?title=${safePackageName}@${id})
**Usage** \`$ hypermod --packages ${packageName}${seperator}${id} path/to/source\`
:::
${readme ? readme : fallback}
`;
}
interface DocsData {
name: string;
config: CodeshiftConfig;
}
async function main() {
const communityCodemods = fs.readdirSync(COMMUNITY_PATH);
const data: DocsData[] = [];
const directories = communityCodemods.filter(dir => junk.not(dir));
for (const dir of directories) {
const configMeta = await fetchConfig(path.join(COMMUNITY_PATH, dir));
if (!configMeta || !configMeta.config) {
throw new Error(`Unable to locate config for path: ${dir}`);
}
data.push({ name: dir, config: configMeta.config });
}
cleanTargetDir(DOCS_PATH);
console.log(data);
data.forEach(({ name, config }) => {
const safeName = name.replace('@', '');
const safeRawName = safeName.replace('__', '/');
const rawName = name.replace('__', '/');
const urlSafeName = encodeURIComponent(name);
const packageLink = `[${rawName}](https://www.npmjs.com/package/${rawName})`;
fs.outputFileSync(
path.join(DOCS_PATH, `${name}.mdx`),
`---
id: ${safeName}
title: ${safeRawName}
slug: /registry/${safeName}
keywords: [codemods, ${safeRawName}, code evolution, code migration, package updates, automated code updates]
description: Explore codemods for ${rawName}.
---
${
config.targets?.length
? `**Target package(s):**
${config
.targets!.map(
target => `- [${target}](https://www.npmjs.com/package/${target})`,
)
.join('\n')}
`
: ''
}
${
config.maintainers?.length
? `**Maintainers:**
${config
.maintainers!.map(
maintainer => `- [${maintainer}](https://github.com/${maintainer})`,
)
.join('\n')}
`
: ''
}
${
config.transforms && Object.keys(config.transforms).length
? `
## Transforms
${Object.keys(config.transforms)
.map(key =>
renderTransform(key, name, 'transform', safeName, urlSafeName, packageLink),
)
.join('')}
`
: ''
}
${
config.presets && Object.keys(config.presets).length
? `
## Presets
${Object.keys(config.presets)
.map(key =>
renderTransform(key, name, 'preset', safeName, urlSafeName, packageLink),
)
.join('')}
`
: ''
}
`,
);
});
}
main();