Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to exclude directories from index.js #50

Merged
merged 5 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions src/bin/create-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
gajus marked this conversation as resolved.
Show resolved Hide resolved
'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
Expand Down
16 changes: 8 additions & 8 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
@@ -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';
gajus marked this conversation as resolved.
Show resolved Hide resolved
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';
5 changes: 3 additions & 2 deletions src/utilities/readDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export default (directoryPath, options = {}) => {

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

let children;
Expand Down Expand Up @@ -111,7 +112,7 @@ export default (directoryPath, options = {}) => {
return false;
}

if (isDirectory && !hasIndex(absolutePath)) {
if (isDirectory && (!hasIndex(absolutePath) || ignoreDirectories)) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/utilities/writeIndexCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default (directoryPaths, options = {}) => {
const siblings = readDirectory(directoryPath, {
config,
extensions: options.extensions,
ignoreDirectories: options.ignoreDirectories,
silent: options.ignoreUnsafe
});

Expand Down
6 changes: 6 additions & 0 deletions test/readDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('readDirectory()', () => {

expect(names).to.deep.equal(['bar', 'foo']);
});

it('excludes directories if ignoreDirectories = true', () => {
const names = readDirectory(path.resolve(fixturesPath, 'children-index'), {ignoreDirectories: true});

expect(names).to.deep.equal([]);
});
});
context('target directory contains files', () => {
it('refers to the files (with extension)', () => {
Expand Down