feat: reworking the CLI for more standard output
This commit is contained in:
parent
0a078698f7
commit
41c9e40cef
2
Justfile
2
Justfile
|
@ -12,7 +12,7 @@ build: colors examples
|
||||||
|
|
||||||
# Generate dist/colors.css
|
# Generate dist/colors.css
|
||||||
colors: format
|
colors: format
|
||||||
node src/generate.mjs
|
node src/cli.mjs css variables -o colors.css
|
||||||
prettier colors.css --write
|
prettier colors.css --write
|
||||||
|
|
||||||
# Generate examples/theme.css
|
# Generate examples/theme.css
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "@priduck-color-theme/base",
|
"name": "@priduck-color-theme/priduck-cli",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"description": "A color-theme based on a single hue in the LCH colorspace.",
|
"description": "A color-theme based on a single hue in the LCH colorspace.",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
@ -12,6 +12,7 @@
|
||||||
],
|
],
|
||||||
"author": "D. Moonfire",
|
"author": "D. Moonfire",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"index": "./src/cli.mjs",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mkdirp": "^3.0.1"
|
"mkdirp": "^3.0.1"
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,7 +5,6 @@ import * as paletteCmd from "./cmds/palette.mjs";
|
||||||
|
|
||||||
// Set up the top-level command.
|
// Set up the top-level command.
|
||||||
yargs(hideBin(process.argv))
|
yargs(hideBin(process.argv))
|
||||||
.scriptName("priduck")
|
|
||||||
.usage("$0 <cmd> [args]")
|
.usage("$0 <cmd> [args]")
|
||||||
.command(cssCmd)
|
.command(cssCmd)
|
||||||
.command(paletteCmd)
|
.command(paletteCmd)
|
||||||
|
|
|
@ -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 * as colors from "../../colors.mjs";
|
||||||
|
import { write } from "../../output.mjs";
|
||||||
|
|
||||||
export const command = "variables [path.css]";
|
export const command = "variables [path.css]";
|
||||||
export const desc = "Generate a CSS file that uses variables to generate";
|
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) {
|
export function builder(argv) {
|
||||||
argv.option("o", {
|
argv.option("o", {
|
||||||
alias: "output",
|
alias: "output",
|
||||||
demandOption: true,
|
|
||||||
describe: "The output path for colors.css",
|
describe: "The output path for colors.css",
|
||||||
type: "string",
|
type: "string",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handler(argv) {
|
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.
|
// Start with the basic CSS header.
|
||||||
let lines = [":root {"];
|
let lines = [":root {"];
|
||||||
|
|
||||||
|
@ -31,7 +20,7 @@ export async function handler(argv) {
|
||||||
// rotated around the color wheel.
|
// rotated around the color wheel.
|
||||||
for (const color of colors.colorList) {
|
for (const color of colors.colorList) {
|
||||||
// Figure out the rotation.
|
// 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
|
// For each hue, we have ten levels of brightness ranging from very dark
|
||||||
// to very light.
|
// to very light.
|
||||||
|
@ -55,8 +44,6 @@ export async function handler(argv) {
|
||||||
// Finish up the lines.
|
// Finish up the lines.
|
||||||
lines.push("}");
|
lines.push("}");
|
||||||
|
|
||||||
// Write out the files.
|
// Write out the results to the console or a file.
|
||||||
await fs.writeFile(file, Buffer.from(lines.join("\n")));
|
await write(argv, lines);
|
||||||
|
|
||||||
console.log("wrote", 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 * as colors from "../colors.mjs";
|
||||||
|
import { write } from "../output.mjs";
|
||||||
|
|
||||||
export const command = "palette [path.gpl]";
|
export const command = "palette [path.gpl]";
|
||||||
export const desc = "Generate a GNU Imp/Inkscape palette";
|
export const desc = "Generate a GNU Imp/Inkscape palette";
|
||||||
|
@ -10,7 +8,7 @@ export function builder(argv) {
|
||||||
argv
|
argv
|
||||||
.option("c", {
|
.option("c", {
|
||||||
alias: "hue",
|
alias: "hue",
|
||||||
default: 220,
|
demandOption: true,
|
||||||
describe: "The hue (0-360) for the base color",
|
describe: "The hue (0-360) for the base color",
|
||||||
type: "number",
|
type: "number",
|
||||||
})
|
})
|
||||||
|
@ -36,22 +34,12 @@ export function builder(argv) {
|
||||||
})
|
})
|
||||||
.option("o", {
|
.option("o", {
|
||||||
alias: "output",
|
alias: "output",
|
||||||
demandOption: true,
|
|
||||||
describe: "The output path for colors.css",
|
describe: "The output path for colors.css",
|
||||||
type: "string",
|
type: "string",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handler(argv) {
|
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.
|
// Start with the header.
|
||||||
let lines = ["GIMP Palette", `Name: ${argv.name}`, "Columns: 30", ""];
|
let lines = ["GIMP Palette", `Name: ${argv.name}`, "Columns: 30", ""];
|
||||||
|
|
||||||
|
@ -72,8 +60,6 @@ export async function handler(argv) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write out the files.
|
// Write out the results to the console or a file.
|
||||||
await fs.writeFile(file, Buffer.from(lines.join("\n")));
|
await write(argv, lines);
|
||||||
|
|
||||||
console.log("wrote", 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.
|
* 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} base The base hue, in the range [0-360)
|
||||||
* @param {number} color The color code, in the range of [0-9]
|
* @param {number} color The color code, in the range of [0-9]
|
||||||
*/
|
*/
|
||||||
export function getHue(base, color) {
|
export function getHue(base, color) {
|
||||||
return (base + color * 36) % 360;
|
return (base + getHueRotation(color)) % 360;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
35
src/output.mjs
Normal file
35
src/output.mjs
Normal 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);
|
||||||
|
}
|
Loading…
Reference in a new issue