- Add functionality to ignore files based on provided
regular expressions
- Support writeIndex and writeIndexCli
- in index.js config must be specified as below
// @create-index {"ignore":["/foo.js"]}
This commit is contained in:
committed by
Gajus Kuizinas
parent
5fea5e63d7
commit
bf45925380
@@ -0,0 +1,2 @@
|
||||
// eslint-disable-next-line
|
||||
export const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/;
|
||||
@@ -24,8 +24,10 @@ const buildExportBlock = (files) => {
|
||||
|
||||
export default (filePaths, options = {}) => {
|
||||
let code;
|
||||
let configCode;
|
||||
|
||||
code = '';
|
||||
configCode = '';
|
||||
|
||||
if (options.banner) {
|
||||
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
|
||||
@@ -37,7 +39,11 @@ export default (filePaths, options = {}) => {
|
||||
code += '\n';
|
||||
}
|
||||
|
||||
code += '// @create-index\n\n';
|
||||
if (options.config && _.size(options.config) > 0) {
|
||||
configCode += ' ' + JSON.stringify(options.config);
|
||||
}
|
||||
|
||||
code += '// @create-index' + configCode + '\n\n';
|
||||
|
||||
if (filePaths.length) {
|
||||
const sortedFilePaths = filePaths.sort();
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export default (directoryPath) => {
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexPath);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -2,8 +2,6 @@ import chalk from 'chalk';
|
||||
import moment from 'moment';
|
||||
|
||||
export default (...append) => {
|
||||
/* eslint-disable no-console */
|
||||
// eslint-disable-next-line
|
||||
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
|
||||
|
||||
/* eslint-enable no-console */
|
||||
};
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import hasIndex from './hasIndex';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
const hasIndex = (directoryPath) => {
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexPath);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const hasNoExtension = (fileName) => {
|
||||
const matches = fileName.match(/\./g);
|
||||
|
||||
@@ -54,15 +43,47 @@ const removeDuplicates = (files, preferredExtension) => {
|
||||
});
|
||||
};
|
||||
|
||||
export default (directoryPath, options = {}) => {
|
||||
let children;
|
||||
const removeIgnoredFiles = (files, ignorePatterns = []) => {
|
||||
if (ignorePatterns.length === 0) {
|
||||
return files;
|
||||
}
|
||||
|
||||
const patterns = ignorePatterns.map((pattern) => {
|
||||
if (_.startsWith(pattern, '/') && _.endsWith(pattern, '/')) {
|
||||
const patternWithoutSlashes = pattern.slice(1, -1);
|
||||
|
||||
return new RegExp(patternWithoutSlashes);
|
||||
}
|
||||
|
||||
return new RegExp(pattern);
|
||||
});
|
||||
|
||||
return _.filter(files, (fileName) => {
|
||||
let pattern;
|
||||
|
||||
for (pattern of patterns) {
|
||||
if (fileName.match(pattern) !== null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
||||
export default (directoryPath, options = {}) => {
|
||||
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const {
|
||||
extensions = ['js'],
|
||||
config = {}
|
||||
} = options;
|
||||
|
||||
let children;
|
||||
|
||||
children = fs.readdirSync(directoryPath);
|
||||
const {extensions = ['js']} = options;
|
||||
|
||||
children = _.filter(children, (fileName) => {
|
||||
const absolutePath = path.resolve(directoryPath, fileName);
|
||||
@@ -98,6 +119,7 @@ export default (directoryPath, options = {}) => {
|
||||
});
|
||||
|
||||
children = removeDuplicates(children, extensions[0]);
|
||||
children = removeIgnoredFiles(children, config.ignore);
|
||||
|
||||
return children.sort();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import hasIndex from './hasIndex';
|
||||
import {CREATE_INDEX_PATTERN} from './constants';
|
||||
|
||||
export default (directoryPath) => {
|
||||
if (!hasIndex(directoryPath)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
const indexContents = fs.readFileSync(indexPath, 'utf-8');
|
||||
const found = indexContents.match(CREATE_INDEX_PATTERN);
|
||||
const configLine = typeof found[1] === 'string' ? found[1].trim() : '';
|
||||
|
||||
if (configLine.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
let config;
|
||||
|
||||
try {
|
||||
config = JSON.parse(configLine);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
'"' + indexPath + '" contains invalid configuration object.\n' +
|
||||
'Configuration object must be a valid JSON.'
|
||||
);
|
||||
}
|
||||
|
||||
return config;
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {CREATE_INDEX_PATTERN} from './constants';
|
||||
|
||||
export default (targetDirectory, options = {}) => {
|
||||
const silent = options.silent;
|
||||
@@ -33,7 +34,7 @@ export default (targetDirectory, options = {}) => {
|
||||
|
||||
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
if (!indexFile.match(/(?:^|[\n\r]+)\/\/ @create-index[\n\r]+/)) {
|
||||
if (!indexFile.match(CREATE_INDEX_PATTERN)) {
|
||||
if (silent) {
|
||||
return false;
|
||||
} else {
|
||||
|
||||
@@ -4,6 +4,7 @@ import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import readIndexConfig from './readIndexConfig';
|
||||
import sortByDepth from './sortByDepth';
|
||||
|
||||
export default (directoryPaths, options = {}) => {
|
||||
@@ -13,8 +14,10 @@ export default (directoryPaths, options = {}) => {
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
const siblings = readDirectory(directoryPath, options);
|
||||
const indexCode = createIndexCode(siblings);
|
||||
const config = readIndexConfig(directoryPath);
|
||||
const optionsWithConfig = Object.assign({}, options, {config});
|
||||
const siblings = readDirectory(directoryPath, optionsWithConfig);
|
||||
const indexCode = createIndexCode(siblings, {config});
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
|
||||
@@ -5,6 +5,7 @@ import chalk from 'chalk';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import readIndexConfig from './readIndexConfig';
|
||||
import sortByDepth from './sortByDepth';
|
||||
import log from './log';
|
||||
import findIndexFiles from './findIndexFiles';
|
||||
@@ -44,13 +45,17 @@ export default (directoryPaths, options = {}) => {
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
let existingIndexCode;
|
||||
|
||||
const config = readIndexConfig(directoryPath);
|
||||
|
||||
const siblings = readDirectory(directoryPath, {
|
||||
config,
|
||||
extensions: options.extensions,
|
||||
silent: options.ignoreUnsafe
|
||||
});
|
||||
|
||||
const indexCode = createIndexCode(siblings, {
|
||||
banner: options.banner
|
||||
banner: options.banner,
|
||||
config
|
||||
});
|
||||
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
Reference in New Issue
Block a user