Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76c6d4b5ee | ||
|
|
017b834393 | ||
|
|
669e36fe67 | ||
|
|
1a50178f71 | ||
|
|
c9d4fa2471 | ||
|
|
5b496536c6 | ||
|
|
bc314be2a1 | ||
|
|
0dbb06ecc6 | ||
|
|
64af79d428 | ||
|
|
38c4094655 | ||
|
|
1f87f9a8b3 | ||
|
|
6aac149a8b | ||
|
|
1efd1c6c7b | ||
|
|
ed8e1d0699 | ||
|
|
bf45925380 | ||
|
|
5fea5e63d7 | ||
|
|
7353f04394 | ||
|
|
65892e3bb8 | ||
|
|
893d9931d4 | ||
|
|
9113572a43 | ||
|
|
cedb893e7c | ||
|
|
2bafc5bb85 | ||
|
|
5839536cf0 | ||
|
|
219f546c67 | ||
|
|
2450b7202e | ||
|
|
707ae70240 | ||
|
|
c6952fa1e1 | ||
|
|
e6ebaec387 | ||
|
|
1f0a517797 | ||
|
|
7867b8f8c7 | ||
|
|
9cfc2ad367 | ||
|
|
ab6c399241 | ||
|
|
fdb17d0179 | ||
|
|
d7e2528999 | ||
|
|
974142c38c | ||
|
|
be0e6fdc75 | ||
|
|
91c2e4eae3 | ||
|
|
c41edde1cd | ||
|
|
d05b635834 | ||
|
|
9278b25dcd | ||
|
|
3c5e96f8c0 | ||
|
|
696b817ce4 | ||
|
|
2faeba74b3 | ||
|
|
eac728be0b | ||
|
|
2fa0d6635f | ||
|
|
08b9abc45a | ||
|
|
a61cb3a14e | ||
|
|
272a836c96 | ||
|
|
78109764f7 | ||
|
|
335152f0d4 | ||
|
|
858085656b | ||
|
|
7990da993f | ||
|
|
057949b3db | ||
|
|
9579637f8e | ||
|
|
0fa96772a8 | ||
|
|
99fd807dfb | ||
|
|
67be5ec70c | ||
|
|
a8a5332d5c | ||
|
|
329533a130 | ||
|
|
c5f0452f10 | ||
|
|
00c8312dc1 | ||
|
|
36ea8228ee | ||
|
|
f41753b978 | ||
|
|
a23c3ad866 | ||
|
|
dc80afa816 |
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"@babel/plugin-transform-modules-commonjs",
|
||||||
|
"@babel/plugin-proposal-export-default-from",
|
||||||
|
"@babel/plugin-proposal-export-namespace-from",
|
||||||
|
"@babel/plugin-transform-destructuring",
|
||||||
|
"@babel/plugin-transform-parameters"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_size = 2
|
||||||
|
indent_style = space
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
test/fixtures
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
* text eol=lf
|
||||||
+5
-2
@@ -1,9 +1,12 @@
|
|||||||
node_modules
|
|
||||||
coverage
|
coverage
|
||||||
dist
|
dist
|
||||||
|
node_modules
|
||||||
*.log
|
*.log
|
||||||
.*
|
.*
|
||||||
|
!.babelrc
|
||||||
|
!.editorconfig
|
||||||
|
!.eslintignore
|
||||||
|
!.eslintrc
|
||||||
!.gitignore
|
!.gitignore
|
||||||
!.npmignore
|
!.npmignore
|
||||||
!.babelrc
|
|
||||||
!.travis.yml
|
!.travis.yml
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
|
coverage
|
||||||
fixtures
|
fixtures
|
||||||
src
|
src
|
||||||
tests
|
test
|
||||||
coverage
|
|
||||||
.*
|
.*
|
||||||
*.log
|
*.log
|
||||||
|
|||||||
+10
-2
@@ -1,7 +1,15 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- '5.0'
|
- node
|
||||||
- '4.0'
|
- 7
|
||||||
|
- 6
|
||||||
|
- 5
|
||||||
|
script:
|
||||||
|
- npm run lint
|
||||||
|
- npm run test
|
||||||
|
- npm run build
|
||||||
|
after_success:
|
||||||
|
- 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
|
||||||
|
|||||||
@@ -6,6 +6,72 @@
|
|||||||
|
|
||||||
`create-index` program creates (and maintains) ES6 `./index.js` file in target directories that imports and exports sibling files and directories.
|
`create-index` program creates (and maintains) ES6 `./index.js` file in target directories that imports and exports sibling files and directories.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```sh
|
||||||
|
> tree ./
|
||||||
|
./
|
||||||
|
├── bar.js
|
||||||
|
└── foo.js
|
||||||
|
|
||||||
|
0 directories, 2 files
|
||||||
|
|
||||||
|
> create-index ./
|
||||||
|
[13:17:34] Target directories [ './' ]
|
||||||
|
[13:17:34] Update index: false
|
||||||
|
[13:17:34] ./index.js [created index]
|
||||||
|
[13:17:34] Done
|
||||||
|
|
||||||
|
> tree
|
||||||
|
.
|
||||||
|
├── bar.js
|
||||||
|
├── foo.js
|
||||||
|
└── index.js
|
||||||
|
|
||||||
|
0 directories, 3 files
|
||||||
|
```
|
||||||
|
|
||||||
|
This created `index.js` with:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// @create-index
|
||||||
|
|
||||||
|
export { default as bar } from './bar.js';
|
||||||
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Lets create a new file and re-run `create-index`:
|
||||||
|
|
||||||
|
```js
|
||||||
|
> touch baz.js
|
||||||
|
> tree ./
|
||||||
|
./
|
||||||
|
├── bar.js
|
||||||
|
├── baz.js
|
||||||
|
├── foo.js
|
||||||
|
└── index.js
|
||||||
|
|
||||||
|
0 directories, 4 files
|
||||||
|
|
||||||
|
> create-index ./
|
||||||
|
[13:21:55] Target directories [ './' ]
|
||||||
|
[13:21:55] Update index: false
|
||||||
|
[13:21:55] ./index.js [updated index]
|
||||||
|
[13:21:55] Done
|
||||||
|
```
|
||||||
|
|
||||||
|
This have updated `index.js` file:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// @create-index
|
||||||
|
|
||||||
|
export { default as bar } from './bar.js';
|
||||||
|
export { default as baz } from './baz.js';
|
||||||
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Using CLI Program
|
### Using CLI Program
|
||||||
@@ -16,20 +82,35 @@ npm install create-index
|
|||||||
create-index --help
|
create-index --help
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--update-index 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]
|
||||||
|
--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
|
create-index index file in the target
|
||||||
target (./src, ./src/utilities)
|
(./src, ./src/utilities) directories.
|
||||||
directories.
|
create-index --update ./src ./tests Finds all create-index index files in
|
||||||
create-index --update-index ./src Finds all create-index index files
|
the target directories and descending
|
||||||
./tests in the target directories and
|
directories. Updates found index
|
||||||
descending directories. Updates
|
files.
|
||||||
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
|
||||||
@@ -88,10 +169,10 @@ Note that the `writeIndex` function is synchronous.
|
|||||||
If there is no `./index.js`, it will create a new file, e.g.
|
If there is no `./index.js`, it will create a new file, e.g.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
'create index';
|
// @create-index
|
||||||
```
|
```
|
||||||
|
|
||||||
Created index file must start with `'create index';\n\n`. This is used to make sure that `create-index` does not accidentally overwrite your local files.
|
Created index file must start with `// @create-index\n\n`. This is used to make sure that `create-index` does not accidentally overwrite your local files.
|
||||||
|
|
||||||
If there are sibling files, index file will `import` them and `export`, e.g.
|
If there are sibling files, index file will `import` them and `export`, e.g.
|
||||||
|
|
||||||
@@ -108,10 +189,10 @@ drwxr-xr-x 2 gajus staff 68B 6 Jan 15:29 foo
|
|||||||
Given the above directory contents, `./index.js` will be:
|
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,
|
||||||
@@ -123,6 +204,40 @@ When file has the same name as a sibling directory, file `import` takes preceden
|
|||||||
|
|
||||||
Directories that do not have `./index.js` in themselves will be excluded.
|
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
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
// 'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
// 'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
'create index';
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
'create index';
|
|
||||||
|
|
||||||
export bar from './bar';
|
|
||||||
export foo from './foo.js';
|
|
||||||
|
|
||||||
+47
-32
@@ -1,44 +1,59 @@
|
|||||||
{
|
{
|
||||||
"name": "create-index",
|
"author": {
|
||||||
"description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.",
|
"email": "gajus@gajus.com",
|
||||||
"version": "0.1.0",
|
"name": "Gajus Kuizinas",
|
||||||
"main": "./dist/index.js",
|
"url": "http://gajus.com"
|
||||||
|
},
|
||||||
"bin": "./dist/bin/create-index.js",
|
"bin": "./dist/bin/create-index.js",
|
||||||
"repository": {
|
"dependencies": {
|
||||||
"type": "git",
|
"chalk": "^4.0.0",
|
||||||
"url": "https://github.com/gajus/create-index"
|
"glob": "^7.1.6",
|
||||||
|
"lodash": "^4.17.15",
|
||||||
|
"moment": "^2.25.3",
|
||||||
|
"yargs": "^15.3.1"
|
||||||
|
},
|
||||||
|
"description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/cli": "^7.0.0",
|
||||||
|
"@babel/core": "^7.0.0",
|
||||||
|
"@babel/plugin-proposal-export-default-from": "^7.0.0",
|
||||||
|
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
|
||||||
|
"@babel/plugin-transform-destructuring": "^7.0.0",
|
||||||
|
"@babel/plugin-transform-modules-commonjs": "^7.0.0",
|
||||||
|
"@babel/plugin-transform-parameters": "^7.0.0",
|
||||||
|
"@babel/register": "^7.0.0",
|
||||||
|
"chai": "^4.2.0",
|
||||||
|
"eslint": "^7.0.0",
|
||||||
|
"eslint-config-canonical": "^20.0.4",
|
||||||
|
"husky": "^4.2.5",
|
||||||
|
"mocha": "^7.1.2",
|
||||||
|
"semantic-release": "^17.0.7"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=5"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"index",
|
"index",
|
||||||
"import",
|
"import",
|
||||||
"export"
|
"export"
|
||||||
],
|
],
|
||||||
"author": {
|
|
||||||
"name": "Gajus Kuizinas",
|
|
||||||
"email": "gajus@gajus.com",
|
|
||||||
"url": "http://gajus.com"
|
|
||||||
},
|
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"name": "create-index",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/gajus/create-index"
|
||||||
|
},
|
||||||
|
"husky": {
|
||||||
|
"hooks": {
|
||||||
|
"pre-commit": "npm run test"
|
||||||
|
}
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"pragmatist": "node ./node_modules/.bin/pragmatist --es5",
|
"build": "rm -fr ./dist && NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
|
||||||
"lint": "npm run pragmatist lint",
|
"create-index": "node ./dist/bin/create-index ./src/utilities",
|
||||||
"test": "npm run pragmatist test",
|
"lint": "NODE_ENV=development eslint ./src ./test",
|
||||||
"build": "npm run pragmatist build",
|
"test": "NODE_ENV=development mocha --require @babel/register"
|
||||||
"watch": "npm run pragmatist watch",
|
|
||||||
"watch-lint": "npm run pragmatist watch-lint",
|
|
||||||
"watch-test": "npm run pragmatist watch-test",
|
|
||||||
"watch-build": "npm run pragmatist watch-build",
|
|
||||||
"create-index": "node ./dist/bin/create-index ./src/utilities"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"version": "0.0.0-development"
|
||||||
"chai": "^3.5.0",
|
|
||||||
"chalk": "^1.1.1",
|
|
||||||
"moment": "^2.11.1",
|
|
||||||
"pragmatist": "^3.0.3"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"glob": "^6.0.4",
|
|
||||||
"lodash": "^4.0.1",
|
|
||||||
"yargs": "^4.0.0-alpha1"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+75
-12
@@ -1,25 +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';
|
||||||
|
|
||||||
let argv;
|
const argv = yargs
|
||||||
|
|
||||||
argv = yargs
|
|
||||||
.demand(1)
|
.demand(1)
|
||||||
.options({
|
.options({
|
||||||
'update-index': {
|
recursive: {
|
||||||
|
alias: 'r',
|
||||||
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: 'Create/update index files recursively. Halts on any unsafe "index.js" files.',
|
||||||
type: 'boolean'
|
type: 'boolean',
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
.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-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
|
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({
|
||||||
|
update: {
|
||||||
|
alias: 'u',
|
||||||
|
default: false,
|
||||||
|
description: 'Updates only previously created index files (recursively).',
|
||||||
|
type: 'boolean',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.options({
|
||||||
|
banner: {
|
||||||
|
description: 'Add a custom banner at the top of the index file',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.options({
|
||||||
|
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._, {
|
||||||
updateIndex: argv.updateIndex
|
banner: argv.banner,
|
||||||
|
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]+/;
|
||||||
@@ -1,12 +1,7 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
let buildExportBlock,
|
const safeVariableName = (fileName) => {
|
||||||
safeVariableName;
|
const indexOfDot = fileName.indexOf('.');
|
||||||
|
|
||||||
safeVariableName = (fileName) => {
|
|
||||||
let indexOfDot;
|
|
||||||
|
|
||||||
indexOfDot = fileName.indexOf('.');
|
|
||||||
|
|
||||||
if (indexOfDot === -1) {
|
if (indexOfDot === -1) {
|
||||||
return fileName;
|
return fileName;
|
||||||
@@ -15,11 +10,11 @@ safeVariableName = (fileName) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
buildExportBlock = (files) => {
|
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');
|
||||||
@@ -27,14 +22,31 @@ buildExportBlock = (files) => {
|
|||||||
return importBlock;
|
return importBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (filePaths) => {
|
export default (filePaths, options = {}) => {
|
||||||
let code,
|
let code;
|
||||||
sortedFilePaths;
|
let configCode;
|
||||||
|
|
||||||
code = '\'create index\';\n\n';
|
code = '';
|
||||||
|
configCode = '';
|
||||||
|
|
||||||
|
if (options.banner) {
|
||||||
|
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
|
||||||
|
|
||||||
|
banners.forEach((banner) => {
|
||||||
|
code += banner + '\n';
|
||||||
|
});
|
||||||
|
|
||||||
|
code += '\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) {
|
||||||
sortedFilePaths = filePaths.sort();
|
const sortedFilePaths = filePaths.sort();
|
||||||
|
|
||||||
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,22 @@
|
|||||||
|
import path from 'path';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
import path from 'path';
|
|
||||||
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 no-empty */
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
|
||||||
/* eslint-enable no-empty */
|
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
+9
-10
@@ -1,11 +1,10 @@
|
|||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export createIndexCode from './createIndexCode.js';
|
|
||||||
export findIndexFiles from './findIndexFiles.js';
|
|
||||||
export log from './log.js';
|
|
||||||
export readDirectory from './readDirectory.js';
|
|
||||||
export sortByDepth from './sortByDepth.js';
|
|
||||||
export validateTargetDirectory from './validateTargetDirectory.js';
|
|
||||||
export writeIndex from './writeIndex.js';
|
|
||||||
export writeIndexCli from './writeIndexCli.js';
|
|
||||||
|
|
||||||
|
export createIndexCode from './createIndexCode';
|
||||||
|
export findIndexFiles from './findIndexFiles';
|
||||||
|
export log from './log';
|
||||||
|
export readDirectory from './readDirectory';
|
||||||
|
export sortByDepth from './sortByDepth';
|
||||||
|
export validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
export writeIndex from './writeIndex';
|
||||||
|
export writeIndexCli from './writeIndexCli';
|
||||||
|
|||||||
@@ -2,7 +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,68 +1,94 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import _ from 'lodash';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import hasIndex from './hasIndex';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
let hasIndex,
|
const hasNoExtension = (fileName) => {
|
||||||
hasMultipleExtensions,
|
const matches = fileName.match(/\./g);
|
||||||
hasNoExtension,
|
|
||||||
isSafeName,
|
|
||||||
removeDuplicates;
|
|
||||||
|
|
||||||
hasIndex = (directoryPath) => {
|
|
||||||
let indexPath;
|
|
||||||
|
|
||||||
indexPath = path.resolve(directoryPath, 'index.js');
|
|
||||||
|
|
||||||
try {
|
|
||||||
fs.statSync(indexPath);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
hasNoExtension = (fileName) => {
|
|
||||||
let matches;
|
|
||||||
|
|
||||||
matches = fileName.match(/\./g);
|
|
||||||
|
|
||||||
return !matches;
|
return !matches;
|
||||||
};
|
};
|
||||||
|
|
||||||
hasMultipleExtensions = (fileName) => {
|
const hasMultipleExtensions = (fileName) => {
|
||||||
let matches;
|
const matches = fileName.match(/\./g);
|
||||||
|
|
||||||
matches = fileName.match(/\./g);
|
|
||||||
|
|
||||||
return matches && matches.length > 1;
|
return matches && matches.length > 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
isSafeName = (fileName) => {
|
const isSafeName = (fileName) => {
|
||||||
return /^[a-z][a-z0-9\.]+$/i.test(fileName);
|
return /^[_a-z][\w.]*$/i.test(fileName);
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
children = _.filter(children, (fileName) => {
|
children = _.filter(children, (fileName) => {
|
||||||
let absolutePath,
|
const absolutePath = path.resolve(directoryPath, fileName);
|
||||||
isDirectory;
|
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||||
|
|
||||||
absolutePath = path.resolve(directoryPath, fileName);
|
|
||||||
|
|
||||||
isDirectory = fs.statSync(absolutePath).isDirectory();
|
|
||||||
|
|
||||||
if (!isSafeName(fileName)) {
|
if (!isSafeName(fileName)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -76,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,35 +1,47 @@
|
|||||||
import _ from 'lodash';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import {CREATE_INDEX_PATTERN} from './constants';
|
||||||
|
|
||||||
export default (targetDirectory) => {
|
export default (targetDirectory, options = {}) => {
|
||||||
let indexFile,
|
const silent = options.silent;
|
||||||
indexFilePath,
|
let stats;
|
||||||
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.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+13
-18
@@ -1,30 +1,25 @@
|
|||||||
import _ from 'lodash';
|
|
||||||
import createIndexCode from './createIndexCode';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import _ from 'lodash';
|
||||||
|
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 = {}) => {
|
||||||
let sortedDirectoryPaths;
|
const sortedDirectoryPaths = sortByDepth(directoryPaths)
|
||||||
|
.filter((directoryPath) => {
|
||||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
return validateTargetDirectory(directoryPath, {outputFile: options.outputFile,
|
||||||
|
silent: options.ignoreUnsafe});
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
|
||||||
validateTargetDirectory(directoryPath);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
let indexCode,
|
const config = readIndexConfig(directoryPath, options);
|
||||||
indexFilePath,
|
const optionsWithConfig = Object.assign({}, options, {config});
|
||||||
siblings;
|
const siblings = readDirectory(directoryPath, optionsWithConfig);
|
||||||
|
const indexCode = createIndexCode(siblings, optionsWithConfig);
|
||||||
siblings = readDirectory(directoryPath);
|
const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js');
|
||||||
|
|
||||||
indexCode = createIndexCode(siblings);
|
|
||||||
|
|
||||||
indexFilePath = path.resolve(directoryPath, 'index.js');
|
|
||||||
|
|
||||||
fs.writeFileSync(indexFilePath, indexCode);
|
fs.writeFileSync(indexFilePath, indexCode);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import _ from 'lodash';
|
|
||||||
import createIndexCode from './createIndexCode';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
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';
|
||||||
import chalk from 'chalk';
|
|
||||||
|
|
||||||
export default (directoryPaths, options = {}) => {
|
export default (directoryPaths, options = {}) => {
|
||||||
let sortedDirectoryPaths;
|
let sortedDirectoryPaths;
|
||||||
@@ -15,45 +16,67 @@ 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;
|
||||||
indexCode,
|
|
||||||
indexFilePath,
|
|
||||||
siblings;
|
|
||||||
|
|
||||||
siblings = readDirectory(directoryPath);
|
const config = readIndexConfig(directoryPath, options);
|
||||||
|
|
||||||
indexCode = createIndexCode(siblings);
|
const siblings = readDirectory(directoryPath, {
|
||||||
|
config,
|
||||||
|
extensions: options.extensions,
|
||||||
|
ignoreDirectories: options.ignoreDirectories,
|
||||||
|
silent: options.ignoreUnsafe,
|
||||||
|
});
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* eslint-enable no-empty */
|
/* eslint-enable no-empty */
|
||||||
|
|
||||||
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"
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/* eslint-disable no-restricted-syntax */
|
||||||
|
|
||||||
|
import {
|
||||||
|
expect,
|
||||||
|
} from 'chai';
|
||||||
|
import createIndexCode from '../src/utilities/createIndexCode';
|
||||||
|
import codeExample from './codeExample';
|
||||||
|
|
||||||
|
describe('createIndexCode()', () => {
|
||||||
|
it('describes no children', () => {
|
||||||
|
const indexCode = createIndexCode([]);
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
it('describes a single child', () => {
|
||||||
|
const indexCode = createIndexCode(['foo']);
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index
|
||||||
|
|
||||||
|
export { default as foo } from './foo';
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
it('describes multiple children', () => {
|
||||||
|
const indexCode = createIndexCode(['bar', 'foo']);
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index
|
||||||
|
|
||||||
|
export { default as bar } from './bar';
|
||||||
|
export { default as foo } from './foo';
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
context('file with extension', () => {
|
||||||
|
it('removes the extension from the export statement', () => {
|
||||||
|
const indexCode = createIndexCode(['foo.js']);
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index
|
||||||
|
|
||||||
|
export { default as foo } from './foo.js';
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('multiple, unsorted', () => {
|
||||||
|
it('sorts the files', () => {
|
||||||
|
const indexCode = createIndexCode(['foo', 'bar']);
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
|
// @create-index
|
||||||
|
|
||||||
|
export { default as bar } from './bar';
|
||||||
|
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';
|
||||||
|
`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import {
|
||||||
|
expect,
|
||||||
|
} from 'chai';
|
||||||
|
import glob from 'glob';
|
||||||
|
import findIndexFiles from '../src/utilities/findIndexFiles';
|
||||||
|
|
||||||
|
const fixturesPath = path.resolve(__dirname, '../../fixtures/find-index-files');
|
||||||
|
|
||||||
|
describe('findIndexFiles()', () => {
|
||||||
|
it('finds only the directories that have an existing valid index file', () => {
|
||||||
|
let names;
|
||||||
|
|
||||||
|
names = findIndexFiles(path.resolve(fixturesPath));
|
||||||
|
names = names.sort();
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(glob.sync(path.resolve(fixturesPath, '**/find-*')));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// @create-index
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
// @create-index
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
// @create-index
|
||||||
+1
@@ -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
|
||||||
@@ -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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// @create-index
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// @create-index
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user