Skip to content

Commit

Permalink
feat: support zero length folder name for pure conditional wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Oct 15, 2019
1 parent 82932da commit 87cd4cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/write-project/2-filter-by-features.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const applicable = require('../applicable');
const path = require('path');
const FILTERED_FILE = /^(.+)__if_(.+)$/;
const FILTERED_FILE = /^(.*)__if_(.+)$/;

// filter by features on some-file.ext__if_sass_or_less
// support nested condition on every folder level
Expand All @@ -20,9 +20,15 @@ module.exports = function(features) {
const cleanPart = match[1];
const condition = match[2];

if (cleanPart === '' && i === ii - 1) {
throw new Error('cannot have an empty filename before __if_: ' + file.relative);
}

if (applicable(features, condition)) {
// Remove __if_x from the part.
cleanParts.push(cleanPart);
if (cleanPart !== '') {
cleanParts.push(cleanPart);
}
} else {
// Not applicable, skip this file
return;
Expand Down
23 changes: 23 additions & 0 deletions test/write-project/2-filter-by-features.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ test('filterByFeatures rejects file failed condition', t => {
t.falsy(newFile);
});

test('filterByFeatures rejects empty file name', t => {
const file = new Vinyl({
cwd: '/',
base: '/test/',
path: '/test/__if_c',
contents: Buffer.from('abc')
});
t.throws(() => filter(file));
});

test('filterByFeatures picks folder meets condition', t => {
const file = new Vinyl({
cwd: '/',
Expand All @@ -54,6 +64,19 @@ test('filterByFeatures picks folder meets condition', t => {
t.is(newFile.contents.toString(), 'abc');
});

test('filterByFeatures supports zero length folder', t => {
const file = new Vinyl({
cwd: '/',
base: '/test/',
path: '/test/__if_b/file.any__if_a',
contents: Buffer.from('abc')
});
const newFile = filter(file);
t.is(newFile.basename, 'file.any');
t.is(newFile.path.replace(/\\/g, '/'), '/test/file.any');
t.is(newFile.contents.toString(), 'abc');
});

test('filterByFeatures rejects folder failed condition', t => {
const file = new Vinyl({
cwd: '/',
Expand Down

0 comments on commit 87cd4cb

Please sign in to comment.