-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.js
109 lines (94 loc) · 3.36 KB
/
builder.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
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
99
100
101
102
103
104
105
106
107
108
109
const fs = require("fs");
const path = require("path");
const REGEX_PLACEHOLDER = /bssnippet-\d{1,2}-(?:default-)?\w*/g;
const REGEX_NAMED_PLACEHOLDER =
/bssnippet-(?<index>\d{1,2})-(?<type>\w*)-?(?<value>\w*)/;
const VARIANTS = "primary,secondary,success,danger,warning,info,light,dark";
const SIZES = "lg,md,sm,xs";
const COLUMNS = "1,2,3,4,5,6,7,8,9,10,11,12";
const DIRECTIONS = "center,end,start";
const DROP_DIRECTIONS_V4 = "down,up,left,right";
const DROP_DIRECTIONS_V5 = "down-center,up,up-center,end,start";
function buildPlaceholderOptions(placeholder, options) {
return `\${${placeholder.index}|${options}|}`;
}
function buildPlaceholderDefault(placeholder) {
return `\${${placeholder.index}:${placeholder.value.replace(/_/g, "-")}}`;
}
function buildPlaceholderContent(fileLocation, placeholder) {
switch (placeholder.type) {
case "variants":
return buildPlaceholderOptions(placeholder, VARIANTS);
case "sizes":
return buildPlaceholderOptions(placeholder, SIZES);
case "columns":
return buildPlaceholderOptions(placeholder, COLUMNS);
case "directions":
return buildPlaceholderOptions(placeholder, DIRECTIONS);
case "drop_directions_v4":
return buildPlaceholderOptions(placeholder, DROP_DIRECTIONS_V4);
case "drop_directions_v5":
return buildPlaceholderOptions(placeholder, DROP_DIRECTIONS_V5);
case "default":
return buildPlaceholderDefault(placeholder);
default:
throw new Error(
`Unknown placeholder type: '${placeholder.type}' in file: '${fileLocation}'`
);
}
}
function findPlaceholderDeclaration(fileContent) {
const placeholdersMatches = fileContent.match(REGEX_PLACEHOLDER) || [];
return placeholdersMatches.map((placeholder) => {
const groups = placeholder.match(REGEX_NAMED_PLACEHOLDER).groups;
return { name: placeholder, ...groups };
});
}
function replacePlaceholders(fileLocation) {
let fileContent = fs.readFileSync(fileLocation, "utf8");
const placeholders = findPlaceholderDeclaration(fileContent);
placeholders.forEach((placeholder) => {
const placeholderContent = buildPlaceholderContent(
fileLocation,
placeholder
);
fileContent = fileContent.replace(
new RegExp(placeholder.name, "g"),
placeholderContent
);
});
return fileContent;
}
function buildSnippet(bsVersion, fileLocation) {
const fileContent = replacePlaceholders(fileLocation);
const fileName = path.basename(fileLocation, ".haml");
const html5 = fileName === "html5";
const name = html5
? "HTML5 Bootstrap"
: fileName.replace(/-/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());
const prefix = html5 ? "!bs" : "bs";
return [
name,
{
prefix: `${prefix}${bsVersion}-${fileName}`,
body: fileContent.split("\n"),
},
];
}
const BS_VERSIONS = ["4", "5"];
const bootstrapDir = path.join(__dirname, "bootstrap");
BS_VERSIONS.forEach((bsVersion) => {
const files = fs
.readdirSync(path.join(bootstrapDir, bsVersion))
.filter((file) => file.endsWith(".haml"));
const snippets = {};
files.forEach((file) => {
const fileLocation = path.join(bootstrapDir, bsVersion, file);
const [name, snippet] = buildSnippet(bsVersion, fileLocation);
snippets[name] = snippet;
});
fs.writeFileSync(
path.join(__dirname, `snippets/bootstrap${bsVersion}.code-snippets`),
JSON.stringify(snippets, null, 2)
);
});