-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathconfig.ts
98 lines (87 loc) · 3.19 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* globals process */
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import slugify from 'slugify';
interface SiteOptions {
/** The site's default page description */
description?: string;
/** URL to the site's favicon */
favicon?: string;
/** URL to the demo page's main brand logo */
logoUrl?: string;
/** URLs to stylesheets to add to the demo (absolute from cwd) */
stylesheets?: string[];
/** Title for main page of the demo */
title?: string;
/** Site subpath for components. default: 'components' i.e. 'https://patternflyelements.org/components' */
componentSubpath?: string;
}
export interface PfeConfig {
/** rootDir of the package. Default process.cwd() */
rootDir?: string;
/** object mapping custom element name to page title */
aliases?: Record<string, string> ;
/** Directory containing the custom elements, defaults to `elements` */
elementsDir?: string;
/** absolute URL to the web page representing the repo root in source control, with trailing slash. default 'https://github.com/patternfly/patternfly-elements/tree/main/' */
sourceControlURLPrefix?: string ;
/** absolute URL prefix for demos, with trailing slash. Default 'https://patternflyelements.org/' */
demoURLPrefix?: string ;
/** custom elements namespace. Default 'pf' */
tagPrefix?: string;
/** Dev Server site options */
site?: SiteOptions;
}
const SITE_DEFAULTS: Required<SiteOptions> = {
description: `PatternFly Elements: A set of community-created web components based on PatternFly design.`,
favicon: '/docs/images/logo/pf-logo-small.svg',
logoUrl: '/docs/images/logo/pf-logo-small.svg',
stylesheets: [],
title: 'PatternFly Elements',
componentSubpath: 'components',
};
const DEFAULT_CONFIG: PfeConfig = {
demoURLPrefix: 'https://patternflyelements.org/',
sourceControlURLPrefix: 'https://github.com/patternfly/patternfly-elements/tree/main/',
elementsDir: 'elements',
tagPrefix: 'pf',
aliases: {},
};
function tryJson(path: string) {
try {
return JSON.parse(readFileSync(path, 'utf8'));
} catch {
return {};
}
}
export function getPfeConfig(rootDir = process.cwd()): Required<PfeConfig> {
const jsonConfig = tryJson(join(rootDir, '.pfe.config.json'));
return {
...DEFAULT_CONFIG,
rootDir,
...jsonConfig,
site: {
...SITE_DEFAULTS,
...jsonConfig.site ?? {},
},
};
}
const slugsConfigMap = new Map<string, { config: PfeConfig; slugs: Map<string, string> }>();
const reverseSlugifyObject = ([k, v]: [string, string]): [string, string] =>
[slugify(v, { lower: true }), k];
function getSlugsMap(rootDir: string) {
if (!slugsConfigMap.get(rootDir)) {
const config = getPfeConfig(rootDir);
const slugs = new Map(Object.entries(config.aliases).map(reverseSlugifyObject));
slugsConfigMap.set(rootDir, { slugs, config });
}
return slugsConfigMap.get(rootDir)!;
}
/**
* Returns the prefixed custom element name for a given slug
*/
export function deslugify(slug: string, rootDir = process.cwd()): string {
const { slugs, config } = getSlugsMap(rootDir);
const prefixedSlug = (slug.startsWith(`${config.tagPrefix}-`)) ? slug : `${config.tagPrefix}-${slug}`;
return slugs.get(slug) ?? prefixedSlug;
}