From 50a2b85d7f353ffe42aefa9b7d5e40cf7def04c0 Mon Sep 17 00:00:00 2001 From: Joe Whittles Date: Wed, 23 Jan 2019 16:30:19 +0000 Subject: [PATCH 1/5] Add option to exclude directories from index.js --- src/bin/create-index.js | 24 +++++++++++++++++++++--- src/utilities/index.js | 16 ++++++++-------- src/utilities/readDirectory.js | 5 +++-- src/utilities/writeIndexCli.js | 1 + test/readDirectory.js | 6 ++++++ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/bin/create-index.js b/src/bin/create-index.js index 6ea78e0..4919cf3 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -23,6 +23,14 @@ const argv = yargs type: 'boolean' } }) + .options({ + ignoreDirectories: { + alias: 'd', + default: false, + description: 'Ignores importing directories into the index file, even if they have a safe "index.js".', + type: 'boolean' + } + }) .options({ update: { alias: 'u', @@ -45,14 +53,24 @@ const argv = yargs 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.') + .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, + ignoreDirectories: argv.ignoreDirectories, ignoreUnsafe: argv.ignoreUnsafe, recursive: argv.recursive, updateIndex: argv.update diff --git a/src/utilities/index.js b/src/utilities/index.js index b38d324..fd84145 100644 --- a/src/utilities/index.js +++ b/src/utilities/index.js @@ -1,10 +1,10 @@ // @create-index -export createIndexCode from './createIndexCode.js'; -export findIndexFiles from './findIndexFiles.js'; -export log from './log.js'; -export readDirectory from './readDirectory.js'; -export sortByDepth from './sortByDepth.js'; -export validateTargetDirectory from './validateTargetDirectory.js'; -export writeIndex from './writeIndex.js'; -export writeIndexCli from './writeIndexCli.js'; +export createIndexCode from './createIndexCode'; +export findIndexFiles from './findIndexFiles'; +export log from './log'; +export readDirectory from './readDirectory'; +export sortByDepth from './sortByDepth'; +export validateTargetDirectory from './validateTargetDirectory'; +export writeIndex from './writeIndex'; +export writeIndexCli from './writeIndexCli'; diff --git a/src/utilities/readDirectory.js b/src/utilities/readDirectory.js index 2c7550b..06424ab 100644 --- a/src/utilities/readDirectory.js +++ b/src/utilities/readDirectory.js @@ -78,7 +78,8 @@ export default (directoryPath, options = {}) => { const { extensions = ['js'], - config = {} + config = {}, + ignoreDirs = false } = options; let children; @@ -111,7 +112,7 @@ export default (directoryPath, options = {}) => { return false; } - if (isDirectory && !hasIndex(absolutePath)) { + if (isDirectory && (!hasIndex(absolutePath) || ignoreDirs)) { return false; } diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index df0b513..29f9313 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -50,6 +50,7 @@ export default (directoryPaths, options = {}) => { const siblings = readDirectory(directoryPath, { config, extensions: options.extensions, + ignoreDirs: options.ignoreDirectories, silent: options.ignoreUnsafe }); diff --git a/test/readDirectory.js b/test/readDirectory.js index 46a4016..f50ffa9 100644 --- a/test/readDirectory.js +++ b/test/readDirectory.js @@ -34,6 +34,12 @@ describe('readDirectory()', () => { expect(names).to.deep.equal(['bar', 'foo']); }); + + it('excludes directories if ignoreDirs = true', () => { + const names = readDirectory(path.resolve(fixturesPath, 'children-index'), {ignoreDirs: true}); + + expect(names).to.deep.equal([]); + }); }); context('target directory contains files', () => { it('refers to the files (with extension)', () => { From 61315e3ad70914e55f325ade4657229db63688f9 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Thu, 24 Jan 2019 13:57:49 +0000 Subject: [PATCH 2/5] Update readDirectory.js --- src/utilities/readDirectory.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utilities/readDirectory.js b/src/utilities/readDirectory.js index 06424ab..f7a7e09 100644 --- a/src/utilities/readDirectory.js +++ b/src/utilities/readDirectory.js @@ -79,7 +79,7 @@ export default (directoryPath, options = {}) => { const { extensions = ['js'], config = {}, - ignoreDirs = false + ignoreDirectories = false } = options; let children; @@ -112,7 +112,7 @@ export default (directoryPath, options = {}) => { return false; } - if (isDirectory && (!hasIndex(absolutePath) || ignoreDirs)) { + if (isDirectory && (!hasIndex(absolutePath) || ignoreDirectories)) { return false; } From 3bf7f6998f2f2508d197d7fcd79c6dcbc38dc885 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Thu, 24 Jan 2019 14:03:33 +0000 Subject: [PATCH 3/5] Update writeIndexCli.js --- src/utilities/writeIndexCli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index 29f9313..e7626ff 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -50,7 +50,7 @@ export default (directoryPaths, options = {}) => { const siblings = readDirectory(directoryPath, { config, extensions: options.extensions, - ignoreDirs: options.ignoreDirectories, + ignoreDirectories: options.ignoreDirectories, silent: options.ignoreUnsafe }); From 18604782b637395c3d75f7de86611464da7de322 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Thu, 24 Jan 2019 14:03:45 +0000 Subject: [PATCH 4/5] Update readDirectory.js --- test/readDirectory.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/readDirectory.js b/test/readDirectory.js index f50ffa9..31ea9be 100644 --- a/test/readDirectory.js +++ b/test/readDirectory.js @@ -35,8 +35,8 @@ describe('readDirectory()', () => { expect(names).to.deep.equal(['bar', 'foo']); }); - it('excludes directories if ignoreDirs = true', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-index'), {ignoreDirs: true}); + it('excludes directories if ignoreDirectories = true', () => { + const names = readDirectory(path.resolve(fixturesPath, 'children-index'), {ignoreDirectories: true}); expect(names).to.deep.equal([]); }); From 3778e67b2926b23b772f7e437421c17305140eaa Mon Sep 17 00:00:00 2001 From: Joe Whittles Date: Fri, 25 Jan 2019 13:20:45 +0000 Subject: [PATCH 5/5] Update README --- README.md | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3c7a053..b535461 100644 --- a/README.md +++ b/README.md @@ -82,21 +82,34 @@ npm install create-index create-index --help Options: - --recursive, -r Create/update index files recursively. Halts on any unsafe - "index.js" files. [boolean] [default: false] - --ignoreUnsafe, -i Ignores unsafe "index.js" files instead of halting. + --recursive, -r Create/update index files recursively. Halts on any + unsafe "index.js" files. [boolean] [default: false] + --ignoreUnsafe, -i Ignores unsafe "index.js" files instead of halting. [boolean] [default: false] - --update, -u Updates only previously created index files (recursively). + --ignoreDirectories, -d Ignores importing directories into the index file, + even if they have a safe "index.js". [boolean] [default: false] - --banner Add a custom banner at the top of the index file [string] + --update, -u Updates only previously created index files + (recursively). [boolean] [default: false] + --banner Add a custom banner at the top of the index file + [string] + --extensions, -x Allows some extensions to be parsed as valid source. + First extension will always be preferred to homonyms + with another allowed extension. + [array] [default: ["js"]] Examples: - create-index ./src ./src/utilities Creates or updates an existing - create-index index file in the target - (./src, ./src/utilities) directories. - create-index --update ./src ./tests Finds all create-index index files in the - target directories and descending - directories. Updates found index files. + create-index ./src ./src/utilities Creates or updates an existing + create-index index file in the target + (./src, ./src/utilities) directories. + create-index --update ./src ./tests Finds all create-index index files in + the target directories and descending + directories. Updates found index + files. + 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. ``` ### Using `create-index` Programmatically