Skip to content

Commit d9f0abb

Browse files
committed
sonarlint fixes and refactorings
Signed-off-by: Nick Boldt <[email protected]>
1 parent a1e7615 commit d9f0abb

File tree

1 file changed

+64
-45
lines changed

1 file changed

+64
-45
lines changed

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

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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 =
34-
config.remote && config.remote.origin && config.remote.origin.url;
33+
const gitconfigremoteoriginurl = config?.remote?.origin?.url;
3534
updatePackageMetadata(opts, gitconfigremoteoriginurl);
3635
}
3736

@@ -41,7 +40,7 @@ interface Repository {
4140
directory: string;
4241
}
4342

44-
interface packageJSON {
43+
interface PackageJSON {
4544
name: string;
4645
version: string;
4746
main: string;
@@ -78,8 +77,8 @@ const path = {
7877
* @param {String} DotDotPath relative path
7978
* @returns {String} resolved absolutePath
8079
*/
81-
resolveRelativeFromAbsolute(DotDotPath: String) {
82-
const pathsArray = DotDotPath.replaceAll(/[\/|\\]/g, '/').split('/');
80+
resolveRelativeFromAbsolute(DotDotPath: string) {
81+
const pathsArray = DotDotPath.replaceAll(/[/|\\]/g, '/').split('/');
8382
const map = pathsArray.reduce(
8483
(acc, e) => acc.set(e, (acc.get(e) || 0) + 1),
8584
new Map(),
@@ -92,17 +91,14 @@ const path = {
9291
},
9392
};
9493

95-
function findCodeowners(element: String) {
94+
function findCodeowners(element: string) {
9695
if (fs.existsSync(`${element}/.github/CODEOWNERS`)) {
9796
return element; // found the root dir
9897
}
9998
return findCodeowners(`${element}/..`);
10099
}
101100

102-
export function updatePackageMetadata(
103-
opts: OptionValues,
104-
gitconfigremoteoriginurl: string,
105-
) {
101+
function loadPackageJSON(opts: OptionValues) {
106102
// load the package.json from the specified (or current) folder
107103
const workingDir = `${process.cwd()}/${opts.dir}`;
108104
console.log(`Updating ${workingDir} / package.json`);
@@ -116,9 +112,63 @@ export function updatePackageMetadata(
116112
const packageJSONPath = join(workingDir, 'package.json');
117113
const packageJSON = JSON.parse(
118114
readFileSync(packageJSONPath, 'utf8'),
119-
) as packageJSON;
115+
) as PackageJSON;
116+
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+
}
120137

121-
/* now let's change some values */
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);
122172

123173
// 1. add backstage version matching the current value of backstage.json in this repo
124174
if (fs.existsSync(join(rootdir, '/backstage.json'))) {
@@ -158,39 +208,8 @@ export function updatePackageMetadata(
158208
packageJSON.homepage = new URL(opts.homepage);
159209
packageJSON.bugs = new URL(opts.bugs);
160210

161-
// initialize empty string array if not already present
162-
if (!packageJSON.keywords) {
163-
packageJSON.keywords = [];
164-
}
165-
166-
// eslint-disable-next-line prefer-const
167-
let newKeywords = opts.keywords.split(',');
168-
// if already have keywords, replace lifecycle and support with new values (if defined)
169-
if (packageJSON.keywords.length > 0) {
170-
for (let i = 0; i < packageJSON.keywords.length; i++) {
171-
// can only have ONE lifecycle and one support keyword, so remove replace any existing values
172-
if (
173-
packageJSON.keywords[i].startsWith('lifecycle:') ||
174-
packageJSON.keywords[i].startsWith('support:')
175-
) {
176-
for (let j = 0; j < newKeywords.length; j++) {
177-
if (
178-
newKeywords[j].startsWith('lifecycle:') ||
179-
newKeywords[j].startsWith('support:')
180-
) {
181-
// replace existing; remove from array
182-
packageJSON.keywords[i] = newKeywords[j];
183-
newKeywords.splice(j, 1);
184-
}
185-
}
186-
}
187-
}
188-
}
189-
// add in the remaining keywords + dedupe
190-
for (let j = 0; j < newKeywords.length; j++) {
191-
packageJSON.keywords.push(newKeywords[j]);
192-
}
193-
packageJSON.keywords = _.uniq(packageJSON.keywords);
211+
// add keywords (replace some), then uniquify
212+
addNewKeywords(packageJSON, opts);
194213

195214
/* all done! */
196215

0 commit comments

Comments
 (0)