Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65892e3bb8 | ||
|
|
893d9931d4 | ||
|
|
9113572a43 | ||
|
|
cedb893e7c | ||
|
|
2bafc5bb85 | ||
|
|
5839536cf0 | ||
|
|
219f546c67 |
@@ -82,11 +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]
|
||||||
--banner Add a custom banner at the top of the index file [string]
|
--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
|
||||||
|
|||||||
+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"
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-1
@@ -7,11 +7,27 @@ import {
|
|||||||
|
|
||||||
const argv = yargs
|
const argv = yargs
|
||||||
.demand(1)
|
.demand(1)
|
||||||
|
.options({
|
||||||
|
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({
|
.options({
|
||||||
update: {
|
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'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -27,5 +43,7 @@ const argv = yargs
|
|||||||
|
|
||||||
writeIndexCli(argv._, {
|
writeIndexCli(argv._, {
|
||||||
banner: argv.banner,
|
banner: argv.banner,
|
||||||
|
ignoreUnsafe: argv.ignoreUnsafe,
|
||||||
|
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');
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -37,10 +37,12 @@ const removeDuplicates = (files) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
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) {
|
||||||
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
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]+/)) {
|
||||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
if (silent) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,11 @@ 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);
|
||||||
|
|||||||
@@ -15,25 +15,35 @@ 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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
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, {silent: options.ignoreUnsafe});
|
||||||
|
|
||||||
const indexCode = createIndexCode(siblings, {
|
const indexCode = createIndexCode(siblings, {
|
||||||
banner: options.banner
|
banner: options.banner
|
||||||
@@ -54,7 +64,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';
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+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