|
| 1 | +/* |
| 2 | + * Copyright The Backstage Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import fs from 'fs'; |
| 18 | +import inquirer, { DistinctQuestion } from 'inquirer'; |
| 19 | +import glob from 'glob'; |
| 20 | +import yaml from 'yaml'; |
| 21 | + |
| 22 | +import { |
| 23 | + EXTENSIONS_API_VERSION, |
| 24 | + MarketplaceKind, |
| 25 | + MarketplacePackage, |
| 26 | + MarketplacePlugin, |
| 27 | +} from '@red-hat-developer-hub/backstage-plugin-marketplace-common'; |
| 28 | + |
| 29 | +export default async function init() { |
| 30 | + const pluginFolders = await glob.glob('plugins/*/package.json/../'); |
| 31 | + |
| 32 | + const autoGeneratePackages = pluginFolders.length === 0; |
| 33 | + |
| 34 | + // current working basename |
| 35 | + const cwd = process.cwd().split('/').pop() ?? ''; |
| 36 | + // replace spaces with dashes and uppercase all first letters |
| 37 | + const defaultTitle = cwd |
| 38 | + .split('-') |
| 39 | + .map(word => word.charAt(0).toUpperCase() + word.slice(1)) |
| 40 | + .join(' '); |
| 41 | + |
| 42 | + const questions: DistinctQuestion[] = [ |
| 43 | + { |
| 44 | + type: 'input', |
| 45 | + name: 'title', |
| 46 | + message: 'Plugin name (display name)', |
| 47 | + default: defaultTitle, |
| 48 | + }, |
| 49 | + ]; |
| 50 | + |
| 51 | + if (autoGeneratePackages) { |
| 52 | + questions.push({ |
| 53 | + type: 'input', |
| 54 | + name: 'packageName', |
| 55 | + message: 'NPM package name incl. org (e.g. @my-org/my-plugin)', |
| 56 | + }); |
| 57 | + questions.push({ |
| 58 | + type: 'checkbox', |
| 59 | + name: 'packages', |
| 60 | + message: 'Packages', |
| 61 | + choices: [ |
| 62 | + { name: 'Frontend', value: 'frontend', checked: true }, |
| 63 | + { name: 'Backend', value: 'backend', checked: true }, |
| 64 | + ], |
| 65 | + }); |
| 66 | + } else { |
| 67 | + questions.push({ |
| 68 | + type: 'checkbox', |
| 69 | + name: 'packages', |
| 70 | + message: 'Packages', |
| 71 | + choices: pluginFolders.map(pluginFolder => ({ |
| 72 | + name: pluginFolder, |
| 73 | + value: pluginFolder, |
| 74 | + checked: true, |
| 75 | + })), |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + const answers = await inquirer.prompt(questions); |
| 80 | + |
| 81 | + console.log('answers', answers); |
| 82 | + |
| 83 | + const { title, packageName, namespace, author } = answers; |
| 84 | + |
| 85 | + let name = (packageName || (title as string)).toLowerCase().replace(/^@/, ''); |
| 86 | + if (name.includes('/')) { |
| 87 | + name = name.substring(name.lastIndexOf('/') + 1); |
| 88 | + } |
| 89 | + |
| 90 | + const plugin: MarketplacePlugin = { |
| 91 | + apiVersion: EXTENSIONS_API_VERSION, |
| 92 | + kind: MarketplaceKind.Plugin, |
| 93 | + metadata: { |
| 94 | + namespace, |
| 95 | + name, |
| 96 | + title, |
| 97 | + description: 'Plugin summary', |
| 98 | + }, |
| 99 | + spec: { |
| 100 | + author, |
| 101 | + description: '# Plugin name\n\nFull plugin description...', |
| 102 | + }, |
| 103 | + }; |
| 104 | + console.log(yaml.stringify(plugin).trim()); |
| 105 | + |
| 106 | + if (autoGeneratePackages) { |
| 107 | + if (answers.packages.includes('frontend')) { |
| 108 | + const frontendPackage: MarketplacePackage = { |
| 109 | + apiVersion: EXTENSIONS_API_VERSION, |
| 110 | + kind: MarketplaceKind.Package, |
| 111 | + metadata: { |
| 112 | + namespace, |
| 113 | + name: packageName |
| 114 | + .toLowerCase() |
| 115 | + .replace(/^@/, '') |
| 116 | + .replaceAll(/[^a-z0-9]/g, '-'), |
| 117 | + title: packageName, |
| 118 | + }, |
| 119 | + spec: { |
| 120 | + packageName, |
| 121 | + version: '0.1.0', |
| 122 | + partOf: [plugin.metadata.name], |
| 123 | + }, |
| 124 | + }; |
| 125 | + console.log('---'); |
| 126 | + console.log(yaml.stringify(frontendPackage).trim()); |
| 127 | + } |
| 128 | + |
| 129 | + if (answers.packages.includes('backend')) { |
| 130 | + const backendPackage: MarketplacePackage = { |
| 131 | + apiVersion: EXTENSIONS_API_VERSION, |
| 132 | + kind: MarketplaceKind.Package, |
| 133 | + metadata: { |
| 134 | + namespace, |
| 135 | + name: `${packageName |
| 136 | + .toLowerCase() |
| 137 | + .replace(/^@/, '') |
| 138 | + .replaceAll(/[^a-z0-9]/g, '-')}-backend`, |
| 139 | + title: `${packageName}-backend`, |
| 140 | + }, |
| 141 | + spec: { |
| 142 | + packageName: `${packageName}-backend`, |
| 143 | + version: '0.1.0', |
| 144 | + partOf: [plugin.metadata.name], |
| 145 | + }, |
| 146 | + }; |
| 147 | + console.log('---'); |
| 148 | + console.log(yaml.stringify(backendPackage).trim()); |
| 149 | + } |
| 150 | + } else { |
| 151 | + answers.packages.forEach((pluginFolder: string) => { |
| 152 | + const packageJsonPath = `${pluginFolder}/package.json`; |
| 153 | + const packageJson = JSON.parse( |
| 154 | + fs.readFileSync(packageJsonPath, { encoding: 'utf8' }), |
| 155 | + ); |
| 156 | + |
| 157 | + const pkg: MarketplacePackage = { |
| 158 | + apiVersion: EXTENSIONS_API_VERSION, |
| 159 | + kind: MarketplaceKind.Package, |
| 160 | + metadata: { |
| 161 | + namespace, |
| 162 | + name: packageJson.name |
| 163 | + .toLowerCase() |
| 164 | + .replace(/^@/, '') |
| 165 | + .replaceAll(/[^a-z0-9]/g, '-'), |
| 166 | + }, |
| 167 | + spec: { |
| 168 | + packageName: packageJson.name, |
| 169 | + version: packageJson.version, |
| 170 | + partOf: [plugin.metadata.name], |
| 171 | + }, |
| 172 | + }; |
| 173 | + console.log('---'); |
| 174 | + console.log(yaml.stringify(pkg).trim()); |
| 175 | + }); |
| 176 | + } |
| 177 | +} |
0 commit comments