forked from rob-balfre/svelte-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_theming_variables_md.js
53 lines (48 loc) · 1.59 KB
/
generate_theming_variables_md.js
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
const fs = require("fs");
const path = require("path");
const { find } = require("find-in-files");
const VARIABLE_USAGE_PATTERN = "var\\(--([A-Za-z\\-_]*)";
const SOURCE_FOLDER = path.join(__dirname, "..", "src");
const DOC_FILE_PATH = path.join(__dirname, "theming_variables.md");
const VARIABLE_SECTION_PATTERN = /(<!-- List start -->)(.|\n)*(<!-- List end -->)/gm;
(async () => {
const searchResults = await find(
VARIABLE_USAGE_PATTERN,
SOURCE_FOLDER,
".svelte$"
);
const justTheMatchedParts = Object.keys(searchResults).reduce(
(accumulator, nextKey) => [
...accumulator,
...searchResults[nextKey].matches
],
[]
);
const uniqueMatches = [...new Set(justTheMatchedParts).values()];
uniqueMatches.sort();
const matchesAsMarkdownListItems = uniqueMatches.map(b =>
b.replace(/var\((--[A-Za-z\-_]*)/, "- `$1`")
);
const START_TAG_CAPTURE_GROUP = "$1";
const END_TAG_CAPTURE_GROUP = "$3";
const newDependencySectionAsRegexReplaceExpression = [
START_TAG_CAPTURE_GROUP,
...matchesAsMarkdownListItems,
END_TAG_CAPTURE_GROUP
].join("\n");
const oldContent = fs.readFileSync(DOC_FILE_PATH).toString();
const oldFileDoesNotContainSection =
oldContent.search(VARIABLE_SECTION_PATTERN) === -1;
if (oldFileDoesNotContainSection) {
console.error(`Could not find variable section in ${DOC_FILE_PATH}`);
process.exit(1);
}
fs.writeFileSync(
DOC_FILE_PATH,
oldContent.replace(
VARIABLE_SECTION_PATTERN,
newDependencySectionAsRegexReplaceExpression
)
);
console.log(`Successfully wrote to ${DOC_FILE_PATH}`);
})();