|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +/** |
| 5 | + * ### > INSTALL |
| 6 | + * |
| 7 | + * Generate installation instructions in a markdown table format |
| 8 | + * |
| 9 | + * **Options:** |
| 10 | + * - `packageName` (optional): The name of the package to install. If not provided, will try to read from package.json |
| 11 | + * |
| 12 | + * **Example:** |
| 13 | + * ```md |
| 14 | + * <!-- doc-gen INSTALL packageName=my-package --> |
| 15 | + * Installation instructions will be generated here |
| 16 | + * <!-- end-doc-gen --> |
| 17 | + * ``` |
| 18 | + * |
| 19 | + * Default `matchWord` is `doc-gen` |
| 20 | + * |
| 21 | + * --- |
| 22 | + * @param {string} content The current content of the comment block |
| 23 | + * @param {object} options The options passed in from the comment declaration |
| 24 | + * @return {string} Updated content to place in the content block |
| 25 | + */ |
| 26 | +function install(api) { |
| 27 | + console.log('INSTALL API', api) |
| 28 | + const { options } = api |
| 29 | + const { isDev = false } = options |
| 30 | + let packageName = options.packageName |
| 31 | + |
| 32 | + // If no package name provided, try to read from package.json |
| 33 | + if (!packageName) { |
| 34 | + try { |
| 35 | + const packageJsonPath = path.join(process.cwd(), 'package.json') |
| 36 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')) |
| 37 | + packageName = packageJson.name |
| 38 | + } catch (error) { |
| 39 | + console.log('Could not read package.json:', error.message) |
| 40 | + return 'Error: No package name provided and could not read from package.json' |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + let header = '# Installation' |
| 45 | + if (options.header) { |
| 46 | + header = (options.header.startsWith('#')) ? options.header : `# ${options.header}` |
| 47 | + } |
| 48 | + header = `${header}\n\n` |
| 49 | + |
| 50 | + let body = `Install the \`${packageName}\` cli using your favorite package manager.` |
| 51 | + if (options.body) { |
| 52 | + body = (options.body.startsWith('\n')) ? options.body : `\n${options.body}` |
| 53 | + } |
| 54 | + body = `${body}\n` |
| 55 | + |
| 56 | + if (!packageName) { |
| 57 | + return 'Error: No package name provided' |
| 58 | + } |
| 59 | + |
| 60 | + const flag = isDev ? ' -D' : ' ' |
| 61 | + const space = isDev ? ' ' : '' |
| 62 | + |
| 63 | + return `${header}${body} |
| 64 | +| package manager | command | |
| 65 | +| --------------- | ------- | |
| 66 | +| npm | \`npm install ${packageName}${flag}\` | |
| 67 | +| pnpm | \`pnpm add ${packageName}${flag}\` | |
| 68 | +| yarn | \`yarn add ${packageName}${flag}\` | |
| 69 | +| bun | \`bun install ${packageName}\`${space} |` |
| 70 | +} |
| 71 | + |
| 72 | +module.exports = install |
0 commit comments