Skip to content

Adding flags to use implicit defaults and wildcard folders #55

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ node_modules
!.editorconfig
!.eslintignore
!.eslintrc
!.gitattributes
!.gitignore
!.npmignore
!.travis.yml
package-lock.json
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use dashes for CLI program parameters, i.e. --implicit-default.

Copy link
Author

@toddtarsi toddtarsi Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gajus - Are you sure? I was following the convention outlined in ignoreDirectories and ignoreUnsafe.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. That should not have been the case originally. Please keep the convention. I will release a breaking change later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gajus - Sounds good. If everything else is okay, I'll make some tests over the next couple days to check for any funny business between the various flags.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the status of this PR? I would love this change.

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
Expand Down
20 changes: 19 additions & 1 deletion src/bin/create-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -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
});
42 changes: 33 additions & 9 deletions src/utilities/createIndexCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) => {
Expand All @@ -28,7 +53,6 @@ export default (filePaths, options = {}) => {

code = '';
configCode = '';

if (options.banner) {
const banners = _.isArray(options.banner) ? options.banner : [options.banner];

Expand All @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src/utilities/writeIndexCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down