Skip to content

Commit

Permalink
Feat(gajus#31): Add ignore option
Browse files Browse the repository at this point in the history
- This commit add functionality to ignore files
based on provided regexps.
- in index.js config must be specified as below

    // @create-index {"ignore":["/foo.js"]}
  • Loading branch information
hinok committed Mar 12, 2017
1 parent 5fea5e6 commit 02c7c9e
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 38 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"create-index": "node ./dist/bin/create-index ./src/utilities",
"lint": "cross-env NODE_ENV=development eslint ./src ./tests",
"precommit": "npm run test",
"test": "npm run build && npm run lint && cross-env NODE_ENV=development mocha --compilers js:babel-register"
"test": "npm run build && npm run lint && cross-env NODE_ENV=development mocha --compilers js:babel-register",
"test:watch": "cross-env NODE_ENV=development mocha --watch --reporter spec --bail --compilers js:babel-register"
},
"version": "1.1.0"
}
3 changes: 3 additions & 0 deletions src/utilities/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/;

export {CREATE_INDEX_PATTERN};
8 changes: 7 additions & 1 deletion src/utilities/createIndexCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const buildExportBlock = (files) => {

export default (filePaths, options = {}) => {
let code;
let configCode;

code = '';
configCode = '';

if (options.banner) {
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
Expand All @@ -37,7 +39,11 @@ export default (filePaths, options = {}) => {
code += '\n';
}

code += '// @create-index\n\n';
if (options.config && _.size(options.config) > 0) {
configCode += ' ' + JSON.stringify(options.config);
}

code += '// @create-index' + configCode + '\n\n';

if (filePaths.length) {
const sortedFilePaths = filePaths.sort();
Expand Down
14 changes: 14 additions & 0 deletions src/utilities/hasIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from 'fs';
import path from 'path';

export default (directoryPath) => {
const indexPath = path.resolve(directoryPath, 'index.js');

try {
fs.statSync(indexPath);

return true;
} catch (error) {
return false;
}
};
4 changes: 1 addition & 3 deletions src/utilities/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import chalk from 'chalk';
import moment from 'moment';

export default (...append) => {
/* eslint-disable no-console */
/* eslint-disable no-console */
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);

/* eslint-enable no-console */
};
52 changes: 37 additions & 15 deletions src/utilities/readDirectory.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import fs from 'fs';
import path from 'path';
import _ from 'lodash';
import hasIndex from './hasIndex';
import validateTargetDirectory from './validateTargetDirectory';

const hasIndex = (directoryPath) => {
const indexPath = path.resolve(directoryPath, 'index.js');

try {
fs.statSync(indexPath);

return true;
} catch (error) {
return false;
}
};

const hasNoExtension = (fileName) => {
const matches = fileName.match(/\./g);

Expand Down Expand Up @@ -54,15 +43,47 @@ const removeDuplicates = (files, preferredExtension) => {
});
};

export default (directoryPath, options = {}) => {
let children;
const removeIgnoredFiles = (files, ignorePatterns = []) => {
if (ignorePatterns.length === 0) {
return files;
}

const patterns = ignorePatterns.map((pattern) => {
if (_.startsWith(pattern, '/') && _.endsWith(pattern, '/')) {
const patternWithoutSlashes = pattern.slice(1, -1);

return new RegExp(patternWithoutSlashes);
}

return new RegExp(pattern);
});

return _.filter(files, (fileName) => {
let pattern;

for (pattern of patterns) {
if (fileName.match(pattern) !== null) {
return false;
}
}

return true;
});
};

export default (directoryPath, options = {}) => {
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
return false;
}

const {
extensions = ['js'],
config = {}
} = options;

let children;

children = fs.readdirSync(directoryPath);
const {extensions = ['js']} = options;

children = _.filter(children, (fileName) => {
const absolutePath = path.resolve(directoryPath, fileName);
Expand Down Expand Up @@ -98,6 +119,7 @@ export default (directoryPath, options = {}) => {
});

children = removeDuplicates(children, extensions[0]);
children = removeIgnoredFiles(children, config.ignore);

return children.sort();
};
40 changes: 40 additions & 0 deletions src/utilities/readIndexConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from 'fs';
import path from 'path';
import hasIndex from './hasIndex';
import {CREATE_INDEX_PATTERN} from './constants';

const isString = (str) => {
return typeof str === 'string';
};

const isEmptyString = (str) => {
return str.length === 0;
};

export default (directoryPath) => {
if (!hasIndex(directoryPath)) {
return {};
}

const indexPath = path.resolve(directoryPath, 'index.js');
const indexContents = fs.readFileSync(indexPath, 'utf-8');
const found = indexContents.match(CREATE_INDEX_PATTERN);
const configLine = isString(found[1]) ? found[1].trim() : '';

if (isEmptyString(configLine)) {
return {};
}

let config;

try {
config = JSON.parse(configLine);
} catch (error) {
throw new Error(
'"' + indexPath + '" contains invalid configuration object.\n' +
'Configuration object must be a valid JSON.'
);
}

return config;
};
3 changes: 2 additions & 1 deletion src/utilities/validateTargetDirectory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import {CREATE_INDEX_PATTERN} from './constants';

export default (targetDirectory, options = {}) => {
const silent = options.silent;
Expand Down Expand Up @@ -33,7 +34,7 @@ export default (targetDirectory, options = {}) => {

const indexFile = fs.readFileSync(indexFilePath, 'utf8');

if (!indexFile.match(/(?:^|[\n\r]+)\/\/ @create-index[\n\r]+/)) {
if (!indexFile.match(CREATE_INDEX_PATTERN)) {
if (silent) {
return false;
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/utilities/writeIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _ from 'lodash';
import createIndexCode from './createIndexCode';
import validateTargetDirectory from './validateTargetDirectory';
import readDirectory from './readDirectory';
import readIndexConfig from './readIndexConfig';
import sortByDepth from './sortByDepth';

export default (directoryPaths, options = {}) => {
Expand All @@ -13,8 +14,10 @@ export default (directoryPaths, options = {}) => {
});

_.forEach(sortedDirectoryPaths, (directoryPath) => {
const siblings = readDirectory(directoryPath, options);
const indexCode = createIndexCode(siblings);
const config = readIndexConfig(directoryPath);
const optionsWithConfig = Object.assign({}, options, {config});
const siblings = readDirectory(directoryPath, optionsWithConfig);
const indexCode = createIndexCode(siblings, {config});
const indexFilePath = path.resolve(directoryPath, 'index.js');

fs.writeFileSync(indexFilePath, indexCode);
Expand Down
26 changes: 21 additions & 5 deletions test/createIndexCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('createIndexCode()', () => {

expect(indexCode).to.equal(codeExample(`
// @create-index
`));
`));
});
it('describes a single child', () => {
const indexCode = createIndexCode(['foo']);
Expand All @@ -21,7 +21,7 @@ describe('createIndexCode()', () => {
// @create-index
export { default as foo } from './foo';
`));
`));
});
it('describes multiple children', () => {
const indexCode = createIndexCode(['bar', 'foo']);
Expand All @@ -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', () => {
Expand All @@ -41,7 +41,7 @@ export { default as foo } from './foo';
// @create-index
export { default as foo } from './foo.js';
`));
`));
});
});
context('multiple, unsorted', () => {
Expand All @@ -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';
`));
});
});
});
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/read-index-config/with-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// @create-index {"ignore": ["/foo.js$/"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// @create-index {ignore: 'foo'}
1 change: 1 addition & 0 deletions test/fixtures/read-index-config/without-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// @create-index
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions test/fixtures/write-index/with-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @create-index {"ignore":["/bar.js$/"]}

export { default as foo } from './foo.js';

56 changes: 56 additions & 0 deletions test/readIndexConfig.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Loading

0 comments on commit 02c7c9e

Please sign in to comment.