Skip to content

Commit f12e198

Browse files
authored
chore: add codemod for heading, label, paragraph (#690)
* chore: add heading, label and paragraph codemode
1 parent a72d447 commit f12e198

26 files changed

+856
-2
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__testfixtures__

bin/build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ echo "start build on $CONCURRENCY parallel process"
1414
lerna exec --concurrency $CONCURRENCY \
1515
--ignore @alfalab/core-components-vars \
1616
--ignore @alfalab/core-components-themes \
17+
--ignore @alfalab/core-components-codemod \
1718
-- $(pwd)/bin/rollup.sh
1819

1920
# собираю css пакеты

docs/12.migrations.stories.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Description } from '@storybook/addon-docs/blocks';
2+
import migrationsReadme from '../packages/codemod/README.md';
3+
4+
<Meta title='Гайдлайны|Миграция со старых компонентов' />
5+
6+
<Description>{migrationsReadme}</Description>

jest.codemod.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
'\\.css$': 'identity-obj-proxy',
2020
},
2121
testMatch: ['**/*.test.ts?(x)', '!**/*.screenshots.test.ts?(x)'],
22+
testPathIgnorePatterns: ['codemod'],
2223
coverageReporters: ['lcov', 'text', 'text-summary', 'clover'],
2324
coveragePathIgnorePatterns: ['index.ts'],
2425
};

lerna.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"ignoreChanges": [
1010
"*.stories.*",
1111
"**/__snapshots__/**",
12-
"*.test.*"
12+
"*.test.*",
13+
"**/codemod/**"
1314
]
1415

1516
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"lint:js": "eslint ./packages --ext .ts,.tsx,.js,.jsx",
2828
"test": "jest --watchAll=false --env=jsdom-sixteen",
2929
"test:screenshots": "jest --config=jest.screenshots.config.js",
30+
"test:codemod": "jest --config=jest.codemod.config.js packages/codemod/",
3031
"format": "prettier --write \"./{packages,bin}/**/*.{ts,tsx,js,jsx,css,json}\"",
3132
"clean": "rimraf packages/*/{tsconfig.tsbuildinfo,dist} && rimraf {dist,build}",
3233
"cm": "git-cz",

packages/codemod/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

packages/codemod/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Тулзы для миграции с arui-feather на core-components.
2+
3+
С помощью данного cli-инструмента можно быстро заменить компоненты из arui-feather на компоненты из core-components.
4+
5+
На данный момент, для замены доступны следуюшие компоненты из arui-feather:
6+
7+
- Heading
8+
- Paragraph
9+
- Label
10+
11+
### Использование
12+
13+
1. Установить к себе на проект:
14+
15+
```bash
16+
$ yarn add --dev @alfalab/core-components-codemod
17+
```
18+
19+
2. Заменить компоненты:
20+
21+
Можно заменить сразу все доступные компоненты:
22+
23+
```bash
24+
$ ./node_modules/.bin/core-components-codemod src/**/*.tsx
25+
```
26+
27+
Можно заменять компоненты частично. Например только Label и Paragraph:
28+
29+
```bash
30+
$ ./node_modules/.bin/core-components-codemod --components=Label,Paragraph src/**/*.tsx
31+
```
32+
33+
В большинстве случаев можно заменить один компонент на другой и однозначно поменять ему пропсы. Но это не всегда возможно. В таких случаях вы увидите предупреждение, и должны будете руками поменять пропсы у компонента.
34+
35+
Сейчас замена компонентов доступна только для кода, написанного на `typescript`. Если кому-то нужно мигрировать с `js` - дайте знать, докрутим.

packages/codemod/bin/index.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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();

packages/codemod/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@alfalab/core-components-codemod",
3+
"version": "1.0.0",
4+
"description": "Codemod tools for migrating from feather to core-components",
5+
"keywords": [],
6+
"license": "MIT",
7+
"scripts": {
8+
"postinstall": "npm i -g jscodeshift @types/jscodeshift lodash > /dev/null 2>&1 || exit 0"
9+
},
10+
"publishConfig": {
11+
"access": "public"
12+
},
13+
"bin": "./bin/index.js",
14+
"dependencies": {
15+
"chalk": "^4.1.1",
16+
"lodash.kebabcase": "^4.1.1",
17+
"shelljs": "^0.8.4",
18+
"yargs": "^17.0.1"
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import Heading from 'arui-feather/heading';
3+
4+
const someVar = false;
5+
6+
export const Component = () => (
7+
<React.Fragment>
8+
<Heading size='xs'>Heading xs</Heading>
9+
<Heading size='s' hasDefaultMargins={true}>Heading s</Heading>
10+
<Heading size='m'>Heading m</Heading>
11+
<Heading size='l'>Heading l</Heading>
12+
<Heading size='xl'>Heading xl</Heading>
13+
<Heading className='custom' data-test-id='test-id' id='id' hasDefaultMargins={false}>Heading</Heading>
14+
<Heading size={someVar ? 'xs' : 'l'}>Heading</Heading>
15+
</React.Fragment>
16+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import { Typography } from '@alfalab/core-components/typography';
3+
4+
const someVar = false;
5+
6+
export const Component = () => (
7+
<React.Fragment>
8+
<Typography.TitleResponsive view='xsmall' tag='h5' defaultMargins={true} font='system' color='primary'>Heading xs</Typography.TitleResponsive>
9+
<Typography.TitleResponsive view='small' defaultMargins={true} tag='h4' font='system' color='primary'>Heading s</Typography.TitleResponsive>
10+
<Typography.TitleResponsive view='medium' tag='h3' defaultMargins={true} font='system' color='primary'>Heading m</Typography.TitleResponsive>
11+
<Typography.TitleResponsive view='large' tag='h2' defaultMargins={true} font='system' color='primary'>Heading l</Typography.TitleResponsive>
12+
<Typography.TitleResponsive view='xlarge' tag='h1' defaultMargins={true} font='system' color='primary'>Heading xl</Typography.TitleResponsive>
13+
<Typography.TitleResponsive className='custom' dataTestId='test-id' id='id' view='xlarge' tag='h1' font='system' color='primary'>Heading</Typography.TitleResponsive>
14+
<Typography.TitleResponsive view={someVar ? 'xs' : 'l'} defaultMargins={true} font='system' color='primary'>Heading</Typography.TitleResponsive>
15+
</React.Fragment>
16+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// eslint-disable-next-line prefer-destructuring
2+
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
3+
4+
jest.autoMockOff();
5+
6+
defineTest(__dirname, 'transform', null, 'transform', { parser: 'tsx' });

0 commit comments

Comments
 (0)