14 Commits
Author SHA1 Message Date
Gajus Kuizinas 76c6d4b5ee fix: update dependencies 2020-05-18 10:11:25 -07:00
Gajus Kuizinas 017b834393 Merge branch 'danielpza-master' 2020-05-18 10:02:12 -07:00
Gajus Kuizinas 669e36fe67 chore: remove unused script 2020-05-18 10:02:11 -07:00
Daniel Perez AlvarezandGajus Kuizinas 1a50178f71 docs: recover -x footer 2020-05-18 10:01:37 -07:00
Daniel Perez AlvarezandGajus Kuizinas c9d4fa2471 fix trailing comma 2020-05-18 10:01:37 -07:00
Daniel Perez AlvarezandGajus Kuizinas 5b496536c6 update and fixes 2020-05-18 10:01:37 -07:00
Daniel Perez AlvarezandGajus Kuizinas bc314be2a1 update 2020-05-18 10:01:37 -07:00
Daniel Perez AlvarezandGajus Kuizinas 0dbb06ecc6 chore: add prepare script 2020-05-18 10:01:37 -07:00
Daniel Perez AlvarezandGajus Kuizinas 64af79d428 docs: update yargs and readme 2020-05-18 10:01:37 -07:00
Andrei MiulescuandGajus Kuizinas 38c4094655 feat: add outputFile for typescript etc 2020-05-18 10:01:37 -07:00
beralivandGajus Kuizinas 1f87f9a8b3 feat: cannot use create-index for short folders (#51)
* feat: file cannot be started from _ and be one symbol
* test: add children-directories-short-name for readDirectory
2019-10-27 13:37:12 +00:00
Joe WhittlesandGajus Kuizinas 6aac149a8b feat: add option to exclude directories from index.js (#50)
* Add option to exclude directories from index.js
* Update readDirectory.js
* Update writeIndexCli.js
* Update readDirectory.js
* Update README
2019-01-25 17:49:18 +00:00
Gajus KuizinasandGitHub 1efd1c6c7b chore: update .editorconfig 2018-11-12 23:43:25 +00:00
James LewisandGajus Kuizinas ed8e1d0699 feat: using writeIndex programatically was not respecting passed options configuration (#41)
* Create .gitattributes

* pass full options object to createIndexCode in writeIndex
2017-12-08 15:15:38 +00:00
32 changed files with 208 additions and 126 deletions
+5 -4
View File
@@ -1,8 +1,9 @@
{ {
"plugins": [ "plugins": [
"transform-es2015-modules-commonjs", "@babel/plugin-transform-modules-commonjs",
"transform-export-extensions", "@babel/plugin-proposal-export-default-from",
"transform-es2015-destructuring", "@babel/plugin-proposal-export-namespace-from",
"transform-es2015-parameters" "@babel/plugin-transform-destructuring",
"@babel/plugin-transform-parameters"
] ]
} }
+4 -2
View File
@@ -1,7 +1,9 @@
root = true root = true
[*] [*]
charset = utf-8
end_of_line = lf end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2 indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
+1 -1
View File
@@ -1 +1 @@
./test/fixtures test/fixtures
+1
View File
@@ -0,0 +1 @@
* text eol=lf
+5 -1
View File
@@ -4,8 +4,12 @@ node_js:
- 7 - 7
- 6 - 6
- 5 - 5
script:
- npm run lint
- npm run test
- npm run build
after_success: after_success:
- semantic-release pre && npm publish && semantic-release post - semantic-release
notifications: notifications:
email: false email: false
sudo: false sudo: false
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2016, Gajus Kuizinas (http://gajus.com/) Copyright (c) 2020, Gajus Kuizinas (http://gajus.com/)
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
+21 -7
View File
@@ -82,21 +82,35 @@ npm install create-index
create-index --help create-index --help
Options: Options:
--recursive, -r Create/update index files recursively. Halts on any unsafe --recursive, -r Create/update index files recursively. Halts on any
"index.js" files. [boolean] [default: false] unsafe "index.js" files. [boolean] [default: false]
--ignoreUnsafe, -i Ignores unsafe "index.js" files instead of halting. --ignoreUnsafe, -i Ignores unsafe "index.js" files instead of halting.
[boolean] [default: false] [boolean] [default: false]
--update, -u Updates only previously created index files (recursively). --ignoreDirectories, -d Ignores importing directories into the index file,
even if they have a safe "index.js".
[boolean] [default: false] [boolean] [default: false]
--banner Add a custom banner at the top of the index file [string] --update, -u Updates only previously created index files
(recursively). [boolean] [default: false]
--banner Add a custom banner at the top of the index file
[string]
--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: Examples:
create-index ./src ./src/utilities Creates or updates an existing create-index ./src ./src/utilities Creates or updates an existing
create-index index file in the target create-index index file in the target
(./src, ./src/utilities) directories. (./src, ./src/utilities) directories.
create-index --update ./src ./tests Finds all create-index index files in the create-index --update ./src ./tests Finds all create-index index files in
target directories and descending the target directories and descending
directories. Updates found index files. directories. Updates found index
files.
create-index ./src --extensions js jsx Creates or updates an existing
create-index index file in the target
(./src) directory for both .js and
.jsx extensions.
``` ```
### Using `create-index` Programmatically ### Using `create-index` Programmatically
+30 -25
View File
@@ -6,27 +6,28 @@
}, },
"bin": "./dist/bin/create-index.js", "bin": "./dist/bin/create-index.js",
"dependencies": { "dependencies": {
"chalk": "^1.1.3", "chalk": "^4.0.0",
"glob": "^7.1.1", "glob": "^7.1.6",
"lodash": "^4.16.6", "lodash": "^4.17.15",
"moment": "^2.15.2", "moment": "^2.25.3",
"yargs": "^6.3.0" "yargs": "^15.3.1"
}, },
"description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.", "description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.",
"devDependencies": { "devDependencies": {
"babel-cli": "^6.18.0", "@babel/cli": "^7.0.0",
"babel-plugin-transform-es2015-destructuring": "^6.18.0", "@babel/core": "^7.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0", "@babel/plugin-proposal-export-default-from": "^7.0.0",
"babel-plugin-transform-es2015-parameters": "^6.18.0", "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"babel-plugin-transform-export-extensions": "^6.8.0", "@babel/plugin-transform-destructuring": "^7.0.0",
"babel-register": "^6.18.0", "@babel/plugin-transform-modules-commonjs": "^7.0.0",
"chai": "^3.5.0", "@babel/plugin-transform-parameters": "^7.0.0",
"cross-env": "^3.1.3", "@babel/register": "^7.0.0",
"eslint": "^3.9.1", "chai": "^4.2.0",
"eslint-config-canonical": "^4.0.0", "eslint": "^7.0.0",
"husky": "^0.11.9", "eslint-config-canonical": "^20.0.4",
"mocha": "^3.1.2", "husky": "^4.2.5",
"semantic-release": "^6.3.2" "mocha": "^7.1.2",
"semantic-release": "^17.0.7"
}, },
"engines": { "engines": {
"node": ">=5" "node": ">=5"
@@ -43,12 +44,16 @@
"type": "git", "type": "git",
"url": "https://github.com/gajus/create-index" "url": "https://github.com/gajus/create-index"
}, },
"scripts": { "husky": {
"build": "cross-env NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist", "hooks": {
"create-index": "node ./dist/bin/create-index ./src/utilities", "pre-commit": "npm run test"
"lint": "cross-env NODE_ENV=development eslint ./src ./tests", }
"precommit": "npm run test",
"test": "npm run build && npm run lint && cross-env NODE_ENV=development mocha --compilers js:babel-register"
}, },
"version": "1.1.0" "scripts": {
"build": "rm -fr ./dist && NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
"create-index": "node ./dist/bin/create-index ./src/utilities",
"lint": "NODE_ENV=development eslint ./src ./test",
"test": "NODE_ENV=development mocha --require @babel/register"
},
"version": "0.0.0-development"
} }
+44 -15
View File
@@ -1,8 +1,10 @@
#!/usr/bin/env node #!/usr/bin/env node
/* eslint-disable filenames/match-regex */
import yargs from 'yargs'; import yargs from 'yargs';
import { import {
writeIndexCli writeIndexCli,
} from '../utilities'; } from '../utilities';
const argv = yargs const argv = yargs
@@ -12,48 +14,75 @@ const argv = yargs
alias: 'r', alias: 'r',
default: false, default: false,
description: 'Create/update index files recursively. Halts on any unsafe "index.js" files.', description: 'Create/update index files recursively. Halts on any unsafe "index.js" files.',
type: 'boolean' type: 'boolean',
} },
}) })
.options({ .options({
ignoreUnsafe: { ignoreUnsafe: {
alias: 'i', alias: 'i',
default: false, default: false,
description: 'Ignores unsafe "index.js" files instead of halting.', description: 'Ignores unsafe "index.js" files instead of halting.',
type: 'boolean' type: 'boolean',
} },
})
.options({
ignoreDirectories: {
alias: 'd',
default: false,
description: 'Ignores importing directories into the index file, even if they have a safe "index.js".',
type: 'boolean',
},
}) })
.options({ .options({
update: { update: {
alias: 'u', alias: 'u',
default: false, default: false,
description: 'Updates only previously created index files (recursively).', description: 'Updates only previously created index files (recursively).',
type: 'boolean' type: 'boolean',
} },
}) })
.options({ .options({
banner: { banner: {
description: 'Add a custom banner at the top of the index file', description: 'Add a custom banner at the top of the index file',
type: 'string' type: 'string',
} },
}) })
.options({ .options({
extensions: { extensions: {
alias: 'x', alias: 'x',
default: ['js'], default: ['js'],
description: 'Allows some extensions to be parsed as valid source. First extension will always be preferred to homonyms with another allowed extension.', description: 'Allows some extensions to be parsed as valid source. First extension will always be preferred to homonyms with another allowed extension.',
type: 'array' type: 'array',
} },
}) })
.example('create-index ./src ./src/utilities', 'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.') .options({
.example('create-index --update ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.') outputFile: {
.example('create-index ./src --extensions js jsx', 'Creates or updates an existing create-index index file in the target (./src) directory for both .js and .jsx extensions.') 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.',
)
.example(
'create-index --update ./src ./tests',
'Finds all create-index index files in the target directories and descending directories. Updates found index files.',
)
.example(
'create-index ./src --extensions js jsx',
'Creates or updates an existing create-index index file in the target (./src) directory for both .js and .jsx extensions.',
)
.argv; .argv;
writeIndexCli(argv._, { writeIndexCli(argv._, {
banner: argv.banner, banner: argv.banner,
extensions: argv.extensions, extensions: argv.extensions,
ignoreDirectories: argv.ignoreDirectories,
ignoreUnsafe: argv.ignoreUnsafe, ignoreUnsafe: argv.ignoreUnsafe,
outputFile: argv.outputFile,
recursive: argv.recursive, recursive: argv.recursive,
updateIndex: argv.update updateIndex: argv.update,
}); });
+1 -1
View File
@@ -1,4 +1,4 @@
export { export {
findIndexFiles, findIndexFiles,
writeIndex writeIndex,
} from './utilities'; } from './utilities';
-1
View File
@@ -1,2 +1 @@
// eslint-disable-next-line
export const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/; export const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/;
+4 -2
View File
@@ -4,7 +4,8 @@ import glob from 'glob';
import validateTargetDirectory from './validateTargetDirectory'; import validateTargetDirectory from './validateTargetDirectory';
export default (directoryPath, options = {}) => { export default (directoryPath, options = {}) => {
let fileName, targetDirectories; let fileName;
let targetDirectories;
fileName = options.fileName || 'index.js'; fileName = options.fileName || 'index.js';
fileName = './**/' + fileName; fileName = './**/' + fileName;
@@ -13,7 +14,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 outputFile: options.fileName,
silent: options.silent,
}); });
}); });
+3 -3
View File
@@ -1,14 +1,14 @@
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);
return true; return true;
} catch (error) { } catch {
return false; return false;
} }
}; };
+8 -8
View File
@@ -1,10 +1,10 @@
// @create-index // @create-index
export createIndexCode from './createIndexCode.js'; export createIndexCode from './createIndexCode';
export findIndexFiles from './findIndexFiles.js'; export findIndexFiles from './findIndexFiles';
export log from './log.js'; export log from './log';
export readDirectory from './readDirectory.js'; export readDirectory from './readDirectory';
export sortByDepth from './sortByDepth.js'; export sortByDepth from './sortByDepth';
export validateTargetDirectory from './validateTargetDirectory.js'; export validateTargetDirectory from './validateTargetDirectory';
export writeIndex from './writeIndex.js'; export writeIndex from './writeIndex';
export writeIndexCli from './writeIndexCli.js'; export writeIndexCli from './writeIndexCli';
+1 -1
View File
@@ -2,6 +2,6 @@ import chalk from 'chalk';
import moment from 'moment'; import moment from 'moment';
export default (...append) => { export default (...append) => {
// eslint-disable-next-line // eslint-disable-next-line no-console
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append); console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
}; };
+9 -8
View File
@@ -17,7 +17,7 @@ const hasMultipleExtensions = (fileName) => {
}; };
const isSafeName = (fileName) => { const isSafeName = (fileName) => {
return /^[a-z][a-z0-9._]+$/i.test(fileName); return /^[_a-z][\w.]*$/i.test(fileName);
}; };
const stripExtension = (fileName) => { const stripExtension = (fileName) => {
@@ -27,7 +27,7 @@ const stripExtension = (fileName) => {
return fileName; return fileName;
} }
return fileName.substr(0, pos); return fileName.slice(0, Math.max(0, pos));
}; };
const removeDuplicates = (files, preferredExtension) => { const removeDuplicates = (files, preferredExtension) => {
@@ -72,13 +72,14 @@ 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;
} }
const { const {
extensions = ['js'], extensions = ['js'],
config = {} config = {},
ignoreDirectories = false,
} = options; } = options;
let children; let children;
@@ -101,17 +102,17 @@ export default (directoryPath, options = {}) => {
return false; return false;
} }
if (_.startsWith(fileName, 'index.js')) { if (_.startsWith(fileName, options.outputFile || 'index.js')) {
return false; return false;
} }
if (!isDirectory && !extensions.some((ext) => { if (!isDirectory && !extensions.some((extension) => {
return _.endsWith(fileName, '.' + ext); return _.endsWith(fileName, '.' + extension);
})) { })) {
return false; return false;
} }
if (isDirectory && !hasIndex(absolutePath)) { if (isDirectory && (!hasIndex(absolutePath, options) || ignoreDirectories)) {
return false; return false;
} }
+5 -5
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() : '';
@@ -21,10 +21,10 @@ export default (directoryPath) => {
try { try {
config = JSON.parse(configLine); config = JSON.parse(configLine);
} catch (error) { } catch {
throw new Error( throw new Error(
'"' + indexPath + '" contains invalid configuration object.\n' + '"' + indexPath + '" contains invalid configuration object.\n' +
'Configuration object must be a valid JSON.' 'Configuration object must be a valid JSON.',
); );
} }
+3 -3
View File
@@ -8,7 +8,7 @@ export default (targetDirectory, options = {}) => {
try { try {
stats = fs.statSync(targetDirectory); stats = fs.statSync(targetDirectory);
} catch (error) { } catch {
if (silent) { if (silent) {
return false; return false;
} else { } else {
@@ -24,11 +24,11 @@ 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);
} catch (error) { } catch {
return true; return true;
} }
+5 -4
View File
@@ -10,15 +10,16 @@ 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, {outputFile: options.outputFile,
silent: options.ignoreUnsafe});
}); });
_.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, {config}); 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);
}); });
+13 -10
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 {
@@ -25,10 +26,10 @@ export default (directoryPaths, options = {}) => {
} }
if (options.updateIndex || options.recursive) { if (options.updateIndex || options.recursive) {
sortedDirectoryPaths = _.map(sortedDirectoryPaths, (dir) => { sortedDirectoryPaths = _.map(sortedDirectoryPaths, (directory) => {
return findIndexFiles(dir, { return findIndexFiles(directory, {
fileName: options.updateIndex ? 'index.js' : '*', fileName: options.updateIndex ? options.outputFile || 'index.js' : '*',
silent: options.updateIndex || options.ignoreUnsafe silent: options.updateIndex || options.ignoreUnsafe,
}); });
}); });
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths); sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
@@ -39,32 +40,34 @@ export default (directoryPaths, options = {}) => {
} }
sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => { sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => {
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe}); return validateTargetDirectory(directoryPath, {outputFile: options.outputFile,
silent: options.ignoreUnsafe});
}); });
_.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,
extensions: options.extensions, extensions: options.extensions,
silent: options.ignoreUnsafe ignoreDirectories: options.ignoreDirectories,
silent: options.ignoreUnsafe,
}); });
const indexCode = createIndexCode(siblings, { const indexCode = createIndexCode(siblings, {
banner: options.banner, banner: options.banner,
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');
/* eslint-disable no-empty */ /* eslint-disable no-empty */
} catch (error) { } catch {
} }
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "canonical/mocha"
}
+2 -2
View File
@@ -1,7 +1,7 @@
/* eslint-disable no-restricted-syntax */ /* eslint-disable no-restricted-syntax */
import { import {
expect expect,
} from 'chai'; } from 'chai';
import createIndexCode from '../src/utilities/createIndexCode'; import createIndexCode from '../src/utilities/createIndexCode';
import codeExample from './codeExample'; import codeExample from './codeExample';
@@ -60,7 +60,7 @@ export { default as foo } from './foo';
context('with config', () => { context('with config', () => {
it('should append config', () => { it('should append config', () => {
const config = { const config = {
ignore: ['/^zoo/'] ignore: ['/^zoo/'],
}; };
const indexCode = createIndexCode(['foo', 'bar'], {config}); const indexCode = createIndexCode(['foo', 'bar'], {config});
+1 -1
View File
@@ -1,6 +1,6 @@
import path from 'path'; import path from 'path';
import { import {
expect expect,
} from 'chai'; } from 'chai';
import glob from 'glob'; import glob from 'glob';
import findIndexFiles from '../src/utilities/findIndexFiles'; import findIndexFiles from '../src/utilities/findIndexFiles';
@@ -0,0 +1 @@
// @create-index
@@ -0,0 +1 @@
// @create-index
@@ -0,0 +1 @@
// @create-index
@@ -0,0 +1 @@
// @create-index
+18 -5
View File
@@ -1,6 +1,6 @@
import path from 'path'; import path from 'path';
import { import {
expect expect,
} from 'chai'; } from 'chai';
import readDirectory from '../src/utilities/readDirectory'; import readDirectory from '../src/utilities/readDirectory';
@@ -21,6 +21,13 @@ describe('readDirectory()', () => {
expect(names).to.deep.equal(['present.js']); expect(names).to.deep.equal(['present.js']);
}); });
}); });
context('target directory contains child directories (short safe name)', () => {
it('gets names of the children directories', () => {
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-short-name'));
expect(names).to.deep.equal(['F', 'T', '__', 'o']);
});
});
context('target directory contains child directories (unsafe name)', () => { context('target directory contains child directories (unsafe name)', () => {
it('gets names of the children directories', () => { it('gets names of the children directories', () => {
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name')); const names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name'));
@@ -34,6 +41,12 @@ describe('readDirectory()', () => {
expect(names).to.deep.equal(['bar', 'foo']); expect(names).to.deep.equal(['bar', 'foo']);
}); });
it('excludes directories if ignoreDirectories = true', () => {
const names = readDirectory(path.resolve(fixturesPath, 'children-index'), {ignoreDirectories: true});
expect(names).to.deep.equal([]);
});
}); });
context('target directory contains files', () => { context('target directory contains files', () => {
it('refers to the files (with extension)', () => { it('refers to the files (with extension)', () => {
@@ -58,7 +71,7 @@ describe('readDirectory()', () => {
}); });
context('target directory contains non js files, and allowing only jsx', () => { context('target directory contains non js files, and allowing only jsx', () => {
it('prefers file', () => { it('prefers file', () => {
const options = { extensions: ['jsx'] }; const options = {extensions: ['jsx']};
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options); const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
expect(names).to.deep.equal(['bar.jsx']); expect(names).to.deep.equal(['bar.jsx']);
@@ -66,7 +79,7 @@ describe('readDirectory()', () => {
}); });
context('target directory contains non js files, and allowing both js and jsx', () => { context('target directory contains non js files, and allowing both js and jsx', () => {
it('prefers file', () => { it('prefers file', () => {
const options = { extensions: ['js', 'jsx'] }; const options = {extensions: ['js', 'jsx']};
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options); const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
expect(names).to.deep.equal(['bar.jsx', 'present.js']); expect(names).to.deep.equal(['bar.jsx', 'present.js']);
@@ -74,7 +87,7 @@ describe('readDirectory()', () => {
}); });
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JS as it is first extension listed', () => { context('target directory contains homonyms files, and allowing both js and jsx, will prefer JS as it is first extension listed', () => {
it('prefers file', () => { it('prefers file', () => {
const options = { extensions: ['js', 'jsx'] }; const options = {extensions: ['js', 'jsx']};
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options); const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
expect(names).to.deep.equal(['bar.js', 'present.js']); expect(names).to.deep.equal(['bar.js', 'present.js']);
@@ -82,7 +95,7 @@ describe('readDirectory()', () => {
}); });
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JSX as it is first extension listed', () => { context('target directory contains homonyms files, and allowing both js and jsx, will prefer JSX as it is first extension listed', () => {
it('prefers file', () => { it('prefers file', () => {
const options = { extensions: ['jsx', 'js'] }; const options = {extensions: ['jsx', 'js']};
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options); const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
expect(names).to.deep.equal(['bar.jsx', 'present.js']); expect(names).to.deep.equal(['bar.jsx', 'present.js']);
+4 -4
View File
@@ -1,6 +1,6 @@
import path from 'path'; import path from 'path';
import { import {
expect expect,
} from 'chai'; } from 'chai';
import readIndexConfig from '../src/utilities/readIndexConfig'; import readIndexConfig from '../src/utilities/readIndexConfig';
@@ -8,15 +8,15 @@ const fixtures = {
noIndex: path.resolve(__dirname, 'fixtures/read-index-config/no-index'), noIndex: path.resolve(__dirname, 'fixtures/read-index-config/no-index'),
withConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-config'), withConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-config'),
withInvalidConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-invalid-config'), withInvalidConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-invalid-config'),
withoutConfig: path.resolve(__dirname, 'fixtures/read-index-config/without-config') withoutConfig: path.resolve(__dirname, 'fixtures/read-index-config/without-config'),
}; };
const expectedValues = { const expectedValues = {
noIndex: {}, noIndex: {},
withConfig: { withConfig: {
ignore: ['/foo.js$/'] ignore: ['/foo.js$/'],
}, },
withoutConfig: {} withoutConfig: {},
}; };
describe('readIndexConfig()', () => { describe('readIndexConfig()', () => {
+3 -3
View File
@@ -1,7 +1,7 @@
import { import {
expect expect,
} from 'chai'; } from 'chai';
import sortByDepth from './../src/utilities/sortByDepth'; import sortByDepth from '../src/utilities/sortByDepth';
describe('sortByDepth()', () => { describe('sortByDepth()', () => {
it('sorts from deepest to the most shallow', () => { it('sorts from deepest to the most shallow', () => {
@@ -9,7 +9,7 @@ describe('sortByDepth()', () => {
'/b', '/b',
'/a', '/a',
'/a/b/c', '/a/b/c',
'/a/b' '/a/b',
]; ];
const sortedPaths = sortByDepth(paths); const sortedPaths = sortByDepth(paths);
+1 -1
View File
@@ -2,7 +2,7 @@
import path from 'path'; import path from 'path';
import { import {
expect expect,
} from 'chai'; } from 'chai';
import validateTargetDirectory from '../src/utilities/validateTargetDirectory'; import validateTargetDirectory from '../src/utilities/validateTargetDirectory';
+2 -2
View File
@@ -3,7 +3,7 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { import {
expect expect,
} from 'chai'; } from 'chai';
import writeIndex from '../src/utilities/writeIndex'; import writeIndex from '../src/utilities/writeIndex';
import codeExample from './codeExample'; import codeExample from './codeExample';
@@ -40,7 +40,7 @@ export { default as foo } from './foo.js';
it('creates index with config in target directory', () => { it('creates index with config in target directory', () => {
const indexFilePath = path.resolve(fixturesPath, 'with-config/index.js'); const indexFilePath = path.resolve(fixturesPath, 'with-config/index.js');
// eslint-disable-next-line // eslint-disable-next-line quotes
const ignoredExportLine = `export { default as bar } from './bar.js';`; const ignoredExportLine = `export { default as bar } from './bar.js';`;
appendToFile(indexFilePath, ignoredExportLine); appendToFile(indexFilePath, ignoredExportLine);