forked from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateRerunSuggestion.ts
67 lines (60 loc) · 1.72 KB
/
createRerunSuggestion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { allArgOptions } from "../shared/options/args.js";
import {
ExclusionKey,
getExclusions,
} from "../shared/options/augmentOptionsWithExcludes.js";
import { Options } from "../shared/types.js";
function getFirstMatchingArg(key: string) {
return Object.keys(allArgOptions).find(
(arg) => arg.replaceAll("-", "") === key.toLowerCase(),
);
}
export function createRerunSuggestion(options: Partial<Options>): string {
const optionsNormalized = {
...options,
email: undefined,
...(options.email && {
emailGitHub: options.email.github,
emailNpm: options.email.npm,
}),
...(options.guide
? {
guide: options.guide.href,
guideTitle: options.guide.title,
}
: { guide: undefined }),
...(options.logo
? {
logo: options.logo.src,
logoAlt: options.logo.alt,
}
: { logo: undefined }),
};
const args = Object.entries(optionsNormalized)
// Sort so the base is first, then the rest are sorted alphabetically
.sort(([a], [b]) =>
a === "base" ? -1 : b === "base" ? 1 : a.localeCompare(b),
)
// Filter out all entries that have a key in the excluded object or have a falsy value
.filter(
([key, value]) =>
getExclusions(options, optionsNormalized.base)[key as ExclusionKey] ==
undefined && !!value,
)
.map(([key, value]) => {
return `--${getFirstMatchingArg(key)} ${stringifyValue(value)}`;
})
.join(" ");
return `npx create-typescript-app --mode ${options.mode} ${args}`;
}
function stringifyValue(
value: boolean | string | string[] | undefined,
): string {
if (Array.isArray(value)) {
return stringifyValue(value.join(" "));
}
const valueStringified = `${value}`;
return valueStringified.includes(" ")
? `"${valueStringified}"`
: valueStringified;
}