Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fea5e63d7 | ||
|
|
7353f04394 | ||
|
|
65892e3bb8 | ||
|
|
893d9931d4 | ||
|
|
9113572a43 | ||
|
|
cedb893e7c | ||
|
|
2bafc5bb85 | ||
|
|
5839536cf0 |
@@ -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';
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -177,8 +177,8 @@ Given the above directory contents, `./index.js` will be:
|
|||||||
```js
|
```js
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
import bar from './bar';
|
import { default as bar } from './bar';
|
||||||
import foo from './foo.js';
|
import { default as foo } from './foo.js';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
bar,
|
bar,
|
||||||
|
|||||||
+5
-4
@@ -21,6 +21,7 @@
|
|||||||
"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",
|
||||||
"husky": "^0.11.9",
|
"husky": "^0.11.9",
|
||||||
@@ -43,11 +44,11 @@
|
|||||||
"url": "https://github.com/gajus/create-index"
|
"url": "https://github.com/gajus/create-index"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
|
"build": "cross-env NODE_ENV=production babel --source-maps --copy-files ./src --out-dir ./dist",
|
||||||
"create-index": "node ./dist/bin/create-index ./src/utilities",
|
"create-index": "node ./dist/bin/create-index ./src/utilities",
|
||||||
"lint": "NODE_ENV=development eslint ./src ./tests",
|
"lint": "cross-env NODE_ENV=development eslint ./src ./tests",
|
||||||
"precommit": "npm run test",
|
"precommit": "npm run test",
|
||||||
"test": "npm run build && npm run lint && NODE_ENV=development mocha --compilers js:babel-register"
|
"test": "npm run build && npm run lint && cross-env NODE_ENV=development mocha --compilers js:babel-register"
|
||||||
},
|
},
|
||||||
"version": "1.0.0"
|
"version": "1.1.0"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,12 +37,22 @@ const argv = yargs
|
|||||||
type: 'string'
|
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._, {
|
||||||
banner: argv.banner,
|
banner: argv.banner,
|
||||||
|
extensions: argv.extensions,
|
||||||
ignoreUnsafe: argv.ignoreUnsafe,
|
ignoreUnsafe: argv.ignoreUnsafe,
|
||||||
recursive: argv.recursive,
|
recursive: argv.recursive,
|
||||||
updateIndex: argv.update
|
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');
|
||||||
|
|||||||
@@ -31,9 +31,26 @@ 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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,6 +62,7 @@ export default (directoryPath, options = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@@ -66,7 +84,9 @@ export default (directoryPath, options = {}) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
if (!isDirectory && !extensions.some((ext) => {
|
||||||
|
return _.endsWith(fileName, '.' + ext);
|
||||||
|
})) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +97,7 @@ export default (directoryPath, options = {}) => {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
children = removeDuplicates(children);
|
children = removeDuplicates(children, extensions[0]);
|
||||||
|
|
||||||
return children.sort();
|
return children.sort();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export default (directoryPaths, options = {}) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_.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');
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ export default (directoryPaths, options = {}) => {
|
|||||||
if (options.updateIndex) {
|
if (options.updateIndex) {
|
||||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||||
} else {
|
} else {
|
||||||
log('Recursive:', options.ignoreUnsafe ? chalk.green('true') : chalk.red('false'));
|
log('Recursive:', options.recursive ? chalk.green('true') : chalk.red('false'));
|
||||||
log('Ignore unsafe:', options.ignoreUnsafe ? 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) {
|
if (options.updateIndex || options.recursive) {
|
||||||
@@ -43,7 +44,10 @@ export default (directoryPaths, options = {}) => {
|
|||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
let existingIndexCode;
|
let existingIndexCode;
|
||||||
|
|
||||||
const siblings = readDirectory(directoryPath, {silent: options.ignoreUnsafe});
|
const siblings = readDirectory(directoryPath, {
|
||||||
|
extensions: options.extensions,
|
||||||
|
silent: options.ignoreUnsafe
|
||||||
|
});
|
||||||
|
|
||||||
const indexCode = createIndexCode(siblings, {
|
const indexCode = createIndexCode(siblings, {
|
||||||
banner: options.banner
|
banner: options.banner
|
||||||
@@ -64,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 {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ describe('createIndexCode()', () => {
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
it('describes multiple children', () => {
|
it('describes multiple children', () => {
|
||||||
@@ -29,8 +29,8 @@ export foo from './foo';
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
context('file with extension', () => {
|
context('file with extension', () => {
|
||||||
@@ -40,7 +40,7 @@ export foo from './foo';
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -51,8 +51,8 @@ export foo from './foo.js';
|
|||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo';
|
export { default as foo } from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
|
// @create-index
|
||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
// @create-index
|
// @create-index
|
||||||
|
|
||||||
export bar from './bar';
|
export { default as bar } from './bar';
|
||||||
export foo from './foo.js';
|
export { default as foo } from './foo.js';
|
||||||
|
|
||||||
|
|||||||
@@ -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(() => {
|
||||||
|
|||||||
+2
-2
@@ -29,8 +29,8 @@ describe('writeIndex()', () => {
|
|||||||
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