Skip to content

Commit c7c8bb0

Browse files
committed
Refactor CLI tool to support custom transformations and plugins
1 parent c15d106 commit c7c8bb0

File tree

2 files changed

+85
-36
lines changed

2 files changed

+85
-36
lines changed

README.md

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ CLI tool to refactor [atomizer](https://acss.io/) codebases to [tailwindcss](htt
44

55
![Screen Recording 2023-12-12 at 3 15 36 PM](https://github.com/ShareChat/atomizer-tailwindcss-migrator/assets/141409866/c1af2ba5-5734-48e5-b6a4-780504be10cd)
66

7-
87
## Features
98

109
- Transform atomizer classes to tailwindcss classes
@@ -34,71 +33,121 @@ pnpm
3433
pnpm add @mohalla-tech/atomizer-tailwindcss-migrator -g
3534
```
3635

36+
Now you can run the migrator using `tw-mg` command
37+
38+
## Options
39+
40+
### -h, --help
41+
42+
Show help
43+
44+
### -s, --style - required
45+
46+
Path to atomizer generated css file, it contains all the generated classes
47+
48+
### -t, --target <glob-pattern> - required
49+
50+
Target files to transform, supports glob pattern
51+
52+
### -d, --dry-run
53+
54+
Dry run mode, will only generate report and open it
55+
56+
### -no, --no-open
57+
58+
Do not open report in browser
59+
60+
### -m, --mappings
61+
62+
Path to json file with mappings from atomizer classes to tailwindcss classes, for example we may want to replace `$fzTitle` variable with `title` in tailwindcss variable so any class like `Fz($fzTitle)` will be replaced with `text-title`
63+
64+
Ex:
65+
66+
```json
67+
{
68+
"$fzTitle": "title"
69+
}
70+
```
71+
72+
### -p, --plugins
73+
74+
Path to js file with plugins which will be loaded by the migrator this can be helpful if you want to do some custom transformation
75+
for help on writing plugins see [Writing Plugins](#writing-plugins)
76+
3777
## Usage
3878

39-
for help
79+
if we want to run without any mappings or plugins
4080

4181
```bash
42-
tw-mg -h
82+
tw-mg -s ./path/to/atomizer.css -t ./path/to/target/files
4383
```
4484

45-
Lets say we have our atomizer css class at path `src/styles/main.css` and we want to transform all components under `src/components/atoms/`
85+
with mappings
4686

4787
```bash
48-
tw-mg -s src/styles/main.css -t "src/components/atoms/*.js"
88+
tw-mg -s ./path/to/atomizer.css -t ./path/to/target/files -m ./path/to/mappings.json
4989
```
5090

51-
For dry run -- it will only generate report and open it
91+
with plugins
5292

5393
```bash
54-
tw-mg -s src/styles/main.css -t "src/components/atoms/*.js" --dry-run
94+
tw-mg -s ./path/to/atomizer.css -t ./path/to/target/files -p ./path/to/plugins.js
5595
```
5696

57-
If you want to replace atomizer variables name with other names in tailwind you can pass json file with mapping from atomic var to tailwind var
58-
59-
Eg: mappings.json
97+
with mappings and plugins
6098

61-
```json
62-
{
63-
"fzTitle": "title",
64-
"$someLongCamelCase": "short-snake-case"
65-
}
99+
```bash
100+
tw-mg -s ./path/to/atomizer.css -t ./path/to/target/files -m ./path/to/mappings.json -p ./path/to/plugins.js
66101
```
67102

68-
it will replace var `fzTitle` with `title` and so on.
103+
dry run mode - it will only generate report and open it
69104

70105
```bash
71-
tw-mg -s src/styles/main.css -t "src/components/atoms/*.js" -m 'mappings.json' -d
106+
tw-mg -s ./path/to/atomizer.css -t ./path/to/target/files -d
72107
```
73108

74-
If you do not want to open the report by default
109+
## Example
75110

76-
```bash
77-
tw-mg -s src/styles/main.css -t "src/components/atoms/*.js" -d -no
111+
we have a file `/src/styles/main.css` with atomizer classes
112+
113+
```css
114+
.Fz($fzTitle) {
115+
font-size: $fzTitle;
116+
}
117+
118+
.D\(f\) {
119+
display: flex;
120+
}
121+
122+
.Bgc\(c\) {
123+
background-color: $c;
124+
}
78125
```
79126

80-
after running these a `transform-report.html` will be generated and opened in browser which will contain all the details.
127+
we want to transform all the atomizer classes to tailwindcss classes within `/src/components` directory
81128

82-
## Loading Plugins
129+
```bash
130+
tw-mg -s ./src/styles/main.css -t ./src/components/**/*.jsx -d
131+
```
83132

84-
You can load plugins by passing `-p` or `--plugins` flag
133+
this will generate a report and open it in browser, you can then review the changes and apply them by removing `-d` flag
85134

86135
```bash
87-
tw-mg -s src/styles/main.css -t "src/components/atoms/*.js" -p "plugin.js"
136+
tw-mg -s ./src/styles/main.css -t ./src/components/**/*.jsx
88137
```
89138

90139
## Writing Plugins
91140

92-
Plugin file must export an array of plugins which will be loaded by the migrator.
141+
Plugin file must export an array of plugins which will be loaded by the migrator, plugins must be an object with `name` and `plugin` keys where `name` is the name of the plugin and `plugin` is a function which will be called for each atomizer class with `className` and `mappings` as arguments, `className` is the atomizer class and `mappings` is the mappings object passed to the migrator
93142

94143
```js
95144
module.exports = [
96145
{
97146
name: 'plugin-name',
98-
plugin: function (atomizer, mappings) {
147+
plugin: function (className, mappings) {
99148
// do something with atomizer
100149
// return null in case you want to skip this plugin
101-
return atomizer;
150+
return newClassName;
102151
},
103152
},
104153
];

src/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env node
22

3-
import path from 'path';
4-
import open from 'open';
5-
import { readFile, writeFile } from 'fs/promises';
63
import { Command } from 'commander';
7-
import tranformFile from './lib/replacer.js';
4+
import { readFile, writeFile } from 'fs/promises';
85
import { globSync } from 'glob';
9-
import { extractClasses } from './helpers/index.js';
6+
import { createRequire } from 'node:module';
7+
import open from 'open';
8+
import path from 'path';
109
import generateHTMLReport from './helpers/html.js';
10+
import { extractClasses } from './helpers/index.js';
1111
import loadPlugins from './helpers/plugin.js';
12-
import { createRequire } from 'node:module';
12+
import tranformFile from './lib/replacer.js';
1313

1414
const require = createRequire(import.meta.url);
1515

@@ -29,7 +29,7 @@ program
2929
'-s, --style <file>',
3030
'stylesheet file to use for picking up atomizer classes'
3131
)
32-
.requiredOption('-t, --transform <globPattern>', 'files to transform')
32+
.requiredOption('-t, --target <globPattern>', 'files to transform')
3333
.option('-p, --plugins <file>', 'plugins file to use for custom transforms')
3434
.option('-m, --mappings <file>', 'mappings file to use for custom variables')
3535
.option('-d, --dry-run', 'dry run to see the changes')
@@ -49,8 +49,8 @@ if (options.style) {
4949
styleFile = options.style;
5050
}
5151

52-
if (options.transform) {
53-
filePattern = options.transform;
52+
if (options.target) {
53+
filePattern = options.target;
5454
}
5555

5656
if (options.mappings) {

0 commit comments

Comments
 (0)