From 211b5ae702c141e24e17fdc861872901965cacff Mon Sep 17 00:00:00 2001 From: Ava Gaiety W Date: Tue, 23 Sep 2025 22:03:20 -0600 Subject: [PATCH] handlebars for yaml and toml gen --- bun.lockb | Bin 7401 -> 7371 bytes dist/palette.toml | 28 ++++++------ dist/palette.yml | 6 +-- index.ts | 4 +- package.json | 2 +- src/build/templates/toml.hbs | 21 +++++++++ src/build/templates/yaml.hbs | 14 ++++++ src/build/toml.ts | 84 ++++++++++++----------------------- src/build/yaml.ts | 72 ++++++++++-------------------- src/helpers/color.ts | 3 ++ src/helpers/file.ts | 35 ++++++++++----- src/palette.toml | 13 ------ 12 files changed, 133 insertions(+), 149 deletions(-) create mode 100644 src/build/templates/toml.hbs create mode 100644 src/build/templates/yaml.hbs delete mode 100644 src/palette.toml diff --git a/bun.lockb b/bun.lockb index e26b0b3321d71d7f22fc75accc009ad6caaa8064..0afce470f2241c791862826b8243c29134323abb 100755 GIT binary patch delta 2103 zcma)7X;4#F6n-xWOMrkZk_2!Rg$e^N1dtsGj&(rLB8ZJ0!J5Q~AS5vfvRDVO)_%~ zcuU*6?#D#paUPt8vcqZ*>CQouu|e@@ii$%76EUaFB?zvGqGX6fBSJ$bCrVI+9$_Ge zEmZJ=2rs`0qp=v}0L_bXeZ`KzKDZ;6S!~G&e-6ZQ6F4h4P|8i9k3kDpMn8j5u8iZw zg;HJuodqpCnN2-r5eMgZar7&Qv61luFkd{IaxT!ED0(@x*vROEFlrM89W>9CEQC~2 zh_wO6P6+XK_Mg-M$hQkNFZ6%+hW&O0@Ki7t7THfOJJ-kmALaPvE{KQwMvm)l0eA2t8x;*QYN zxXU8HlKHp&}Io9l;O%~`T_S)@2!pAe7$gai-dd6&T z4R>F?_~YaHf#+uiYVYaaznT|QC+|9+k#jyPIHlt3)%Qgwz&9=YGgJR3+1aqEq2*;n zdmSFEt8MbF5{0KV`x)wl=?SsIFkX-Hq|X>el9+wR?_k zd9kG3Ge#9ATwDAaj`Kxj3e#;@$*QG(j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= "verdigris" version = "0.0.1" license = "GPL-3.0-only" sourcecode = "https://git.basking.monster/gaiety/verdigris" - + [author] name = "Gaiety" email = "ava+verdigris@gaiety.me" url = "https://gaiety.me" - + [colors] - + [colors.pit] name = "Pit" l = 10 c = 3.5 h = 225 hex = "#00040B" - + [colors.depths] name = "Depths" l = 30 c = 3.5 h = 225 hex = "#19323B" - + [colors.stope] name = "Stope" l = 50 c = 3.5 h = 225 hex = "#4E6872" - + [colors.text] name = "Text" l = 90 c = 3.5 h = 225 hex = "#C6E4F0" - + [colors.orange] name = "Orange" l = 80 c = 14 h = 45 hex = "#FF9F6F" - + [colors.teal] name = "Teal" l = 80 c = 14 h = 180 hex = "#23DBC1" - + [colors.red] name = "Red" l = 80 c = 7 h = 22.5 hex = "#E8ADA9" - + [colors.yellow] name = "Yellow" l = 80 c = 7 h = 56.25 hex = "#E1B392" - + [colors.green] name = "Green" l = 80 c = 7 h = 157.5 hex = "#98CCAC" - + [colors.blue] name = "Blue" l = 80 c = 7 h = 225 hex = "#8CC8E0" - + [colors.purple] name = "Purple" l = 80 c = 7 h = 292.5 hex = "#BEB6E8" - \ No newline at end of file + diff --git a/dist/palette.yml b/dist/palette.yml index 706cc9c..6bf9a46 100644 --- a/dist/palette.yml +++ b/dist/palette.yml @@ -2,12 +2,12 @@ name: "verdigris" version: "0.0.1" license: "GPL-3.0-only" sourcecode: "https://git.basking.monster/gaiety/verdigris" - + author: name: "Gaiety" email: "ava+verdigris@gaiety.me" url: "https://gaiety.me" - + common: pit: "#00040B" depths: "#19323B" @@ -19,4 +19,4 @@ common: yellow: "#E1B392" green: "#98CCAC" blue: "#8CC8E0" - purple: "#BEB6E8" \ No newline at end of file + purple: "#BEB6E8" diff --git a/index.ts b/index.ts index 4454cc1..6d808c4 100644 --- a/index.ts +++ b/index.ts @@ -46,8 +46,8 @@ program log(options.verbose, await buildYAML(), timer) if (options.verbose) console.info('Building Ports...') - log(options.verbose, await buildKitty(), timer) - log(options.verbose, await buildNeovim(), timer) + // log(options.verbose, await buildKitty(), timer) + // log(options.verbose, await buildNeovim(), timer) }); program.parse(); diff --git a/package.json b/package.json index 541c39e..8241174 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "chalk": "^5.6.2", "color-convert": "^3.1.2", "commander": "^14.0.1", - "estilo": "^2.1.3", + "handlebars": "^4.7.8" }, "peerDependencies": { "typescript": "^5.0.0" diff --git a/src/build/templates/toml.hbs b/src/build/templates/toml.hbs new file mode 100644 index 0000000..d1132e2 --- /dev/null +++ b/src/build/templates/toml.hbs @@ -0,0 +1,21 @@ +name = "{{name}}" +version = "{{version}}" +license = "{{license}}" +sourcecode = "{{url}}" + +[author] +name = "{{author.name}}" +email = "{{author.email}}" +url = "{{author.url}}" + +[colors] + +{{#each colors}} +[colors.{{this.nameSnakeCase}}] +name = "{{this.name}}" +l = {{this.l}} +c = {{this.c}} +h = {{this.h}} +hex = "{{this.hex}}" + +{{/each}} diff --git a/src/build/templates/yaml.hbs b/src/build/templates/yaml.hbs new file mode 100644 index 0000000..b1506ba --- /dev/null +++ b/src/build/templates/yaml.hbs @@ -0,0 +1,14 @@ +name: "{{name}}" +version: "{{version}}" +license: "{{license}}" +sourcecode: "{{url}}" + +author: + name: "{{author.name}}" + email: "{{author.email}}" + url: "{{author.url}}" + +common: +{{#each colors}} + {{this.nameSnakeCase}}: "{{this.hex}}" +{{/each}} diff --git a/src/build/toml.ts b/src/build/toml.ts index 2a407ae..0103727 100644 --- a/src/build/toml.ts +++ b/src/build/toml.ts @@ -1,70 +1,42 @@ import { name, version, license, url, author } from '../../package.json' import appRoot from 'app-root-path' -import { rmFile, mkFilePath, appendToFile } from "../helpers/file" +import { renderToFile } from "../helpers/file" import colors from '../palette' -import type color from '../helpers/color' -import { toSnakeCase } from '../helpers/string' +import Color from '../helpers/color' -const file = appRoot + '/dist/palette.toml' -const appendToTOML = async (content: string) => await appendToFile(file, content) +const templateFile = appRoot + '/src/build/templates/toml.hbs' +const outputFile = appRoot + '/dist/palette.toml' export default async function(isVerbose = false): Promise { - if (isVerbose) console.info('Removing previous build', file) try { - await rmFile(file) + await renderToFile(templateFile, outputFile, getData(), isVerbose) + + return outputFile } catch (error) { - console.info('TOML: Previous build already removed') + throw error } - if (isVerbose) console.info('Ensuring path and file exist', file) - await mkFilePath(file) - if (isVerbose) console.info('Writing header info to file', file) - await appendToTOML(headerTOML()) - if (isVerbose) console.info('Writing author info to file', file) - await appendToTOML(authorTOML()) - if (isVerbose) console.info('Writing color palette to file', file) - await appendToTOML(colorHeaderTOML()) - for (let index = 0; index < colors.length; index++) { - const color = colors[index] - if (isVerbose) console.info(`${color.name}…`, file) - await appendToTOML(colorTOML(color)) +} + +function getData(): any { + return { + name, + version, + license, + url, + author, + colors: colors.map(getDataColors) } - - return file } -function headerTOML(): string { - return `name = "${name}" -version = "${version}" -license = "${license}" -sourcecode = "${url}" - ` -} +function getDataColors(color: Color): any { + const { name, nameSnakeCase, l, c, h } = color -function authorTOML(): string { - const { name, email, url } = author - - return ` -[author] -name = "${name}" -email = "${email}" -url = "${url}" - ` -} - -function colorHeaderTOML(): string { - return ` -[colors] - ` -} - -function colorTOML(color: color): string { - const { name, l, c, h } = color - return ` -[colors.${toSnakeCase(name)}] -name = "${name}" -l = ${l} -c = ${c} -h = ${h} -hex = "${color.toHex()}" - ` + return { + name, + nameSnakeCase, + l, + c, + h, + hex: color.toHex() + } } diff --git a/src/build/yaml.ts b/src/build/yaml.ts index be9f441..6a24b2f 100644 --- a/src/build/yaml.ts +++ b/src/build/yaml.ts @@ -1,62 +1,38 @@ import { name, version, license, url, author } from '../../package.json' import appRoot from 'app-root-path' -import { rmFile, mkFilePath, appendToFile } from "../helpers/file" -import { toSnakeCase } from '../helpers/string' import colors from '../palette' -import type color from '../helpers/color' +import Color from '../helpers/color' +import { renderToFile } from '../helpers/file' -const file = appRoot + '/dist/palette.yml' -const appendToYAML = async (content: string) => await appendToFile(file, content) +const templateFile = appRoot + '/src/build/templates/yaml.hbs' +const outputFile = appRoot + '/dist/palette.yml' export default async function(isVerbose = false): Promise { - if (isVerbose) console.info('Removing previous build', file) try { - await rmFile(file) + await renderToFile(templateFile, outputFile, getData(), isVerbose) + + return outputFile } catch (error) { - console.info('YAML: Previous build already removed') + throw error } - if (isVerbose) console.info('Ensuring path and file exist', file) - await mkFilePath(file) - if (isVerbose) console.info('Writing header info to file', file) - await appendToYAML(headerYAML()) - if (isVerbose) console.info('Writing author info to file', file) - await appendToYAML(authorYAML()) - if (isVerbose) console.info('Writing color palette to file', file) - await appendToYAML(colorHeaderYAML()) - for (let index = 0; index < colors.length; index++) { - const color = colors[index] - if (isVerbose) console.info(`${color.name}…`, file) - await appendToYAML(colorYAML(color)) +} + +function getData(): any { + return { + name, + version, + license, + url, + author, + colors: colors.map(getDataColors) } - - return file } -function headerYAML(): string { - return `name: "${name}" -version: "${version}" -license: "${license}" -sourcecode: "${url}" - ` -} +function getDataColors(color: Color): any { + const { nameSnakeCase } = color -function authorYAML(): string { - const { name, email, url } = author - - return ` -author: - name: "${name}" - email: "${email}" - url: "${url}" - ` -} - -function colorHeaderYAML(): string { - return ` -common:` -} - -function colorYAML(color: color): string { - return ` - ${toSnakeCase(color.name)}: "${color.toHex()}"` + return { + nameSnakeCase, + hex: color.toHex() + } } diff --git a/src/helpers/color.ts b/src/helpers/color.ts index 3dacea5..5b87090 100644 --- a/src/helpers/color.ts +++ b/src/helpers/color.ts @@ -1,4 +1,5 @@ import convert from 'color-convert' +import { toSnakeCase } from './string' export interface OklchInterface { l: number, @@ -31,6 +32,8 @@ export default class { return true } + get nameSnakeCase() { return toSnakeCase(this.#name) } + get l() { return this.#l } set l(value: number) { if (this._isLValid(value)) this.#l = value diff --git a/src/helpers/file.ts b/src/helpers/file.ts index b30f0cd..392f1ec 100644 --- a/src/helpers/file.ts +++ b/src/helpers/file.ts @@ -1,25 +1,36 @@ -import { appendFile, mkdir, rm } from 'node:fs/promises'; +import { compile } from 'handlebars'; +import { appendFile, mkdir, rm, readFile, writeFile } from 'node:fs/promises'; import { dirname } from 'node:path' -export async function mkFilePath(file: string) { +const log = (isVerbose = true, ...rest: any[]) => { + if (isVerbose) console.info(...rest) +} + +export async function mkFilePath(file: string, isVerbose = false) { try { + log(isVerbose, 'Ensuring path and file exist', file) await mkdir(dirname(file), { recursive: true }) } catch (error) { throw error } } -export async function rmFile(file: string) { +export async function renderToFile( + templateFile: string, outputFile: string, data: any, isVerbose = false +): Promise { try { - await rm(file) - } catch (error) { - throw error - } -} - -export async function appendToFile(file: string, content: string) { - try { - await appendFile(file, content) + await mkFilePath(outputFile, isVerbose) + + log(isVerbose, 'Reading in template', templateFile) + const template = (await readFile(templateFile)).toString() + + log(isVerbose, 'Compiling template', templateFile, 'with data', data) + const result = compile(template)(data) + + log(isVerbose, 'Saving result', outputFile) + await writeFile(outputFile, result) + + return result } catch (error) { throw error } diff --git a/src/palette.toml b/src/palette.toml deleted file mode 100644 index 37963db..0000000 --- a/src/palette.toml +++ /dev/null @@ -1,13 +0,0 @@ -name = "verdigris" -version = "0.0.1" - -[author] -name = "Gaiety" -email = "ava+verdigris@gaiety.me" - -[colors] - -[colors.bg] -l = 0.2 -c = 0.02 -h =