-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce CDN class to centralize client package url's
Use package.json to determine the package version.
- Loading branch information
Showing
5 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/nl/avisi/structurizr/site/generatr/site/views/CDN.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package nl.avisi.structurizr.site.generatr.site.views | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.jsonObject | ||
import kotlinx.serialization.json.jsonPrimitive | ||
import java.lang.IllegalStateException | ||
|
||
class CDN { | ||
companion object { | ||
private const val CDN_BASE = "https://cdn.jsdelivr.net/npm/" | ||
private val json = Json { ignoreUnknownKeys = true } | ||
|
||
private val packageJson = object {}.javaClass.getResource("/package.json")?.readText() | ||
?: throw IllegalStateException("package.json not found") | ||
|
||
private val dependencies = json.parseToJsonElement(packageJson).jsonObject["dependencies"] | ||
?.jsonObject | ||
?.map { Dependency(it.key, it.value.jsonPrimitive.content) } | ||
?: throw IllegalStateException("dependencies element not found in package.json") | ||
|
||
fun bulmaCss() = dependencies.single { it.name == "bulma" }.let { | ||
"${it.baseUrl()}/css/bulma.min.css" | ||
} | ||
|
||
fun katexJs() = dependencies.single { it.name == "katex" }.let { | ||
"${it.baseUrl()}/dist/katex.min.js" | ||
} | ||
|
||
fun katexCss() = dependencies.single { it.name == "katex" }.let { | ||
"${it.baseUrl()}/dist/katex.min.css" | ||
} | ||
|
||
fun lunrJs() = dependencies.single { it.name == "lunr" }.let { | ||
"${it.baseUrl()}/lunr.min.js" | ||
} | ||
|
||
fun lunrLanguagesStemmerJs() = dependencies.single { it.name == "lunr-languages" }.let { | ||
"${it.baseUrl()}@${it.version}/min/lunr.stemmer.support.min.js" | ||
} | ||
|
||
fun lunrLanguagesJs(language: String) = dependencies.single { it.name == "lunr-languages" }.let { | ||
"${it.baseUrl()}/min/lunr.$language.min.js" | ||
} | ||
|
||
fun mermaidJs() = dependencies.single { it.name == "mermaid" }.let { | ||
"${it.baseUrl()}/dist/mermaid.esm.min.mjs" | ||
} | ||
|
||
fun webfontloaderJs() = dependencies.single { it.name == "webfontloader" }.let { | ||
"${it.baseUrl()}/webfontloader.js" | ||
} | ||
} | ||
|
||
@Serializable | ||
data class Dependency(val name: String, val version: String) { | ||
fun baseUrl() = "$CDN_BASE$name@$version" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/test/kotlin/nl/avisi/structurizr/site/generatr/site/views/CDNTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nl.avisi.structurizr.site.generatr.site.views | ||
|
||
import assertk.all | ||
import assertk.assertThat | ||
import assertk.assertions.doesNotContain | ||
import assertk.assertions.endsWith | ||
import assertk.assertions.startsWith | ||
import org.junit.jupiter.api.DynamicTest | ||
import org.junit.jupiter.api.TestFactory | ||
|
||
class CDNTest { | ||
@TestFactory | ||
fun `cdn locations`() = listOf( | ||
CDN.bulmaCss() to "/css/bulma.min.css", | ||
CDN.katexJs() to "/dist/katex.min.js", | ||
CDN.katexCss() to "/dist/katex.min.css", | ||
CDN.lunrJs() to "/lunr.min.js", | ||
CDN.lunrLanguagesStemmerJs() to "/min/lunr.stemmer.support.min.js", | ||
CDN.lunrLanguagesJs("en") to "/min/lunr.en.min.js", | ||
CDN.mermaidJs() to "/dist/mermaid.esm.min.mjs", | ||
CDN.webfontloaderJs() to "/webfontloader.js" | ||
).map { (url, suffix) -> | ||
DynamicTest.dynamicTest(url) { | ||
assertThat(url).all { | ||
startsWith("https://") | ||
endsWith(suffix) | ||
doesNotContain("~", "^", ">", "<", "=", ":=") | ||
} | ||
} | ||
} | ||
} |