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

(Feature) Add custom template function #43

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 5 additions & 4 deletions src/utilities/createIndexCode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import exportNamedIndex from './exportNamedIndex';

const safeVariableName = (fileName) => {
const indexOfDot = fileName.indexOf('.');
Expand All @@ -10,19 +11,19 @@ const safeVariableName = (fileName) => {
}
};

const buildExportBlock = (files) => {
const buildExportBlock = (files, templateFunction) => {
let importBlock;

importBlock = _.map(files, (fileName) => {
return 'export { default as ' + safeVariableName(fileName) + ' } from \'./' + fileName + '\';';
return templateFunction(safeVariableName(fileName), fileName);
});

importBlock = importBlock.join('\n');

return importBlock;
};

export default (filePaths, options = {}) => {
export default (filePaths, options = {}, templateFunction = exportNamedIndex) => {
let code;
let configCode;

Expand All @@ -48,7 +49,7 @@ export default (filePaths, options = {}) => {
if (filePaths.length) {
const sortedFilePaths = filePaths.sort();

code += buildExportBlock(sortedFilePaths) + '\n\n';
code += buildExportBlock(sortedFilePaths, templateFunction) + '\n\n';
}

return code;
Expand Down
3 changes: 3 additions & 0 deletions src/utilities/exportNamedIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (variableName, fileName) => {
return 'export { default as ' + variableName + ' } from \'./' + fileName + '\';';
};
5 changes: 3 additions & 2 deletions src/utilities/writeIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import validateTargetDirectory from './validateTargetDirectory';
import readDirectory from './readDirectory';
import readIndexConfig from './readIndexConfig';
import sortByDepth from './sortByDepth';
import exportNamedIndex from './exportNamedIndex';

export default (directoryPaths, options = {}) => {
export default (directoryPaths, options = {}, templateFunction = exportNamedIndex) => {
const sortedDirectoryPaths = sortByDepth(directoryPaths)
.filter((directoryPath) => {
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe});
Expand All @@ -17,7 +18,7 @@ export default (directoryPaths, options = {}) => {
const config = readIndexConfig(directoryPath);
const optionsWithConfig = Object.assign({}, options, {config});
const siblings = readDirectory(directoryPath, optionsWithConfig);
const indexCode = createIndexCode(siblings, optionsWithConfig);
const indexCode = createIndexCode(siblings, optionsWithConfig, templateFunction);
const indexFilePath = path.resolve(directoryPath, 'index.js');

fs.writeFileSync(indexFilePath, indexCode);
Expand Down
16 changes: 16 additions & 0 deletions test/createIndexCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,20 @@ export { default as foo } from './foo';
`));
});
});

context('with custom template function', () => {
it('should use template function', () => {
const config = {
ignore: ['/^zoo/']
};
const indexCode = createIndexCode(['foo', 'bar'], {config}, (varName, fileName) => `export * as ${varName} from './${fileName}';`);

expect(indexCode).to.equal(codeExample(`
// @create-index {"ignore":["/^zoo/"]}

export * as bar from './bar';
export * as foo from './foo';
`));
});
});
});