Merge branch 'danielpza-master'
This commit is contained in:
@@ -97,6 +97,7 @@ Options:
|
||||
First extension will always be preferred to homonyms
|
||||
with another allowed extension.
|
||||
[array] [default: ["js"]]
|
||||
--outputFile, -o Output file [string] [default: "index.js"] [array] [default: ["js"]]
|
||||
|
||||
Examples:
|
||||
create-index ./src ./src/utilities Creates or updates an existing
|
||||
|
||||
@@ -53,6 +53,14 @@ const argv = yargs
|
||||
type: 'array'
|
||||
}
|
||||
})
|
||||
.options({
|
||||
outputFile: {
|
||||
alias: 'o',
|
||||
default: 'index.js',
|
||||
description: 'Output file',
|
||||
type: 'string'
|
||||
}
|
||||
})
|
||||
.example(
|
||||
'create-index ./src ./src/utilities',
|
||||
'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.'
|
||||
@@ -73,5 +81,6 @@ writeIndexCli(argv._, {
|
||||
ignoreDirectories: argv.ignoreDirectories,
|
||||
ignoreUnsafe: argv.ignoreUnsafe,
|
||||
recursive: argv.recursive,
|
||||
outputFile: argv.outputFile,
|
||||
updateIndex: argv.update
|
||||
});
|
||||
|
||||
@@ -13,7 +13,8 @@ export default (directoryPath, options = {}) => {
|
||||
|
||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||
return validateTargetDirectory(path.dirname(targetDirectoryPath), {
|
||||
silent: options.silent
|
||||
silent: options.silent,
|
||||
outputFile: options.fileName
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export default (directoryPath) => {
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
export default (directoryPath, options = {}) => {
|
||||
const indexPath = path.resolve(directoryPath, options.outputFile || 'index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexPath);
|
||||
|
||||
@@ -72,7 +72,7 @@ const removeIgnoredFiles = (files, ignorePatterns = []) => {
|
||||
};
|
||||
|
||||
export default (directoryPath, options = {}) => {
|
||||
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
|
||||
if (!validateTargetDirectory(directoryPath, options)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ export default (directoryPath, options = {}) => {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_.startsWith(fileName, 'index.js')) {
|
||||
if (_.startsWith(fileName, options.outputFile || 'index.js')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ export default (directoryPath, options = {}) => {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isDirectory && (!hasIndex(absolutePath) || ignoreDirectories)) {
|
||||
if (isDirectory && (!hasIndex(absolutePath, options) || ignoreDirectories)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,12 @@ import path from 'path';
|
||||
import hasIndex from './hasIndex';
|
||||
import {CREATE_INDEX_PATTERN} from './constants';
|
||||
|
||||
export default (directoryPath) => {
|
||||
if (!hasIndex(directoryPath)) {
|
||||
export default (directoryPath, options = {}) => {
|
||||
if (!hasIndex(directoryPath, options)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
const indexPath = path.resolve(directoryPath, options.outputFile || '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() : '';
|
||||
|
||||
@@ -24,7 +24,7 @@ export default (targetDirectory, options = {}) => {
|
||||
}
|
||||
}
|
||||
|
||||
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||
const indexFilePath = path.resolve(targetDirectory, './' + (options.outputFile || 'index.js'));
|
||||
|
||||
try {
|
||||
fs.statSync(indexFilePath);
|
||||
|
||||
@@ -10,15 +10,15 @@ import sortByDepth from './sortByDepth';
|
||||
export default (directoryPaths, options = {}) => {
|
||||
const sortedDirectoryPaths = sortByDepth(directoryPaths)
|
||||
.filter((directoryPath) => {
|
||||
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
||||
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe, outputFile: options.outputFile});
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
const config = readIndexConfig(directoryPath);
|
||||
const config = readIndexConfig(directoryPath, options);
|
||||
const optionsWithConfig = Object.assign({}, options, {config});
|
||||
const siblings = readDirectory(directoryPath, optionsWithConfig);
|
||||
const indexCode = createIndexCode(siblings, optionsWithConfig);
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js');
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
});
|
||||
|
||||
@@ -16,6 +16,7 @@ export default (directoryPaths, options = {}) => {
|
||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
|
||||
log('Target directories', sortedDirectoryPaths);
|
||||
log('Output file', options.outputFile);
|
||||
if (options.updateIndex) {
|
||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||
} else {
|
||||
@@ -27,7 +28,7 @@ export default (directoryPaths, options = {}) => {
|
||||
if (options.updateIndex || options.recursive) {
|
||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, (dir) => {
|
||||
return findIndexFiles(dir, {
|
||||
fileName: options.updateIndex ? 'index.js' : '*',
|
||||
fileName: options.updateIndex ? options.outputFile || 'index.js' : '*',
|
||||
silent: options.updateIndex || options.ignoreUnsafe
|
||||
});
|
||||
});
|
||||
@@ -39,13 +40,13 @@ export default (directoryPaths, options = {}) => {
|
||||
}
|
||||
|
||||
sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => {
|
||||
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
||||
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe, outputFile: options.outputFile});
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
let existingIndexCode;
|
||||
|
||||
const config = readIndexConfig(directoryPath);
|
||||
const config = readIndexConfig(directoryPath, options);
|
||||
|
||||
const siblings = readDirectory(directoryPath, {
|
||||
config,
|
||||
@@ -59,7 +60,7 @@ export default (directoryPaths, options = {}) => {
|
||||
config
|
||||
});
|
||||
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js');
|
||||
|
||||
try {
|
||||
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
Reference in New Issue
Block a user