Skip to content

Commit 747db58

Browse files
committed
feat: basic transform
1 parent 8f9e5bb commit 747db58

10 files changed

Lines changed: 90 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"trailingComma": "es5"
3737
},
3838
"devDependencies": {
39+
"@types/glob": "^7.2.0",
3940
"@types/node": "^17.0.13",
4041
"dripip": "^0.10.0",
4142
"husky": "^7.0.4",

src/api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import postcss, { Root } from 'postcss';
2+
3+
export interface TransformAPI {
4+
parse(source: string): Root;
5+
}
6+
7+
const parse: TransformAPI['parse'] = source => {
8+
return postcss.parse(source);
9+
};
10+
11+
export const api: TransformAPI = { parse };

src/cli.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,27 @@ const run = async () => {
1414
const cli = cac(`${NAME}`);
1515

1616
cli
17-
.command('[files]', 'File path to transform. Glob patterns are supported.')
17+
.command(
18+
'[files]',
19+
'File path to transform. Note glob patterns are supported but must be wrapped in quotes.'
20+
)
1821
.example(`${NAME} ./a.css`)
1922
.example(`${NAME} ./src/a.css`)
20-
.example(`${NAME} ./src/**/*.css`)
21-
.example(`${NAME} ./**/*.css`)
23+
.example(`${NAME} "./src/**/*.css"`)
24+
.example(`${NAME} "./**/*.css"`)
2225
.option('-t, --transform <transform>', 'Path to the transform file', {
2326
default: './transform.ts',
2427
})
25-
.action(async (files: string[], flags) => {
28+
.action(async (files: string, flags) => {
2629
const { transform } = flags;
2730

2831
await processTransform({ files, transform });
2932
});
3033

3134
cli.help();
3235
cli.version(VERSION);
33-
cli.parse(process.argv, { run: false });
3436

37+
cli.parse(process.argv, { run: false });
3538
await cli.runMatchedCommand();
3639
};
3740

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { traverse, Processors } from './traverse';
2+
export { Transform } from './transform';

src/process-transform.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import glob from 'glob';
2+
import fs from 'fs';
13
import { loadTransform } from './load-transform';
4+
import { TransformFileInfo } from './transform';
5+
import { api } from './api';
26

37
interface ProcessTransformOptions {
48
/**
5-
* The list of file paths to process and run through the transform.
9+
* The file path to process and run through the transform.
610
*/
7-
files: string[];
11+
files: string;
812

913
/**
1014
* The transform path to run each file through.
@@ -13,8 +17,18 @@ interface ProcessTransformOptions {
1317
}
1418

1519
export const processTransform = async (options: ProcessTransformOptions) => {
16-
console.log(options);
1720
const transform = await loadTransform(options.transform);
21+
const files = glob.sync(options.files);
1822

19-
transform();
23+
files.map(file => {
24+
const source = fs.readFileSync(file, 'utf8').toString();
25+
const fileInfo: TransformFileInfo = { path: file, source };
26+
const result = transform(fileInfo, api);
27+
28+
console.log(result);
29+
30+
if (result !== null) {
31+
fs.writeFileSync(file, result);
32+
}
33+
});
2034
};

src/transform.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
interface TransformFileInfo {}
1+
import { TransformAPI } from './api';
22

3-
interface TransformAPI {}
3+
export interface TransformFileInfo {
4+
/**
5+
* The file path for the current file being transformed.
6+
*/
7+
path: string;
8+
9+
/**
10+
* The file contents for the current file being transformed.
11+
*/
12+
source: string;
13+
}
414

515
export type Transform = (
16+
/**
17+
* Metadata for the current file being transformed.
18+
*/
619
fileInfo: TransformFileInfo,
720
api: TransformAPI
821
) => null | string;

test/css/a.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.class {
2+
margin: 42px;
3+
color: blue;
4+
padding-right: 10em;
5+
background-color: rgba(0, 0, 0, 0.2);
6+
}

test/css/b.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.another-selector {
2+
width: 100%;
3+
margin: -16px;
4+
color: green;
5+
}

test/transforms/color-to-red.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
export const t = () => {
2-
console.log('HELLO WORLD TRANSFORM.');
1+
import { Transform } from '../../src';
2+
3+
export const transform: Transform = (fileInfo, api) => {
4+
const root = api.parse(fileInfo.source);
5+
6+
root.walk(node => {
7+
if (node.type === 'decl' && node.prop === 'color') {
8+
node.value = 'red';
9+
}
10+
});
11+
12+
return root.toString();
313
};

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,14 @@
14561456
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
14571457
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
14581458

1459+
"@types/glob@^7.2.0":
1460+
version "7.2.0"
1461+
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
1462+
integrity sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==
1463+
dependencies:
1464+
"@types/minimatch" "*"
1465+
"@types/node" "*"
1466+
14591467
"@types/graceful-fs@^4.1.2":
14601468
version "4.1.5"
14611469
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
@@ -1501,6 +1509,11 @@
15011509
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
15021510
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
15031511

1512+
"@types/minimatch@*":
1513+
version "3.0.5"
1514+
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
1515+
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==
1516+
15041517
"@types/ms@*":
15051518
version "0.7.31"
15061519
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"

0 commit comments

Comments
 (0)