Compare commits
+3
-4
@@ -1,12 +1,11 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
|
- node
|
||||||
- 7
|
- 7
|
||||||
- 6
|
- 6
|
||||||
- 5
|
- 5
|
||||||
scripts:
|
after_success:
|
||||||
- npm run test
|
- semantic-release pre && npm publish && semantic-release post
|
||||||
- npm run lint
|
|
||||||
- npm run build
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ This created `index.js` with:
|
|||||||
```js
|
```js
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar.js';
|
export { default as bar } from './bar.js';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -66,9 +66,9 @@ This have updated `index.js` file:
|
|||||||
```js
|
```js
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar.js';
|
export { default as bar } from './bar.js';
|
||||||
export baz from './baz.js';
|
export { default as baz } from './baz.js';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -82,10 +82,13 @@ npm install create-index
|
|||||||
create-index --help
|
create-index --help
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--update, -u Recursively iterates target directories looking for "index.js"
|
--recursive, -r Create/update index files recursively. Halts on any unsafe
|
||||||
files that start with "'create index';\n" (create-index index
|
"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]
|
||||||
|
--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:
|
Examples:
|
||||||
create-index ./src ./src/utilities Creates or updates an existing
|
create-index ./src ./src/utilities Creates or updates an existing
|
||||||
@@ -172,10 +175,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,
|
||||||
@@ -187,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.
|
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.
|
||||||
|
|||||||
+36
-32
@@ -1,33 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "create-index",
|
|
||||||
"description": "Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"main": "./dist/index.js",
|
|
||||||
"bin": "./dist/bin/create-index.js",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/gajus/create-index"
|
|
||||||
},
|
|
||||||
"keywords": [
|
|
||||||
"index",
|
|
||||||
"import",
|
|
||||||
"export"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": ">=5"
|
|
||||||
},
|
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Gajus Kuizinas",
|
|
||||||
"email": "gajus@gajus.com",
|
"email": "gajus@gajus.com",
|
||||||
|
"name": "Gajus Kuizinas",
|
||||||
"url": "http://gajus.com"
|
"url": "http://gajus.com"
|
||||||
},
|
},
|
||||||
"license": "BSD-3-Clause",
|
"bin": "./dist/bin/create-index.js",
|
||||||
"scripts": {
|
"dependencies": {
|
||||||
"lint": "eslint ./src ./tests",
|
"chalk": "^1.1.3",
|
||||||
"test": "mocha --compilers js:babel-register",
|
"glob": "^7.1.1",
|
||||||
"build": "babel --source-maps --copy-files ./src --out-dir ./dist",
|
"lodash": "^4.16.6",
|
||||||
"create-index": "node ./dist/bin/create-index ./src/utilities"
|
"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": {
|
"devDependencies": {
|
||||||
"babel-cli": "^6.18.0",
|
"babel-cli": "^6.18.0",
|
||||||
"babel-plugin-transform-es2015-destructuring": "^6.18.0",
|
"babel-plugin-transform-es2015-destructuring": "^6.18.0",
|
||||||
@@ -36,15 +21,34 @@
|
|||||||
"babel-plugin-transform-export-extensions": "^6.8.0",
|
"babel-plugin-transform-export-extensions": "^6.8.0",
|
||||||
"babel-register": "^6.18.0",
|
"babel-register": "^6.18.0",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
|
"cross-env": "^3.1.3",
|
||||||
"eslint": "^3.9.1",
|
"eslint": "^3.9.1",
|
||||||
"eslint-config-canonical": "^4.0.0",
|
"eslint-config-canonical": "^4.0.0",
|
||||||
"mocha": "^3.1.2"
|
"husky": "^0.11.9",
|
||||||
|
"mocha": "^3.1.2",
|
||||||
|
"semantic-release": "^6.3.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"engines": {
|
||||||
"glob": "^7.1.1",
|
"node": ">=5"
|
||||||
"lodash": "^4.16.6",
|
},
|
||||||
"yargs": "^6.3.0",
|
"keywords": [
|
||||||
"chalk": "^1.1.3",
|
"index",
|
||||||
"moment": "^2.15.2"
|
"import",
|
||||||
}
|
"export"
|
||||||
|
],
|
||||||
|
"license": "BSD-3-Clause",
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"name": "create-index",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/gajus/create-index"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "cross-env NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
|
||||||
|
"create-index": "node ./dist/bin/create-index ./src/utilities",
|
||||||
|
"lint": "cross-env NODE_ENV=development eslint ./src ./tests",
|
||||||
|
"precommit": "npm run test",
|
||||||
|
"test": "npm run build && npm run lint && cross-env NODE_ENV=development mocha --compilers js:babel-register"
|
||||||
|
},
|
||||||
|
"version": "1.1.0"
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-3
@@ -8,17 +8,52 @@ import {
|
|||||||
const argv = yargs
|
const argv = yargs
|
||||||
.demand(1)
|
.demand(1)
|
||||||
.options({
|
.options({
|
||||||
'update': {
|
recursive: {
|
||||||
|
alias: 'r',
|
||||||
|
default: false,
|
||||||
|
description: 'Create/update index files recursively. Halts on any unsafe "index.js" files.',
|
||||||
|
type: 'boolean'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.options({
|
||||||
|
ignoreUnsafe: {
|
||||||
|
alias: 'i',
|
||||||
|
default: false,
|
||||||
|
description: 'Ignores unsafe "index.js" files instead of halting.',
|
||||||
|
type: 'boolean'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.options({
|
||||||
|
update: {
|
||||||
alias: 'u',
|
alias: 'u',
|
||||||
default: false,
|
default: false,
|
||||||
description: 'Recursively iterates target directories looking for "index.js" files that start with "\'create index\';\\n" (create-index index file). Updates found index files. Does not create new index files.',
|
description: 'Updates only previously created index files (recursively).',
|
||||||
type: 'boolean'
|
type: 'boolean'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.options({
|
||||||
|
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'
|
||||||
|
}
|
||||||
|
})
|
||||||
.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 ./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 --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,
|
||||||
|
ignoreUnsafe: argv.ignoreUnsafe,
|
||||||
|
recursive: argv.recursive,
|
||||||
|
updateIndex: argv.update
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const buildExportBlock = (files) => {
|
|||||||
let importBlock;
|
let importBlock;
|
||||||
|
|
||||||
importBlock = _.map(files, (fileName) => {
|
importBlock = _.map(files, (fileName) => {
|
||||||
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
return 'export { default as ' + safeVariableName(fileName) + ' } from \'./' + fileName + '\';';
|
||||||
});
|
});
|
||||||
|
|
||||||
importBlock = importBlock.join('\n');
|
importBlock = importBlock.join('\n');
|
||||||
@@ -22,10 +22,22 @@ const buildExportBlock = (files) => {
|
|||||||
return importBlock;
|
return importBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (filePaths) => {
|
export default (filePaths, options = {}) => {
|
||||||
let code;
|
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) {
|
if (filePaths.length) {
|
||||||
const sortedFilePaths = filePaths.sort();
|
const sortedFilePaths = filePaths.sort();
|
||||||
|
|||||||
@@ -3,23 +3,18 @@ import _ from 'lodash';
|
|||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
export default (directoryPath) => {
|
export default (directoryPath, options = {}) => {
|
||||||
let targetDirectories;
|
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) => {
|
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||||
try {
|
return validateTargetDirectory(path.dirname(targetDirectoryPath), {
|
||||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
silent: options.silent
|
||||||
|
});
|
||||||
return true;
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export createIndexCode from './createIndexCode.js';
|
export createIndexCode from './createIndexCode.js';
|
||||||
export findIndexFiles from './findIndexFiles.js';
|
export findIndexFiles from './findIndexFiles.js';
|
||||||
@@ -8,4 +8,3 @@ export sortByDepth from './sortByDepth.js';
|
|||||||
export validateTargetDirectory from './validateTargetDirectory.js';
|
export validateTargetDirectory from './validateTargetDirectory.js';
|
||||||
export writeIndex from './writeIndex.js';
|
export writeIndex from './writeIndex.js';
|
||||||
export writeIndexCli from './writeIndexCli.js';
|
export writeIndexCli from './writeIndexCli.js';
|
||||||
|
|
||||||
|
|||||||
@@ -31,18 +31,38 @@ const isSafeName = (fileName) => {
|
|||||||
return /^[a-z][a-z0-9._]+$/i.test(fileName);
|
return /^[a-z][a-z0-9._]+$/i.test(fileName);
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeDuplicates = (files) => {
|
const stripExtension = (fileName) => {
|
||||||
|
const pos = fileName.lastIndexOf('.');
|
||||||
|
|
||||||
|
if (pos === -1) {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName.substr(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) => {
|
export default (directoryPath, options = {}) => {
|
||||||
let children;
|
let children;
|
||||||
|
|
||||||
validateTargetDirectory(directoryPath);
|
if (!validateTargetDirectory(directoryPath, {silent: options.silent})) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
children = fs.readdirSync(directoryPath);
|
children = fs.readdirSync(directoryPath);
|
||||||
|
const {extensions = ['js']} = options;
|
||||||
|
|
||||||
children = _.filter(children, (fileName) => {
|
children = _.filter(children, (fileName) => {
|
||||||
const absolutePath = path.resolve(directoryPath, fileName);
|
const absolutePath = path.resolve(directoryPath, fileName);
|
||||||
@@ -64,7 +84,9 @@ export default (directoryPath) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
if (!isDirectory && !extensions.some((ext) => {
|
||||||
|
return _.endsWith(fileName, '.' + ext);
|
||||||
|
})) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +97,7 @@ export default (directoryPath) => {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
children = removeDuplicates(children);
|
children = removeDuplicates(children, extensions[0]);
|
||||||
|
|
||||||
return children.sort();
|
return children.sort();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,27 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import _ from 'lodash';
|
|
||||||
|
|
||||||
export default (targetDirectory) => {
|
export default (targetDirectory, options = {}) => {
|
||||||
|
const silent = options.silent;
|
||||||
let stats;
|
let stats;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stats = fs.statSync(targetDirectory);
|
stats = fs.statSync(targetDirectory);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
throw new Error('"' + targetDirectory + '" is not a directory.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||||
|
|
||||||
@@ -25,9 +33,14 @@ export default (targetDirectory) => {
|
|||||||
|
|
||||||
const 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.');
|
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,15 +6,14 @@ import validateTargetDirectory from './validateTargetDirectory';
|
|||||||
import readDirectory from './readDirectory';
|
import readDirectory from './readDirectory';
|
||||||
import sortByDepth from './sortByDepth';
|
import sortByDepth from './sortByDepth';
|
||||||
|
|
||||||
export default (directoryPaths) => {
|
export default (directoryPaths, options = {}) => {
|
||||||
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
const sortedDirectoryPaths = sortByDepth(directoryPaths)
|
||||||
|
.filter((directoryPath) => {
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
return validateTargetDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
||||||
validateTargetDirectory(directoryPath);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
const siblings = readDirectory(directoryPath);
|
const siblings = readDirectory(directoryPath, options);
|
||||||
const indexCode = createIndexCode(siblings);
|
const indexCode = createIndexCode(siblings);
|
||||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
|
|||||||
@@ -15,27 +15,43 @@ 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'));
|
|
||||||
|
|
||||||
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, (dir) => {
|
||||||
|
return findIndexFiles(dir, {
|
||||||
|
fileName: options.updateIndex ? '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, {silent: options.ignoreUnsafe});
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
let existingIndexCode;
|
let existingIndexCode;
|
||||||
|
|
||||||
const siblings = readDirectory(directoryPath);
|
const siblings = readDirectory(directoryPath, {
|
||||||
|
extensions: options.extensions,
|
||||||
|
silent: options.ignoreUnsafe
|
||||||
|
});
|
||||||
|
|
||||||
const indexCode = createIndexCode(siblings);
|
const indexCode = createIndexCode(siblings, {
|
||||||
|
banner: options.banner
|
||||||
|
});
|
||||||
|
|
||||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
@@ -52,7 +68,7 @@ export default (directoryPaths, options = {}) => {
|
|||||||
fs.writeFileSync(indexFilePath, indexCode);
|
fs.writeFileSync(indexFilePath, indexCode);
|
||||||
|
|
||||||
if (existingIndexCode && existingIndexCode === indexCode) {
|
if (existingIndexCode && existingIndexCode === indexCode) {
|
||||||
log(indexFilePath, chalk.yellow('[index have not changed]'));
|
log(indexFilePath, chalk.yellow('[index has not changed]'));
|
||||||
} else if (existingIndexCode && existingIndexCode !== indexCode) {
|
} else if (existingIndexCode && existingIndexCode !== indexCode) {
|
||||||
log(indexFilePath, chalk.green('[updated index]'));
|
log(indexFilePath, chalk.green('[updated index]'));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+11
-11
@@ -11,26 +11,26 @@ describe('createIndexCode()', () => {
|
|||||||
const indexCode = createIndexCode([]);
|
const indexCode = createIndexCode([]);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
// @create-index
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
it('describes a single child', () => {
|
it('describes a single child', () => {
|
||||||
const indexCode = createIndexCode(['foo']);
|
const indexCode = createIndexCode(['foo']);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
it('describes multiple children', () => {
|
it('describes multiple children', () => {
|
||||||
const indexCode = createIndexCode(['bar', 'foo']);
|
const indexCode = createIndexCode(['bar', 'foo']);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
context('file with extension', () => {
|
context('file with extension', () => {
|
||||||
@@ -38,9 +38,9 @@ export foo from './foo';
|
|||||||
const indexCode = createIndexCode(['foo.js']);
|
const indexCode = createIndexCode(['foo.js']);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -49,10 +49,10 @@ export foo from './foo.js';
|
|||||||
const indexCode = createIndexCode(['foo', 'bar']);
|
const indexCode = createIndexCode(['foo', 'bar']);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
|
// @create-index
|
||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,45 @@ describe('readDirectory()', () => {
|
|||||||
expect(names).to.deep.equal(['present.js']);
|
expect(names).to.deep.equal(['present.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
context('target directory contains non js files, and not configured to allow that', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'));
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains non js files, and allowing only jsx', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = { extensions: ['jsx'] };
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.jsx']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains non js files, and allowing both js and jsx', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = { extensions: ['js', 'jsx'] };
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.jsx', 'present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JS as it is first extension listed', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = { extensions: ['js', 'jsx'] };
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.js', 'present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('target directory contains homonyms files, and allowing both js and jsx, will prefer JSX as it is first extension listed', () => {
|
||||||
|
it('prefers file', () => {
|
||||||
|
const options = { extensions: ['jsx', 'js'] };
|
||||||
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-alt-extension-with-homonyms'), options);
|
||||||
|
|
||||||
|
expect(names).to.deep.equal(['bar.jsx', 'present.js']);
|
||||||
|
});
|
||||||
|
});
|
||||||
context('target directory contains files with no extension', () => {
|
context('target directory contains files with no extension', () => {
|
||||||
it('ignores files', () => {
|
it('ignores files', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ describe('validateTargetDirectory()', () => {
|
|||||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
context('safe with banner', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index-with-banner'))).to.equal(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
context('unsafe', () => {
|
context('unsafe', () => {
|
||||||
it('throws an error', () => {
|
it('throws an error', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
@@ -43,5 +48,10 @@ describe('validateTargetDirectory()', () => {
|
|||||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
context('unsafe ignored', () => {
|
||||||
|
it('returns false', () => {
|
||||||
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'unsafe-index'), {silent: true})).to.equal(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+3
-3
@@ -27,10 +27,10 @@ describe('writeIndex()', () => {
|
|||||||
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user