handlebars for yaml and toml gen

This commit is contained in:
Ava Gaiety W 2025-09-23 22:03:20 -06:00
parent 3fb0a2360a
commit 211b5ae702
12 changed files with 133 additions and 149 deletions

BIN
bun.lockb

Binary file not shown.

28
dist/palette.toml vendored
View file

@ -2,88 +2,88 @@ name = "verdigris"
version = "0.0.1" version = "0.0.1"
license = "GPL-3.0-only" license = "GPL-3.0-only"
sourcecode = "https://git.basking.monster/gaiety/verdigris" sourcecode = "https://git.basking.monster/gaiety/verdigris"
[author] [author]
name = "Gaiety" name = "Gaiety"
email = "ava+verdigris@gaiety.me" email = "ava+verdigris@gaiety.me"
url = "https://gaiety.me" url = "https://gaiety.me"
[colors] [colors]
[colors.pit] [colors.pit]
name = "Pit" name = "Pit"
l = 10 l = 10
c = 3.5 c = 3.5
h = 225 h = 225
hex = "#00040B" hex = "#00040B"
[colors.depths] [colors.depths]
name = "Depths" name = "Depths"
l = 30 l = 30
c = 3.5 c = 3.5
h = 225 h = 225
hex = "#19323B" hex = "#19323B"
[colors.stope] [colors.stope]
name = "Stope" name = "Stope"
l = 50 l = 50
c = 3.5 c = 3.5
h = 225 h = 225
hex = "#4E6872" hex = "#4E6872"
[colors.text] [colors.text]
name = "Text" name = "Text"
l = 90 l = 90
c = 3.5 c = 3.5
h = 225 h = 225
hex = "#C6E4F0" hex = "#C6E4F0"
[colors.orange] [colors.orange]
name = "Orange" name = "Orange"
l = 80 l = 80
c = 14 c = 14
h = 45 h = 45
hex = "#FF9F6F" hex = "#FF9F6F"
[colors.teal] [colors.teal]
name = "Teal" name = "Teal"
l = 80 l = 80
c = 14 c = 14
h = 180 h = 180
hex = "#23DBC1" hex = "#23DBC1"
[colors.red] [colors.red]
name = "Red" name = "Red"
l = 80 l = 80
c = 7 c = 7
h = 22.5 h = 22.5
hex = "#E8ADA9" hex = "#E8ADA9"
[colors.yellow] [colors.yellow]
name = "Yellow" name = "Yellow"
l = 80 l = 80
c = 7 c = 7
h = 56.25 h = 56.25
hex = "#E1B392" hex = "#E1B392"
[colors.green] [colors.green]
name = "Green" name = "Green"
l = 80 l = 80
c = 7 c = 7
h = 157.5 h = 157.5
hex = "#98CCAC" hex = "#98CCAC"
[colors.blue] [colors.blue]
name = "Blue" name = "Blue"
l = 80 l = 80
c = 7 c = 7
h = 225 h = 225
hex = "#8CC8E0" hex = "#8CC8E0"
[colors.purple] [colors.purple]
name = "Purple" name = "Purple"
l = 80 l = 80
c = 7 c = 7
h = 292.5 h = 292.5
hex = "#BEB6E8" hex = "#BEB6E8"

6
dist/palette.yml vendored
View file

@ -2,12 +2,12 @@ name: "verdigris"
version: "0.0.1" version: "0.0.1"
license: "GPL-3.0-only" license: "GPL-3.0-only"
sourcecode: "https://git.basking.monster/gaiety/verdigris" sourcecode: "https://git.basking.monster/gaiety/verdigris"
author: author:
name: "Gaiety" name: "Gaiety"
email: "ava+verdigris@gaiety.me" email: "ava+verdigris@gaiety.me"
url: "https://gaiety.me" url: "https://gaiety.me"
common: common:
pit: "#00040B" pit: "#00040B"
depths: "#19323B" depths: "#19323B"
@ -19,4 +19,4 @@ common:
yellow: "#E1B392" yellow: "#E1B392"
green: "#98CCAC" green: "#98CCAC"
blue: "#8CC8E0" blue: "#8CC8E0"
purple: "#BEB6E8" purple: "#BEB6E8"

View file

@ -46,8 +46,8 @@ program
log(options.verbose, await buildYAML(), timer) log(options.verbose, await buildYAML(), timer)
if (options.verbose) console.info('Building Ports...') if (options.verbose) console.info('Building Ports...')
log(options.verbose, await buildKitty(), timer) // log(options.verbose, await buildKitty(), timer)
log(options.verbose, await buildNeovim(), timer) // log(options.verbose, await buildNeovim(), timer)
}); });
program.parse(); program.parse();

View file

@ -16,7 +16,7 @@
"chalk": "^5.6.2", "chalk": "^5.6.2",
"color-convert": "^3.1.2", "color-convert": "^3.1.2",
"commander": "^14.0.1", "commander": "^14.0.1",
"estilo": "^2.1.3", "handlebars": "^4.7.8"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.0.0" "typescript": "^5.0.0"

View file

@ -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}}

View file

@ -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}}

View file

@ -1,70 +1,42 @@
import { name, version, license, url, author } from '../../package.json' import { name, version, license, url, author } from '../../package.json'
import appRoot from 'app-root-path' import appRoot from 'app-root-path'
import { rmFile, mkFilePath, appendToFile } from "../helpers/file" import { renderToFile } from "../helpers/file"
import colors from '../palette' import colors from '../palette'
import type color from '../helpers/color' import Color from '../helpers/color'
import { toSnakeCase } from '../helpers/string'
const file = appRoot + '/dist/palette.toml' const templateFile = appRoot + '/src/build/templates/toml.hbs'
const appendToTOML = async (content: string) => await appendToFile(file, content) const outputFile = appRoot + '/dist/palette.toml'
export default async function(isVerbose = false): Promise<string> { export default async function(isVerbose = false): Promise<string> {
if (isVerbose) console.info('Removing previous build', file)
try { try {
await rmFile(file) await renderToFile(templateFile, outputFile, getData(), isVerbose)
return outputFile
} catch (error) { } 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) function getData(): any {
await appendToTOML(headerTOML()) return {
if (isVerbose) console.info('Writing author info to file', file) name,
await appendToTOML(authorTOML()) version,
if (isVerbose) console.info('Writing color palette to file', file) license,
await appendToTOML(colorHeaderTOML()) url,
for (let index = 0; index < colors.length; index++) { author,
const color = colors[index] colors: colors.map(getDataColors)
if (isVerbose) console.info(`${color.name}`, file)
await appendToTOML(colorTOML(color))
} }
return file
} }
function headerTOML(): string { function getDataColors(color: Color): any {
return `name = "${name}" const { name, nameSnakeCase, l, c, h } = color
version = "${version}"
license = "${license}"
sourcecode = "${url}"
`
}
function authorTOML(): string { return {
const { name, email, url } = author name,
nameSnakeCase,
return ` l,
[author] c,
name = "${name}" h,
email = "${email}" hex: color.toHex()
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()}"
`
} }

View file

@ -1,62 +1,38 @@
import { name, version, license, url, author } from '../../package.json' import { name, version, license, url, author } from '../../package.json'
import appRoot from 'app-root-path' import appRoot from 'app-root-path'
import { rmFile, mkFilePath, appendToFile } from "../helpers/file"
import { toSnakeCase } from '../helpers/string'
import colors from '../palette' 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 templateFile = appRoot + '/src/build/templates/yaml.hbs'
const appendToYAML = async (content: string) => await appendToFile(file, content) const outputFile = appRoot + '/dist/palette.yml'
export default async function(isVerbose = false): Promise<string> { export default async function(isVerbose = false): Promise<string> {
if (isVerbose) console.info('Removing previous build', file)
try { try {
await rmFile(file) await renderToFile(templateFile, outputFile, getData(), isVerbose)
return outputFile
} catch (error) { } 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) function getData(): any {
await appendToYAML(headerYAML()) return {
if (isVerbose) console.info('Writing author info to file', file) name,
await appendToYAML(authorYAML()) version,
if (isVerbose) console.info('Writing color palette to file', file) license,
await appendToYAML(colorHeaderYAML()) url,
for (let index = 0; index < colors.length; index++) { author,
const color = colors[index] colors: colors.map(getDataColors)
if (isVerbose) console.info(`${color.name}`, file)
await appendToYAML(colorYAML(color))
} }
return file
} }
function headerYAML(): string { function getDataColors(color: Color): any {
return `name: "${name}" const { nameSnakeCase } = color
version: "${version}"
license: "${license}"
sourcecode: "${url}"
`
}
function authorYAML(): string { return {
const { name, email, url } = author nameSnakeCase,
hex: color.toHex()
return ` }
author:
name: "${name}"
email: "${email}"
url: "${url}"
`
}
function colorHeaderYAML(): string {
return `
common:`
}
function colorYAML(color: color): string {
return `
${toSnakeCase(color.name)}: "${color.toHex()}"`
} }

View file

@ -1,4 +1,5 @@
import convert from 'color-convert' import convert from 'color-convert'
import { toSnakeCase } from './string'
export interface OklchInterface { export interface OklchInterface {
l: number, l: number,
@ -31,6 +32,8 @@ export default class {
return true return true
} }
get nameSnakeCase() { return toSnakeCase(this.#name) }
get l() { return this.#l } get l() { return this.#l }
set l(value: number) { set l(value: number) {
if (this._isLValid(value)) this.#l = value if (this._isLValid(value)) this.#l = value

View file

@ -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' 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 { try {
log(isVerbose, 'Ensuring path and file exist', file)
await mkdir(dirname(file), { recursive: true }) await mkdir(dirname(file), { recursive: true })
} catch (error) { } catch (error) {
throw error throw error
} }
} }
export async function rmFile(file: string) { export async function renderToFile(
templateFile: string, outputFile: string, data: any, isVerbose = false
): Promise<string> {
try { try {
await rm(file) await mkFilePath(outputFile, isVerbose)
} catch (error) {
throw error log(isVerbose, 'Reading in template', templateFile)
} const template = (await readFile(templateFile)).toString()
}
log(isVerbose, 'Compiling template', templateFile, 'with data', data)
export async function appendToFile(file: string, content: string) { const result = compile(template)(data)
try {
await appendFile(file, content) log(isVerbose, 'Saving result', outputFile)
await writeFile(outputFile, result)
return result
} catch (error) { } catch (error) {
throw error throw error
} }

View file

@ -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 =