From 5fea5e63d70d7aea989b7b391fd095397bc06433 Mon Sep 17 00:00:00 2001 From: Guillaume Boddaert Date: Mon, 30 Jan 2017 10:56:50 +0100 Subject: [PATCH] feat: add --extension/-x parameter overwrite default .js extension parser (#35) --- src/bin/create-index.js | 10 +++++ src/utilities/readDirectory.js | 28 +++++++++++-- src/utilities/writeIndex.js | 2 +- src/utilities/writeIndexCli.js | 6 ++- .../bar.js | 0 .../bar.jsx | 0 .../present.js | 0 .../children-files-alt-extension/bar.jsx | 0 .../children-files-alt-extension/foo.css | 0 .../children-files-alt-extension/present.js | 0 test/readDirectory.js | 39 +++++++++++++++++++ 11 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/read-directory/children-files-alt-extension-with-homonyms/bar.js create mode 100644 test/fixtures/read-directory/children-files-alt-extension-with-homonyms/bar.jsx create mode 100644 test/fixtures/read-directory/children-files-alt-extension-with-homonyms/present.js create mode 100644 test/fixtures/read-directory/children-files-alt-extension/bar.jsx create mode 100644 test/fixtures/read-directory/children-files-alt-extension/foo.css create mode 100644 test/fixtures/read-directory/children-files-alt-extension/present.js diff --git a/src/bin/create-index.js b/src/bin/create-index.js index c0c5ef7..6ea78e0 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -37,12 +37,22 @@ const argv = yargs 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 --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; writeIndexCli(argv._, { banner: argv.banner, + extensions: argv.extensions, ignoreUnsafe: argv.ignoreUnsafe, recursive: argv.recursive, updateIndex: argv.update diff --git a/src/utilities/readDirectory.js b/src/utilities/readDirectory.js index b9a6d56..8ce8da8 100644 --- a/src/utilities/readDirectory.js +++ b/src/utilities/readDirectory.js @@ -31,9 +31,26 @@ const isSafeName = (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 !_.includes(files, fileName + '.js'); + const withoutExtension = stripExtension(fileName); + const mainAlternative = withoutExtension + '.' + preferredExtension; + + if (mainAlternative === fileName) { + return true; + } + + return !_.includes(files, mainAlternative); }); }; @@ -45,6 +62,7 @@ export default (directoryPath, options = {}) => { } children = fs.readdirSync(directoryPath); + const {extensions = ['js']} = options; children = _.filter(children, (fileName) => { const absolutePath = path.resolve(directoryPath, fileName); @@ -66,7 +84,9 @@ export default (directoryPath, options = {}) => { return false; } - if (!isDirectory && !_.endsWith(fileName, '.js')) { + if (!isDirectory && !extensions.some((ext) => { + return _.endsWith(fileName, '.' + ext); + })) { return false; } @@ -77,7 +97,7 @@ export default (directoryPath, options = {}) => { return true; }); - children = removeDuplicates(children); + children = removeDuplicates(children, extensions[0]); return children.sort(); }; diff --git a/src/utilities/writeIndex.js b/src/utilities/writeIndex.js index ffb832e..bfbba01 100644 --- a/src/utilities/writeIndex.js +++ b/src/utilities/writeIndex.js @@ -13,7 +13,7 @@ export default (directoryPaths, options = {}) => { }); _.forEach(sortedDirectoryPaths, (directoryPath) => { - const siblings = readDirectory(directoryPath); + const siblings = readDirectory(directoryPath, options); const indexCode = createIndexCode(siblings); const indexFilePath = path.resolve(directoryPath, 'index.js'); diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index 10eb35a..f37a0fa 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -20,6 +20,7 @@ export default (directoryPaths, options = {}) => { } else { log('Recursive:', options.recursive ? 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) { @@ -43,7 +44,10 @@ export default (directoryPaths, options = {}) => { _.forEach(sortedDirectoryPaths, (directoryPath) => { let existingIndexCode; - const siblings = readDirectory(directoryPath, {silent: options.ignoreUnsafe}); + const siblings = readDirectory(directoryPath, { + extensions: options.extensions, + silent: options.ignoreUnsafe + }); const indexCode = createIndexCode(siblings, { banner: options.banner diff --git a/test/fixtures/read-directory/children-files-alt-extension-with-homonyms/bar.js b/test/fixtures/read-directory/children-files-alt-extension-with-homonyms/bar.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/read-directory/children-files-alt-extension-with-homonyms/bar.jsx b/test/fixtures/read-directory/children-files-alt-extension-with-homonyms/bar.jsx new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/read-directory/children-files-alt-extension-with-homonyms/present.js b/test/fixtures/read-directory/children-files-alt-extension-with-homonyms/present.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/read-directory/children-files-alt-extension/bar.jsx b/test/fixtures/read-directory/children-files-alt-extension/bar.jsx new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/read-directory/children-files-alt-extension/foo.css b/test/fixtures/read-directory/children-files-alt-extension/foo.css new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/read-directory/children-files-alt-extension/present.js b/test/fixtures/read-directory/children-files-alt-extension/present.js new file mode 100644 index 0000000..e69de29 diff --git a/test/readDirectory.js b/test/readDirectory.js index 2166178..46a4016 100644 --- a/test/readDirectory.js +++ b/test/readDirectory.js @@ -49,6 +49,45 @@ describe('readDirectory()', () => { 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', () => { it('ignores files', () => { const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));