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

Update: Added --extension/-x parameter overwrite default .js extension #35

Merged
merged 1 commit into from
Jan 30, 2017
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
10 changes: 10 additions & 0 deletions src/bin/create-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions src/utilities/readDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -77,7 +97,7 @@ export default (directoryPath, options = {}) => {
return true;
});

children = removeDuplicates(children);
children = removeDuplicates(children, extensions[0]);

return children.sort();
};
2 changes: 1 addition & 1 deletion src/utilities/writeIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
6 changes: 5 additions & 1 deletion src/utilities/writeIndexCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Empty file.
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions test/readDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down