Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
81 lines (70 loc) · 2.29 KB
/
Copy pathutils.js
File metadata and controls
81 lines (70 loc) · 2.29 KB
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
const path = require("path");
const fg = require("fast-glob");
const cwd = require("process").cwd;
const componentsPrefix = "ecc-utils-design-";
const npmDir = path.join("./dist");
const getAllComponents = (metadata) => {
const allComponents = [];
metadata.modules.forEach((module) => {
module.declarations?.forEach((declaration) => {
if (declaration.customElement) {
const component = declaration;
const { path } = module;
if (component && component.tagName) {
allComponents.push(Object.assign(component, { path }));
}
}
});
});
return allComponents;
};
// do not convert to arrow function
// because this is taking advantage of function hoisting when it is added to the CEM config file
function pascalCase(str) {
if (!str || typeof str !== "string") return "";
return str
.match(/[a-z]+/gi)
.map(function (word) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join("");
}
function normalizePath(filename) {
return filename.split(path.win32.sep).join(path.posix.sep);
}
// Escapes text for a template literal, so that it evaluates to the same text.
// Backslashes go first: escaping them last would also escape the backslashes
// that the other replacements add
function escapeTemplateLiteral(text) {
return text
.replaceAll("\\", "\\\\")
.replaceAll("`", "\\`")
.replaceAll("${", "\\${");
}
// The entry points that tsup builds from a package's source directory: the
// root, every component, provider and React wrapper, and the utilities
// (`utils/index.ts`, if any), which a package exports as a subpath of its
// own, so that apps can import them without registering the components
function getBuildEntries(sourceDir) {
// fast-glob takes forward slashes only
const dir = normalizePath(sourceDir);
return [
`${dir}/index.ts`,
...fg.sync(`${dir}/components/**/!(*.(test)).ts`, {
ignore: ["**/tests/**"],
}),
...fg.sync(`${dir}/providers/**/*.ts`, { ignore: ["**/tests/**"] }),
...fg.sync(`${dir}/react/**/*.ts`),
...fg.sync(`${dir}/utils/index.ts`),
];
}
module.exports = {
componentsPrefix,
getBuildEntries,
npmDir,
getAllComponents,
pascalCase,
reactDir: path.join(cwd(), ".", "src", "react"),
normalizePath,
escapeTemplateLiteral,
};