Skip to content

Commit 12f7ae1

Browse files
Rhidp 1502 (#1641)
* feat: RHIDP-1502 add method for janus-cli package metadata Signed-off-by: Nick Boldt <[email protected]> experimenting with a new packages/cli/src/commands/metadata/index.ts command Signed-off-by: Nick Boldt <[email protected]> lint changes and fix to readme Signed-off-by: Nick Boldt <[email protected]> implement metadata generation from CODEOWNERS and some hardcoded values Signed-off-by: Nick Boldt <[email protected]> implement metadata generation from CODEOWNERS and some hardcoded values Signed-off-by: Nick Boldt <[email protected]> quieter output; updated sample; regen yarn lock Signed-off-by: Nick Boldt <[email protected]> quieter output; updated sample; regen yarn lock Signed-off-by: Nick Boldt <[email protected]> move hardcoded defaults into cmdline options; support running in root folder against a specific plugins/* folder Signed-off-by: Nick Boldt <[email protected]> change description and instuctions Signed-off-by: Nick Boldt <[email protected]> include script for batch updating all the RH supported plugins included in showcase; for now, don't update metadata in the non-included plugins Signed-off-by: Nick Boldt <[email protected]> switch from execSync to run git to using gitconfiglocal (to just parse the .git/config file) (sonarlint) Signed-off-by: Nick Boldt <[email protected]> * include individual plugin/*/package.json updates Signed-off-by: Nick Boldt <[email protected]> * feat: RHIDP-1502 add method for janus-cli package metadata Signed-off-by: Nick Boldt <[email protected]> experimenting with a new packages/cli/src/commands/metadata/index.ts command Signed-off-by: Nick Boldt <[email protected]> lint changes and fix to readme Signed-off-by: Nick Boldt <[email protected]> implement metadata generation from CODEOWNERS and some hardcoded values Signed-off-by: Nick Boldt <[email protected]> implement metadata generation from CODEOWNERS and some hardcoded values Signed-off-by: Nick Boldt <[email protected]> quieter output; updated sample; regen yarn lock Signed-off-by: Nick Boldt <[email protected]> quieter output; updated sample; regen yarn lock Signed-off-by: Nick Boldt <[email protected]> move hardcoded defaults into cmdline options; support running in root folder against a specific plugins/* folder Signed-off-by: Nick Boldt <[email protected]> change description and instuctions Signed-off-by: Nick Boldt <[email protected]> include script for batch updating all the RH supported plugins included in showcase; for now, don't update metadata in the non-included plugins Signed-off-by: Nick Boldt <[email protected]> switch from execSync to run git to using gitconfiglocal (to just parse the .git/config file) (sonarlint) Signed-off-by: Nick Boldt <[email protected]> switch yarn lock to use registry.yarnpkg.com Signed-off-by: Nick Boldt <[email protected]> * add missing type declaration for gitconfiglocal Signed-off-by: Nick Boldt <[email protected]> * add missing copyright / fix date Signed-off-by: Nick Boldt <[email protected]> add TODOs to the plugins/plugin-metadata.sh and plugins/plugin-metadata.json files Signed-off-by: Nick Boldt <[email protected]> * process all 42 plugins in the repo Signed-off-by: Nick Boldt <[email protected]> * process all 42 plugins in the repo Signed-off-by: Nick Boldt <[email protected]> * refactor command.ts Signed-off-by: Paul Schultz <[email protected]> * fix sh Signed-off-by: Paul Schultz <[email protected]> * add lines Signed-off-by: Paul Schultz <[email protected]> * Update packages/cli/src/commands/metadata/command.ts --------- Signed-off-by: Nick Boldt <[email protected]> Signed-off-by: Paul Schultz <[email protected]> Co-authored-by: Nick Boldt <[email protected]>
1 parent 84426d4 commit 12f7ae1

File tree

44 files changed

+226
-222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+226
-222
lines changed

packages/cli/src/commands/metadata/command.ts

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
// test with ./packages/cli/bin/janus-cli package metadata --help
1818

19+
import Codeowners from 'codeowners';
1920
import { OptionValues } from 'commander';
2021
import gitconfig from 'gitconfiglocal';
21-
import _ from 'lodash';
2222

2323
import * as fs from 'fs';
2424
import { readFileSync, writeFileSync } from 'node:fs';
@@ -30,8 +30,7 @@ const pGitconfig = promisify(gitconfig);
3030

3131
export async function command(opts: OptionValues): Promise<void> {
3232
const config = await pGitconfig(process.cwd());
33-
const gitconfigremoteoriginurl = config?.remote?.origin?.url;
34-
updatePackageMetadata(opts, gitconfigremoteoriginurl);
33+
updatePackageMetadata(opts, config?.remote?.origin?.url);
3534
}
3635

3736
interface Repository {
@@ -40,7 +39,7 @@ interface Repository {
4039
directory: string;
4140
}
4241

43-
interface PackageJSON {
42+
interface PackageJson {
4443
name: string;
4544
version: string;
4645
main: string;
@@ -74,20 +73,20 @@ interface PackageJSON {
7473
const path = {
7574
/**
7675
* @method resolveRelativeFromAbsolute resolves a relative path from an absolute path
77-
* @param {String} DotDotPath relative path
78-
* @returns {String} resolved absolutePath
76+
* @param {string} DotDotPath relative path
77+
* @returns {string} resolved absolutePath
7978
*/
8079
resolveRelativeFromAbsolute(DotDotPath: string) {
81-
const pathsArray = DotDotPath.replaceAll(/[/|\\]/g, '/').split('/');
80+
const pathsArray = DotDotPath.replaceAll(/[\/|\\]/g, '/').split('/');
8281
const map = pathsArray.reduce(
8382
(acc, e) => acc.set(e, (acc.get(e) || 0) + 1),
8483
new Map(),
8584
);
8685
// console.log(` @ ${DotDotPath} => ${pathsArray} (${map.get("..")} backs)`)
87-
const rootdir = pathsArray.slice(0, -(map.get('..') * 2)).join('/');
86+
const rootDir = pathsArray.slice(0, -(map.get('..') * 2)).join('/');
8887
// console.log(` @ root dir: ${rootdir}`)
89-
const relativeDir = process.cwd().replaceAll(`${rootdir}/`, '');
90-
return [rootdir, relativeDir];
88+
const relativeDir = process.cwd().replaceAll(`${rootDir}/`, '');
89+
return [rootDir, relativeDir];
9190
},
9291
};
9392

@@ -98,103 +97,79 @@ function findCodeowners(element: string) {
9897
return findCodeowners(`${element}/..`);
9998
}
10099

101-
function loadPackageJSON(opts: OptionValues) {
100+
function separateKeywords(array: string[]): {
101+
keywords: string[];
102+
lifecycle?: string;
103+
support?: string;
104+
} {
105+
return array.reduce(
106+
(prev, keyword) => {
107+
// separate lifecycle keyword
108+
if (keyword.startsWith('lifecycle:')) {
109+
return { ...prev, lifecycle: keyword };
110+
}
111+
112+
// separate support keyword
113+
if (keyword.startsWith('support:')) {
114+
return { ...prev, support: keyword };
115+
}
116+
117+
// keep the remaining keywords together
118+
prev.keywords.push(keyword);
119+
return prev;
120+
},
121+
{ keywords: [] } as {
122+
keywords: string[];
123+
lifecycle?: string;
124+
support?: string;
125+
},
126+
);
127+
}
128+
129+
export function updatePackageMetadata(
130+
opts: OptionValues,
131+
gitconfigRemoteOriginUrl: string,
132+
) {
102133
// load the package.json from the specified (or current) folder
103134
const workingDir = `${process.cwd()}/${opts.dir}`;
104135
console.log(`Updating ${workingDir} / package.json`);
105136

106137
// compute the root dir and relative path to the current dir
107-
const [rootdir, relative_path] = opts.dir
138+
const [rootDir, relativePath] = opts.dir
108139
? [process.cwd(), opts.dir]
109140
: path.resolveRelativeFromAbsolute(findCodeowners(`${process.cwd()}`));
110141
// console.log(` @ rootdir = ${rootdir}, relative_path = ${relative_path}`)
111142

112143
const packageJSONPath = join(workingDir, 'package.json');
113144
const packageJSON = JSON.parse(
114145
readFileSync(packageJSONPath, 'utf8'),
115-
) as PackageJSON;
146+
) as PackageJson;
116147

117-
return [packageJSON, packageJSONPath, rootdir, relative_path];
118-
}
119-
120-
function replaceKeywordsInPackageJSON(
121-
packageJSON: PackageJSON,
122-
newKeywords: string[],
123-
i: any,
124-
) {
125-
for (let j = 0; j < newKeywords.length; j++) {
126-
if (
127-
newKeywords[j].startsWith('lifecycle:') ||
128-
newKeywords[j].startsWith('support:')
129-
) {
130-
// replace existing; remove from array
131-
packageJSON.keywords[i] = newKeywords[j];
132-
newKeywords.splice(j, 1);
133-
}
134-
}
135-
return packageJSON;
136-
}
137-
138-
function addNewKeywords(packageJSON: PackageJSON, opts: OptionValues) {
139-
// initialize empty string array if not already present
140-
if (!packageJSON.keywords) {
141-
packageJSON.keywords = [];
142-
}
143-
144-
// eslint-disable-next-line prefer-const
145-
let newKeywords = opts.keywords.split(',');
146-
// if already have keywords, replace lifecycle and support with new values (if defined)
147-
if (packageJSON.keywords.length > 0) {
148-
for (let i = 0; i < packageJSON.keywords.length; i++) {
149-
// can only have ONE lifecycle and one support keyword, so remove replace any existing values
150-
if (
151-
packageJSON.keywords[i].startsWith('lifecycle:') ||
152-
packageJSON.keywords[i].startsWith('support:')
153-
) {
154-
packageJSON = replaceKeywordsInPackageJSON(packageJSON, newKeywords, i);
155-
}
156-
}
157-
}
158-
// add in the remaining keywords + dedupe
159-
for (const element of newKeywords) {
160-
packageJSON.keywords.push(element);
161-
}
162-
packageJSON.keywords = _.uniq(packageJSON.keywords);
163-
return [packageJSON, opts];
164-
}
165-
166-
export function updatePackageMetadata(
167-
opts: OptionValues,
168-
gitconfigremoteoriginurl: string,
169-
) {
170-
const [packageJSON, packageJSONPath, rootdir, relative_path] =
171-
loadPackageJSON(opts);
148+
/* now let's change some values */
172149

173150
// 1. add backstage version matching the current value of backstage.json in this repo
174-
if (fs.existsSync(join(rootdir, '/backstage.json'))) {
151+
if (fs.existsSync(join(rootDir, '/backstage.json'))) {
175152
packageJSON.backstage['supported-versions'] = JSON.parse(
176-
readFileSync(join(rootdir, '/backstage.json'), 'utf8'),
153+
readFileSync(join(rootDir, '/backstage.json'), 'utf8'),
177154
).version;
178-
// console.log(packageJSON.backstage)
179155
}
180156

181157
// 2. set up repository values and the current path as repo.directory
182158
const repo = {} as Repository;
183159
repo.type = 'git';
184-
repo.url = gitconfigremoteoriginurl
160+
repo.url = gitconfigRemoteOriginUrl
185161
.toString()
186162
.replaceAll('[email protected]:', 'https://github.com/')
187163
.replaceAll('.git', '')
188164
.trim();
189-
repo.directory = relative_path;
165+
repo.directory = relativePath;
190166
packageJSON.repository = repo;
191167

192168
// 3. load owners from CODEOWNERS file, using this package.json's repository.directory field to compute maintainer groups
193-
let owners = [];
169+
let owners: string[] = [];
194170
if (packageJSON.repository.directory) {
195-
const Codeowners = require('codeowners');
196171
const repos = new Codeowners();
197-
owners = repos.getOwner(relative_path);
172+
owners = repos.getOwner(relativePath);
198173
} else {
199174
console.log(
200175
` ! Could not load .github/CODEOWNERS file, so cannot update maintainers in package.json`,
@@ -208,10 +183,39 @@ export function updatePackageMetadata(
208183
packageJSON.homepage = new URL(opts.homepage);
209184
packageJSON.bugs = new URL(opts.bugs);
210185

211-
// add keywords (replace some), then uniquify
212-
addNewKeywords(packageJSON, opts);
186+
// initialize empty string array if not already present
187+
if (!packageJSON.keywords) {
188+
packageJSON.keywords = [];
189+
}
190+
191+
// if already have keywords, replace lifecycle and support with new values (if defined)
192+
// we can only have ONE lifecycle and one support keyword, so remove replace any existing values
193+
const {
194+
keywords: oldKeywords,
195+
lifecycle: oldLifecycle,
196+
support: oldSupport,
197+
} = separateKeywords(packageJSON.keywords);
198+
199+
const {
200+
keywords: optsKeywords,
201+
lifecycle: optsLifecycle,
202+
support: optsSupport,
203+
} = separateKeywords(opts.keywords.split(','));
204+
205+
const newKeywords = oldKeywords.concat(optsKeywords);
206+
207+
// if there is a lifecycle keyword, push to the beginning of the array
208+
if (oldLifecycle || optsLifecycle) {
209+
newKeywords.unshift(optsLifecycle ?? oldLifecycle ?? '');
210+
}
211+
212+
// if there is a support keyword, push to the beginning of the array
213+
if (oldSupport || optsSupport) {
214+
newKeywords.unshift(optsSupport ?? oldSupport ?? '');
215+
}
213216

214-
/* all done! */
217+
// dedupe new keywords
218+
packageJSON.keywords = Array.from(new Set(newKeywords));
215219

216220
// write changes to file
217221
writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2), 'utf8');

plugins/3scale-backend/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@
7272
"directory": "plugins/3scale-backend"
7373
},
7474
"keywords": [
75-
"backstage",
76-
"plugin",
7775
"support:tech-preview",
78-
"lifecycle:active"
76+
"lifecycle:active",
77+
"backstage",
78+
"plugin"
7979
],
8080
"homepage": "https://red.ht/rhdh",
8181
"bugs": "https://github.com/janus-idp/backstage-plugins/issues",
8282
"maintainers": [
8383
"@janus-idp/maintainers-plugins"
8484
],
8585
"author": "Red Hat"
86-
}
86+
}

plugins/aap-backend/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@
7272
"directory": "plugins/aap-backend"
7373
},
7474
"keywords": [
75-
"backstage",
76-
"plugin",
7775
"support:tech-preview",
78-
"lifecycle:active"
76+
"lifecycle:active",
77+
"backstage",
78+
"plugin"
7979
],
8080
"homepage": "https://red.ht/rhdh",
8181
"bugs": "https://github.com/janus-idp/backstage-plugins/issues",
8282
"maintainers": [
8383
"@janus-idp/maintainers-plugins"
8484
],
8585
"author": "Red Hat"
86-
}
86+
}

plugins/acr/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@
6666
"directory": "plugins/acr"
6767
},
6868
"keywords": [
69-
"backstage",
70-
"plugin",
7169
"support:tech-preview",
72-
"lifecycle:active"
70+
"lifecycle:active",
71+
"backstage",
72+
"plugin"
7373
],
7474
"homepage": "https://red.ht/rhdh",
7575
"bugs": "https://github.com/janus-idp/backstage-plugins/issues",
7676
"maintainers": [
7777
"@janus-idp/maintainers-plugins"
7878
],
7979
"author": "Red Hat"
80-
}
80+
}

plugins/analytics-module-matomo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
"support:tech-preview",
6868
"lifecycle:active"
6969
]
70-
}
70+
}

plugins/analytics-provider-segment/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@
7979
"support:production",
8080
"lifecycle:active"
8181
]
82-
}
82+
}

plugins/argocd/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@
8686
"directory": "plugins/argocd"
8787
},
8888
"keywords": [
89-
"backstage",
90-
"plugin",
9189
"support:production",
9290
"lifecycle:active",
91+
"backstage",
92+
"plugin",
9393
"usage:ci-cd",
9494
"usage:integration"
9595
],
9696
"homepage": "https://red.ht/rhdh",
9797
"bugs": "https://github.com/janus-idp/backstage-plugins/issues"
98-
}
98+
}

plugins/bulk-import/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@
7474
"directory": "plugins/bulk-import"
7575
},
7676
"keywords": [
77-
"backstage",
78-
"plugin",
7977
"support:tech-preview",
80-
"lifecycle:active"
78+
"lifecycle:active",
79+
"backstage",
80+
"plugin"
8181
],
8282
"homepage": "https://red.ht/rhdh",
8383
"bugs": "https://github.com/janus-idp/backstage-plugins/issues",
8484
"maintainers": [
8585
"@janus-idp/maintainers-plugins"
8686
],
8787
"author": "The Backstage Community"
88-
}
88+
}

plugins/dynamic-plugins-info/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@
6464
"directory": "plugins/dynamic-plugins-info"
6565
},
6666
"keywords": [
67-
"backstage",
68-
"plugin",
6967
"support:production",
70-
"lifecycle:active"
68+
"lifecycle:active",
69+
"backstage",
70+
"plugin"
7171
],
7272
"homepage": "https://red.ht/rhdh",
7373
"bugs": "https://github.com/janus-idp/backstage-plugins/issues",
7474
"maintainers": [
7575
"@janus-idp/maintainers-plugins"
7676
],
7777
"author": "Red Hat"
78-
}
78+
}

0 commit comments

Comments
 (0)