feat: add ignore option (fix #31) (#37)

- Add functionality to ignore files based on provided
regular expressions
- Support writeIndex and writeIndexCli
- in index.js config must be specified as below

    // @create-index {"ignore":["/foo.js"]}
This commit is contained in:
Dawid Karabin
2017-03-13 20:42:46 +00:00
committed by Gajus Kuizinas
parent 5fea5e63d7
commit bf45925380
20 changed files with 257 additions and 39 deletions
+21 -5
View File
@@ -12,7 +12,7 @@ describe('createIndexCode()', () => {
expect(indexCode).to.equal(codeExample(`
// @create-index
`));
`));
});
it('describes a single child', () => {
const indexCode = createIndexCode(['foo']);
@@ -21,7 +21,7 @@ describe('createIndexCode()', () => {
// @create-index
export { default as foo } from './foo';
`));
`));
});
it('describes multiple children', () => {
const indexCode = createIndexCode(['bar', 'foo']);
@@ -31,7 +31,7 @@ export { default as foo } from './foo';
export { default as bar } from './bar';
export { default as foo } from './foo';
`));
`));
});
context('file with extension', () => {
it('removes the extension from the export statement', () => {
@@ -41,7 +41,7 @@ export { default as foo } from './foo';
// @create-index
export { default as foo } from './foo.js';
`));
`));
});
});
context('multiple, unsorted', () => {
@@ -53,7 +53,23 @@ export { default as foo } from './foo.js';
export { default as bar } from './bar';
export { default as foo } from './foo';
`));
`));
});
});
context('with config', () => {
it('should append config', () => {
const config = {
ignore: ['/^zoo/']
};
const indexCode = createIndexCode(['foo', 'bar'], {config});
expect(indexCode).to.equal(codeExample(`
// @create-index {"ignore":["/^zoo/"]}
export { default as bar } from './bar';
export { default as foo } from './foo';
`));
});
});
});
View File
+1
View File
@@ -0,0 +1 @@
// @create-index {"ignore": ["/foo.js$/"]}
@@ -0,0 +1 @@
// @create-index {ignore: 'foo'}
@@ -0,0 +1 @@
// @create-index
View File
View File
+4
View File
@@ -0,0 +1,4 @@
// @create-index {"ignore":["/bar.js$/"]}
export { default as foo } from './foo.js';
+56
View File
@@ -0,0 +1,56 @@
import path from 'path';
import {
expect
} from 'chai';
import readIndexConfig from '../src/utilities/readIndexConfig';
const fixtures = {
noIndex: path.resolve(__dirname, 'fixtures/read-index-config/no-index'),
withConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-config'),
withInvalidConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-invalid-config'),
withoutConfig: path.resolve(__dirname, 'fixtures/read-index-config/without-config')
};
const expectedValues = {
noIndex: {},
withConfig: {
ignore: ['/foo.js$/']
},
withoutConfig: {}
};
describe('readIndexConfig()', () => {
context('When valid config is defined', () => {
it('reads config object', () => {
const config = readIndexConfig(fixtures.withConfig);
expect(config).to.deep.equal(expectedValues.withConfig);
});
});
context('When invalid config is defined', () => {
it('should throw an error', () => {
const wrappedReadIndexConfig = () => {
readIndexConfig(fixtures.withInvalidConfig);
};
expect(wrappedReadIndexConfig).to.throw(/Configuration object must be a valid JSON./);
});
});
context('When config is NOT defined', () => {
it('returns an empty object', () => {
const config = readIndexConfig(fixtures.withoutConfig);
expect(config).to.deep.equal(expectedValues.withoutConfig);
});
});
context('When index file doesn\'t exist', () => {
it('returns an empty object', () => {
const config = readIndexConfig(fixtures.withoutConfig);
expect(config).to.deep.equal(expectedValues.noIndex);
});
});
});
+33 -11
View File
@@ -8,29 +8,51 @@ import {
import writeIndex from '../src/utilities/writeIndex';
import codeExample from './codeExample';
const readFile = (filePath) => {
return fs.readFileSync(filePath, 'utf8');
};
const removeFile = (filePath) => {
fs.unlinkSync(filePath);
};
const appendToFile = (filePath, content) => {
fs.appendFileSync(filePath, content, 'utf-8');
};
const fixturesPath = path.resolve(__dirname, 'fixtures/write-index');
describe('writeIndex()', () => {
it('creates index in target directory', () => {
const indexFilePath = path.resolve(fixturesPath, 'mixed/index.js');
try {
fs.unlinkSync(indexFilePath);
// eslint-disable-next-line no-empty
} catch (error) {
}
removeFile(indexFilePath);
writeIndex([path.resolve(fixturesPath, 'mixed')]);
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
const indexCode = readFile(indexFilePath);
expect(indexCode).to.equal(codeExample(`
// @create-index
export { default as bar } from './bar';
export { default as foo } from './foo.js';
`));
`));
});
it('creates index with config in target directory', () => {
const indexFilePath = path.resolve(fixturesPath, 'with-config/index.js');
// eslint-disable-next-line
const ignoredExportLine = `export { default as bar } from './bar.js';`;
appendToFile(indexFilePath, ignoredExportLine);
expect(readFile(indexFilePath).includes(ignoredExportLine)).to.equal(true);
writeIndex([path.resolve(fixturesPath, 'with-config')]);
const indexCode = readFile(indexFilePath);
expect(indexCode).to.equal(codeExample(`
// @create-index {"ignore":["/bar.js$/"]}
export { default as foo } from './foo.js';
`));
});
});