Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order for plugin set with config and cli #319

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"build": "rimraf lib && babel src -d lib",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pretest": "npm run build",
"test": "nyc ava test/test-cli.js --timeout=1m --verbose"
"test": "nyc ava test/test-*.js --timeout=1m --verbose"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I once again forgot to return 👍

},
"files": [
"lib/"
Expand Down
26 changes: 25 additions & 1 deletion src/cfg-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,31 @@ export default ({input, flags = {}}) => {
}

if (use) {
use = [].concat(use).reduce((cfg, key) => mergeOptions(cfg, {plugins: {[key]: flags[toCamelCase(key)] || {}}}), {});
const configPluginOptions = config?.plugins ?? {};

// Plugins defined via CLI options take precedence over the ones from config file.
use = [].concat(use).reduce((cfg, name) => {
let cliOptions = flags[toCamelCase(name)];
let configOptions = configPluginOptions[name];
// We merge this way because options can be both strings and objects.
const merged = mergeOptions({ [name]: configOptions }, { [name]: cliOptions || {}});
// Assigning as we loop `use` makes sure that the order in cfg.plugins is correct.
cfg.plugins[name] = merged[name];
if (configOptions) {
delete configPluginOptions[name];
}
return cfg;
}, { plugins: {} });

// Add the remaining plugins if there is any.
if (config && config.plugins) {
for (let name in configPluginOptions) {
use.plugins[name] = configPluginOptions[name];
}
// Now all the plugins are in `use.plugins`.
// Delete `config.plugins` for correct merging later: mergeOptions(config, {...}, use)
delete config.plugins;
}
}

if (!config && !use) {
Expand Down
6 changes: 3 additions & 3 deletions test/expected/output-bem.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<span class="MadTeaParty myComponent">
<span class="MadTeaParty--march-title myTitle">Super Title</span>
<span class="MadTeaParty--march-text myText">Awesome Text</span>
<span class="myComponent MadTeaParty">
<span class="myTitle MadTeaParty--march-title">Super Title</span>
<span class="myText MadTeaParty--march-text">Awesome Text</span>
</span>
12 changes: 12 additions & 0 deletions test/test-cfg-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ test('should return config when CLI output param priority', t => {

t.deepEqual(config.output, expected);
});

test('should resolve plugins set via config and stdin (use) in order', async t => {
const input = 'input.html';
const flags = {
use: ['posthtml-d', 'posthtml-bem'],
posthtmlBem: { foo: 'after' },
posthtmlD: { bar: 'before' },
config: 'test/config/.config-plugins'
};
const config = cfgResolve({input, flags});
t.deepEqual(Object.keys(config.plugins), ['posthtml-d', 'posthtml-bem']);
});
28 changes: 14 additions & 14 deletions test/test-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ test('Check version', async t => {
t.is(stdout, version);
});

test('Transform html witch config in package.json', async t => {
test('Transform html with config in package.json', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, ['test/fixtures/input.html', '-o', filename]);
t.true(await pathExists(filename));
t.is((await read('test/expected/output-config-pkg.html')), (await read(filename)));
});

test('Transform html witch indent', async t => {
test('Transform html with indent', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, ['test/fixtures/input-indent.html', '-o', filename]);
t.true(await pathExists(filename));
t.is((await read('test/expected/output-indent.html')), (await read(filename)));
});

test('Transform html witch config in file', async t => {
test('Transform html with config in file', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/config.json']);
t.true(await pathExists(filename));
t.is((await read('test/expected/output-config-file.html')), (await read(filename)));
});

test('Transform html witch dot config in file', async t => {
test('Transform html with dot config in file', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/.config']);
Expand All @@ -65,7 +65,7 @@ test('Transform html from two file', async t => {
t.is((await read('test/expected/output-indent.html')), (await read(`${folder}/input-indent.html`)));
});

// test('Transform html witch options replace', async t => {
// test('Transform html with options replace', async t => {
// t.plan(2);
// const folder = await tempfile();
// await copy(['test/fixtures/input.html', 'test/fixtures/input-indent.html'], folder);
Expand All @@ -74,7 +74,7 @@ test('Transform html from two file', async t => {
// t.is((await read('test/expected/output-indent.html')), (await read(`${folder}/input-indent.html`)));
// });

test('Transform html witch config in file and stdin options use', async t => {
test('Transform html with config in file and stdin options use', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, [
Expand All @@ -94,7 +94,7 @@ test('Transform html witch config in file and stdin options use', async t => {
t.is((await read('test/expected/output-bem.html')), (await read(filename)));
});

test('Transform html witch stdin options use', async t => {
test('Transform html with stdin options use', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, [
Expand All @@ -110,29 +110,29 @@ test('Transform html witch stdin options use', async t => {
t.is((await read('test/expected/output-custom-elements.html')), (await read(filename)));
});

test('Transform html witch stdin options use two key', async t => {
test('Transform html with stdin options use two key', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, [
'test/fixtures/input-bem.html',
'-o',
filename,
'-u',
'posthtml-custom-elements',
'--posthtml-custom-elements.defaultTag',
'span',
'-u',
'posthtml-bem',
'--posthtml-bem.elemPrefix=--',
'--posthtml-bem.modPrefix',
'_',
'--posthtml-bem.modDlmtr'
'--posthtml-bem.modDlmtr',
'-u',
'posthtml-custom-elements',
'--posthtml-custom-elements.defaultTag',
'span'
]);
t.true(await pathExists(filename));
t.is((await read('test/expected/output-bem.html')), (await read(filename)));
});

test('Transform html stdin options use witch modules', async t => {
test('Transform html stdin options use with modules', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, [
Expand Down