|
| 1 | +import path from 'node:path'; |
| 2 | +import os from 'node:os'; |
| 3 | +import fs from 'node:fs/promises'; |
| 4 | +import phpfmt from 'phpfmt'; |
| 5 | +import { dirname } from 'dirname-filename-esm'; |
| 6 | + |
| 7 | +const __dirname = dirname(import.meta); |
| 8 | + |
| 9 | +const pkgJsonPath = path.join(__dirname, '../package.json'); |
| 10 | +const readmePath: string = path.join(__dirname, '../README.md'); |
| 11 | + |
| 12 | +try { |
| 13 | + const pkg = JSON.parse(String(await fs.readFile(pkgJsonPath))); |
| 14 | + const configuration = pkg.contributes.configuration; |
| 15 | + |
| 16 | + let config: string = |
| 17 | + '| Key | Type | Description | Default |' + |
| 18 | + os.EOL + |
| 19 | + '| -------- | ----------- | ----------- | ----------- |' + |
| 20 | + os.EOL; |
| 21 | + |
| 22 | + for (const configKey of Object.keys(configuration.properties)) { |
| 23 | + const configValue = configuration.properties[configKey]; |
| 24 | + config += `| ${configKey} | `; |
| 25 | + |
| 26 | + if (typeof configValue.type === 'string') { |
| 27 | + config += `\`${configValue.type}\``; |
| 28 | + } else if (Array.isArray(configValue.type)) { |
| 29 | + config += `\`${configValue.type.join(' \\| ')}\``; |
| 30 | + } |
| 31 | + config += ` | ${configValue.description}`; |
| 32 | + |
| 33 | + if (typeof configValue.default === 'string') { |
| 34 | + config += ` | "${configValue.default}"`; |
| 35 | + } else if (typeof configValue.default === 'number') { |
| 36 | + config += ` | ${configValue.default}`; |
| 37 | + } else if ( |
| 38 | + Array.isArray(configValue.default) || |
| 39 | + typeof configValue.default === 'boolean' |
| 40 | + ) { |
| 41 | + config += ` | ${JSON.stringify(configValue.default)}`; |
| 42 | + } else { |
| 43 | + throw new Error('uncovered type'); |
| 44 | + } |
| 45 | + |
| 46 | + config += ' | ' + os.EOL; |
| 47 | + } |
| 48 | + |
| 49 | + let readmeContent = String(await fs.readFile(readmePath)); |
| 50 | + readmeContent = readmeContent.replace( |
| 51 | + /<!-- Configuration START -->([\s\S]*)<!-- Configuration END -->/, |
| 52 | + () => { |
| 53 | + return ( |
| 54 | + '<!-- Configuration START -->' + |
| 55 | + os.EOL + |
| 56 | + config + |
| 57 | + os.EOL + |
| 58 | + '<!-- Configuration END -->' |
| 59 | + ); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + const { Transformation } = await import('../src/Transformation'); |
| 64 | + const transformation = new Transformation('php', phpfmt.v2); |
| 65 | + |
| 66 | + const transformations = await transformation.getTransformations(); |
| 67 | + |
| 68 | + readmeContent = readmeContent.replace( |
| 69 | + /<!-- Transformations START -->([\s\S]*)<!-- Transformations END -->/, |
| 70 | + () => { |
| 71 | + return ( |
| 72 | + '<!-- Transformations START -->' + |
| 73 | + os.EOL + |
| 74 | + '| Key | Description |' + |
| 75 | + os.EOL + |
| 76 | + '| -------- | ----------- |' + |
| 77 | + os.EOL + |
| 78 | + transformations |
| 79 | + .map(item => { |
| 80 | + let row = `| ${item.key} | `; |
| 81 | + row += item.description; |
| 82 | + row += ' |'; |
| 83 | + return row; |
| 84 | + }) |
| 85 | + .join(os.EOL) + |
| 86 | + os.EOL + |
| 87 | + '<!-- Transformations END -->' |
| 88 | + ); |
| 89 | + } |
| 90 | + ); |
| 91 | + const passes = transformation.getPasses(); |
| 92 | + |
| 93 | + const enums = passes.map(pass => { |
| 94 | + const p = transformations.find(t => t.key === pass); |
| 95 | + return { |
| 96 | + enum: pass, |
| 97 | + description: p?.description ?? 'Core pass' |
| 98 | + }; |
| 99 | + }); |
| 100 | + |
| 101 | + pkg.contributes.configuration.properties['phpfmt.passes'].items.enum = |
| 102 | + enums.map(o => o.enum); |
| 103 | + pkg.contributes.configuration.properties[ |
| 104 | + 'phpfmt.passes' |
| 105 | + ].items.enumDescriptions = enums.map(o => o.description); |
| 106 | + pkg.contributes.configuration.properties['phpfmt.exclude'].items.enum = |
| 107 | + enums.map(o => o.enum); |
| 108 | + pkg.contributes.configuration.properties[ |
| 109 | + 'phpfmt.exclude' |
| 110 | + ].items.enumDescriptions = enums.map(o => o.description); |
| 111 | + |
| 112 | + await fs.writeFile(readmePath, readmeContent); |
| 113 | + await fs.writeFile(pkgJsonPath, JSON.stringify(pkg, null, 2) + os.EOL); |
| 114 | +} catch (err) { |
| 115 | + console.error(err); |
| 116 | + process.exit(1); |
| 117 | +} |
0 commit comments