Skip to content

Commit

Permalink
feat: Add option to provide a custom template function
Browse files Browse the repository at this point in the history
  • Loading branch information
coleturner committed Jan 28, 2018
1 parent ed8e1d0 commit 82926a5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
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';
`));
});
});
});

0 comments on commit 82926a5

Please sign in to comment.