feat: add --extension/-x parameter overwrite default .js extension parser (#35)

This commit is contained in:
Guillaume Boddaert
2017-01-30 09:56:50 +00:00
committed by Gajus Kuizinas
parent 7353f04394
commit 5fea5e63d7
11 changed files with 79 additions and 6 deletions
+10
View File
@@ -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
+24 -4
View File
@@ -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();
};
+1 -1
View File
@@ -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');
+5 -1
View File
@@ -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
+39
View File
@@ -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'));