|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -/* tslint:disable:no-eval no-console */ |
4 | | -import fs from 'fs'; |
| 3 | +import commander from 'commander'; |
5 | 4 | import inquirer from 'inquirer'; |
6 | | -import isEmpty from 'lodash.isempty'; |
7 | | - |
8 | | -import { baseTemplateString, defaultColors, defaultScreens, defaultSpacing, generateTypes } from './utils'; |
9 | | - |
10 | | -inquirer |
11 | | - .prompt([ |
12 | | - { |
13 | | - name: 'configFilename', |
14 | | - type: 'input', |
15 | | - default: 'tailwind.config.js', |
16 | | - message: 'Tailwind configuration filename', |
17 | | - }, |
18 | | - { |
19 | | - name: 'outputFilename', |
20 | | - type: 'input', |
21 | | - default: 'tailwindcss-classnames.ts', |
22 | | - message: 'Name of the file with generated types', |
23 | | - }, |
24 | | - ]) |
25 | | - .then(answers => { |
26 | | - fs.readFile(`./${answers.configFilename}`, { encoding: 'utf-8' }, (err, data: any) => { |
27 | | - if (err) { |
28 | | - console.error(err); |
29 | | - } |
30 | | - |
31 | | - data = data.replace(/('|")?plugins('|")? *: *\[(.*|\n)*?\],?/g, ''); |
32 | | - |
33 | | - const CONFIG = eval(data); |
34 | | - const THEME_CONFIG = CONFIG.theme; |
35 | | - const PREFIX_CONFIG = CONFIG.prefix; |
36 | | - const SEPARATOR_CONFIG = CONFIG.separator; |
37 | | - |
38 | | - const prefix = isEmpty(PREFIX_CONFIG) ? '' : PREFIX_CONFIG; |
39 | | - const separator = isEmpty(SEPARATOR_CONFIG) ? ':' : SEPARATOR_CONFIG; |
40 | | - const themeColors = isEmpty(THEME_CONFIG?.colors) ? defaultColors : THEME_CONFIG?.colors; |
41 | | - const extendedThemeColors = THEME_CONFIG?.extend?.colors; |
42 | | - const AllColors = extendedThemeColors ? { ...themeColors, ...extendedThemeColors } : themeColors; |
43 | | - |
44 | | - const backgroundColors: string[] = []; |
45 | | - const placeholderColors: string[] = []; |
46 | | - const borderColors: string[] = []; |
47 | | - const textColors: string[] = []; |
48 | | - // theme: { |
49 | | - // colors: { |
50 | | - // colorkey: colorVal ( "#fff" | {light: "#fff", lighter: "#f0f0f0",...} ) |
51 | | - // } |
52 | | - // } |
53 | | - const colors = Object.keys(AllColors); |
54 | | - for (let i = 0; i < colors.length; i += 1) { |
55 | | - const colorKey = colors[i]; |
56 | | - const colorVal = AllColors[colorKey]; |
57 | | - if (colorVal instanceof Object) { |
58 | | - const colorVariants = Object.keys(colorVal); |
59 | | - colorVariants.map((variant: string) => { |
60 | | - if (variant === 'default') { |
61 | | - variant = ''; |
62 | | - } else { |
63 | | - variant = `-${variant}`; |
64 | | - } |
65 | | - backgroundColors.push(`${prefix}bg-${colorKey}${variant}`); |
66 | | - placeholderColors.push(`${prefix}placeholder-${colorKey}${variant}`); |
67 | | - borderColors.push(`${prefix}border-${colorKey}${variant}`); |
68 | | - textColors.push(`${prefix}text-${colorKey}${variant}`); |
69 | | - }); |
70 | | - } else { |
71 | | - backgroundColors.push(`${prefix}bg-${colorKey}`); |
72 | | - placeholderColors.push(`${prefix}placeholder-${colorKey}`); |
73 | | - borderColors.push(`${prefix}border-${colorKey}`); |
74 | | - textColors.push(`${prefix}text-${colorKey}`); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - const themeBreakpoints = isEmpty(THEME_CONFIG?.screens) ? defaultScreens : THEME_CONFIG?.screens; |
79 | | - const extendedThemeBreakpoints = THEME_CONFIG?.extend?.screens; |
80 | | - const breakpoints = extendedThemeBreakpoints |
81 | | - ? { ...themeBreakpoints, ...extendedThemeBreakpoints } |
82 | | - : themeBreakpoints; |
83 | | - |
84 | | - const breakpointExportStatements: string[] = []; |
85 | | - const breakpointCreateCustomParams: string[] = []; |
86 | | - const breakpointCreateCustomReturns: string[] = []; |
87 | | - const maxWidthByBreakpoints: string[] = []; |
88 | | - |
89 | | - Object.keys(breakpoints).map((breakpoint: string) => { |
90 | | - breakpointExportStatements.push( |
91 | | - `export const ${breakpoint}: TPseudoClass = className => ('${prefix}${breakpoint}${separator}' + className) as TTailwindString;`, |
92 | | - ); |
93 | | - breakpointCreateCustomParams.push(`${breakpoint}: TPseudoClass<T>;`); |
94 | | - breakpointCreateCustomReturns.push(`${breakpoint},`); |
95 | | - maxWidthByBreakpoints.push(`${prefix}max-w-screen-${breakpoint}`); |
96 | | - }); |
97 | | - |
98 | | - const themeSpacings = isEmpty(THEME_CONFIG?.spacing) ? defaultSpacing : THEME_CONFIG?.spacing; |
99 | | - const extendedThemeSpacings = THEME_CONFIG?.extend?.spacing; |
100 | | - const allSpacings = extendedThemeSpacings ? { ...themeSpacings, ...extendedThemeSpacings } : themeSpacings; |
101 | | - |
102 | | - const paddingSpacings: string[] = []; |
103 | | - const marginSpacings: string[] = []; |
104 | | - const widthSpacings: string[] = []; |
105 | | - const heightSpacings: string[] = []; |
106 | | - |
107 | | - const sides = ['', 'y', 'x', 't', 'r', 'b', 'l']; |
108 | | - |
109 | | - sides.map(side => { |
110 | | - paddingSpacings.push(`${prefix}p${side}-auto`); |
111 | | - marginSpacings.push(`${prefix}m${side}-auto`); |
| 5 | +import { createFileWithGeneratedTypes } from './createFile'; |
| 6 | + |
| 7 | +commander |
| 8 | + .option('-c, --config <config>', 'Name of the TailwindCSS config file') |
| 9 | + .option('-o, --output <output>', 'Name of the file with the generated types', 'tailwindcss-classnames.ts') |
| 10 | + .action(({ config, output }) => { |
| 11 | + if (config) { |
| 12 | + createFileWithGeneratedTypes({ |
| 13 | + configFilename: config, |
| 14 | + outputFilename: output, |
112 | 15 | }); |
113 | | - |
114 | | - Object.keys(allSpacings).map((spacing, i) => { |
115 | | - widthSpacings.push(`${prefix}w-${spacing}`); |
116 | | - heightSpacings.push(`${prefix}h-${spacing}`); |
117 | | - sides.map(side => { |
118 | | - paddingSpacings.push(`${prefix}p${side}-${spacing}`); |
119 | | - marginSpacings.push(`${prefix}m${side}-${spacing}`); |
120 | | - if (parseInt(spacing, 10) !== 0 && Object.values(allSpacings)[i] !== 0) { |
121 | | - paddingSpacings.push(`${prefix}-p${side}-${spacing}`); |
122 | | - marginSpacings.push(`${prefix}-m${side}-${spacing}`); |
| 16 | + } else { |
| 17 | + inquirer |
| 18 | + .prompt([ |
| 19 | + { |
| 20 | + name: 'configFilename', |
| 21 | + type: 'input', |
| 22 | + default: 'tailwind.config.js', |
| 23 | + message: 'Tailwind configuration filename', |
| 24 | + }, |
| 25 | + { |
| 26 | + name: 'outputFilename', |
| 27 | + type: 'input', |
| 28 | + default: 'tailwindcss-classnames.ts', |
| 29 | + message: 'Name of the file with generated types', |
| 30 | + }, |
| 31 | + ]) |
| 32 | + .then(answers => { |
| 33 | + createFileWithGeneratedTypes({ |
| 34 | + configFilename: answers.configFilename, |
| 35 | + outputFilename: answers.outputFilename, |
| 36 | + }); |
| 37 | + }) |
| 38 | + .catch(error => { |
| 39 | + if (error.isTtyError) { |
| 40 | + console.error("Prompt couldn't be rendered in the current environment"); |
| 41 | + } else { |
| 42 | + console.error('Something went wrong with the prompt'); |
123 | 43 | } |
124 | 44 | }); |
125 | | - }); |
126 | | - |
127 | | - const result = baseTemplateString |
128 | | - .replace(/_PREFIX_/g, prefix) |
129 | | - .replace(/_SEPARATOR_/g, separator) |
130 | | - .replace(/MAX_WIDTH_BY_BREAKPOINTS/g, generateTypes(maxWidthByBreakpoints)) |
131 | | - .replace(/BREAKPOINT_EXPORT_STATEMENTS/g, breakpointExportStatements.join('\n\n')) |
132 | | - .replace(/BREAKPOINTS_CREATE_CUSTOM_PARAMS/g, breakpointCreateCustomParams.join('\n ')) |
133 | | - .replace(/BREAKPOINTS_CREATE_CUSTOM_RETURNS/g, breakpointCreateCustomReturns.join('\n ')) |
134 | | - .replace(/PADDINGS/g, generateTypes(paddingSpacings)) |
135 | | - .replace(/MARGINS/g, generateTypes(marginSpacings)) |
136 | | - .replace(/WIDTH_SPACINGS/g, generateTypes(widthSpacings)) |
137 | | - .replace(/HEIGHT_SPACINGS/g, generateTypes(heightSpacings)) |
138 | | - .replace(/BACKGROUND_COLORS/g, generateTypes(backgroundColors)) |
139 | | - .replace(/PLACEHOLDER_COLORS/g, generateTypes(placeholderColors)) |
140 | | - .replace(/BORDER_COLORS/g, generateTypes(borderColors)) |
141 | | - .replace(/TEXT_COLORS/g, generateTypes(textColors)); |
142 | | - |
143 | | - fs.writeFile(`${answers.outputFilename}`, result, 'utf8', error => { |
144 | | - if (error) { |
145 | | - console.error(error); |
146 | | - } |
147 | | - }); |
148 | | - }); |
149 | | - }) |
150 | | - .catch(error => { |
151 | | - if (error.isTtyError) { |
152 | | - console.error("Prompt couldn't be rendered in the current environment"); |
153 | | - } else { |
154 | | - console.error('Something went wrong with the prompt'); |
155 | 45 | } |
156 | 46 | }); |
| 47 | + |
| 48 | +commander.parse(process.argv); |
0 commit comments