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.

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)
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))
} }
return file function getData(): any {
return {
name,
version,
license,
url,
author,
colors: colors.map(getDataColors)
}
} }
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)
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))
} }
return file function getData(): any {
return {
name,
version,
license,
url,
author,
colors: colors.map(getDataColors)
}
} }
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 =