From 38c409465597a0ea3f98a345367ddddb9b2a4f7a Mon Sep 17 00:00:00 2001 From: Andrei Miulescu Date: Sun, 11 Aug 2019 13:22:27 +1000 Subject: [PATCH 1/8] feat: add outputFile for typescript etc --- src/utilities/writeIndex.js | 2 +- src/utilities/writeIndexCli.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utilities/writeIndex.js b/src/utilities/writeIndex.js index d12d96a..89a9ccf 100644 --- a/src/utilities/writeIndex.js +++ b/src/utilities/writeIndex.js @@ -18,7 +18,7 @@ export default (directoryPaths, 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, optionsWithConfig.outputFile || 'index.js'); fs.writeFileSync(indexFilePath, indexCode); }); diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index e7626ff..0b8d12d 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -27,7 +27,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 }); }); From 64af79d42866b174dd7eedace8e924c95b9e09a2 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 27 Sep 2019 19:06:00 +0000 Subject: [PATCH 2/8] docs: update yargs and readme --- README.md | 2 +- src/bin/create-index.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b535461..43444fa 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Options: --extensions, -x Allows some extensions to be parsed as valid source. 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 diff --git a/src/bin/create-index.js b/src/bin/create-index.js index 4919cf3..dfb657b 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -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.' From 0dbb06ecc6b3d0d005adf60c025f1f31014824a0 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 27 Sep 2019 19:07:21 +0000 Subject: [PATCH 3/8] chore: add prepare script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 7212176..b72f4b0 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "url": "https://github.com/gajus/create-index" }, "scripts": { + "prepare": "npm run build", "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", From bc314be2a1f6fb513cafb1ee3137bd6df4db1cce Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 27 Sep 2019 19:23:10 +0000 Subject: [PATCH 4/8] update --- src/bin/create-index.js | 1 + src/utilities/writeIndexCli.js | 1 + 2 files changed, 2 insertions(+) diff --git a/src/bin/create-index.js b/src/bin/create-index.js index dfb657b..055d154 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -81,5 +81,6 @@ writeIndexCli(argv._, { ignoreDirectories: argv.ignoreDirectories, ignoreUnsafe: argv.ignoreUnsafe, recursive: argv.recursive, + outputFile: argv.outputFile, updateIndex: argv.update }); diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index 0b8d12d..12aae29 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -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 { From 5b496536c64ea9542b5e5f0a09f50c77798a3a52 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 27 Sep 2019 20:06:46 +0000 Subject: [PATCH 5/8] update and fixes --- src/utilities/findIndexFiles.js | 3 ++- src/utilities/hasIndex.js | 4 ++-- src/utilities/readDirectory.js | 6 +++--- src/utilities/readIndexConfig.js | 6 +++--- src/utilities/validateTargetDirectory.js | 2 +- src/utilities/writeIndex.js | 6 +++--- src/utilities/writeIndexCli.js | 6 +++--- 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/utilities/findIndexFiles.js b/src/utilities/findIndexFiles.js index 7f9af10..a27b855 100644 --- a/src/utilities/findIndexFiles.js +++ b/src/utilities/findIndexFiles.js @@ -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 }); }); diff --git a/src/utilities/hasIndex.js b/src/utilities/hasIndex.js index 1146e4c..e8dc4f5 100644 --- a/src/utilities/hasIndex.js +++ b/src/utilities/hasIndex.js @@ -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); diff --git a/src/utilities/readDirectory.js b/src/utilities/readDirectory.js index 1fa333d..ddf5f41 100644 --- a/src/utilities/readDirectory.js +++ b/src/utilities/readDirectory.js @@ -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; } diff --git a/src/utilities/readIndexConfig.js b/src/utilities/readIndexConfig.js index 5bb8a2a..b702b73 100644 --- a/src/utilities/readIndexConfig.js +++ b/src/utilities/readIndexConfig.js @@ -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() : ''; diff --git a/src/utilities/validateTargetDirectory.js b/src/utilities/validateTargetDirectory.js index 99f457b..7649234 100644 --- a/src/utilities/validateTargetDirectory.js +++ b/src/utilities/validateTargetDirectory.js @@ -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); diff --git a/src/utilities/writeIndex.js b/src/utilities/writeIndex.js index 89a9ccf..27e65aa 100644 --- a/src/utilities/writeIndex.js +++ b/src/utilities/writeIndex.js @@ -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, optionsWithConfig.outputFile || 'index.js'); + const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js'); fs.writeFileSync(indexFilePath, indexCode); }); diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index 12aae29..e69bd87 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -40,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, @@ -60,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'); From c9d4fa24712a93967fab95ec8d585a222858ad90 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 27 Sep 2019 20:29:33 +0000 Subject: [PATCH 6/8] fix trailing comma --- src/bin/create-index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/create-index.js b/src/bin/create-index.js index 055d154..ba5a102 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -58,7 +58,7 @@ const argv = yargs alias: 'o', default: 'index.js', description: 'Output file', - type: 'string', + type: 'string' } }) .example( From 1a50178f71a271f8e8f6da531ca8dab013998df5 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 27 Sep 2019 20:40:11 +0000 Subject: [PATCH 7/8] docs: recover -x footer --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 43444fa..1f1be6b 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ Options: --extensions, -x Allows some extensions to be parsed as valid source. 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: From 669e36fe673a4404a0752a01079f188a78aa549f Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Mon, 18 May 2020 10:02:11 -0700 Subject: [PATCH 8/8] chore: remove unused script --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index b72f4b0..7212176 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "url": "https://github.com/gajus/create-index" }, "scripts": { - "prepare": "npm run build", "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",