3 Commits
Author SHA1 Message Date
Dawid KarabinandGajus Kuizinas bf45925380 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"]}
2017-03-13 20:42:46 +00:00
Guillaume BoddaertandGajus Kuizinas 5fea5e63d7 feat: add --extension/-x parameter overwrite default .js extension parser (#35) 2017-01-30 09:56:50 +00:00
Guillaume BoddaertandGajus Kuizinas 7353f04394 docs: apply current export format in README after 2.0.0 release (#34) 2017-01-29 14:05:09 +00:00
28 changed files with 341 additions and 50 deletions
+41 -7
View File
@@ -36,8 +36,8 @@ This created `index.js` with:
```js ```js
// @create-index // @create-index
export bar from './bar.js'; export { default as bar } from './bar.js';
export foo from './foo.js'; export { default as foo } from './foo.js';
``` ```
@@ -66,9 +66,9 @@ This have updated `index.js` file:
```js ```js
// @create-index // @create-index
export bar from './bar.js'; export { default as bar } from './bar.js';
export baz from './baz.js'; export { default as baz } from './baz.js';
export foo from './foo.js'; export { default as foo } from './foo.js';
``` ```
@@ -177,8 +177,8 @@ Given the above directory contents, `./index.js` will be:
```js ```js
// @create-index // @create-index
import bar from './bar'; import { default as bar } from './bar';
import foo from './foo.js'; import { default as foo } from './foo.js';
export { export {
bar, bar,
@@ -193,3 +193,37 @@ Directories that do not have `./index.js` in themselves will be excluded.
When run again, `create-index` will update existing `./index.js` if it starts with `// @create-index\n\n`. When run again, `create-index` will update existing `./index.js` if it starts with `// @create-index\n\n`.
If `create-index` is executed against a directory that contains `./index.js`, which does not start with `// @create-index\n\n`, an error will be thrown. If `create-index` is executed against a directory that contains `./index.js`, which does not start with `// @create-index\n\n`, an error will be thrown.
## Ignore files on `--update`
`create-index` can ignore files in a directory if `./index.js` contains special object with defined `ignore` property which takes `an array` of `regular expressions` defined as `strings`, e.g.
```js
> cat index.js
// @create-index {"ignore": ["/baz.js$/"]}
```
```js
> tree ./
./
├── bar.js
├── baz.js
├── foo.js
└── index.js
0 directories, 4 files
```
Given the above directory contents, after running `create-index` with `--update` flag, `./index.js` will be:
```js
// @create-index {"ignore": ["/baz.js$/"]}
import { default as bar } from './bar.js';
import { default as foo } from './foo.js';
export {
bar,
foo
};
```
+10
View File
@@ -37,12 +37,22 @@ const argv = yargs
type: 'string' type: 'string'
} }
}) })
.options({
extensions: {
alias: 'x',
default: ['js'],
description: 'Allows some extensions to be parsed as valid source. First extension will always be preferred to homonyms with another allowed extension.',
type: 'array'
}
})
.example('create-index ./src ./src/utilities', 'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.') .example('create-index ./src ./src/utilities', 'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.')
.example('create-index --update ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.') .example('create-index --update ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
.example('create-index ./src --extensions js jsx', 'Creates or updates an existing create-index index file in the target (./src) directory for both .js and .jsx extensions.')
.argv; .argv;
writeIndexCli(argv._, { writeIndexCli(argv._, {
banner: argv.banner, banner: argv.banner,
extensions: argv.extensions,
ignoreUnsafe: argv.ignoreUnsafe, ignoreUnsafe: argv.ignoreUnsafe,
recursive: argv.recursive, recursive: argv.recursive,
updateIndex: argv.update updateIndex: argv.update
+2
View File
@@ -0,0 +1,2 @@
// eslint-disable-next-line
export const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/;
+7 -1
View File
@@ -24,8 +24,10 @@ const buildExportBlock = (files) => {
export default (filePaths, options = {}) => { export default (filePaths, options = {}) => {
let code; let code;
let configCode;
code = ''; code = '';
configCode = '';
if (options.banner) { if (options.banner) {
const banners = _.isArray(options.banner) ? options.banner : [options.banner]; const banners = _.isArray(options.banner) ? options.banner : [options.banner];
@@ -37,7 +39,11 @@ export default (filePaths, options = {}) => {
code += '\n'; 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) { if (filePaths.length) {
const sortedFilePaths = filePaths.sort(); const sortedFilePaths = filePaths.sort();
+14
View File
@@ -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;
}
};
+1 -3
View File
@@ -2,8 +2,6 @@ import chalk from 'chalk';
import moment from 'moment'; import moment from 'moment';
export default (...append) => { export default (...append) => {
/* eslint-disable no-console */ // eslint-disable-next-line
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append); console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
/* eslint-enable no-console */
}; };
+60 -18
View File
@@ -1,20 +1,9 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import _ from 'lodash'; import _ from 'lodash';
import hasIndex from './hasIndex';
import validateTargetDirectory from './validateTargetDirectory'; 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 hasNoExtension = (fileName) => {
const matches = fileName.match(/\./g); const matches = fileName.match(/\./g);
@@ -31,19 +20,69 @@ const isSafeName = (fileName) => {
return /^[a-z][a-z0-9._]+$/i.test(fileName); return /^[a-z][a-z0-9._]+$/i.test(fileName);
}; };
const removeDuplicates = (files) => { const stripExtension = (fileName) => {
const pos = fileName.lastIndexOf('.');
if (pos === -1) {
return fileName;
}
return fileName.substr(0, pos);
};
const removeDuplicates = (files, preferredExtension) => {
return _.filter(files, (fileName) => { return _.filter(files, (fileName) => {
return !_.includes(files, fileName + '.js'); const withoutExtension = stripExtension(fileName);
const mainAlternative = withoutExtension + '.' + preferredExtension;
if (mainAlternative === fileName) {
return true;
}
return !_.includes(files, mainAlternative);
});
};
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 = {}) => { export default (directoryPath, options = {}) => {
let children;
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) { if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
return false; return false;
} }
const {
extensions = ['js'],
config = {}
} = options;
let children;
children = fs.readdirSync(directoryPath); children = fs.readdirSync(directoryPath);
children = _.filter(children, (fileName) => { children = _.filter(children, (fileName) => {
@@ -66,7 +105,9 @@ export default (directoryPath, options = {}) => {
return false; return false;
} }
if (!isDirectory && !_.endsWith(fileName, '.js')) { if (!isDirectory && !extensions.some((ext) => {
return _.endsWith(fileName, '.' + ext);
})) {
return false; return false;
} }
@@ -77,7 +118,8 @@ export default (directoryPath, options = {}) => {
return true; return true;
}); });
children = removeDuplicates(children); children = removeDuplicates(children, extensions[0]);
children = removeIgnoredFiles(children, config.ignore);
return children.sort(); return children.sort();
}; };
+32
View File
@@ -0,0 +1,32 @@
import fs from 'fs';
import path from 'path';
import hasIndex from './hasIndex';
import {CREATE_INDEX_PATTERN} from './constants';
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 = typeof found[1] === 'string' ? found[1].trim() : '';
if (configLine.length === 0) {
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;
};
+2 -1
View File
@@ -1,5 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import {CREATE_INDEX_PATTERN} from './constants';
export default (targetDirectory, options = {}) => { export default (targetDirectory, options = {}) => {
const silent = options.silent; const silent = options.silent;
@@ -33,7 +34,7 @@ export default (targetDirectory, options = {}) => {
const indexFile = fs.readFileSync(indexFilePath, 'utf8'); const indexFile = fs.readFileSync(indexFilePath, 'utf8');
if (!indexFile.match(/(?:^|[\n\r]+)\/\/ @create-index[\n\r]+/)) { if (!indexFile.match(CREATE_INDEX_PATTERN)) {
if (silent) { if (silent) {
return false; return false;
} else { } else {
+5 -2
View File
@@ -4,6 +4,7 @@ import _ from 'lodash';
import createIndexCode from './createIndexCode'; import createIndexCode from './createIndexCode';
import validateTargetDirectory from './validateTargetDirectory'; import validateTargetDirectory from './validateTargetDirectory';
import readDirectory from './readDirectory'; import readDirectory from './readDirectory';
import readIndexConfig from './readIndexConfig';
import sortByDepth from './sortByDepth'; import sortByDepth from './sortByDepth';
export default (directoryPaths, options = {}) => { export default (directoryPaths, options = {}) => {
@@ -13,8 +14,10 @@ export default (directoryPaths, options = {}) => {
}); });
_.forEach(sortedDirectoryPaths, (directoryPath) => { _.forEach(sortedDirectoryPaths, (directoryPath) => {
const siblings = readDirectory(directoryPath); const config = readIndexConfig(directoryPath);
const indexCode = createIndexCode(siblings); const optionsWithConfig = Object.assign({}, options, {config});
const siblings = readDirectory(directoryPath, optionsWithConfig);
const indexCode = createIndexCode(siblings, {config});
const indexFilePath = path.resolve(directoryPath, 'index.js'); const indexFilePath = path.resolve(directoryPath, 'index.js');
fs.writeFileSync(indexFilePath, indexCode); fs.writeFileSync(indexFilePath, indexCode);
+11 -2
View File
@@ -5,6 +5,7 @@ import chalk from 'chalk';
import createIndexCode from './createIndexCode'; import createIndexCode from './createIndexCode';
import validateTargetDirectory from './validateTargetDirectory'; import validateTargetDirectory from './validateTargetDirectory';
import readDirectory from './readDirectory'; import readDirectory from './readDirectory';
import readIndexConfig from './readIndexConfig';
import sortByDepth from './sortByDepth'; import sortByDepth from './sortByDepth';
import log from './log'; import log from './log';
import findIndexFiles from './findIndexFiles'; import findIndexFiles from './findIndexFiles';
@@ -20,6 +21,7 @@ export default (directoryPaths, options = {}) => {
} else { } else {
log('Recursive:', options.recursive ? chalk.green('true') : chalk.red('false')); log('Recursive:', options.recursive ? chalk.green('true') : chalk.red('false'));
log('Ignore unsafe:', options.ignoreUnsafe ? chalk.green('true') : chalk.red('false')); log('Ignore unsafe:', options.ignoreUnsafe ? chalk.green('true') : chalk.red('false'));
log('Extensions:', chalk.green(options.extensions));
} }
if (options.updateIndex || options.recursive) { if (options.updateIndex || options.recursive) {
@@ -43,10 +45,17 @@ export default (directoryPaths, options = {}) => {
_.forEach(sortedDirectoryPaths, (directoryPath) => { _.forEach(sortedDirectoryPaths, (directoryPath) => {
let existingIndexCode; let existingIndexCode;
const siblings = readDirectory(directoryPath, {silent: options.ignoreUnsafe}); const config = readIndexConfig(directoryPath);
const siblings = readDirectory(directoryPath, {
config,
extensions: options.extensions,
silent: options.ignoreUnsafe
});
const indexCode = createIndexCode(siblings, { const indexCode = createIndexCode(siblings, {
banner: options.banner banner: options.banner,
config
}); });
const indexFilePath = path.resolve(directoryPath, 'index.js'); const indexFilePath = path.resolve(directoryPath, 'index.js');
+16
View File
@@ -51,6 +51,22 @@ export { default as foo } from './foo.js';
expect(indexCode).to.equal(codeExample(` expect(indexCode).to.equal(codeExample(`
// @create-index // @create-index
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 bar } from './bar';
export { default as foo } from './foo'; 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';
+39
View File
@@ -49,6 +49,45 @@ describe('readDirectory()', () => {
expect(names).to.deep.equal(['present.js']); expect(names).to.deep.equal(['present.js']);
}); });
}); });
context('target directory contains non js files, and not configured to allow that', () => {
it('prefers file', () => {
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'));
expect(names).to.deep.equal(['present.js']);
});
});
context('target directory contains non js files, and allowing only jsx', () => {
it('prefers file', () => {
const options = { extensions: ['jsx'] };
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
expect(names).to.deep.equal(['bar.jsx']);
});
});
context('target directory contains non js files, and allowing both js and jsx', () => {
it('prefers file', () => {
const options = { extensions: ['js', 'jsx'] };
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
expect(names).to.deep.equal(['bar.jsx', 'present.js']);
});
});
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JS as it is first extension listed', () => {
it('prefers file', () => {
const options = { extensions: ['js', 'jsx'] };
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
expect(names).to.deep.equal(['bar.js', 'present.js']);
});
});
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JSX as it is first extension listed', () => {
it('prefers file', () => {
const options = { extensions: ['jsx', 'js'] };
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
expect(names).to.deep.equal(['bar.jsx', 'present.js']);
});
});
context('target directory contains files with no extension', () => { context('target directory contains files with no extension', () => {
it('ignores files', () => { it('ignores files', () => {
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension')); const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
+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);
});
});
});
+32 -10
View File
@@ -8,28 +8,50 @@ import {
import writeIndex from '../src/utilities/writeIndex'; import writeIndex from '../src/utilities/writeIndex';
import codeExample from './codeExample'; 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'); const fixturesPath = path.resolve(__dirname, 'fixtures/write-index');
describe('writeIndex()', () => { describe('writeIndex()', () => {
it('creates index in target directory', () => { it('creates index in target directory', () => {
const indexFilePath = path.resolve(fixturesPath, 'mixed/index.js'); const indexFilePath = path.resolve(fixturesPath, 'mixed/index.js');
try { removeFile(indexFilePath);
fs.unlinkSync(indexFilePath);
// eslint-disable-next-line no-empty
} catch (error) {
}
writeIndex([path.resolve(fixturesPath, 'mixed')]); writeIndex([path.resolve(fixturesPath, 'mixed')]);
const indexCode = readFile(indexFilePath);
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
expect(indexCode).to.equal(codeExample(` expect(indexCode).to.equal(codeExample(`
// @create-index // @create-index
export { default as bar } from './bar'; 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'; export { default as foo } from './foo.js';
`)); `));
}); });