|
| 1 | +#! /usr/bin/env node |
| 2 | +/* eslint-disable @typescript-eslint/no-var-requires, no-console */ |
| 3 | + |
| 4 | +const yargs = require('yargs/yargs'); |
| 5 | +const shell = require('shelljs'); |
| 6 | +const chalk = require('chalk'); |
| 7 | +const path = require('path'); |
| 8 | +const kebab = require('lodash.kebabcase'); |
| 9 | +const { hideBin } = require('yargs/helpers'); |
| 10 | + |
| 11 | +const getTransformerPath = componentName => |
| 12 | + path.resolve(__dirname, `../src/${kebab(componentName)}/transform.ts`); |
| 13 | + |
| 14 | +const availableComponentsTransformers = ['Label', 'Heading', 'Paragraph']; |
| 15 | + |
| 16 | +function main() { |
| 17 | + const { argv } = yargs(hideBin(process.argv)); |
| 18 | + |
| 19 | + if (argv.help) { |
| 20 | + // TODO: show help |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if (!argv._ || argv._.length === 0) { |
| 25 | + console.log( |
| 26 | + chalk.red( |
| 27 | + 'Укажите glob для файлов, которые нужно трансформировать, например src/**/*.tsx', |
| 28 | + ), |
| 29 | + ); |
| 30 | + |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (argv.components && typeof argv.components !== 'string') { |
| 35 | + console.log( |
| 36 | + chalk.red( |
| 37 | + 'Укажите компоненты, которые хотите заменить, например --components="Label,Heading,Paragraph"', |
| 38 | + ), |
| 39 | + ); |
| 40 | + |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + let components = availableComponentsTransformers; |
| 45 | + |
| 46 | + if (argv.components) { |
| 47 | + components = argv.components.split(','); |
| 48 | + } |
| 49 | + |
| 50 | + components.forEach(componentName => { |
| 51 | + if (!availableComponentsTransformers.includes(componentName)) { |
| 52 | + console.log(chalk.yellow(`Для компонента ${componentName} еще нет трансформера`)); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const transformer = getTransformerPath(componentName); |
| 57 | + |
| 58 | + try { |
| 59 | + console.log(chalk.green(`Трансформируем ${componentName}:`)); |
| 60 | + shell.exec(`jscodeshift --parser=tsx --transform=${transformer} ${argv._.join(' ')}`); |
| 61 | + } catch (error) { |
| 62 | + console.log(chalk.red(error)); |
| 63 | + } |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +main(); |
0 commit comments