feat: reworking the CLI for more standard output

This commit is contained in:
D. Moonfire 2024-05-09 23:22:47 -05:00
parent 0a078698f7
commit 41c9e40cef
7 changed files with 56 additions and 39 deletions

View file

@ -12,7 +12,7 @@ build: colors examples
# Generate dist/colors.css
colors: format
node src/generate.mjs
node src/cli.mjs css variables -o colors.css
prettier colors.css --write
# Generate examples/theme.css

View file

@ -1,5 +1,5 @@
{
"name": "@priduck-color-theme/base",
"name": "@priduck-color-theme/priduck-cli",
"version": "0.1.0",
"description": "A color-theme based on a single hue in the LCH colorspace.",
"repository": {
@ -12,6 +12,7 @@
],
"author": "D. Moonfire",
"license": "MIT",
"index": "./src/cli.mjs",
"devDependencies": {
"mkdirp": "^3.0.1"
},

View file

@ -5,7 +5,6 @@ import * as paletteCmd from "./cmds/palette.mjs";
// Set up the top-level command.
yargs(hideBin(process.argv))
.scriptName("priduck")
.usage("$0 <cmd> [args]")
.command(cssCmd)
.command(paletteCmd)

View file

@ -1,7 +1,5 @@
import { mkdirp } from "mkdirp";
import path from "path";
import fs from "node:fs/promises";
import * as colors from "../../colors.mjs";
import { write } from "../../output.mjs";
export const command = "variables [path.css]";
export const desc = "Generate a CSS file that uses variables to generate";
@ -9,21 +7,12 @@ export const desc = "Generate a CSS file that uses variables to generate";
export function builder(argv) {
argv.option("o", {
alias: "output",
demandOption: true,
describe: "The output path for colors.css",
type: "string",
});
}
export async function handler(argv) {
// Figure out where we need to write the files.
const file = argv.output;
const dir = path.dirname(file);
console.log("writing", file);
await mkdirp(dir);
// Start with the basic CSS header.
let lines = [":root {"];
@ -31,7 +20,7 @@ export async function handler(argv) {
// rotated around the color wheel.
for (const color of colors.colorList) {
// Figure out the rotation.
const rotation = colors.getHue(color);
const rotation = colors.getHueRotation(color);
// For each hue, we have ten levels of brightness ranging from very dark
// to very light.
@ -55,8 +44,6 @@ export async function handler(argv) {
// Finish up the lines.
lines.push("}");
// Write out the files.
await fs.writeFile(file, Buffer.from(lines.join("\n")));
console.log("wrote", file);
// Write out the results to the console or a file.
await write(argv, lines);
}

View file

@ -1,7 +1,5 @@
import { mkdirp } from "mkdirp";
import path from "path";
import fs from "node:fs/promises";
import * as colors from "../colors.mjs";
import { write } from "../output.mjs";
export const command = "palette [path.gpl]";
export const desc = "Generate a GNU Imp/Inkscape palette";
@ -10,7 +8,7 @@ export function builder(argv) {
argv
.option("c", {
alias: "hue",
default: 220,
demandOption: true,
describe: "The hue (0-360) for the base color",
type: "number",
})
@ -36,22 +34,12 @@ export function builder(argv) {
})
.option("o", {
alias: "output",
demandOption: true,
describe: "The output path for colors.css",
type: "string",
});
}
export async function handler(argv) {
// Figure out where we need to write the files.
const file = argv.output;
const dir = path.dirname(file);
console.log("base hue", argv.hue);
console.log("writing", file);
await mkdirp(dir);
// Start with the header.
let lines = ["GIMP Palette", `Name: ${argv.name}`, "Columns: 30", ""];
@ -72,8 +60,6 @@ export async function handler(argv) {
}
}
// Write out the files.
await fs.writeFile(file, Buffer.from(lines.join("\n")));
console.log("wrote", file);
// Write out the results to the console or a file.
await write(argv, lines);
}

View file

@ -18,11 +18,20 @@ export const brightnessList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
/**
* Gets the degree rotation for the given color code.
*
* @param {number} color The color code, in the range of [0-9]
*/
export function getHueRotation(color) {
return color * 36;
}
/**
* Gets the hue for a given base hue and color.
*
* @param {number} base The base hue, in the range [0-360)
* @param {number} color The color code, in the range of [0-9]
*/
export function getHue(base, color) {
return (base + color * 36) % 360;
return (base + getHueRotation(color)) % 360;
}
/**

35
src/output.mjs Normal file
View file

@ -0,0 +1,35 @@
import { mkdirp } from "mkdirp";
import path from "path";
import fs from "node:fs/promises";
/**
* Writes the output to either the console if there is no output or it is
* set to "-", otherwise write it out as a file.
*
* @param {{output: string | undefined }} argv
* @param {string | string[]} output
*/
export async function write(argv, input) {
// If we are writing to the console, we can just dump it.
const text = Array.isArray(input) ? input.join("\n") : input;
if (!argv.output || argv.output === "-") {
console.log(text);
return;
}
// Otherwise, we are writing a file, so we need to make sure the directory
// exists.
const file = argv.output;
const dir = path.dirname(file);
console.log("writing", file);
await mkdirp(dir);
// Now, write out the file.
await fs.writeFile(file, Buffer.from(text));
console.log("wrote", file);
}