Compare commits
@@ -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
@@ -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
@@ -1 +1 @@
|
|||||||
./test/fixtures
|
test/fixtures
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
* text eol=lf
|
||||||
+5
-1
@@ -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,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
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ This created `index.js` with:
|
|||||||
```js
|
```js
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar.js';
|
export { default as bar } from './bar.js';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -66,9 +66,9 @@ This have updated `index.js` file:
|
|||||||
```js
|
```js
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar.js';
|
export { default as bar } from './bar.js';
|
||||||
export baz from './baz.js';
|
export { default as baz } from './baz.js';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -82,19 +82,35 @@ npm install create-index
|
|||||||
create-index --help
|
create-index --help
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--update, -u Recursively iterates target directories looking for "index.js"
|
--recursive, -r Create/update index files recursively. Halts on any
|
||||||
files that start with "// @create-index\n" (create-index index
|
unsafe "index.js" files. [boolean] [default: false]
|
||||||
file). Updates found index files. Does not create new index
|
--ignoreUnsafe, -i Ignores unsafe "index.js" files instead of halting.
|
||||||
files. [boolean] [default: false]
|
[boolean] [default: false]
|
||||||
--banner Add a custom banner at the top of the index file [string]
|
--ignoreDirectories, -d Ignores importing directories into the index file,
|
||||||
|
even if they have a safe "index.js".
|
||||||
|
[boolean] [default: false]
|
||||||
|
--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
|
||||||
@@ -175,8 +191,8 @@ Given the above directory contents, `./index.js` will be:
|
|||||||
```js
|
```js
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
import bar from './bar';
|
import { default as bar } from './bar';
|
||||||
import foo from './foo.js';
|
import { default as foo } from './foo.js';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
bar,
|
bar,
|
||||||
@@ -191,3 +207,37 @@ Directories that do not have `./index.js` in themselves will be excluded.
|
|||||||
When run again, `create-index` will update existing `./index.js` if it starts with `// @create-index\n\n`.
|
When run again, `create-index` will update existing `./index.js` if it starts with `// @create-index\n\n`.
|
||||||
|
|
||||||
If `create-index` is executed against a directory that contains `./index.js`, which does not start with `// @create-index\n\n`, an error will be thrown.
|
If `create-index` is executed against a directory that contains `./index.js`, which does not start with `// @create-index\n\n`, an error will be thrown.
|
||||||
|
|
||||||
|
## Ignore files on `--update`
|
||||||
|
|
||||||
|
`create-index` can ignore files in a directory if `./index.js` contains special object with defined `ignore` property which takes `an array` of `regular expressions` defined as `strings`, e.g.
|
||||||
|
|
||||||
|
```js
|
||||||
|
> cat index.js
|
||||||
|
// @create-index {"ignore": ["/baz.js$/"]}
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
> tree ./
|
||||||
|
./
|
||||||
|
├── bar.js
|
||||||
|
├── baz.js
|
||||||
|
├── foo.js
|
||||||
|
└── index.js
|
||||||
|
|
||||||
|
0 directories, 4 files
|
||||||
|
```
|
||||||
|
|
||||||
|
Given the above directory contents, after running `create-index` with `--update` flag, `./index.js` will be:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// @create-index {"ignore": ["/baz.js$/"]}
|
||||||
|
|
||||||
|
import { default as bar } from './bar.js';
|
||||||
|
import { default as foo } from './foo.js';
|
||||||
|
|
||||||
|
export {
|
||||||
|
bar,
|
||||||
|
foo
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|||||||
+28
-22
@@ -6,26 +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",
|
||||||
"eslint": "^3.9.1",
|
"@babel/register": "^7.0.0",
|
||||||
"eslint-config-canonical": "^4.0.0",
|
"chai": "^4.2.0",
|
||||||
"husky": "^0.11.9",
|
"eslint": "^7.0.0",
|
||||||
"mocha": "^3.1.2",
|
"eslint-config-canonical": "^20.0.4",
|
||||||
"semantic-release": "^6.3.2"
|
"husky": "^4.2.5",
|
||||||
|
"mocha": "^7.1.2",
|
||||||
|
"semantic-release": "^17.0.7"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=5"
|
"node": ">=5"
|
||||||
@@ -42,12 +44,16 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/gajus/create-index"
|
"url": "https://github.com/gajus/create-index"
|
||||||
},
|
},
|
||||||
|
"husky": {
|
||||||
|
"hooks": {
|
||||||
|
"pre-commit": "npm run test"
|
||||||
|
}
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
|
"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",
|
"create-index": "node ./dist/bin/create-index ./src/utilities",
|
||||||
"lint": "NODE_ENV=development eslint ./src ./tests",
|
"lint": "NODE_ENV=development eslint ./src ./test",
|
||||||
"precommit": "npm run test",
|
"test": "NODE_ENV=development mocha --require @babel/register"
|
||||||
"test": "npm run build && npm run lint && NODE_ENV=development mocha --compilers js:babel-register"
|
|
||||||
},
|
},
|
||||||
"version": "1.0.0"
|
"version": "0.0.0-development"
|
||||||
}
|
}
|
||||||
|
|||||||
+66
-9
@@ -1,31 +1,88 @@
|
|||||||
#!/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
|
||||||
.demand(1)
|
.demand(1)
|
||||||
|
.options({
|
||||||
|
recursive: {
|
||||||
|
alias: 'r',
|
||||||
|
default: false,
|
||||||
|
description: 'Create/update index files recursively. Halts on any unsafe "index.js" files.',
|
||||||
|
type: 'boolean',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.options({
|
||||||
|
ignoreUnsafe: {
|
||||||
|
alias: 'i',
|
||||||
|
default: false,
|
||||||
|
description: 'Ignores unsafe "index.js" files instead of halting.',
|
||||||
|
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: 'Recursively iterates target directories looking for "index.js" files that start with "// @create-index\\n" (create-index index file). Updates found index files. Does not create new index files.',
|
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',
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
.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.')
|
extensions: {
|
||||||
|
alias: 'x',
|
||||||
|
default: ['js'],
|
||||||
|
description: 'Allows some extensions to be parsed as valid source. First extension will always be preferred to homonyms with another allowed extension.',
|
||||||
|
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.',
|
||||||
|
)
|
||||||
|
.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,
|
||||||
updateIndex: argv.update
|
extensions: argv.extensions,
|
||||||
|
ignoreDirectories: argv.ignoreDirectories,
|
||||||
|
ignoreUnsafe: argv.ignoreUnsafe,
|
||||||
|
outputFile: argv.outputFile,
|
||||||
|
recursive: argv.recursive,
|
||||||
|
updateIndex: argv.update,
|
||||||
});
|
});
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
export {
|
export {
|
||||||
findIndexFiles,
|
findIndexFiles,
|
||||||
writeIndex
|
writeIndex,
|
||||||
} from './utilities';
|
} from './utilities';
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
export const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/;
|
||||||
@@ -14,7 +14,7 @@ const buildExportBlock = (files) => {
|
|||||||
let importBlock;
|
let importBlock;
|
||||||
|
|
||||||
importBlock = _.map(files, (fileName) => {
|
importBlock = _.map(files, (fileName) => {
|
||||||
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
return 'export { default as ' + safeVariableName(fileName) + ' } from \'./' + fileName + '\';';
|
||||||
});
|
});
|
||||||
|
|
||||||
importBlock = importBlock.join('\n');
|
importBlock = importBlock.join('\n');
|
||||||
@@ -24,8 +24,10 @@ const buildExportBlock = (files) => {
|
|||||||
|
|
||||||
export default (filePaths, options = {}) => {
|
export default (filePaths, options = {}) => {
|
||||||
let code;
|
let code;
|
||||||
|
let configCode;
|
||||||
|
|
||||||
code = '';
|
code = '';
|
||||||
|
configCode = '';
|
||||||
|
|
||||||
if (options.banner) {
|
if (options.banner) {
|
||||||
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
|
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
|
||||||
@@ -37,7 +39,11 @@ export default (filePaths, options = {}) => {
|
|||||||
code += '\n';
|
code += '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
code += '// @create-index\n\n';
|
if (options.config && _.size(options.config) > 0) {
|
||||||
|
configCode += ' ' + JSON.stringify(options.config);
|
||||||
|
}
|
||||||
|
|
||||||
|
code += '// @create-index' + configCode + '\n\n';
|
||||||
|
|
||||||
if (filePaths.length) {
|
if (filePaths.length) {
|
||||||
const sortedFilePaths = filePaths.sort();
|
const sortedFilePaths = filePaths.sort();
|
||||||
|
|||||||
@@ -3,23 +3,20 @@ import _ from 'lodash';
|
|||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
export default (directoryPath) => {
|
export default (directoryPath, options = {}) => {
|
||||||
|
let fileName;
|
||||||
let targetDirectories;
|
let targetDirectories;
|
||||||
|
|
||||||
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
fileName = options.fileName || 'index.js';
|
||||||
|
fileName = './**/' + fileName;
|
||||||
|
|
||||||
|
targetDirectories = glob.sync(path.join(directoryPath, fileName));
|
||||||
|
|
||||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||||
try {
|
return validateTargetDirectory(path.dirname(targetDirectoryPath), {
|
||||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
outputFile: options.fileName,
|
||||||
|
silent: options.silent,
|
||||||
return true;
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
export default (directoryPath, options = {}) => {
|
||||||
|
const indexPath = path.resolve(directoryPath, options.outputFile || 'index.js');
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.statSync(indexPath);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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';
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import chalk from 'chalk';
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
export default (...append) => {
|
export default (...append) => {
|
||||||
/* eslint-disable no-console */
|
// 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);
|
||||||
|
|
||||||
/* eslint-enable no-console */
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,20 +1,9 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import hasIndex from './hasIndex';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
const hasIndex = (directoryPath) => {
|
|
||||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
|
||||||
|
|
||||||
try {
|
|
||||||
fs.statSync(indexPath);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const hasNoExtension = (fileName) => {
|
const hasNoExtension = (fileName) => {
|
||||||
const matches = fileName.match(/\./g);
|
const matches = fileName.match(/\./g);
|
||||||
|
|
||||||
@@ -28,19 +17,72 @@ 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 removeDuplicates = (files) => {
|
const stripExtension = (fileName) => {
|
||||||
|
const pos = fileName.lastIndexOf('.');
|
||||||
|
|
||||||
|
if (pos === -1) {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName.slice(0, Math.max(0, pos));
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeDuplicates = (files, preferredExtension) => {
|
||||||
return _.filter(files, (fileName) => {
|
return _.filter(files, (fileName) => {
|
||||||
return !_.includes(files, fileName + '.js');
|
const withoutExtension = stripExtension(fileName);
|
||||||
|
const mainAlternative = withoutExtension + '.' + preferredExtension;
|
||||||
|
|
||||||
|
if (mainAlternative === fileName) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !_.includes(files, mainAlternative);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (directoryPath) => {
|
const removeIgnoredFiles = (files, ignorePatterns = []) => {
|
||||||
let children;
|
if (ignorePatterns.length === 0) {
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
validateTargetDirectory(directoryPath);
|
const patterns = ignorePatterns.map((pattern) => {
|
||||||
|
if (_.startsWith(pattern, '/') && _.endsWith(pattern, '/')) {
|
||||||
|
const patternWithoutSlashes = pattern.slice(1, -1);
|
||||||
|
|
||||||
|
return new RegExp(patternWithoutSlashes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new RegExp(pattern);
|
||||||
|
});
|
||||||
|
|
||||||
|
return _.filter(files, (fileName) => {
|
||||||
|
let pattern;
|
||||||
|
|
||||||
|
for (pattern of patterns) {
|
||||||
|
if (fileName.match(pattern) !== null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default (directoryPath, options = {}) => {
|
||||||
|
if (!validateTargetDirectory(directoryPath, options)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
extensions = ['js'],
|
||||||
|
config = {},
|
||||||
|
ignoreDirectories = false,
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
let children;
|
||||||
|
|
||||||
children = fs.readdirSync(directoryPath);
|
children = fs.readdirSync(directoryPath);
|
||||||
|
|
||||||
@@ -60,22 +102,25 @@ export default (directoryPath) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_.startsWith(fileName, 'index.js')) {
|
if (_.startsWith(fileName, options.outputFile || 'index.js')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
if (!isDirectory && !extensions.some((extension) => {
|
||||||
|
return _.endsWith(fileName, '.' + extension);
|
||||||
|
})) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDirectory && !hasIndex(absolutePath)) {
|
if (isDirectory && (!hasIndex(absolutePath, options) || ignoreDirectories)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
children = removeDuplicates(children);
|
children = removeDuplicates(children, extensions[0]);
|
||||||
|
children = removeIgnoredFiles(children, config.ignore);
|
||||||
|
|
||||||
return children.sort();
|
return children.sort();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import hasIndex from './hasIndex';
|
||||||
|
import {CREATE_INDEX_PATTERN} from './constants';
|
||||||
|
|
||||||
|
export default (directoryPath, options = {}) => {
|
||||||
|
if (!hasIndex(directoryPath, options)) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
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() : '';
|
||||||
|
|
||||||
|
if (configLine.length === 0) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
let config;
|
||||||
|
|
||||||
|
try {
|
||||||
|
config = JSON.parse(configLine);
|
||||||
|
} catch {
|
||||||
|
throw new Error(
|
||||||
|
'"' + indexPath + '" contains invalid configuration object.\n' +
|
||||||
|
'Configuration object must be a valid JSON.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
};
|
||||||
@@ -1,33 +1,47 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import _ from 'lodash';
|
import {CREATE_INDEX_PATTERN} from './constants';
|
||||||
|
|
||||||
export default (targetDirectory) => {
|
export default (targetDirectory, options = {}) => {
|
||||||
|
const silent = options.silent;
|
||||||
let stats;
|
let stats;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stats = fs.statSync(targetDirectory);
|
stats = fs.statSync(targetDirectory);
|
||||||
} catch (error) {
|
} catch {
|
||||||
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
throw new Error('"' + targetDirectory + '" is not a directory.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|
||||||
if (!_.startsWith(indexFile, '// @create-index\n')) {
|
if (!indexFile.match(CREATE_INDEX_PATTERN)) {
|
||||||
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,19 +4,22 @@ import _ from 'lodash';
|
|||||||
import createIndexCode from './createIndexCode';
|
import createIndexCode from './createIndexCode';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
import readDirectory from './readDirectory';
|
import readDirectory from './readDirectory';
|
||||||
|
import readIndexConfig from './readIndexConfig';
|
||||||
import sortByDepth from './sortByDepth';
|
import sortByDepth from './sortByDepth';
|
||||||
|
|
||||||
export default (directoryPaths) => {
|
export default (directoryPaths, options = {}) => {
|
||||||
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
const sortedDirectoryPaths = sortByDepth(directoryPaths)
|
||||||
|
.filter((directoryPath) => {
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
return validateTargetDirectory(directoryPath, {outputFile: options.outputFile,
|
||||||
validateTargetDirectory(directoryPath);
|
silent: options.ignoreUnsafe});
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
const siblings = readDirectory(directoryPath);
|
const config = readIndexConfig(directoryPath, options);
|
||||||
const indexCode = createIndexCode(siblings);
|
const optionsWithConfig = Object.assign({}, options, {config});
|
||||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
const siblings = readDirectory(directoryPath, optionsWithConfig);
|
||||||
|
const indexCode = createIndexCode(siblings, optionsWithConfig);
|
||||||
|
const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js');
|
||||||
|
|
||||||
fs.writeFileSync(indexFilePath, indexCode);
|
fs.writeFileSync(indexFilePath, indexCode);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import chalk from 'chalk';
|
|||||||
import createIndexCode from './createIndexCode';
|
import createIndexCode from './createIndexCode';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
import readDirectory from './readDirectory';
|
import readDirectory from './readDirectory';
|
||||||
|
import readIndexConfig from './readIndexConfig';
|
||||||
import sortByDepth from './sortByDepth';
|
import sortByDepth from './sortByDepth';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
import findIndexFiles from './findIndexFiles';
|
import findIndexFiles from './findIndexFiles';
|
||||||
@@ -15,37 +16,58 @@ export default (directoryPaths, options = {}) => {
|
|||||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||||
|
|
||||||
log('Target directories', sortedDirectoryPaths);
|
log('Target directories', sortedDirectoryPaths);
|
||||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
log('Output file', options.outputFile);
|
||||||
|
|
||||||
if (options.updateIndex) {
|
if (options.updateIndex) {
|
||||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||||
|
} else {
|
||||||
|
log('Recursive:', options.recursive ? chalk.green('true') : chalk.red('false'));
|
||||||
|
log('Ignore unsafe:', options.ignoreUnsafe ? chalk.green('true') : chalk.red('false'));
|
||||||
|
log('Extensions:', chalk.green(options.extensions));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.updateIndex || options.recursive) {
|
||||||
|
sortedDirectoryPaths = _.map(sortedDirectoryPaths, (directory) => {
|
||||||
|
return findIndexFiles(directory, {
|
||||||
|
fileName: options.updateIndex ? options.outputFile || 'index.js' : '*',
|
||||||
|
silent: options.updateIndex || options.ignoreUnsafe,
|
||||||
|
});
|
||||||
|
});
|
||||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||||
|
|
||||||
log('Found index file in:', sortedDirectoryPaths);
|
log('Updating index files in:', sortedDirectoryPaths.reverse().join(', '));
|
||||||
}
|
}
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => {
|
||||||
validateTargetDirectory(directoryPath);
|
return validateTargetDirectory(directoryPath, {outputFile: options.outputFile,
|
||||||
|
silent: options.ignoreUnsafe});
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
let existingIndexCode;
|
let existingIndexCode;
|
||||||
|
|
||||||
const siblings = readDirectory(directoryPath);
|
const config = readIndexConfig(directoryPath, options);
|
||||||
|
|
||||||
const indexCode = createIndexCode(siblings, {
|
const siblings = readDirectory(directoryPath, {
|
||||||
banner: options.banner
|
config,
|
||||||
|
extensions: options.extensions,
|
||||||
|
ignoreDirectories: options.ignoreDirectories,
|
||||||
|
silent: options.ignoreUnsafe,
|
||||||
});
|
});
|
||||||
|
|
||||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
const indexCode = createIndexCode(siblings, {
|
||||||
|
banner: options.banner,
|
||||||
|
config,
|
||||||
|
});
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +76,7 @@ export default (directoryPaths, options = {}) => {
|
|||||||
fs.writeFileSync(indexFilePath, indexCode);
|
fs.writeFileSync(indexFilePath, indexCode);
|
||||||
|
|
||||||
if (existingIndexCode && existingIndexCode === indexCode) {
|
if (existingIndexCode && existingIndexCode === indexCode) {
|
||||||
log(indexFilePath, chalk.yellow('[index have not changed]'));
|
log(indexFilePath, chalk.yellow('[index has not changed]'));
|
||||||
} else if (existingIndexCode && existingIndexCode !== indexCode) {
|
} else if (existingIndexCode && existingIndexCode !== indexCode) {
|
||||||
log(indexFilePath, chalk.green('[updated index]'));
|
log(indexFilePath, chalk.green('[updated index]'));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"extends": "canonical/mocha"
|
||||||
|
}
|
||||||
+23
-7
@@ -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';
|
||||||
@@ -20,7 +20,7 @@ describe('createIndexCode()', () => {
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
it('describes multiple children', () => {
|
it('describes multiple children', () => {
|
||||||
@@ -29,8 +29,8 @@ export foo from './foo';
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
context('file with extension', () => {
|
context('file with extension', () => {
|
||||||
@@ -40,7 +40,7 @@ export foo from './foo';
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -51,8 +51,24 @@ export foo from './foo.js';
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('with config', () => {
|
||||||
|
it('should append config', () => {
|
||||||
|
const config = {
|
||||||
|
ignore: ['/^zoo/'],
|
||||||
|
};
|
||||||
|
const indexCode = createIndexCode(['foo', 'bar'], {config});
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index {"ignore":["/^zoo/"]}
|
||||||
|
|
||||||
|
export { default as bar } from './bar';
|
||||||
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// @create-index {"ignore": ["/foo.js$/"]}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// @create-index {ignore: 'foo'}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// @create-index
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
|
// @create-index
|
||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// @create-index {"ignore":["/bar.js$/"]}
|
||||||
|
|
||||||
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
+53
-1
@@ -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)', () => {
|
||||||
@@ -49,6 +62,45 @@ describe('readDirectory()', () => {
|
|||||||
expect(names).to.deep.equal(['present.js']);
|
expect(names).to.deep.equal(['present.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
context('target directory contains non js files, and not configured to allow that', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'));
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains non js files, and allowing only jsx', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = {extensions: ['jsx']};
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.jsx']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains non js files, and allowing both js and jsx', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = {extensions: ['js', 'jsx']};
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.jsx', 'present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JS as it is first extension listed', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = {extensions: ['js', 'jsx']};
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.js', 'present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JSX as it is first extension listed', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = {extensions: ['jsx', 'js']};
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.jsx', 'present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
context('target directory contains files with no extension', () => {
|
context('target directory contains files with no extension', () => {
|
||||||
it('ignores files', () => {
|
it('ignores files', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import {
|
||||||
|
expect,
|
||||||
|
} from 'chai';
|
||||||
|
import readIndexConfig from '../src/utilities/readIndexConfig';
|
||||||
|
|
||||||
|
const fixtures = {
|
||||||
|
noIndex: path.resolve(__dirname, 'fixtures/read-index-config/no-index'),
|
||||||
|
withConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-config'),
|
||||||
|
withInvalidConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-invalid-config'),
|
||||||
|
withoutConfig: path.resolve(__dirname, 'fixtures/read-index-config/without-config'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectedValues = {
|
||||||
|
noIndex: {},
|
||||||
|
withConfig: {
|
||||||
|
ignore: ['/foo.js$/'],
|
||||||
|
},
|
||||||
|
withoutConfig: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('readIndexConfig()', () => {
|
||||||
|
context('When valid config is defined', () => {
|
||||||
|
it('reads config object', () => {
|
||||||
|
const config = readIndexConfig(fixtures.withConfig);
|
||||||
|
|
||||||
|
expect(config).to.deep.equal(expectedValues.withConfig);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('When invalid config is defined', () => {
|
||||||
|
it('should throw an error', () => {
|
||||||
|
const wrappedReadIndexConfig = () => {
|
||||||
|
readIndexConfig(fixtures.withInvalidConfig);
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(wrappedReadIndexConfig).to.throw(/Configuration object must be a valid JSON./);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('When config is NOT defined', () => {
|
||||||
|
it('returns an empty object', () => {
|
||||||
|
const config = readIndexConfig(fixtures.withoutConfig);
|
||||||
|
|
||||||
|
expect(config).to.deep.equal(expectedValues.withoutConfig);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('When index file doesn\'t exist', () => {
|
||||||
|
it('returns an empty object', () => {
|
||||||
|
const config = readIndexConfig(fixtures.withoutConfig);
|
||||||
|
|
||||||
|
expect(config).to.deep.equal(expectedValues.noIndex);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
+3
-3
@@ -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);
|
||||||
|
|||||||
@@ -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';
|
||||||
|
|
||||||
@@ -36,6 +36,11 @@ describe('validateTargetDirectory()', () => {
|
|||||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
context('safe with banner', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index-with-banner'))).to.equal(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
context('unsafe', () => {
|
context('unsafe', () => {
|
||||||
it('throws an error', () => {
|
it('throws an error', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
@@ -43,5 +48,10 @@ describe('validateTargetDirectory()', () => {
|
|||||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
context('unsafe ignored', () => {
|
||||||
|
it('returns false', () => {
|
||||||
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'unsafe-index'), {silent: true})).to.equal(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+35
-13
@@ -3,34 +3,56 @@
|
|||||||
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';
|
||||||
|
|
||||||
|
const readFile = (filePath) => {
|
||||||
|
return fs.readFileSync(filePath, 'utf8');
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeFile = (filePath) => {
|
||||||
|
fs.unlinkSync(filePath);
|
||||||
|
};
|
||||||
|
|
||||||
|
const appendToFile = (filePath, content) => {
|
||||||
|
fs.appendFileSync(filePath, content, 'utf-8');
|
||||||
|
};
|
||||||
|
|
||||||
const fixturesPath = path.resolve(__dirname, 'fixtures/write-index');
|
const fixturesPath = path.resolve(__dirname, 'fixtures/write-index');
|
||||||
|
|
||||||
describe('writeIndex()', () => {
|
describe('writeIndex()', () => {
|
||||||
it('creates index in target directory', () => {
|
it('creates index in target directory', () => {
|
||||||
const indexFilePath = path.resolve(fixturesPath, 'mixed/index.js');
|
const indexFilePath = path.resolve(fixturesPath, 'mixed/index.js');
|
||||||
|
|
||||||
try {
|
removeFile(indexFilePath);
|
||||||
fs.unlinkSync(indexFilePath);
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
writeIndex([path.resolve(fixturesPath, 'mixed')]);
|
writeIndex([path.resolve(fixturesPath, 'mixed')]);
|
||||||
|
const indexCode = readFile(indexFilePath);
|
||||||
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates index with config in target directory', () => {
|
||||||
|
const indexFilePath = path.resolve(fixturesPath, 'with-config/index.js');
|
||||||
|
// eslint-disable-next-line quotes
|
||||||
|
const ignoredExportLine = `export { default as bar } from './bar.js';`;
|
||||||
|
|
||||||
|
appendToFile(indexFilePath, ignoredExportLine);
|
||||||
|
expect(readFile(indexFilePath).includes(ignoredExportLine)).to.equal(true);
|
||||||
|
|
||||||
|
writeIndex([path.resolve(fixturesPath, 'with-config')]);
|
||||||
|
const indexCode = readFile(indexFilePath);
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index {"ignore":["/bar.js$/"]}
|
||||||
|
|
||||||
|
export { default as foo } from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user