@diskette/palette 0.22.1 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli/cli.js CHANGED
@@ -4,7 +4,8 @@ import { mkdirSync } from 'node:fs';
4
4
  import { relative, resolve } from 'node:path';
5
5
  import { accentColors, grayColors, } from "../types.js";
6
6
  import { getMatchingGrayColor } from "../utils.js";
7
- import { alphaColors, capitalize, getColorHex, writeAccentsFile, writeColorFiles, writeTailwindFile, } from "./utils.js";
7
+ import { buildOutputFiles, confirmOverwrites, writeOutputFiles, } from "./output.js";
8
+ import { alphaColors, capitalize, getColorHex } from "./utils.js";
8
9
  const accentOptions = {
9
10
  Accents: accentColors
10
11
  .filter((c) => c !== 'gray')
@@ -24,7 +25,7 @@ const grayOptions = grayColors.map((c) => {
24
25
  return { value: c, label: `${swatch}${capitalize(c)}` };
25
26
  });
26
27
  async function main() {
27
- p.intro(`palette v0.22.1`);
28
+ p.intro(`palette v0.23.0`);
28
29
  const results = await p.group({
29
30
  colors: () => p.groupMultiselect({
30
31
  message: 'Select accent colors',
@@ -73,23 +74,25 @@ async function main() {
73
74
  const cwd = process.cwd();
74
75
  const outputPath = resolve(cwd, results.output);
75
76
  mkdirSync(outputPath, { recursive: true });
76
- const s = p.spinner();
77
- s.start('Generating CSS files...');
78
- writeColorFiles(allColors, outputPath);
79
- if (results.accents) {
80
- writeAccentsFile(allColors, outputPath);
81
- }
82
- if (results.tailwind) {
83
- writeTailwindFile(allColors, outputPath, results.accents);
84
- }
77
+ // Build file list, confirm overwrites, and write
78
+ const allFiles = buildOutputFiles({
79
+ colors: allColors,
80
+ outputPath,
81
+ accents: results.accents,
82
+ tailwind: results.tailwind,
83
+ });
84
+ const filesToWrite = await confirmOverwrites(allFiles);
85
+ const s = p.spinner({ withGuide: false });
86
+ const writeResults = writeOutputFiles(filesToWrite);
85
87
  s.stop();
86
88
  const rel = relative(cwd, outputPath);
87
89
  const display = rel.startsWith('..') ? rel : `./${rel}`;
88
- const extras = [
89
- results.accents && 'accents.css',
90
- results.tailwind && 'index.css',
91
- ].filter(Boolean);
92
- p.log.success(`Generated ${allColors.length} color${allColors.length > 1 ? 's' : ''}${extras.length ? ` + ${extras.join(', ')}` : ''}`);
90
+ const written = writeResults.filter((r) => r.status === 'written').length;
91
+ const skipped = allFiles.length - filesToWrite.length;
92
+ if (skipped > 0) {
93
+ p.log.warn(`Skipped ${skipped} existing file${skipped > 1 ? 's' : ''}`);
94
+ }
95
+ p.log.success(`Generated ${written} file${written !== 1 ? 's' : ''}`);
93
96
  p.outro(`Saved to ${display}`);
94
97
  }
95
98
  main();
@@ -0,0 +1,19 @@
1
+ export type OutputFile = {
2
+ path: string;
3
+ displayName: string;
4
+ content: () => string;
5
+ skipIfExists?: boolean;
6
+ };
7
+ export type WriteResult = {
8
+ path: string;
9
+ status: 'written' | 'skipped' | 'error';
10
+ error?: Error;
11
+ };
12
+ export declare function buildOutputFiles(config: {
13
+ colors: string[];
14
+ outputPath: string;
15
+ accents?: boolean;
16
+ tailwind?: boolean;
17
+ }): OutputFile[];
18
+ export declare function confirmOverwrites(files: OutputFile[]): Promise<OutputFile[]>;
19
+ export declare function writeOutputFiles(files: OutputFile[]): WriteResult[];
@@ -0,0 +1,106 @@
1
+ import * as p from '@clack/prompts';
2
+ import { existsSync, writeFileSync } from 'node:fs';
3
+ import { relative } from 'node:path';
4
+ import * as palette from "../colors/index.js";
5
+ import { css } from "../css.js";
6
+ import {} from "../types.js";
7
+ import { alphaColors, capitalize } from "./utils.js";
8
+ function isAlphaColor(name) {
9
+ return alphaColors.includes(name);
10
+ }
11
+ function alphaColorFile(name, outputPath) {
12
+ const fileName = name === 'blackAlpha' ? 'black-alpha' : 'white-alpha';
13
+ return {
14
+ path: `${outputPath}/${fileName}.css`,
15
+ displayName: capitalize(fileName.replace('-', ' ')),
16
+ content: () => css.alpha(palette[name]),
17
+ skipIfExists: true,
18
+ };
19
+ }
20
+ function paletteColorFiles(name, outputPath) {
21
+ const color = palette[name];
22
+ return [
23
+ {
24
+ path: `${outputPath}/${name}.css`,
25
+ displayName: capitalize(name),
26
+ content: () => css.palette(color, { schemes: ['light'], alpha: true }),
27
+ },
28
+ {
29
+ path: `${outputPath}/${name}-dark.css`,
30
+ displayName: `${capitalize(name)} Dark`,
31
+ content: () => css.palette(color, { schemes: ['dark'], alpha: true }),
32
+ },
33
+ ];
34
+ }
35
+ function accentsFile(colorNames, outputPath) {
36
+ return {
37
+ path: `${outputPath}/accents.css`,
38
+ displayName: 'Accents',
39
+ content: () => css.accents(colorNames),
40
+ };
41
+ }
42
+ function tailwindFile(names, outputPath, accents) {
43
+ return {
44
+ path: `${outputPath}/index.css`,
45
+ displayName: 'Tailwind Config',
46
+ content: () => css.tailwind(names, { accents }),
47
+ };
48
+ }
49
+ export function buildOutputFiles(config) {
50
+ const { colors, outputPath, accents, tailwind } = config;
51
+ const files = [];
52
+ for (const name of colors) {
53
+ if (isAlphaColor(name)) {
54
+ files.push(alphaColorFile(name, outputPath));
55
+ }
56
+ else {
57
+ files.push(...paletteColorFiles(name, outputPath));
58
+ }
59
+ }
60
+ if (accents) {
61
+ const colorNames = colors.filter((c) => !isAlphaColor(c));
62
+ files.push(accentsFile(colorNames, outputPath));
63
+ }
64
+ if (tailwind) {
65
+ const tailwindNames = colors.map((c) => c === 'blackAlpha' ? 'black' : c === 'whiteAlpha' ? 'white' : c);
66
+ files.push(tailwindFile(tailwindNames, outputPath, accents ?? false));
67
+ }
68
+ return files;
69
+ }
70
+ export async function confirmOverwrites(files) {
71
+ const cwd = process.cwd();
72
+ const newFiles = files.filter((f) => !existsSync(f.path));
73
+ const existing = files.filter((f) => existsSync(f.path));
74
+ // Files that should prompt for overwrite confirmation
75
+ const promptable = existing.filter((f) => !f.skipIfExists);
76
+ if (promptable.length === 0) {
77
+ return newFiles;
78
+ }
79
+ const selected = await p.multiselect({
80
+ message: 'These files already exist. Select to overwrite:',
81
+ options: promptable.map((f) => ({
82
+ value: f.path,
83
+ label: f.displayName,
84
+ hint: relative(cwd, f.path),
85
+ })),
86
+ required: false,
87
+ });
88
+ if (p.isCancel(selected)) {
89
+ p.cancel('Operation cancelled.');
90
+ process.exit(0);
91
+ }
92
+ const selectedSet = new Set(selected);
93
+ const toOverwrite = promptable.filter((f) => selectedSet.has(f.path));
94
+ return [...newFiles, ...toOverwrite];
95
+ }
96
+ export function writeOutputFiles(files) {
97
+ return files.map((file) => {
98
+ try {
99
+ writeFileSync(file.path, file.content());
100
+ return { path: file.path, status: 'written' };
101
+ }
102
+ catch (error) {
103
+ return { path: file.path, status: 'error', error: error };
104
+ }
105
+ });
106
+ }
@@ -1,4 +1,3 @@
1
- import { type Color } from '../types.ts';
2
1
  export declare const alphaColors: readonly ["blackAlpha", "whiteAlpha"];
3
2
  export type AlphaColor = (typeof alphaColors)[number];
4
3
  /**
@@ -10,12 +9,6 @@ export declare const getPackageJson: (cwd: string) => {
10
9
  pkg: any;
11
10
  packageJsonPath: string;
12
11
  } | undefined;
13
- export declare function writeColorFiles(selected: string[], outputPath: string): void;
14
- export declare function writeAlphaColorFile(name: AlphaColor, outputPath: string): void;
15
- export declare function writePaletteColorFiles(name: Color, outputPath: string): void;
16
- export declare function writeAccentsFile(selected: string[], outputPath: string): void;
17
- export declare function writeTailwindFile(selected: string[], outputPath: string, includeAccents: boolean): void;
18
- export declare function writeIfNotExists(path: string, data: string | NodeJS.ArrayBufferView): boolean;
19
12
  export declare const capitalize: (s: string) => string;
20
13
  /** Get hex color (step 9) for a palette color */
21
14
  export declare const getColorHex: (name: string) => string | null;
package/dist/cli/utils.js CHANGED
@@ -1,10 +1,8 @@
1
- import fs, { existsSync, writeFileSync } from 'node:fs';
1
+ import fs from 'node:fs';
2
2
  import { createRequire } from 'node:module';
3
3
  import path from 'node:path';
4
4
  import * as url from 'node:url';
5
5
  import * as colors from "../colors/index.js";
6
- import * as palette from "../colors/index.js";
7
- import { css } from "../css.js";
8
6
  import {} from "../types.js";
9
7
  export const alphaColors = ['blackAlpha', 'whiteAlpha'];
10
8
  const require = createRequire(import.meta.url);
@@ -47,41 +45,6 @@ const findPackageJson = (startDir) => {
47
45
  currentDir = parentDir;
48
46
  }
49
47
  };
50
- export function writeColorFiles(selected, outputPath) {
51
- for (const name of selected) {
52
- if (alphaColors.includes(name)) {
53
- writeAlphaColorFile(name, outputPath);
54
- }
55
- else {
56
- writePaletteColorFiles(name, outputPath);
57
- }
58
- }
59
- }
60
- export function writeAlphaColorFile(name, outputPath) {
61
- const config = palette[name];
62
- const fileName = name === 'blackAlpha' ? 'black-alpha' : 'white-alpha';
63
- writeIfNotExists(`${outputPath}/${fileName}.css`, css.alpha(config));
64
- }
65
- export function writePaletteColorFiles(name, outputPath) {
66
- const color = palette[name];
67
- writeIfNotExists(`${outputPath}/${name}.css`, css.palette(color, { schemes: ['light'], alpha: true }));
68
- writeIfNotExists(`${outputPath}/${name}-dark.css`, css.palette(color, { schemes: ['dark'], alpha: true }));
69
- }
70
- export function writeAccentsFile(selected, outputPath) {
71
- const accentColorNames = selected.filter((c) => !alphaColors.includes(c));
72
- writeIfNotExists(`${outputPath}/accents.css`, css.accents(accentColorNames));
73
- }
74
- export function writeTailwindFile(selected, outputPath, includeAccents) {
75
- const tailwindColorNames = selected.map((c) => c === 'blackAlpha' ? 'black' : c === 'whiteAlpha' ? 'white' : c);
76
- writeIfNotExists(`${outputPath}/index.css`, css.tailwind(tailwindColorNames, { accents: includeAccents }));
77
- }
78
- export function writeIfNotExists(path, data) {
79
- if (existsSync(path)) {
80
- return false;
81
- }
82
- writeFileSync(path, data);
83
- return true;
84
- }
85
48
  export const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
86
49
  /** Get hex color (step 9) for a palette color */
87
50
  export const getColorHex = (name) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@diskette/palette",
3
3
  "type": "module",
4
- "version": "0.22.1",
4
+ "version": "0.23.0",
5
5
  "bin": {
6
6
  "palette": "./dist/cli/cli.js"
7
7
  },