Skip to content

Commit 108e47e

Browse files
committed
Flatten options object
Previously relied on some options being with a `config` object and some not. This goes against typical options APIs and was not necessary. This flattens the config object so all properties are at the same level.
1 parent cf01ca3 commit 108e47e

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

bin/suitcss

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

33
var fs = require('fs-extra');
4+
var assign = require('object-assign-deep');
45
var exists = fs.existsSync;
56
var logger = require('./logger');
67
var suitcss = require('..');
@@ -101,17 +102,13 @@ if (regen) {
101102
/**
102103
* Run for the given input and output.
103104
*/
104-
105105
function run () {
106106
read(input, function (err, buffer) {
107107
if (err) logger.throw(err);
108108
var css = buffer.toString();
109+
var opts = assign({}, {minify: program.minify, root: program.importRoot}, config);
109110

110-
suitcss(css, {
111-
minify: program.minify,
112-
root: program.importRoot,
113-
config: config
114-
}).then(function(result) {
111+
suitcss(css, opts).then(function(result) {
115112
if (output) {
116113
writeFileSync(output, result.css + '\n');
117114
} else {

lib/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@ function preprocessor(css, options) {
8686

8787
function mergeOptions(options) {
8888
options = options || {};
89-
options.config = options.config || {};
9089

91-
var merged = assign({}, defaults, options.config);
90+
var merged = assign({}, defaults, options);
9291

9392
// Set some core options
94-
merged.minify = options.minify;
95-
merged['postcss-import'].root = options.root;
93+
merged['postcss-import'].root = merged.root;
9694

9795
// Call beforeLint function and pass processed css to bem-linter
98-
var beforeLint = options.beforeLint;
96+
var beforeLint = merged.beforeLint;
9997
merged['postcss-import'].transform = function(css, filename) {
10098
if (typeof beforeLint === 'function') {
10199
css = beforeLint(css, filename, merged);
@@ -105,9 +103,9 @@ function mergeOptions(options) {
105103

106104
// Allow additional plugins to be merged with the defaults
107105
// but remove any duplicates so that it respects the new order
108-
if (!isEmpty(options.config.use)) {
109-
var dedupedPlugins = difference(merged.use, options.config.use);
110-
merged.use = dedupedPlugins.concat(options.config.use);
106+
if (!isEmpty(options.use)) {
107+
var dedupedPlugins = difference(merged.use, options.use);
108+
merged.use = dedupedPlugins.concat(options.use);
111109
}
112110

113111
return merged;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"fs-extra": "^0.26.2",
2020
"lodash.difference": "^3.2.2",
2121
"lodash.isempty": "^3.0.4",
22+
"object-assign": "^4.0.1",
2223
"object-assign-deep": "0.0.4",
2324
"pad-component": "0.0.1",
2425
"postcss": "^5.0.12",

test/test.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
2+
beforeLint: function (css) {
3+
console.log('beforeLint ran');
4+
return css;
5+
},
26
use: [
37
"postcss-property-lookup"
48
]

test/test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ describe('suitcss', function () {
6060
var autoprefixer = {browsers: ['> 1%', 'IE 7'], cascade: false};
6161
var opts = mergeOptions({
6262
root: 'test/root',
63-
config: {
64-
autoprefixer: autoprefixer
65-
}
63+
autoprefixer: autoprefixer
6664
});
6765

6866
expect(opts.use).to.eql([
@@ -80,9 +78,7 @@ describe('suitcss', function () {
8078
describe('re-ordering plugins', function() {
8179
it('should allow reordering of use array and remove duplicates', function() {
8280
var opts = mergeOptions({
83-
config: {
84-
use: ['autoprefixer', 'postcss-at2x', 'postcss-calc', 'postcss-reporter']
85-
}
81+
use: ['autoprefixer', 'postcss-at2x', 'postcss-calc', 'postcss-reporter']
8682
});
8783

8884
expect(opts.use).to.eql([
@@ -98,9 +94,7 @@ describe('suitcss', function () {
9894

9995
it('should just append plugins if no duplicates are used', function() {
10096
var opts = mergeOptions({
101-
config: {
102-
use: ['postcss-at2x', 'postcss-property-lookup']
103-
}
97+
use: ['postcss-at2x', 'postcss-property-lookup']
10498
});
10599

106100
expect(opts.use).to.eql([
@@ -208,6 +202,7 @@ describe('cli', function () {
208202
var child = exec('bin/suitcss', function (err, stdout) {
209203
if (err) return done(err);
210204
expect(stdout).to.equal(output);
205+
expect(stdout).to.not.contain('beforeLint ran');
211206
done();
212207
});
213208

@@ -219,6 +214,7 @@ describe('cli', function () {
219214
exec('bin/suitcss -v test/fixtures/cli/input.css test/fixtures/cli/output.css', function (err, stdout) {
220215
if (err) return done(err);
221216
expect(stdout).to.contain('write');
217+
expect(stdout).to.not.contain('beforeLint ran');
222218
done();
223219
});
224220
});
@@ -229,6 +225,7 @@ describe('cli', function () {
229225
var res = read('fixtures/cli/output');
230226
var expected = read('fixtures/component.out');
231227
expect(res).to.equal(expected);
228+
expect(stdout).to.not.contain('beforeLint ran');
232229
done();
233230
});
234231
});
@@ -238,6 +235,7 @@ describe('cli', function () {
238235
if (err) return done(err);
239236
var res = read('fixtures/cli/output');
240237
var expected = read('fixtures/minify.out');
238+
expect(stdout).to.not.contain('beforeLint ran');
241239
expect(res).to.equal(expected);
242240
done();
243241
});
@@ -248,6 +246,7 @@ describe('cli', function () {
248246
if (err) return done(err);
249247
var res = read('fixtures/cli/output');
250248
var expected = read('fixtures/config.out');
249+
expect(stdout).to.contain('beforeLint ran');
251250
expect(res).to.equal(expected);
252251
done();
253252
});

0 commit comments

Comments
 (0)