|
| 1 | +// test with ./packages/cli/bin/janus-cli package metadata --help |
| 2 | + |
| 3 | +import { OptionValues } from 'commander'; |
| 4 | +import _ from 'lodash'; |
| 5 | + |
| 6 | +import * as fs from 'fs'; |
| 7 | +import { execSync } from 'node:child_process'; |
| 8 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 9 | +import { join } from 'node:path'; |
| 10 | + |
| 11 | +export async function command(opts: OptionValues): Promise<void> { |
| 12 | + updatePackageMetadata(opts); |
| 13 | +} |
| 14 | + |
| 15 | +interface Repository { |
| 16 | + type: string; |
| 17 | + url: string; |
| 18 | + directory: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface packageJSON { |
| 22 | + name: string; |
| 23 | + version: string; |
| 24 | + main: string; |
| 25 | + types: string; |
| 26 | + license: string; |
| 27 | + author: string; |
| 28 | + configschema: string; |
| 29 | + |
| 30 | + homepage: URL; |
| 31 | + bugs: URL; |
| 32 | + |
| 33 | + sideEffects: boolean; |
| 34 | + |
| 35 | + publishConfig: {}; |
| 36 | + backstage: { |
| 37 | + role: string; |
| 38 | + 'supported-versions': string; |
| 39 | + }; |
| 40 | + scripts: {}; |
| 41 | + dependencies: {}; |
| 42 | + peerDependencies: {}; |
| 43 | + devDependencies: {}; |
| 44 | + scalprum: {}; |
| 45 | + repository: Repository; |
| 46 | + |
| 47 | + files: string[]; |
| 48 | + maintainers: string[]; |
| 49 | + keywords: string[]; |
| 50 | +} |
| 51 | + |
| 52 | +const path = { |
| 53 | + /** |
| 54 | + * @method resolveRelativeFromAbsolute resolves a relative path from an absolute path |
| 55 | + * @param {String} DotDotPath relative path |
| 56 | + * @returns {String} resolved absolutePath |
| 57 | + */ |
| 58 | + resolveRelativeFromAbsolute(DotDotPath: String) { |
| 59 | + const pathsArray = DotDotPath.replaceAll(/[\/|\\]/g, '/').split('/'); |
| 60 | + const map = pathsArray.reduce( |
| 61 | + (acc, e) => acc.set(e, (acc.get(e) || 0) + 1), |
| 62 | + new Map(), |
| 63 | + ); |
| 64 | + // console.log(` @ ${DotDotPath} => ${pathsArray} (${map.get("..")} backs)`) |
| 65 | + const rootdir = pathsArray.slice(0, -(map.get('..') * 2)).join('/'); |
| 66 | + // console.log(` @ root dir: ${rootdir}`) |
| 67 | + const relativeDir = process.cwd().replaceAll(`${rootdir}/`, ''); |
| 68 | + return [rootdir, relativeDir]; |
| 69 | + }, |
| 70 | +}; |
| 71 | + |
| 72 | +function findCodeowners(element: String) { |
| 73 | + if (fs.existsSync(`${element}/.github/CODEOWNERS`)) { |
| 74 | + return element; // found the root dir |
| 75 | + } |
| 76 | + return findCodeowners(`${element}/..`); |
| 77 | +} |
| 78 | + |
| 79 | +export function updatePackageMetadata(opts: OptionValues) { |
| 80 | + // load the package.json from the specified (or current) folder |
| 81 | + const workingDir = `${process.cwd()}/${opts.dir}`; |
| 82 | + console.log(`Updating ${workingDir} / package.json`); |
| 83 | + |
| 84 | + // compute the root dir and relative path to the current dir |
| 85 | + const [rootdir, relative_path] = opts.dir |
| 86 | + ? [process.cwd(), opts.dir] |
| 87 | + : path.resolveRelativeFromAbsolute(findCodeowners(`${process.cwd()}`)); |
| 88 | + // console.log(` @ rootdir = ${rootdir}, relative_path = ${relative_path}`) |
| 89 | + |
| 90 | + const packageJSONPath = join(workingDir, 'package.json'); |
| 91 | + const packageJSON = JSON.parse( |
| 92 | + readFileSync(packageJSONPath, 'utf8'), |
| 93 | + ) as packageJSON; |
| 94 | + |
| 95 | + /* now let's change some values */ |
| 96 | + |
| 97 | + // 1. add backstage version matching the current value of backstage.json in this repo |
| 98 | + if (fs.existsSync(join(rootdir, '/backstage.json'))) { |
| 99 | + packageJSON.backstage['supported-versions'] = JSON.parse( |
| 100 | + readFileSync(join(rootdir, '/backstage.json'), 'utf8'), |
| 101 | + ).version; |
| 102 | + // console.log(packageJSON.backstage) |
| 103 | + } |
| 104 | + |
| 105 | + // 2. set up repository values and the current path as repo.directory |
| 106 | + const repo = {} as Repository; |
| 107 | + repo.type = 'git'; |
| 108 | + repo.url = execSync('git config --get remote.origin.url') |
| 109 | + .toString() |
| 110 | + .replaceAll('[email protected]:', 'https://github.com/') |
| 111 | + .replaceAll('.git', '') |
| 112 | + .trim(); |
| 113 | + repo.directory = relative_path; |
| 114 | + packageJSON.repository = repo; |
| 115 | + |
| 116 | + // 3. load owners from CODEOWNERS file, using this package.json's repository.directory field to compute maintainer groups |
| 117 | + let owners = []; |
| 118 | + if (packageJSON.repository.directory) { |
| 119 | + const Codeowners = require('codeowners'); |
| 120 | + const repos = new Codeowners(); |
| 121 | + owners = repos.getOwner(relative_path); |
| 122 | + } else { |
| 123 | + console.log( |
| 124 | + ` ! Could not load .github/CODEOWNERS file, so cannot update maintainers in package.json`, |
| 125 | + ); |
| 126 | + } |
| 127 | + packageJSON.maintainers = owners; |
| 128 | + |
| 129 | + // 4. set some hardcoded values based on commandline flags |
| 130 | + packageJSON.author = opts.author; |
| 131 | + packageJSON.license = opts.license; |
| 132 | + packageJSON.homepage = new URL(opts.homepage); |
| 133 | + packageJSON.bugs = new URL(opts.bugs); |
| 134 | + |
| 135 | + // eslint-disable-next-line prefer-const |
| 136 | + let newKeywords = opts.keywords.split(','); |
| 137 | + // if already have keywords, replace lifecycle and support with new values (if defined) |
| 138 | + if (packageJSON.keywords.length > 0) { |
| 139 | + for (let i = 0; i < packageJSON.keywords.length; i++) { |
| 140 | + // can only have ONE lifecycle and one support keyword, so remove replace any existing values |
| 141 | + if ( |
| 142 | + packageJSON.keywords[i].startsWith('lifecycle:') || |
| 143 | + packageJSON.keywords[i].startsWith('support:') |
| 144 | + ) { |
| 145 | + for (let j = 0; j < newKeywords.length; j++) { |
| 146 | + if ( |
| 147 | + newKeywords[j].startsWith('lifecycle:') || |
| 148 | + newKeywords[j].startsWith('support:') |
| 149 | + ) { |
| 150 | + // replace existing; remove from array |
| 151 | + packageJSON.keywords[i] = newKeywords[j]; |
| 152 | + newKeywords.splice(j, 1); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + // add in the remaining keywords + dedupe |
| 159 | + for (let j = 0; j < newKeywords.length; j++) { |
| 160 | + packageJSON.keywords.push(newKeywords[j]); |
| 161 | + } |
| 162 | + packageJSON.keywords = _.uniq(packageJSON.keywords); |
| 163 | + |
| 164 | + /* all done! */ |
| 165 | + |
| 166 | + // write changes to file |
| 167 | + writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2), 'utf8'); |
| 168 | +} |
0 commit comments