Skip to content

Commit

Permalink
Add configTransform option
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 8, 2016
1 parent b54cc2d commit 2978f5c
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 5 deletions.
47 changes: 47 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ authors.
* [options.plugins](#optionsplugins)
* [options.pluginPrefix](#optionspluginprefix)
* [options.presetPrefix](#optionspresetprefix)
* [options.configTransform](#optionsconfigtransform)
* [options.injectedPlugins](#optionsinjectedplugins)
* [options.color](#optionscolor)
* [options.silent](#optionssilent)
Expand Down Expand Up @@ -872,6 +873,52 @@ Where `package.json` contains:
}
```

## `options.configTransform`

Want configuration files in a different format? Pass a `configTransform`
function. It will be invoked with the parsed value from configuration
files and should return a config object (with `presets`, `plugins`,
`settings`, and/or `output`).

* Type: `Function`, optional.

###### Example

The following example processes `readme.md` and loads options from
`custom` (from a `package.json`). `configTransform` is invoked with
those options and transforms it to configuration **unified-engine**
understands.

```js
var engine = require('unified-engine');
var remark = require('remark');

engine({
processor: remark(),
globs: ['readme.md'],
packageField: 'custom',
configTransform: function (options) {
return {
output: (options || {}).generate
}
}
}, function (err) {
if (err) throw err;
});
```

Where `package.json` contains:

```json
{
"name": "foo",
"private": true,
"custom": {
"generate": true
}
}
```

## `options.injectedPlugins`

Already loaded plug-ins to attach with their options to the processor
Expand Down
16 changes: 12 additions & 4 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ function merge(target, configuration, settings, source, options) {
* @param {string} filePath - File location.
* @return {Object} - Parsed JSON.
*/
function load(filePath, packageField) {
function load(filePath, options) {
var configuration = {};
var ext = extname(filePath);
var packageField = options.packageField;
var configTransform = options.configTransform;
var doc;

try {
Expand All @@ -227,6 +229,10 @@ function load(filePath, packageField) {
throw err;
}

if (configTransform) {
configuration = configTransform(configuration);
}

return configuration;
}

Expand Down Expand Up @@ -257,7 +263,7 @@ function optional(config, filePath, settings) {
}

function required(config, filePath, settings, options) {
merge(config, load(filePath, settings.packageField), {
merge(config, load(filePath, settings), {
packageField: settings.packageField,
pluginPrefix: settings.pluginPrefix,
presetPrefix: settings.presetPrefix,
Expand Down Expand Up @@ -339,8 +345,9 @@ function Configuration(settings) {
rcPath = path.resolve(settings.cwd, rcPath);
this.rcPath = rcPath;

/* Load it so we can fail early. */
load(rcPath, settings.packageField);
/* Load it so we can fail early (we don’t do anything with
* the return value though). */
load(rcPath, settings);
}
}

Expand All @@ -360,6 +367,7 @@ function getConfiguration(filePath, callback) {
var configuration = self.cache[directory];
var options = {
rcName: settings.rcName,
configTransform: settings.configTransform,
packageField: settings.packageField,
detectConfig: settings.detectConfig,
pluginPrefix: settings.pluginPrefix,
Expand Down
1 change: 1 addition & 0 deletions lib/file-set-pipeline/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function configure(context, settings) {
detectConfig: settings.detectConfig,
rcName: settings.rcName,
rcPath: settings.rcPath,
configTransform: settings.configTransform,
packageField: settings.packageField,
settings: settings.settings,
plugins: settings.plugins,
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function run(options, callback) {
settings.rcPath = options.rcPath || null;
settings.packageField = options.packageField || null;
settings.settings = options.settings || {};
settings.configTransform = options.configTransform;

/* Ignore. */
detectIgnore = options.detectIgnore;
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ when done.
— When given, optional prefix to use when searching for plug-ins.
* [`presetPrefix`][preset-prefix] (`string`, optional)
— When given, optional prefix to use when searching for presets.
* [`configTransform`][config-transform] (`Function`, optional)
— Transform config files from a different schema.
* [`injectedPlugins`][injected-plugins] (`Array`, optional)
— List of loaded plug-ins to use.
* [`color`][color] (`boolean`, default: `false`)
Expand Down Expand Up @@ -245,6 +247,8 @@ files work.

[preset-prefix]: doc/options.md#optionspresetprefix

[config-transform]: doc/options.md#optionsconfigtransform

[plugins]: doc/options.md#optionsplugins

[injected-plugins]: doc/options.md#optionsinjectedplugins
Expand Down
89 changes: 89 additions & 0 deletions test/configuration-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @author Titus Wormer
* @copyright 2016 Titus Wormer
* @license MIT
* @module unified-engine
* @fileoverview Test suite for `unified-engine`.
*/

'use strict';

/* Dependencies. */
var path = require('path');
var test = require('tape');
var noop = require('./util/noop-processor');
var spy = require('./util/spy');
var engine = require('..');

/* Methods. */
var join = path.join;

/* Constants. */
var fixtures = join(__dirname, 'fixtures');

/* Tests. */
test('`configTransform`', function (t) {
t.plan(1);

t.test('should work', function (st) {
var stderr = spy();

st.plan(7);

engine({
processor: noop.use(function (processor) {
processor.t = st;
}),
streamError: stderr.stream,
cwd: join(fixtures, 'config-transform'),
globs: ['.'],
rcName: '.foorc',
packageField: 'foo',
configTransform: configTransform,
extensions: ['txt']
}, function (err, code, result) {
var cache = result.configuration.cache;
var keys = Object.keys(cache);

st.error(err, 'should not fail fatally');
st.equal(code, 0, 'should exit with `0`');
st.equal(keys.length, 1, 'should have one cache entry');

st.deepEqual(
cache[keys[0]].settings,
{
charlie: true,
bravo: true,
alpha: true,
foxtrot: true,
echo: true,
delta: true
},
'should set the correct settings'
);

st.deepEqual(
cache[keys[0]].plugins[join(fixtures, 'config-transform', 'test.js')],
{
package: ['foo', 'bar', 'baz'],
cascade: 5,
script: true,
nestedScript: true
},
'should pass the correct options to plugins'
);

st.equal(
stderr(),
'nested/one.txt: no issues found\n'
);
});

function configTransform(raw) {
return {
settings: raw.options,
plugins: raw.plugs
};
}
});
});
2 changes: 1 addition & 1 deletion test/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test('configuration', function (t) {

st.error(err, 'should not fail fatally');
st.equal(code, 0, 'should exit with `0`');
st.equal(Object.keys(cache).length, 1, 'should have on cache entry');
st.equal(Object.keys(cache).length, 1, 'should have one cache entry');

st.deepEqual(
cache[cwd],
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/config-transform/.foorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"options": {
"alpha": true
},
"plugs": ["./test"]
}
11 changes: 11 additions & 0 deletions test/fixtures/config-transform/.foorc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
options: {
bravo: true
},
plugs: {
'./test': {
script: true,
cascade: 1
}
}
}
8 changes: 8 additions & 0 deletions test/fixtures/config-transform/nested/.foorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"options": {
"delta": true
},
"plugs": [
"./test"
]
}
10 changes: 10 additions & 0 deletions test/fixtures/config-transform/nested/.foorc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports.plugs = {
'./test': {
nestedScript: true,
cascade: 5
}
};

module.exports.options = {
echo: true
};
Empty file.
10 changes: 10 additions & 0 deletions test/fixtures/config-transform/nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"foo": {
"options": {
"foxtrot": true
},
"plugs": {
"./test": null
}
}
}
13 changes: 13 additions & 0 deletions test/fixtures/config-transform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"foo": {
"options": {
"charlie": true
},
"plugs": {
"./test": {
"package": ["foo", "bar", "baz"],
"cascade": 2
}
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/config-transform/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function (processor, options) {
processor.t.deepEqual(
options,
{
script: true,
package: ['foo', 'bar', 'baz'],
nestedScript: true,
cascade: 5
},
'should pass the correct options to plugin `test`'
);
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require('./ignore');
require('./configuration');
require('./configuration-plugins');
require('./configuration-presets');
require('./configuration-transform');
require('./stdin');
require('./output');
require('./tree');
Expand Down

0 comments on commit 2978f5c

Please sign in to comment.