Scan all descending directories for index.js with 'create index'; (fixes #2)
This commit is contained in:
+12
-1
@@ -9,6 +9,17 @@ let argv;
|
||||
|
||||
argv = yargs
|
||||
.demand(1)
|
||||
.options({
|
||||
'update-index': {
|
||||
default: false,
|
||||
description: 'Recursively iterates target directories looking for "index.js" files that start with "\'create index\';\\n" (create-index index file). Updates found index files. Does not create new index files.',
|
||||
type: 'boolean'
|
||||
}
|
||||
})
|
||||
.example('create-index ./src ./src/utilities', 'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.')
|
||||
.example('create-index --update-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
|
||||
.argv;
|
||||
|
||||
writeIndexCli(argv._);
|
||||
writeIndexCli(argv._, {
|
||||
updateIndex: argv.updateIndex
|
||||
});
|
||||
|
||||
+1
-3
@@ -1,6 +1,4 @@
|
||||
export {
|
||||
createIndexCode,
|
||||
readDirectory,
|
||||
validateTargetDirectory,
|
||||
findIndexFiles,
|
||||
writeIndex
|
||||
} from './utilities';
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import _ from 'lodash';
|
||||
import glob from 'glob';
|
||||
import path from 'path';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
export default (directoryPath) => {
|
||||
let targetDirectories;
|
||||
|
||||
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
||||
|
||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||
try {
|
||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
||||
|
||||
return true;
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
/* eslint-enable no-empty */
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||
|
||||
return targetDirectories;
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
'create index';
|
||||
|
||||
export createIndexCode from './createIndexCode.js';
|
||||
export findIndexFiles from './findIndexFiles.js';
|
||||
export log from './log.js';
|
||||
export readDirectory from './readDirectory.js';
|
||||
export sortByDepth from './sortByDepth.js';
|
||||
|
||||
@@ -6,14 +6,25 @@ import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import sortByDepth from './sortByDepth';
|
||||
import log from './log';
|
||||
import findIndexFiles from './findIndexFiles';
|
||||
import chalk from 'chalk';
|
||||
|
||||
export default (directoryPaths) => {
|
||||
export default (directoryPaths, options = {}) => {
|
||||
let sortedDirectoryPaths;
|
||||
|
||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
|
||||
log('Target directories', sortedDirectoryPaths);
|
||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||
|
||||
if (options.updateIndex) {
|
||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||
|
||||
log('Found index file in:', sortedDirectoryPaths);
|
||||
}
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
|
||||
Reference in New Issue
Block a user