diff --git a/.gitignore b/.gitignore index 67835df..1fbdb2d 100755 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ node_modules !.editorconfig !.eslintignore !.eslintrc +!.gitattributes !.gitignore !.npmignore !.travis.yml +package-lock.json \ No newline at end of file diff --git a/README.md b/README.md index b535461..a508db3 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,14 @@ Options: First extension will always be preferred to homonyms with another allowed extension. [array] [default: ["js"]] + --implicitDefault, -m Uses defaults as implicit instead of named. + export { default as foo } from './foo.js' + becomes export foo from './foo.js' + [boolean] [default: false] + --wildcardFolders, -w Imports all children from directory as wildcard. + export foo from './foo' + becomes export * as foo from './foo' + [boolean] [default: false] Examples: create-index ./src ./src/utilities Creates or updates an existing diff --git a/src/bin/create-index.js b/src/bin/create-index.js index 4919cf3..742c56a 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -53,6 +53,22 @@ const argv = yargs type: 'array' } }) + .options({ + implicitDefault: { + alias: 'm', + default: false, + description: 'Uses implicit defaults as implicit instead of explicit. export { default as thing } from \'./thing.js\' becomes export thing from \'./thing.js\'', + type: 'boolean' + } + }) + .options({ + wildcardFolders: { + alias: 'w', + default: false, + description: 'Export folders as wildcards instead of defaults. Works well with recursion.', + type: 'boolean' + } + }) .example( 'create-index ./src ./src/utilities', 'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.' @@ -72,6 +88,8 @@ writeIndexCli(argv._, { extensions: argv.extensions, ignoreDirectories: argv.ignoreDirectories, ignoreUnsafe: argv.ignoreUnsafe, + implicitDefault: argv.implicitDefault, recursive: argv.recursive, - updateIndex: argv.update + updateIndex: argv.update, + wildcardFolders: argv.wildcardFolders }); diff --git a/src/utilities/createIndexCode.js b/src/utilities/createIndexCode.js index 381a6f4..5ad7a05 100644 --- a/src/utilities/createIndexCode.js +++ b/src/utilities/createIndexCode.js @@ -10,16 +10,41 @@ const safeVariableName = (fileName) => { } }; -const buildExportBlock = (files) => { - let importBlock; +// export (tough bit) from './file +const defaultFactory = (filename, block) => { + return 'export ' + block + ' from \'./' + filename + '\';'; +}; - importBlock = _.map(files, (fileName) => { - return 'export { default as ' + safeVariableName(fileName) + ' } from \'./' + fileName + '\';'; - }); +// tough bit = file +const implicitDefaultExport = (fileName) => { + return defaultFactory(fileName, safeVariableName(fileName)); +}; - importBlock = importBlock.join('\n'); +// tough bit = { default as file } +const explicitDefaultExport = (fileName) => { + return defaultFactory(fileName, '{ default as ' + safeVariableName(fileName) + ' }'); +}; - return importBlock; +// tough bit = * as file +const wildcardExport = (folderName) => { + return defaultFactory(folderName, '* as ' + folderName); +}; + +const buildExportBlock = (files, options) => { + const transform = (file) => { + const isFolder = file.split('.').length === 1; + const defaultExport = options.implicitDefault ? + implicitDefaultExport : + explicitDefaultExport; + + if (isFolder && options.wildcardFolders) { + return wildcardExport(file); + } + + return defaultExport(file); + }; + + return files.map(transform).join('\n'); }; export default (filePaths, options = {}) => { @@ -28,7 +53,6 @@ export default (filePaths, options = {}) => { code = ''; configCode = ''; - if (options.banner) { const banners = _.isArray(options.banner) ? options.banner : [options.banner]; @@ -48,7 +72,7 @@ export default (filePaths, options = {}) => { if (filePaths.length) { const sortedFilePaths = filePaths.sort(); - code += buildExportBlock(sortedFilePaths) + '\n\n'; + code += buildExportBlock(sortedFilePaths, options) + '\n\n'; } return code; diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index e7626ff..3eacfef 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -54,10 +54,7 @@ export default (directoryPaths, options = {}) => { silent: options.ignoreUnsafe }); - const indexCode = createIndexCode(siblings, { - banner: options.banner, - config - }); + const indexCode = createIndexCode(siblings, Object.assign({}, options, {config})); const indexFilePath = path.resolve(directoryPath, 'index.js');