Merge branch 'danielpza-master'

This commit is contained in:
Gajus Kuizinas
2020-05-18 10:02:12 -07:00
9 changed files with 29 additions and 17 deletions
+1
View File
@@ -97,6 +97,7 @@ Options:
First extension will always be preferred to homonyms First extension will always be preferred to homonyms
with another allowed extension. with another allowed extension.
[array] [default: ["js"]] [array] [default: ["js"]]
--outputFile, -o Output file [string] [default: "index.js"] [array] [default: ["js"]]
Examples: Examples:
create-index ./src ./src/utilities Creates or updates an existing create-index ./src ./src/utilities Creates or updates an existing
+9
View File
@@ -53,6 +53,14 @@ const argv = yargs
type: 'array' type: 'array'
} }
}) })
.options({
outputFile: {
alias: 'o',
default: 'index.js',
description: 'Output file',
type: 'string'
}
})
.example( .example(
'create-index ./src ./src/utilities', 'create-index ./src ./src/utilities',
'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.' '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, ignoreDirectories: argv.ignoreDirectories,
ignoreUnsafe: argv.ignoreUnsafe, ignoreUnsafe: argv.ignoreUnsafe,
recursive: argv.recursive, recursive: argv.recursive,
outputFile: argv.outputFile,
updateIndex: argv.update updateIndex: argv.update
}); });
+2 -1
View File
@@ -13,7 +13,8 @@ export default (directoryPath, options = {}) => {
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => { targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
return validateTargetDirectory(path.dirname(targetDirectoryPath), { return validateTargetDirectory(path.dirname(targetDirectoryPath), {
silent: options.silent silent: options.silent,
outputFile: options.fileName
}); });
}); });
+2 -2
View File
@@ -1,8 +1,8 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
export default (directoryPath) => { export default (directoryPath, options = {}) => {
const indexPath = path.resolve(directoryPath, 'index.js'); const indexPath = path.resolve(directoryPath, options.outputFile || 'index.js');
try { try {
fs.statSync(indexPath); fs.statSync(indexPath);
+3 -3
View File
@@ -72,7 +72,7 @@ const removeIgnoredFiles = (files, ignorePatterns = []) => {
}; };
export default (directoryPath, options = {}) => { export default (directoryPath, options = {}) => {
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) { if (!validateTargetDirectory(directoryPath, options)) {
return false; return false;
} }
@@ -102,7 +102,7 @@ export default (directoryPath, options = {}) => {
return false; return false;
} }
if (_.startsWith(fileName, 'index.js')) { if (_.startsWith(fileName, options.outputFile || 'index.js')) {
return false; return false;
} }
@@ -112,7 +112,7 @@ export default (directoryPath, options = {}) => {
return false; return false;
} }
if (isDirectory && (!hasIndex(absolutePath) || ignoreDirectories)) { if (isDirectory && (!hasIndex(absolutePath, options) || ignoreDirectories)) {
return false; return false;
} }
+3 -3
View File
@@ -3,12 +3,12 @@ import path from 'path';
import hasIndex from './hasIndex'; import hasIndex from './hasIndex';
import {CREATE_INDEX_PATTERN} from './constants'; import {CREATE_INDEX_PATTERN} from './constants';
export default (directoryPath) => { export default (directoryPath, options = {}) => {
if (!hasIndex(directoryPath)) { if (!hasIndex(directoryPath, options)) {
return {}; 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 indexContents = fs.readFileSync(indexPath, 'utf-8');
const found = indexContents.match(CREATE_INDEX_PATTERN); const found = indexContents.match(CREATE_INDEX_PATTERN);
const configLine = typeof found[1] === 'string' ? found[1].trim() : ''; const configLine = typeof found[1] === 'string' ? found[1].trim() : '';
+1 -1
View File
@@ -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 { try {
fs.statSync(indexFilePath); fs.statSync(indexFilePath);
+3 -3
View File
@@ -10,15 +10,15 @@ import sortByDepth from './sortByDepth';
export default (directoryPaths, options = {}) => { export default (directoryPaths, options = {}) => {
const sortedDirectoryPaths = sortByDepth(directoryPaths) const sortedDirectoryPaths = sortByDepth(directoryPaths)
.filter((directoryPath) => { .filter((directoryPath) => {
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe}); return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe, outputFile: options.outputFile});
}); });
_.forEach(sortedDirectoryPaths, (directoryPath) => { _.forEach(sortedDirectoryPaths, (directoryPath) => {
const config = readIndexConfig(directoryPath); const config = readIndexConfig(directoryPath, options);
const optionsWithConfig = Object.assign({}, options, {config}); const optionsWithConfig = Object.assign({}, options, {config});
const siblings = readDirectory(directoryPath, optionsWithConfig); const siblings = readDirectory(directoryPath, optionsWithConfig);
const indexCode = createIndexCode(siblings, 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); fs.writeFileSync(indexFilePath, indexCode);
}); });
+5 -4
View File
@@ -16,6 +16,7 @@ export default (directoryPaths, options = {}) => {
sortedDirectoryPaths = sortByDepth(directoryPaths); sortedDirectoryPaths = sortByDepth(directoryPaths);
log('Target directories', sortedDirectoryPaths); log('Target directories', sortedDirectoryPaths);
log('Output file', options.outputFile);
if (options.updateIndex) { if (options.updateIndex) {
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false')); log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
} else { } else {
@@ -27,7 +28,7 @@ export default (directoryPaths, options = {}) => {
if (options.updateIndex || options.recursive) { if (options.updateIndex || options.recursive) {
sortedDirectoryPaths = _.map(sortedDirectoryPaths, (dir) => { sortedDirectoryPaths = _.map(sortedDirectoryPaths, (dir) => {
return findIndexFiles(dir, { return findIndexFiles(dir, {
fileName: options.updateIndex ? 'index.js' : '*', fileName: options.updateIndex ? options.outputFile || 'index.js' : '*',
silent: options.updateIndex || options.ignoreUnsafe silent: options.updateIndex || options.ignoreUnsafe
}); });
}); });
@@ -39,13 +40,13 @@ export default (directoryPaths, options = {}) => {
} }
sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => { sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => {
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe}); return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe, outputFile: options.outputFile});
}); });
_.forEach(sortedDirectoryPaths, (directoryPath) => { _.forEach(sortedDirectoryPaths, (directoryPath) => {
let existingIndexCode; let existingIndexCode;
const config = readIndexConfig(directoryPath); const config = readIndexConfig(directoryPath, options);
const siblings = readDirectory(directoryPath, { const siblings = readDirectory(directoryPath, {
config, config,
@@ -59,7 +60,7 @@ export default (directoryPaths, options = {}) => {
config config
}); });
const indexFilePath = path.resolve(directoryPath, 'index.js'); const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js');
try { try {
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8'); existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');