handlebars for yaml and toml gen
This commit is contained in:
parent
3fb0a2360a
commit
211b5ae702
12 changed files with 133 additions and 149 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
28
dist/palette.toml
vendored
28
dist/palette.toml
vendored
|
@ -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"
|
||||
|
||||
|
||||
|
|
6
dist/palette.yml
vendored
6
dist/palette.yml
vendored
|
@ -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"
|
||||
purple: "#BEB6E8"
|
||||
|
|
4
index.ts
4
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();
|
||||
|
|
|
@ -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"
|
||||
|
|
21
src/build/templates/toml.hbs
Normal file
21
src/build/templates/toml.hbs
Normal 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}}
|
14
src/build/templates/yaml.hbs
Normal file
14
src/build/templates/yaml.hbs
Normal 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}}
|
|
@ -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<string> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<string> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<string> {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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 =
|
Loading…
Add table
Reference in a new issue