Skip to content

Commit 72d78b4

Browse files
committed
feat: enhance license handling with new utility functions
- Added utility functions to find license values, authors, and emails from user answers. - Refactored license file generation to utilize the new functions for improved readability and maintainability. - Removed the deprecated getAnswer function to streamline the codebase.
1 parent c663db3 commit 72d78b4

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

packages/create-gen-app/src/licenses.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ type LicenseTemplateMap = Record<string, string>;
1414
let cachedTemplates: LicenseTemplateMap | null = null;
1515

1616
export type SupportedLicense = string;
17+
export const LICENSE_VALUE_KEYS = ["LICENSE", "license"];
18+
export const LICENSE_AUTHOR_KEYS = [
19+
"USERFULLNAME",
20+
"AUTHOR",
21+
"AUTHORFULLNAME",
22+
"USERNAME",
23+
"fullName",
24+
"author",
25+
"authorFullName",
26+
"userName",
27+
];
28+
export const LICENSE_EMAIL_KEYS = ["USEREMAIL", "EMAIL", "email", "userEmail"];
1729

1830
export function isSupportedLicense(name: string): name is SupportedLicense {
1931
if (!name) {
@@ -58,6 +70,24 @@ export function listSupportedLicenses(): string[] {
5870
return Object.keys(loadLicenseTemplates());
5971
}
6072

73+
export function findLicenseValue(
74+
answers: Record<string, any>
75+
): string | undefined {
76+
return getAnswerValue(answers, LICENSE_VALUE_KEYS);
77+
}
78+
79+
export function findLicenseAuthor(
80+
answers: Record<string, any>
81+
): string | undefined {
82+
return getAnswerValue(answers, LICENSE_AUTHOR_KEYS);
83+
}
84+
85+
export function findLicenseEmail(
86+
answers: Record<string, any>
87+
): string | undefined {
88+
return getAnswerValue(answers, LICENSE_EMAIL_KEYS);
89+
}
90+
6191
function loadLicenseTemplates(): LicenseTemplateMap {
6292
if (cachedTemplates) {
6393
return cachedTemplates;
@@ -106,3 +136,15 @@ function findTemplatesDir(): string | null {
106136
return null;
107137
}
108138

139+
function getAnswerValue(
140+
answers: Record<string, any>,
141+
keys: string[]
142+
): string | undefined {
143+
for (const key of keys) {
144+
const value = answers?.[key];
145+
if (typeof value === "string" && value.trim() !== "") {
146+
return value;
147+
}
148+
}
149+
return undefined;
150+
}

packages/create-gen-app/src/template/replace.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { Transform } from 'stream';
44
import { pipeline } from 'stream/promises';
55

66
import { ExtractedVariables } from '../types';
7-
import { renderLicense, isSupportedLicense } from '../licenses';
7+
import {
8+
renderLicense,
9+
isSupportedLicense,
10+
findLicenseAuthor,
11+
findLicenseEmail,
12+
findLicenseValue,
13+
} from '../licenses';
814

915
/**
1016
* Replace variables in all files in the template directory
@@ -85,7 +91,7 @@ async function ensureLicenseFile(
8591
outputDir: string,
8692
answers: Record<string, any>
8793
): Promise<void> {
88-
const licenseValue = getAnswer(answers, ["LICENSE", "license"]);
94+
const licenseValue = findLicenseValue(answers);
8995
if (typeof licenseValue !== 'string' || licenseValue.trim() === '') {
9096
return;
9197
}
@@ -99,19 +105,10 @@ async function ensureLicenseFile(
99105
}
100106

101107
const author =
102-
getAnswer(answers, [
103-
"USERFULLNAME",
104-
"AUTHOR",
105-
"AUTHORFULLNAME",
106-
"USERNAME",
107-
"fullName",
108-
"author",
109-
"authorFullName",
110-
"userName",
111-
]) ?? "Unknown Author";
108+
findLicenseAuthor(answers) ?? "Unknown Author";
112109

113110
const email =
114-
getAnswer(answers, ["USEREMAIL", "EMAIL", "email", "userEmail"]) ?? "";
111+
findLicenseEmail(answers) ?? "";
115112

116113
const content = renderLicense(selectedLicense, {
117114
author: String(author),
@@ -130,19 +127,6 @@ async function ensureLicenseFile(
130127
);
131128
}
132129

133-
function getAnswer(
134-
answers: Record<string, any>,
135-
keys: string[]
136-
): string | undefined {
137-
for (const key of keys) {
138-
const value = answers?.[key];
139-
if (typeof value === "string" && value.trim() !== "") {
140-
return value;
141-
}
142-
}
143-
return undefined;
144-
}
145-
146130
/**
147131
* Replace variables in a file using streams
148132
* @param sourcePath - Source file path

0 commit comments

Comments
 (0)