diff --git a/Justfile b/Justfile index 763ab66..2a58b12 100644 --- a/Justfile +++ b/Justfile @@ -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 diff --git a/package.json b/package.json index cde3d22..a8f16e8 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/cli.mjs b/src/cli.mjs index fb4dda8..69f84da 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -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 [args]") .command(cssCmd) .command(paletteCmd) diff --git a/src/cmds/css/variables.mjs b/src/cmds/css/variables.mjs index 5c9a42d..96af425 100644 --- a/src/cmds/css/variables.mjs +++ b/src/cmds/css/variables.mjs @@ -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); } diff --git a/src/cmds/palette.mjs b/src/cmds/palette.mjs index 1422f07..0ff8c7e 100644 --- a/src/cmds/palette.mjs +++ b/src/cmds/palette.mjs @@ -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); } diff --git a/src/colors.mjs b/src/colors.mjs index 7975039..d1163b5 100644 --- a/src/colors.mjs +++ b/src/colors.mjs @@ -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; } /** diff --git a/src/output.mjs b/src/output.mjs new file mode 100644 index 0000000..130190a --- /dev/null +++ b/src/output.mjs @@ -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); +}