|  | 
|  | 1 | +import {program} from 'commander'; | 
|  | 2 | +import fs from 'fs'; | 
|  | 3 | +import path from 'path'; | 
|  | 4 | +import rewiremock from 'rewiremock'; | 
|  | 5 | + | 
|  | 6 | +program | 
|  | 7 | +    .option('-c, --config <tailwind.config.js>', 'tailwind config file input') | 
|  | 8 | +    .option( | 
|  | 9 | +        '-o, --output <file>', | 
|  | 10 | +        'generated react-native-tailwind output (default: rn-tailwind.ts)' | 
|  | 11 | +    ); | 
|  | 12 | + | 
|  | 13 | +program.parse(process.argv); | 
|  | 14 | + | 
|  | 15 | +const configFile = program.config || 'simpleConfig.stub.js'; | 
|  | 16 | +const outputFile = program.output || 'rn-tailwind.ts'; | 
|  | 17 | + | 
|  | 18 | +const config = require(path.isAbsolute(configFile) | 
|  | 19 | +    ? configFile | 
|  | 20 | +    : path.join(process.cwd(), configFile)); | 
|  | 21 | +if (!config || !config.theme) { | 
|  | 22 | +    throw new Error('Tailwind config appears to be invalid'); | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +const outputPath = path.isAbsolute(outputFile) | 
|  | 26 | +    ? outputFile | 
|  | 27 | +    : path.join(process.cwd(), outputFile); | 
|  | 28 | +if (fs.existsSync(outputPath)) { | 
|  | 29 | +    throw new Error('Output file already exists - refusing to overwrite.'); | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +// Override internal require in `configHandler.js` with commandline input | 
|  | 33 | +rewiremock('../../../tailwind.config').with(config); | 
|  | 34 | +// Mock StyleSheet.create to just return input object, to get "bare" output from `tailwind.js` | 
|  | 35 | +rewiremock('react-native').with({StyleSheet: {create: o => o}}); | 
|  | 36 | + | 
|  | 37 | +// Run tailwind and color generators | 
|  | 38 | +const tailwind = rewiremock.proxy('./tailwind').default; | 
|  | 39 | +const color = rewiremock.proxy('./color').default; | 
|  | 40 | + | 
|  | 41 | +// Hack: template together a TypeScript file for use in RN project | 
|  | 42 | +const output = ` | 
|  | 43 | +import { StyleSheet } from 'react-native'; | 
|  | 44 | +export const t = StyleSheet.create(${JSON.stringify(tailwind)}); | 
|  | 45 | +export const color = ${JSON.stringify(color)};`; | 
|  | 46 | +fs.writeFileSync(outputPath, output); | 
0 commit comments