Files
create-index/src/utilities/createIndexCode.js
T
laggingreflexandGajus Kuizinas fdb17d0179 feat: ability to add banner (#20)
Lets you add a custom banner.

```sh
create-index src -u --banner "/* eslint-disable */"
```
```js
/* eslint-disable */

// @create-index

import xxx ...
```
2016-11-29 17:45:23 +00:00

50 lines
945 B
JavaScript

import _ from 'lodash';
const safeVariableName = (fileName) => {
const indexOfDot = fileName.indexOf('.');
if (indexOfDot === -1) {
return fileName;
} else {
return fileName.slice(0, indexOfDot);
}
};
const buildExportBlock = (files) => {
let importBlock;
importBlock = _.map(files, (fileName) => {
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
});
importBlock = importBlock.join('\n');
return importBlock;
};
export default (filePaths, options = {}) => {
let code;
code = '';
if (options.banner) {
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
banners.forEach((banner) => {
code += banner + '\n';
});
code += '\n';
}
code += '// @create-index\n\n';
if (filePaths.length) {
const sortedFilePaths = filePaths.sort();
code += buildExportBlock(sortedFilePaths) + '\n\n';
}
return code;
};