Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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,8 @@
|
||||
{
|
||||
"plugins": [
|
||||
"transform-es2015-modules-commonjs",
|
||||
"transform-export-extensions",
|
||||
"transform-es2015-destructuring",
|
||||
"transform-es2015-parameters"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
@@ -0,0 +1 @@
|
||||
./test/fixtures
|
||||
+5
-2
@@ -1,9 +1,12 @@
|
||||
node_modules
|
||||
coverage
|
||||
dist
|
||||
node_modules
|
||||
*.log
|
||||
.*
|
||||
!.babelrc
|
||||
!.editorconfig
|
||||
!.eslintignore
|
||||
!.eslintrc
|
||||
!.gitignore
|
||||
!.npmignore
|
||||
!.babelrc
|
||||
!.travis.yml
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
coverage
|
||||
fixtures
|
||||
src
|
||||
tests
|
||||
coverage
|
||||
test
|
||||
.*
|
||||
*.log
|
||||
|
||||
+6
-2
@@ -1,7 +1,11 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- '5.0'
|
||||
- '4.0'
|
||||
- node
|
||||
- 7
|
||||
- 6
|
||||
- 5
|
||||
after_success:
|
||||
- semantic-release pre && npm publish && semantic-release post
|
||||
notifications:
|
||||
email: false
|
||||
sudo: false
|
||||
|
||||
@@ -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.
|
||||
|
||||
## 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 bar from './bar.js';
|
||||
export 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 bar from './bar.js';
|
||||
export baz from './baz.js';
|
||||
export foo from './foo.js';
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Using CLI Program
|
||||
@@ -16,20 +82,21 @@ npm install create-index
|
||||
create-index --help
|
||||
|
||||
Options:
|
||||
--update-index 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. [boolean] [default: false]
|
||||
--recursive, -r Create/update index files recursively. Halts on any unsafe
|
||||
"index.js" files. [boolean] [default: false]
|
||||
--ignoreUnsafe, -i Ignores unsafe "index.js" files instead of halting.
|
||||
[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]
|
||||
|
||||
Examples:
|
||||
create-index ./src ./src/utilities Creates or updates an existing
|
||||
create-index index file in the
|
||||
target (./src, ./src/utilities)
|
||||
directories.
|
||||
create-index --update-index ./src Finds all create-index index files
|
||||
./tests in the target directories and
|
||||
descending directories. Updates
|
||||
found index files.
|
||||
create-index index file in the target
|
||||
(./src, ./src/utilities) directories.
|
||||
create-index --update ./src ./tests Finds all create-index index files in the
|
||||
target directories and descending
|
||||
directories. Updates found index files.
|
||||
```
|
||||
|
||||
### Using `create-index` Programmatically
|
||||
@@ -88,10 +155,10 @@ Note that the `writeIndex` function is synchronous.
|
||||
If there is no `./index.js`, it will create a new file, e.g.
|
||||
|
||||
```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.
|
||||
|
||||
@@ -108,7 +175,7 @@ drwxr-xr-x 2 gajus staff 68B 6 Jan 15:29 foo
|
||||
Given the above directory contents, `./index.js` will be:
|
||||
|
||||
```js
|
||||
'create index';
|
||||
// @create-index
|
||||
|
||||
import bar from './bar';
|
||||
import foo from './foo.js';
|
||||
@@ -123,6 +190,6 @@ 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.
|
||||
|
||||
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.
|
||||
|
||||
@@ -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';
|
||||
+41
-32
@@ -1,44 +1,53 @@
|
||||
{
|
||||
"name": "create-index",
|
||||
"description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.",
|
||||
"version": "0.1.0",
|
||||
"main": "./dist/index.js",
|
||||
"author": {
|
||||
"email": "gajus@gajus.com",
|
||||
"name": "Gajus Kuizinas",
|
||||
"url": "http://gajus.com"
|
||||
},
|
||||
"bin": "./dist/bin/create-index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/gajus/create-index"
|
||||
"dependencies": {
|
||||
"chalk": "^1.1.3",
|
||||
"glob": "^7.1.1",
|
||||
"lodash": "^4.16.6",
|
||||
"moment": "^2.15.2",
|
||||
"yargs": "^6.3.0"
|
||||
},
|
||||
"description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.",
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.18.0",
|
||||
"babel-plugin-transform-es2015-destructuring": "^6.18.0",
|
||||
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
|
||||
"babel-plugin-transform-es2015-parameters": "^6.18.0",
|
||||
"babel-plugin-transform-export-extensions": "^6.8.0",
|
||||
"babel-register": "^6.18.0",
|
||||
"chai": "^3.5.0",
|
||||
"eslint": "^3.9.1",
|
||||
"eslint-config-canonical": "^4.0.0",
|
||||
"husky": "^0.11.9",
|
||||
"mocha": "^3.1.2",
|
||||
"semantic-release": "^6.3.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=5"
|
||||
},
|
||||
"keywords": [
|
||||
"index",
|
||||
"import",
|
||||
"export"
|
||||
],
|
||||
"author": {
|
||||
"name": "Gajus Kuizinas",
|
||||
"email": "gajus@gajus.com",
|
||||
"url": "http://gajus.com"
|
||||
},
|
||||
"license": "BSD-3-Clause",
|
||||
"main": "./dist/index.js",
|
||||
"name": "create-index",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/gajus/create-index"
|
||||
},
|
||||
"scripts": {
|
||||
"pragmatist": "node ./node_modules/.bin/pragmatist --es5",
|
||||
"lint": "npm run pragmatist lint",
|
||||
"test": "npm run pragmatist test",
|
||||
"build": "npm run pragmatist build",
|
||||
"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"
|
||||
"build": "NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
|
||||
"create-index": "node ./dist/bin/create-index ./src/utilities",
|
||||
"lint": "NODE_ENV=development eslint ./src ./tests",
|
||||
"precommit": "npm run test",
|
||||
"test": "npm run build && npm run lint && NODE_ENV=development mocha --compilers js:babel-register"
|
||||
},
|
||||
"devDependencies": {
|
||||
"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"
|
||||
}
|
||||
"version": "1.0.0"
|
||||
}
|
||||
|
||||
+32
-8
@@ -3,23 +3,47 @@
|
||||
import yargs from 'yargs';
|
||||
import {
|
||||
writeIndexCli
|
||||
} from './../utilities';
|
||||
} from '../utilities';
|
||||
|
||||
let argv;
|
||||
|
||||
argv = yargs
|
||||
const argv = yargs
|
||||
.demand(1)
|
||||
.options({
|
||||
'update-index': {
|
||||
recursive: {
|
||||
alias: 'r',
|
||||
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'
|
||||
}
|
||||
})
|
||||
.options({
|
||||
ignoreUnsafe: {
|
||||
alias: 'i',
|
||||
default: false,
|
||||
description: 'Ignores unsafe "index.js" files instead of halting.',
|
||||
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'
|
||||
}
|
||||
})
|
||||
.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-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
|
||||
.example('create-index --update ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
|
||||
.argv;
|
||||
|
||||
writeIndexCli(argv._, {
|
||||
updateIndex: argv.updateIndex
|
||||
banner: argv.banner,
|
||||
ignoreUnsafe: argv.ignoreUnsafe,
|
||||
recursive: argv.recursive,
|
||||
updateIndex: argv.update
|
||||
});
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
let buildExportBlock,
|
||||
safeVariableName;
|
||||
|
||||
safeVariableName = (fileName) => {
|
||||
let indexOfDot;
|
||||
|
||||
indexOfDot = fileName.indexOf('.');
|
||||
const safeVariableName = (fileName) => {
|
||||
const indexOfDot = fileName.indexOf('.');
|
||||
|
||||
if (indexOfDot === -1) {
|
||||
return fileName;
|
||||
@@ -15,7 +10,7 @@ safeVariableName = (fileName) => {
|
||||
}
|
||||
};
|
||||
|
||||
buildExportBlock = (files) => {
|
||||
const buildExportBlock = (files) => {
|
||||
let importBlock;
|
||||
|
||||
importBlock = _.map(files, (fileName) => {
|
||||
@@ -27,14 +22,25 @@ buildExportBlock = (files) => {
|
||||
return importBlock;
|
||||
};
|
||||
|
||||
export default (filePaths) => {
|
||||
let code,
|
||||
sortedFilePaths;
|
||||
export default (filePaths, options = {}) => {
|
||||
let code;
|
||||
|
||||
code = '\'create index\';\n\n';
|
||||
code = '';
|
||||
|
||||
if (options.banner) {
|
||||
const banners = _.isArray(options.banner) ? options.banner : [options.banner];
|
||||
|
||||
banners.forEach((banner) => {
|
||||
code += banner + '\n';
|
||||
});
|
||||
|
||||
code += '\n';
|
||||
}
|
||||
|
||||
code += '// @create-index\n\n';
|
||||
|
||||
if (filePaths.length) {
|
||||
sortedFilePaths = filePaths.sort();
|
||||
const sortedFilePaths = filePaths.sort();
|
||||
|
||||
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
||||
}
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import glob from 'glob';
|
||||
import path from 'path';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
export default (directoryPath) => {
|
||||
let targetDirectories;
|
||||
export default (directoryPath, options = {}) => {
|
||||
let fileName, 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) => {
|
||||
try {
|
||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
||||
|
||||
return true;
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
/* eslint-enable no-empty */
|
||||
|
||||
return false;
|
||||
return validateTargetDirectory(path.dirname(targetDirectoryPath), {
|
||||
silent: options.silent
|
||||
});
|
||||
});
|
||||
|
||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
'create index';
|
||||
// @create-index
|
||||
|
||||
export createIndexCode from './createIndexCode.js';
|
||||
export findIndexFiles from './findIndexFiles.js';
|
||||
@@ -8,4 +8,3 @@ export sortByDepth from './sortByDepth.js';
|
||||
export validateTargetDirectory from './validateTargetDirectory.js';
|
||||
export writeIndex from './writeIndex.js';
|
||||
export writeIndexCli from './writeIndexCli.js';
|
||||
|
||||
|
||||
@@ -4,5 +4,6 @@ import moment from 'moment';
|
||||
export default (...append) => {
|
||||
/* eslint-disable no-console */
|
||||
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
|
||||
|
||||
/* eslint-enable no-console */
|
||||
};
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
import fs from 'fs';
|
||||
import _ from 'lodash';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
let hasIndex,
|
||||
hasMultipleExtensions,
|
||||
hasNoExtension,
|
||||
isSafeName,
|
||||
removeDuplicates;
|
||||
|
||||
hasIndex = (directoryPath) => {
|
||||
let indexPath;
|
||||
|
||||
indexPath = path.resolve(directoryPath, 'index.js');
|
||||
const hasIndex = (directoryPath) => {
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexPath);
|
||||
@@ -23,46 +15,40 @@ hasIndex = (directoryPath) => {
|
||||
}
|
||||
};
|
||||
|
||||
hasNoExtension = (fileName) => {
|
||||
let matches;
|
||||
|
||||
matches = fileName.match(/\./g);
|
||||
const hasNoExtension = (fileName) => {
|
||||
const matches = fileName.match(/\./g);
|
||||
|
||||
return !matches;
|
||||
};
|
||||
|
||||
hasMultipleExtensions = (fileName) => {
|
||||
let matches;
|
||||
|
||||
matches = fileName.match(/\./g);
|
||||
const hasMultipleExtensions = (fileName) => {
|
||||
const matches = fileName.match(/\./g);
|
||||
|
||||
return matches && matches.length > 1;
|
||||
};
|
||||
|
||||
isSafeName = (fileName) => {
|
||||
return /^[a-z][a-z0-9\.]+$/i.test(fileName);
|
||||
const isSafeName = (fileName) => {
|
||||
return /^[a-z][a-z0-9._]+$/i.test(fileName);
|
||||
};
|
||||
|
||||
removeDuplicates = (files) => {
|
||||
const removeDuplicates = (files) => {
|
||||
return _.filter(files, (fileName) => {
|
||||
return !_.includes(files, fileName + '.js');
|
||||
});
|
||||
};
|
||||
|
||||
export default (directoryPath) => {
|
||||
export default (directoryPath, options = {}) => {
|
||||
let children;
|
||||
|
||||
validateTargetDirectory(directoryPath);
|
||||
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
|
||||
return false;
|
||||
}
|
||||
|
||||
children = fs.readdirSync(directoryPath);
|
||||
|
||||
children = _.filter(children, (fileName) => {
|
||||
let absolutePath,
|
||||
isDirectory;
|
||||
|
||||
absolutePath = path.resolve(directoryPath, fileName);
|
||||
|
||||
isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||
const absolutePath = path.resolve(directoryPath, fileName);
|
||||
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||
|
||||
if (!isSafeName(fileName)) {
|
||||
return false;
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
import _ from 'lodash';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export default (targetDirectory) => {
|
||||
let indexFile,
|
||||
indexFilePath,
|
||||
stats;
|
||||
export default (targetDirectory, options = {}) => {
|
||||
const silent = options.silent;
|
||||
let stats;
|
||||
|
||||
try {
|
||||
stats = fs.statSync(targetDirectory);
|
||||
} catch (error) {
|
||||
if (silent) {
|
||||
return false;
|
||||
} else {
|
||||
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
if (!stats.isDirectory()) {
|
||||
if (silent) {
|
||||
return false;
|
||||
} else {
|
||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
||||
}
|
||||
}
|
||||
|
||||
indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexFilePath);
|
||||
@@ -25,11 +31,16 @@ export default (targetDirectory) => {
|
||||
return true;
|
||||
}
|
||||
|
||||
indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
||||
if (!indexFile.match(/(?:^|[\n\r]+)\/\/ @create-index[\n\r]+/)) {
|
||||
if (silent) {
|
||||
return false;
|
||||
} else {
|
||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,30 +1,21 @@
|
||||
import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import sortByDepth from './sortByDepth';
|
||||
|
||||
export default (directoryPaths) => {
|
||||
let sortedDirectoryPaths;
|
||||
|
||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
export default (directoryPaths, options = {}) => {
|
||||
const sortedDirectoryPaths = sortByDepth(directoryPaths)
|
||||
.filter((directoryPath) => {
|
||||
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
let indexCode,
|
||||
indexFilePath,
|
||||
siblings;
|
||||
|
||||
siblings = readDirectory(directoryPath);
|
||||
|
||||
indexCode = createIndexCode(siblings);
|
||||
|
||||
indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
const siblings = readDirectory(directoryPath);
|
||||
const indexCode = createIndexCode(siblings);
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
});
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import chalk from 'chalk';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import sortByDepth from './sortByDepth';
|
||||
import log from './log';
|
||||
import findIndexFiles from './findIndexFiles';
|
||||
import chalk from 'chalk';
|
||||
|
||||
export default (directoryPaths, options = {}) => {
|
||||
let sortedDirectoryPaths;
|
||||
@@ -15,39 +15,50 @@ export default (directoryPaths, options = {}) => {
|
||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
|
||||
log('Target directories', sortedDirectoryPaths);
|
||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||
|
||||
if (options.updateIndex) {
|
||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||
} else {
|
||||
log('Recursive:', options.ignoreUnsafe ? chalk.green('true') : chalk.red('false'));
|
||||
log('Ignore unsafe:', options.ignoreUnsafe ? chalk.green('true') : chalk.red('false'));
|
||||
}
|
||||
|
||||
if (options.updateIndex || options.recursive) {
|
||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, (dir) => {
|
||||
return findIndexFiles(dir, {
|
||||
fileName: options.updateIndex ? 'index.js' : '*',
|
||||
silent: options.updateIndex || options.ignoreUnsafe
|
||||
});
|
||||
});
|
||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||
|
||||
log('Found index file in:', sortedDirectoryPaths);
|
||||
log('Updating index files in:', sortedDirectoryPaths.reverse().join(', '));
|
||||
}
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
sortedDirectoryPaths = sortedDirectoryPaths.filter((directoryPath) => {
|
||||
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
let existingIndexCode,
|
||||
indexCode,
|
||||
indexFilePath,
|
||||
siblings;
|
||||
let existingIndexCode;
|
||||
|
||||
siblings = readDirectory(directoryPath);
|
||||
const siblings = readDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
||||
|
||||
indexCode = createIndexCode(siblings);
|
||||
const indexCode = createIndexCode(siblings, {
|
||||
banner: options.banner
|
||||
});
|
||||
|
||||
indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
|
||||
/* eslint-enable no-empty */
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/* 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 foo from './foo';
|
||||
`));
|
||||
});
|
||||
it('describes multiple children', () => {
|
||||
const indexCode = createIndexCode(['bar', 'foo']);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export 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 foo from './foo.js';
|
||||
`));
|
||||
});
|
||||
});
|
||||
context('multiple, unsorted', () => {
|
||||
it('sorts the files', () => {
|
||||
const indexCode = createIndexCode(['foo', 'bar']);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export 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
|
||||
+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
|
||||
Vendored
Vendored
@@ -0,0 +1 @@
|
||||
// @create-index
|
||||
@@ -0,0 +1 @@
|
||||
// @create-index
|
||||
@@ -0,0 +1 @@
|
||||
// @create-index
|
||||
@@ -0,0 +1,3 @@
|
||||
/* eslint-disable */
|
||||
|
||||
// @create-index
|
||||
@@ -0,0 +1 @@
|
||||
// @create-index
|
||||
@@ -0,0 +1 @@
|
||||
// @create-index
|
||||
@@ -0,0 +1 @@
|
||||
// @create-index
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
'create index';
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo.js';
|
||||
@@ -0,0 +1,73 @@
|
||||
import path from 'path';
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import readDirectory from '../src/utilities/readDirectory';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, 'fixtures/read-directory');
|
||||
|
||||
describe('readDirectory()', () => {
|
||||
context('target directory contains child directories', () => {
|
||||
it('gets names of the children directories', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories'));
|
||||
|
||||
expect(names).to.deep.equal(['bar', 'foo']);
|
||||
});
|
||||
});
|
||||
context('target directory contains child directories that do not contain index', () => {
|
||||
it('gets names of the children directories', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-without-index'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains child directories (unsafe name)', () => {
|
||||
it('gets names of the children directories', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name'));
|
||||
|
||||
expect(names).to.deep.equal(['present']);
|
||||
});
|
||||
});
|
||||
context('target directory contains ./index.js', () => {
|
||||
it('does not include ./index.js', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-index'));
|
||||
|
||||
expect(names).to.deep.equal(['bar', 'foo']);
|
||||
});
|
||||
});
|
||||
context('target directory contains files', () => {
|
||||
it('refers to the files (with extension)', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files'));
|
||||
|
||||
expect(names).to.deep.equal(['bar.js', 'foo.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains dot files', () => {
|
||||
it('ignores files', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-dot-files'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains files with no extension', () => {
|
||||
it('ignores files', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains files with multiple extensions', () => {
|
||||
it('ignores files', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files-multiple-extensions'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains directories and files with the same name', () => {
|
||||
it('prefers file', () => {
|
||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-and-files'));
|
||||
|
||||
expect(names).to.deep.equal(['foo.js', 'present.js']);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import sortByDepth from './../src/utilities/sortByDepth';
|
||||
|
||||
describe('sortByDepth()', () => {
|
||||
it('sorts from deepest to the most shallow', () => {
|
||||
const paths = [
|
||||
'/b',
|
||||
'/a',
|
||||
'/a/b/c',
|
||||
'/a/b'
|
||||
];
|
||||
|
||||
const sortedPaths = sortByDepth(paths);
|
||||
|
||||
expect(sortedPaths).to.deep.equal(['/a/b/c', '/a/b', '/b', '/a']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
/* eslint-disable max-nested-callbacks */
|
||||
|
||||
import path from 'path';
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import validateTargetDirectory from '../src/utilities/validateTargetDirectory';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, 'fixtures/validate-target-directory');
|
||||
|
||||
describe('validateTargetDirectory()', () => {
|
||||
describe('directory path', () => {
|
||||
context('refers to a directory that does not exist', () => {
|
||||
it('throws an error', () => {
|
||||
expect(() => {
|
||||
validateTargetDirectory(path.resolve(fixturesPath, 'does-not-exist'));
|
||||
}).to.throw(Error, 'Directory "' + path.resolve(fixturesPath, 'does-not-exist') + '" does not exist.');
|
||||
});
|
||||
});
|
||||
context('refers to a file', () => {
|
||||
it('throws an error', () => {
|
||||
expect(() => {
|
||||
validateTargetDirectory(path.resolve(fixturesPath, 'not-a-directory.js'));
|
||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'not-a-directory.js') + '" is not a directory.');
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('target index', () => {
|
||||
context('no index', () => {
|
||||
it('returns true', () => {
|
||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'no-index'))).to.equal(true);
|
||||
});
|
||||
});
|
||||
context('safe', () => {
|
||||
it('returns 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', () => {
|
||||
it('throws an error', () => {
|
||||
expect(() => {
|
||||
validateTargetDirectory(path.resolve(fixturesPath, '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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import writeIndex from '../src/utilities/writeIndex';
|
||||
import codeExample from './codeExample';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, 'fixtures/write-index');
|
||||
|
||||
describe('writeIndex()', () => {
|
||||
it('creates index in target directory', () => {
|
||||
const indexFilePath = path.resolve(fixturesPath, 'mixed/index.js');
|
||||
|
||||
try {
|
||||
fs.unlinkSync(indexFilePath);
|
||||
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
|
||||
writeIndex([path.resolve(fixturesPath, 'mixed')]);
|
||||
|
||||
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
// @create-index
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo.js';
|
||||
`));
|
||||
});
|
||||
});
|
||||
@@ -1,71 +0,0 @@
|
||||
/* 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', () => {
|
||||
let indexCode;
|
||||
|
||||
indexCode = createIndexCode([]);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
'create index';
|
||||
`));
|
||||
});
|
||||
it('describes a single child', () => {
|
||||
let indexCode;
|
||||
|
||||
indexCode = createIndexCode(['foo']);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
'create index';
|
||||
|
||||
export foo from './foo';
|
||||
`));
|
||||
});
|
||||
it('describes multiple children', () => {
|
||||
let indexCode;
|
||||
|
||||
indexCode = createIndexCode(['bar', 'foo']);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
'create index';
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo';
|
||||
`));
|
||||
});
|
||||
context('file with extension', () => {
|
||||
it('removes the extension from the export statement', () => {
|
||||
let indexCode;
|
||||
|
||||
indexCode = createIndexCode(['foo.js']);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
'create index';
|
||||
|
||||
export foo from './foo.js';
|
||||
`));
|
||||
});
|
||||
});
|
||||
context('multiple, unsorted', () => {
|
||||
it('sorts the files', () => {
|
||||
let indexCode;
|
||||
|
||||
indexCode = createIndexCode(['foo', 'bar']);
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
'create index';
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo';
|
||||
`));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
|
||||
import path from 'path';
|
||||
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-*')));
|
||||
});
|
||||
});
|
||||
@@ -1,92 +0,0 @@
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
|
||||
import path from 'path';
|
||||
import readDirectory from './../src/utilities/readDirectory';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/read-directory');
|
||||
|
||||
describe('readDirectory()', () => {
|
||||
context('target directory contains child directories', () => {
|
||||
it('gets names of the children directories', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories'));
|
||||
|
||||
expect(names).to.deep.equal(['bar', 'foo']);
|
||||
});
|
||||
});
|
||||
context('target directory contains child directories that do not contain index', () => {
|
||||
it('gets names of the children directories', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories-without-index'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains child directories (unsafe name)', () => {
|
||||
it('gets names of the children directories', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name'));
|
||||
|
||||
expect(names).to.deep.equal(['present']);
|
||||
});
|
||||
});
|
||||
context('target directory contains ./index.js', () => {
|
||||
it('does not include ./index.js', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-index'));
|
||||
|
||||
expect(names).to.deep.equal(['bar', 'foo']);
|
||||
});
|
||||
});
|
||||
context('target directory contains files', () => {
|
||||
it('refers to the files (with extension)', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-files'));
|
||||
|
||||
expect(names).to.deep.equal(['bar.js', 'foo.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains dot files', () => {
|
||||
it('ignores files', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-dot-files'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains files with no extension', () => {
|
||||
it('ignores files', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains files with multiple extensions', () => {
|
||||
it('ignores files', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-files-multiple-extensions'));
|
||||
|
||||
expect(names).to.deep.equal(['present.js']);
|
||||
});
|
||||
});
|
||||
context('target directory contains directories and files with the same name', () => {
|
||||
it('prefers file', () => {
|
||||
let names;
|
||||
|
||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories-and-files'));
|
||||
|
||||
expect(names).to.deep.equal(['foo.js', 'present.js']);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,22 +0,0 @@
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import sortByDepth from './../src/utilities/sortByDepth';
|
||||
|
||||
describe('sortByDepth()', () => {
|
||||
it('sorts from deepest to the most shallow', () => {
|
||||
let paths,
|
||||
sortedPaths;
|
||||
|
||||
paths = [
|
||||
'/b',
|
||||
'/a',
|
||||
'/a/b/c',
|
||||
'/a/b'
|
||||
];
|
||||
|
||||
sortedPaths = sortByDepth(paths);
|
||||
|
||||
expect(sortedPaths).to.deep.equal(['/a/b/c', '/a/b', '/b', '/a']);
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
/* eslint-disable max-nested-callbacks */
|
||||
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import path from 'path';
|
||||
import validateTargetDirectory from './../src/utilities/validateTargetDirectory';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/validate-target-directory');
|
||||
|
||||
describe('validateTargetDirectory()', () => {
|
||||
describe('directory path', () => {
|
||||
context('refers to a directory that does not exist', () => {
|
||||
it('throws an error', () => {
|
||||
expect(() => {
|
||||
validateTargetDirectory(path.resolve(fixturesPath, 'does-not-exist'));
|
||||
}).to.throw(Error, 'Directory "' + path.resolve(fixturesPath, 'does-not-exist') + '" does not exist.');
|
||||
});
|
||||
});
|
||||
context('refers to a file', () => {
|
||||
it('throws an error', () => {
|
||||
expect(() => {
|
||||
validateTargetDirectory(path.resolve(fixturesPath, 'not-a-directory.js'));
|
||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'not-a-directory.js') + '" is not a directory.');
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('target index', () => {
|
||||
context('no index', () => {
|
||||
it('returns true', () => {
|
||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'no-index'))).to.equal(true);
|
||||
});
|
||||
});
|
||||
context('safe', () => {
|
||||
it('returns true', () => {
|
||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
||||
});
|
||||
});
|
||||
context('unsafe', () => {
|
||||
it('throws an error', () => {
|
||||
expect(() => {
|
||||
validateTargetDirectory(path.resolve(fixturesPath, 'unsafe-index'));
|
||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,39 +0,0 @@
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
|
||||
import {
|
||||
expect
|
||||
} from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import codeExample from './codeExample';
|
||||
import writeIndex from './../src/utilities/writeIndex';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/write-index');
|
||||
|
||||
describe('writeIndex()', () => {
|
||||
it('creates index in target directory', () => {
|
||||
let indexCode,
|
||||
indexFilePath;
|
||||
|
||||
indexFilePath = path.resolve(fixturesPath, './mixed/index.js');
|
||||
|
||||
try {
|
||||
fs.unlinkSync(indexFilePath);
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
/* eslint-enable no-empty */
|
||||
|
||||
writeIndex([path.resolve(fixturesPath, './mixed')]);
|
||||
|
||||
indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
expect(indexCode).to.equal(codeExample(`
|
||||
'create index';
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo.js';
|
||||
`));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user