Scan all descending directories for index.js with 'create index'; (fixes #2)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
fixtures
|
||||
src
|
||||
tests
|
||||
coverage
|
||||
|
||||
@@ -6,6 +6,81 @@
|
||||
|
||||
`create-index` program creates (and maintains) ES6 `./index.js` file in target directories that imports and exports sibling files and directories.
|
||||
|
||||
## Usage
|
||||
|
||||
### Using CLI Program
|
||||
|
||||
```sh
|
||||
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]
|
||||
|
||||
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.
|
||||
```
|
||||
|
||||
### Using `create-index` Programmatically
|
||||
|
||||
```js
|
||||
import {
|
||||
writeIndex
|
||||
} from 'create-index';
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
* @param {Array<string>} directoryPaths
|
||||
* @throws {Error} Directory "..." does not exist.
|
||||
* @throws {Error} "..." is not a directory.
|
||||
* @throws {Error} "..." unsafe index.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
writeIndex;
|
||||
```
|
||||
|
||||
Note that the `writeIndex` function is synchronous.
|
||||
|
||||
```js
|
||||
import {
|
||||
findIndexFiles
|
||||
} from 'create-index';
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
* @param {string} directoryPath
|
||||
* @returns {Array<string>} List of directory paths that have create-index index file.
|
||||
*/
|
||||
findIndexFiles;
|
||||
```
|
||||
|
||||
### Gulp
|
||||
|
||||
Since [Gulp](http://gulpjs.com/) can ran arbitrary JavaScript code, there is no need for a separate plugin. See [Using `create-index` Programmatically](#using-create-index-programmatically).
|
||||
|
||||
```js
|
||||
import {
|
||||
writeIndex
|
||||
} from 'create-index';
|
||||
|
||||
gulp.task('create-index', () => {
|
||||
writeIndex(['./target_directory']);
|
||||
});
|
||||
```
|
||||
|
||||
Note that the `writeIndex` function is synchronous.
|
||||
|
||||
## Implementation
|
||||
|
||||
`create-index` program will look into the target directory.
|
||||
@@ -51,49 +126,3 @@ 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`.
|
||||
|
||||
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.
|
||||
|
||||
## Usage
|
||||
|
||||
### Using CLI Program
|
||||
|
||||
```sh
|
||||
npm install create-index
|
||||
|
||||
create-index ./directory1 ./directory2 ./directory3 ...
|
||||
```
|
||||
|
||||
### Using `create-index` Programmatically
|
||||
|
||||
```js
|
||||
import {
|
||||
writeIndex
|
||||
} from 'create-index';
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
* @param {Array<string>} directoryPaths
|
||||
* @throws {Error} Directory "..." does not exist.
|
||||
* @throws {Error} "..." is not a directory.
|
||||
* @throws {Error} "..." unsafe index.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
writeIndex;
|
||||
```
|
||||
|
||||
Note that the `writeIndex` function is synchronous.
|
||||
|
||||
### Gulp
|
||||
|
||||
Since [Gulp](http://gulpjs.com/) can ran arbitrary JavaScript code, there is no need for a separate plugin. See [Using `create-index` Programmatically](#using-create-index-programmatically).
|
||||
|
||||
```js
|
||||
import {
|
||||
writeIndex
|
||||
} from 'create-index';
|
||||
|
||||
gulp.task('create-index', () => {
|
||||
writeIndex(['./target_directory']);
|
||||
});
|
||||
```
|
||||
|
||||
Note that the `writeIndex` function is synchronous.
|
||||
|
||||
@@ -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,5 @@
|
||||
'create index';
|
||||
|
||||
export bar from './bar';
|
||||
export foo from './foo.js';
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
"pragmatist": "^3.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"glob": "^6.0.4",
|
||||
"lodash": "^4.0.1",
|
||||
"yargs": "^4.0.0-alpha1"
|
||||
}
|
||||
|
||||
+12
-1
@@ -9,6 +9,17 @@ let argv;
|
||||
|
||||
argv = yargs
|
||||
.demand(1)
|
||||
.options({
|
||||
'update-index': {
|
||||
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.',
|
||||
type: 'boolean'
|
||||
}
|
||||
})
|
||||
.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.')
|
||||
.argv;
|
||||
|
||||
writeIndexCli(argv._);
|
||||
writeIndexCli(argv._, {
|
||||
updateIndex: argv.updateIndex
|
||||
});
|
||||
|
||||
+1
-3
@@ -1,6 +1,4 @@
|
||||
export {
|
||||
createIndexCode,
|
||||
readDirectory,
|
||||
validateTargetDirectory,
|
||||
findIndexFiles,
|
||||
writeIndex
|
||||
} from './utilities';
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import _ from 'lodash';
|
||||
import glob from 'glob';
|
||||
import path from 'path';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
export default (directoryPath) => {
|
||||
let targetDirectories;
|
||||
|
||||
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
||||
|
||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||
try {
|
||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
||||
|
||||
return true;
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
/* eslint-enable no-empty */
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||
|
||||
return targetDirectories;
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
'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';
|
||||
|
||||
@@ -6,14 +6,25 @@ 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) => {
|
||||
export default (directoryPaths, options = {}) => {
|
||||
let sortedDirectoryPaths;
|
||||
|
||||
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);
|
||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||
|
||||
log('Found index file in:', sortedDirectoryPaths);
|
||||
}
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
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-*')));
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
import path from 'path';
|
||||
import readDirectory from './../src/utilities/readDirectory';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, './fixtures/read-directory');
|
||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/read-directory');
|
||||
|
||||
describe('readDirectory()', () => {
|
||||
context('target directory contains child directories', () => {
|
||||
@@ -35,7 +35,7 @@ describe('readDirectory()', () => {
|
||||
expect(names).to.deep.equal(['present']);
|
||||
});
|
||||
});
|
||||
context.skip('target directory contains ./index.js', () => {
|
||||
context('target directory contains ./index.js', () => {
|
||||
it('does not include ./index.js', () => {
|
||||
let names;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
import path from 'path';
|
||||
import validateTargetDirectory from './../src/utilities/validateTargetDirectory';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, './fixtures/validate-target-directory');
|
||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/validate-target-directory');
|
||||
|
||||
describe('validateTargetDirectory()', () => {
|
||||
describe('directory path', () => {
|
||||
@@ -31,7 +31,7 @@ describe('validateTargetDirectory()', () => {
|
||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'no-index'))).to.equal(true);
|
||||
});
|
||||
});
|
||||
context.skip('safe', () => {
|
||||
context('safe', () => {
|
||||
it('returns true', () => {
|
||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
||||
});
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ import path from 'path';
|
||||
import codeExample from './codeExample';
|
||||
import writeIndex from './../src/utilities/writeIndex';
|
||||
|
||||
const fixturesPath = path.resolve(__dirname, './fixtures/write-index');
|
||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/write-index');
|
||||
|
||||
describe('writeIndex()', () => {
|
||||
it('creates index in target directory', () => {
|
||||
|
||||
Reference in New Issue
Block a user