|
| 1 | +import {accessSync, readFileSync, writeFileSync} from 'fs' |
| 2 | + |
| 3 | +function generateInstallationInstructions({packageJson: {name}}) { |
| 4 | + return `## Installation |
| 5 | + Available on [npm](https://www.npmjs.com/) as [**${name}**](https://www.npmjs.com/package/${name}). |
| 6 | + \`\`\` |
| 7 | + $ npm install --save ${name} |
| 8 | + \`\`\`` |
| 9 | +} |
| 10 | + |
| 11 | +function generateBrowserSupportInstructions() { |
| 12 | + return `## Browser Support |
| 13 | + Browsers without native [custom element support][support] require a [polyfill][]. |
| 14 | + - Chrome |
| 15 | + - Firefox |
| 16 | + - Safari |
| 17 | + - Microsoft Edge |
| 18 | + [support]: https://caniuse.com/custom-elementsv1 |
| 19 | + [polyfill]: https://github.com/webcomponents/custom-elements` |
| 20 | +} |
| 21 | + |
| 22 | +function generateTitle({packageJson: {name}}) { |
| 23 | + const formattedName = name |
| 24 | + .split('/') |
| 25 | + .at(-1) |
| 26 | + .replace(/-element$/, '') |
| 27 | + return `<${formattedName}> element` |
| 28 | +} |
| 29 | + |
| 30 | +function generateDescription({packageJson: {description}}) { |
| 31 | + return description |
| 32 | +} |
| 33 | + |
| 34 | +export function readme(options) { |
| 35 | + const {filename = 'README.md', exclude = [], title, preamble, footer} = options ?? {} |
| 36 | + |
| 37 | + const packageJson = JSON.parse(readFileSync('./package.json', 'utf8')) |
| 38 | + |
| 39 | + return { |
| 40 | + name: 'readme', |
| 41 | + async packageLinkPhase({customElementsManifest}) { |
| 42 | + const content = [ |
| 43 | + `# ${title || generateTitle({packageJson})}`, |
| 44 | + generateDescription({packageJson}), |
| 45 | + preamble, |
| 46 | + generateInstallationInstructions({packageJson}), |
| 47 | + `## Usage` |
| 48 | + ] |
| 49 | + for (const module of customElementsManifest.modules) { |
| 50 | + for (const {name, tagName, description} of module.declarations.filter(x => x.customElement)) { |
| 51 | + if (exclude.includes(name)) continue |
| 52 | + content.push(`### ${tagName}`) |
| 53 | + content.push(description.trim()) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + content.push(generateBrowserSupportInstructions()) |
| 58 | + content.push(footer) |
| 59 | + |
| 60 | + writeFileSync(filename, content.filter(Boolean).join('\n\n')) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments