diff --git a/bun.lockb b/bun.lockb index e26b0b3..0afce47 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/dist/palette.toml b/dist/palette.toml index 18c9333..239e6cc 100644 --- a/dist/palette.toml +++ b/dist/palette.toml @@ -2,88 +2,88 @@ 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" - + [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 =