Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add outputFile for typescript #54

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"url": "https://github.com/gajus/create-index"
},
"scripts": {
"prepare": "npm run build",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove new script. Not clear why this was added.

"build": "cross-env NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
"create-index": "node ./dist/bin/create-index ./src/utilities",
"lint": "cross-env NODE_ENV=development eslint ./src ./tests",
Expand Down
9 changes: 9 additions & 0 deletions src/bin/create-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -73,5 +81,6 @@ writeIndexCli(argv._, {
ignoreDirectories: argv.ignoreDirectories,
ignoreUnsafe: argv.ignoreUnsafe,
recursive: argv.recursive,
outputFile: argv.outputFile,
updateIndex: argv.update
});
3 changes: 2 additions & 1 deletion src/utilities/findIndexFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/utilities/hasIndex.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/readDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const removeIgnoredFiles = (files, ignorePatterns = []) => {
};

export default (directoryPath, options = {}) => {
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
if (!validateTargetDirectory(directoryPath, options)) {
return false;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ export default (directoryPath, options = {}) => {
return false;
}

if (_.startsWith(fileName, 'index.js')) {
if (_.startsWith(fileName, options.outputFile || 'index.js')) {
return false;
}

Expand All @@ -112,7 +112,7 @@ export default (directoryPath, options = {}) => {
return false;
}

if (isDirectory && (!hasIndex(absolutePath) || ignoreDirectories)) {
if (isDirectory && (!hasIndex(absolutePath, options) || ignoreDirectories)) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/utilities/readIndexConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() : '';
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/validateTargetDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/writeIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
9 changes: 5 additions & 4 deletions src/utilities/writeIndexCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
});
});
Expand All @@ -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,
Expand All @@ -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');
Expand Down