Isolated builds of dependencies

This commit is contained in:
Stéphan Kochen
2021-04-30 11:39:05 +02:00
parent 0f378786d6
commit 153254f62a
14 changed files with 578 additions and 133 deletions
+67
View File
@@ -0,0 +1,67 @@
export const json = JSON.stringify;
/**
* Capitalize the first character.
*/
export const ucfirst = (value: string) =>
value.slice(0, 1).toUpperCase() + value.slice(1);
/**
* Convert to a camel-case starting with uppercase.
*
* Example: `one two three` => `OneTwoThree`
*/
export const upperCamelize = (name: string) =>
name
.split(/[^a-zA-Z0-9]+/g)
.filter((x) => x)
.map((v) => ucfirst(v))
.join("");
/**
* Add a prefix to every line in some text.
*/
export const indent = (
prefix: string,
text: string,
includeEmptyLines = false
): string =>
text
.split("\n")
.map((line: string) => (line || includeEmptyLines ? prefix + line : line))
.join("\n");
/**
* Basic templating rendering.
*
* String values in `vars` will be used for simple substitution
* of `@@KEY@@`, while boolean values will be used for
* conditional sections of code between `#@@ IF KEY` and `#@@
* ENDIF KEY`.
*/
export const renderTmpl = (
tmpl: string,
vars: { [name: string]: string | boolean }
): string => {
let result = tmpl;
for (const [name, value] of Object.entries(vars)) {
if (typeof value === "string") {
result = result.replace(`@@${name}@@`, value);
}
if (typeof value === "boolean") {
const lines = result.split("\n");
const startIdx = lines.indexOf(`#@@ IF ${name}`);
const endIdx = lines.indexOf(`#@@ ENDIF ${name}`);
if (startIdx !== -1 && endIdx > startIdx) {
if (value) {
lines.splice(endIdx, 1);
lines.splice(startIdx, 1);
} else {
lines.splice(startIdx, endIdx - startIdx + 1);
}
result = lines.join("\n");
}
}
}
return result;
};