Skip to content

Commit 9dbfdd9

Browse files
committed
feat: CLI will use configuration files now.
1 parent 38d194e commit 9dbfdd9

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

packages/@css-blocks/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"watch": "^1.0.2"
5757
},
5858
"dependencies": {
59+
"@css-blocks/config": "^0.24.0",
5960
"@css-blocks/core": "^0.24.0",
6061
"chalk": "^2.4.2",
6162
"debug": "^4.1.1",

packages/@css-blocks/cli/src/index.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { search as searchForConfiguration } from "@css-blocks/config";
12
import {
23
BlockFactory,
4+
Configuration,
35
CssBlockError,
46
ErrorWithPosition,
57
Importer,
@@ -119,7 +121,7 @@ export class CLI {
119121
* Validate the syntax of a list of block files.
120122
*/
121123
async validate(blockFiles: Array<string>, options: ValidateOptions) {
122-
let preprocessors: Preprocessors = options.preprocessors ? require(path.resolve(options.preprocessors)) : {};
124+
let preprocessors: Preprocessors | null = options.preprocessors ? require(path.resolve(options.preprocessors)) : null;
123125
let npm = options.npm || false;
124126
let aliases: Aliases = [];
125127
let aliasList = options.alias || [];
@@ -132,7 +134,21 @@ export class CLI {
132134
if (npm) {
133135
importer = new NodeJsImporter(aliases);
134136
}
135-
let factory = new BlockFactory({preprocessors, importer});
137+
let searchDir: string;
138+
let blockOptions: Partial<Configuration>;
139+
if (blockFiles.length > 0) {
140+
searchDir = path.dirname(path.resolve(blockFiles[0]));
141+
} else {
142+
searchDir = process.cwd();
143+
}
144+
blockOptions = await searchForConfiguration(searchDir) || {};
145+
if (preprocessors) {
146+
blockOptions.preprocessors = preprocessors;
147+
}
148+
if (importer) {
149+
blockOptions.importer = importer;
150+
}
151+
let factory = new BlockFactory(blockOptions);
136152
let errorCount = 0;
137153
for (let blockFile of blockFiles) {
138154
let blockFileRelative = path.relative(process.cwd(), path.resolve(blockFile));

packages/@css-blocks/cli/test/cli-test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,12 @@ Found 1 error in 1 file.
111111
assert.equal(cli.exitCode, 1);
112112
});
113113
});
114+
115+
describe("validate with config file", () => {
116+
it("can check syntax for a valid block file", async () => {
117+
let cli = new CLI();
118+
await cli.run(["validate", fixture("config-file/simple.block.scss")]);
119+
assert.equal(cli.output, `ok\t${relFixture("config-file/simple.block.scss")}\n`);
120+
assert.equal(cli.exitCode, 0);
121+
});
122+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"preprocessors": "../../../dist/test/preprocessors.js"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:scope {
2+
color: red;
3+
background-color: white;
4+
5+
&[dark] {
6+
color: darkred;
7+
background-color: gray;
8+
}
9+
}

packages/@css-blocks/cli/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
}
1010
},
1111
"references": [
12-
{"path": "../core"}
12+
{"path": "../core"},
13+
{"path": "../config"}
1314
],
1415
"include": [
1516
"src",

0 commit comments

Comments
 (0)