Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fea5e63d7 | ||
|
|
7353f04394 | ||
|
|
65892e3bb8 |
@@ -36,8 +36,8 @@ This created `index.js` with:
|
||||
```js
|
||||
// @create-index
|
||||
|
||||
export bar from './bar.js';
|
||||
export foo from './foo.js';
|
||||
export { default as bar } from './bar.js';
|
||||
export { default as foo } from './foo.js';
|
||||
|
||||
```
|
||||
|
||||
@@ -66,9 +66,9 @@ This have updated `index.js` file:
|
||||
```js
|
||||
// @create-index
|
||||
|
||||
export bar from './bar.js';
|
||||
export baz from './baz.js';
|
||||
export foo from './foo.js';
|
||||
export { default as bar } from './bar.js';
|
||||
export { default as baz } from './baz.js';
|
||||
export { default as foo } from './foo.js';
|
||||
|
||||
```
|
||||
|
||||
@@ -177,8 +177,8 @@ Given the above directory contents, `./index.js` will be:
|
||||
```js
|
||||
// @create-index
|
||||
|
||||
import bar from './bar';
|
||||
import foo from './foo.js';
|
||||
import { default as bar } from './bar';
|
||||
import { default as foo } from './foo.js';
|
||||
|
||||
export {
|
||||
bar,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -14,7 +14,7 @@ const buildExportBlock = (files) => {
|
||||
let importBlock;
|
||||
|
||||
importBlock = _.map(files, (fileName) => {
|
||||
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
||||
return 'export { default as ' + safeVariableName(fileName) + ' } from \'./' + fileName + '\';';
|
||||
});
|
||||
|
||||
importBlock = importBlock.join('\n');
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -20,7 +20,7 @@ describe('createIndexCode()', () => {
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export foo from './foo';
|
||||
export { default as foo } from './foo';
|
||||
`));
|
||||
});
|
||||
it('describes multiple children', () => {
|
||||
@@ -29,8 +29,8 @@ export foo from './foo';
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo';
|
||||
export { default as bar } from './bar';
|
||||
export { default as foo } from './foo';
|
||||
`));
|
||||
});
|
||||
context('file with extension', () => {
|
||||
@@ -40,7 +40,7 @@ export foo from './foo';
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export foo from './foo.js';
|
||||
export { default as foo } from './foo.js';
|
||||
`));
|
||||
});
|
||||
});
|
||||
@@ -51,8 +51,8 @@ export foo from './foo.js';
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo';
|
||||
export { default as bar } from './bar';
|
||||
export { default as foo } from './foo';
|
||||
`));
|
||||
});
|
||||
});
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo.js';
|
||||
export { default as bar } from './bar';
|
||||
export { default as foo } from './foo.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'));
|
||||
|
||||
+2
-2
@@ -29,8 +29,8 @@ describe('writeIndex()', () => {
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo.js';
|
||||
export { default as bar } from './bar';
|
||||
export { default as foo } from './foo.js';
|
||||
`));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user