-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtonal-cli.js
More file actions
executable file
·98 lines (82 loc) · 2.92 KB
/
tonal-cli.js
File metadata and controls
executable file
·98 lines (82 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
import { Command } from 'commander';
import fs from 'fs';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
import { generatePalette } from './tonal-core.js';
import { exportPalette } from './exporters.js';
import { generatePreviewHTML } from './preview.js';
import open from 'open';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const program = new Command();
const configPath = path.resolve(process.cwd(), 'tonal.config.js');
let config;
try {
const module = await import(pathToFileURL(configPath).href);
config = module.default;
} catch (err) {
console.error('tonal.config.js not found or invalid.');
process.exit(1);
}
const extMap = {
scss: 'scss',
css: 'css',
less: 'less',
stylus: 'styl',
bulma: 'sass',
js: 'js'
};
program
.command('generate')
.description('Generate all palettes from tonal.config.js')
.option('-e, --ext <ext>', 'Output file extension', 'css')
.option('-f, --format <fmt>', 'Color format (hex, oklch, rgb, hsl)', 'hex')
.option('-o, --out-dir <dir>', 'Output directory', './tonal')
.option('--file <name>', 'Output filename (without extension)', 'colors')
.option('-p, --preview', 'Generate and open an HTML preview')
.action(async ({ ext = 'css', format = 'oklch', outDir = './tonal', file = 'colors', preview: previewFlag }) => {
const extMap = {
scss: 'scss',
css: 'css',
less: 'less',
stylus: 'styl',
bulma: 'sass',
js: 'js'
};
const colorFormats = ['hex', 'oklch', 'rgb', 'hsl'];
if (!extMap[ext]) {
console.error(`Unknown file extension: "${ext}". Supported: ${Object.keys(extMap).join(', ')}`);
process.exit(1);
}
if (!colorFormats.includes(format)) {
console.error(`Unknown color format: "${format}". Supported: ${colorFormats.join(', ')}`);
process.exit(1);
}
const extName = extMap[ext];
const fullPath = path.join(outDir, `${file}.${extName}`);
fs.mkdirSync(outDir, { recursive: true });
const allPalettes = generatePalette(config.colors, format);
const output = exportPalette(allPalettes, ext);
fs.writeFileSync(fullPath, output);
console.log(`All palettes exported as .${extName} → ${fullPath}`);
if (previewFlag) {
const previewPath = path.join(outDir, 'preview.html');
generatePreviewHTML(allPalettes, format, previewPath);
await open(previewPath);
}
});
// Commande : preview
program
.command('preview')
.description('Generate an HTML preview of all palettes')
.option('-f, --format <fmt>', 'Display format', 'oklch')
.option('--file <path>', 'HTML output file', 'tonal-preview.html')
.option('-o, --open', 'Open the file in browser')
.action(async ({ format, file, open: openFlag }) => {
const allPalettes = generatePalette(config.colors, format);
generatePreviewHTML(allPalettes, format, file);
if (openFlag) {
await open(file);
}
});
program.parse();