Code style preferences.
This commit is contained in:
@@ -5,9 +5,7 @@ import {
|
|||||||
writeIndexCli
|
writeIndexCli
|
||||||
} from './../utilities';
|
} from './../utilities';
|
||||||
|
|
||||||
let argv;
|
const argv = yargs
|
||||||
|
|
||||||
argv = yargs
|
|
||||||
.demand(1)
|
.demand(1)
|
||||||
.options({
|
.options({
|
||||||
'update-index': {
|
'update-index': {
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
let buildExportBlock,
|
const safeVariableName = (fileName) => {
|
||||||
safeVariableName;
|
const indexOfDot = fileName.indexOf('.');
|
||||||
|
|
||||||
safeVariableName = (fileName) => {
|
|
||||||
let indexOfDot;
|
|
||||||
|
|
||||||
indexOfDot = fileName.indexOf('.');
|
|
||||||
|
|
||||||
if (indexOfDot === -1) {
|
if (indexOfDot === -1) {
|
||||||
return fileName;
|
return fileName;
|
||||||
@@ -15,7 +10,7 @@ safeVariableName = (fileName) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
buildExportBlock = (files) => {
|
const buildExportBlock = (files) => {
|
||||||
let importBlock;
|
let importBlock;
|
||||||
|
|
||||||
importBlock = _.map(files, (fileName) => {
|
importBlock = _.map(files, (fileName) => {
|
||||||
@@ -28,13 +23,12 @@ buildExportBlock = (files) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default (filePaths) => {
|
export default (filePaths) => {
|
||||||
let code,
|
let code;
|
||||||
sortedFilePaths;
|
|
||||||
|
|
||||||
code = '\'create index\';\n\n';
|
code = '\'create index\';\n\n';
|
||||||
|
|
||||||
if (filePaths.length) {
|
if (filePaths.length) {
|
||||||
sortedFilePaths = filePaths.sort();
|
const sortedFilePaths = filePaths.sort();
|
||||||
|
|
||||||
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,8 @@ import _ from 'lodash';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
let hasIndex,
|
const hasIndex = (directoryPath) => {
|
||||||
hasMultipleExtensions,
|
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||||
hasNoExtension,
|
|
||||||
isSafeName,
|
|
||||||
removeDuplicates;
|
|
||||||
|
|
||||||
hasIndex = (directoryPath) => {
|
|
||||||
let indexPath;
|
|
||||||
|
|
||||||
indexPath = path.resolve(directoryPath, 'index.js');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.statSync(indexPath);
|
fs.statSync(indexPath);
|
||||||
@@ -23,27 +15,23 @@ hasIndex = (directoryPath) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
hasNoExtension = (fileName) => {
|
const hasNoExtension = (fileName) => {
|
||||||
let matches;
|
const matches = fileName.match(/\./g);
|
||||||
|
|
||||||
matches = fileName.match(/\./g);
|
|
||||||
|
|
||||||
return !matches;
|
return !matches;
|
||||||
};
|
};
|
||||||
|
|
||||||
hasMultipleExtensions = (fileName) => {
|
const hasMultipleExtensions = (fileName) => {
|
||||||
let matches;
|
const matches = fileName.match(/\./g);
|
||||||
|
|
||||||
matches = fileName.match(/\./g);
|
|
||||||
|
|
||||||
return matches && matches.length > 1;
|
return matches && matches.length > 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
isSafeName = (fileName) => {
|
const isSafeName = (fileName) => {
|
||||||
return /^[a-z][a-z0-9\._]+$/i.test(fileName);
|
return /^[a-z][a-z0-9\._]+$/i.test(fileName);
|
||||||
};
|
};
|
||||||
|
|
||||||
removeDuplicates = (files) => {
|
const removeDuplicates = (files) => {
|
||||||
return _.filter(files, (fileName) => {
|
return _.filter(files, (fileName) => {
|
||||||
return !_.includes(files, fileName + '.js');
|
return !_.includes(files, fileName + '.js');
|
||||||
});
|
});
|
||||||
@@ -57,12 +45,8 @@ export default (directoryPath) => {
|
|||||||
children = fs.readdirSync(directoryPath);
|
children = fs.readdirSync(directoryPath);
|
||||||
|
|
||||||
children = _.filter(children, (fileName) => {
|
children = _.filter(children, (fileName) => {
|
||||||
let absolutePath,
|
const absolutePath = path.resolve(directoryPath, fileName);
|
||||||
isDirectory;
|
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||||
|
|
||||||
absolutePath = path.resolve(directoryPath, fileName);
|
|
||||||
|
|
||||||
isDirectory = fs.statSync(absolutePath).isDirectory();
|
|
||||||
|
|
||||||
if (!isSafeName(fileName)) {
|
if (!isSafeName(fileName)) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
export default (targetDirectory) => {
|
export default (targetDirectory) => {
|
||||||
let indexFile,
|
let stats;
|
||||||
indexFilePath,
|
|
||||||
stats;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stats = fs.statSync(targetDirectory);
|
stats = fs.statSync(targetDirectory);
|
||||||
@@ -17,7 +15,7 @@ export default (targetDirectory) => {
|
|||||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
throw new Error('"' + targetDirectory + '" is not a directory.');
|
||||||
}
|
}
|
||||||
|
|
||||||
indexFilePath = path.resolve(targetDirectory, './index.js');
|
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.statSync(indexFilePath);
|
fs.statSync(indexFilePath);
|
||||||
@@ -25,7 +23,7 @@ export default (targetDirectory) => {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|
||||||
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
||||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||||
|
|||||||
@@ -7,24 +7,16 @@ import readDirectory from './readDirectory';
|
|||||||
import sortByDepth from './sortByDepth';
|
import sortByDepth from './sortByDepth';
|
||||||
|
|
||||||
export default (directoryPaths) => {
|
export default (directoryPaths) => {
|
||||||
let sortedDirectoryPaths;
|
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||||
|
|
||||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
validateTargetDirectory(directoryPath);
|
validateTargetDirectory(directoryPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
let indexCode,
|
const siblings = readDirectory(directoryPath);
|
||||||
indexFilePath,
|
const indexCode = createIndexCode(siblings);
|
||||||
siblings;
|
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
siblings = readDirectory(directoryPath);
|
|
||||||
|
|
||||||
indexCode = createIndexCode(siblings);
|
|
||||||
|
|
||||||
indexFilePath = path.resolve(directoryPath, 'index.js');
|
|
||||||
|
|
||||||
fs.writeFileSync(indexFilePath, indexCode);
|
fs.writeFileSync(indexFilePath, indexCode);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -31,16 +31,13 @@ export default (directoryPaths, options = {}) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
let existingIndexCode,
|
let existingIndexCode;
|
||||||
indexCode,
|
|
||||||
indexFilePath,
|
|
||||||
siblings;
|
|
||||||
|
|
||||||
siblings = readDirectory(directoryPath);
|
const siblings = readDirectory(directoryPath);
|
||||||
|
|
||||||
indexCode = createIndexCode(siblings);
|
const indexCode = createIndexCode(siblings);
|
||||||
|
|
||||||
indexFilePath = path.resolve(directoryPath, 'index.js');
|
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|||||||
@@ -10,18 +10,14 @@ import codeExample from './codeExample';
|
|||||||
|
|
||||||
describe('createIndexCode()', () => {
|
describe('createIndexCode()', () => {
|
||||||
it('describes no children', () => {
|
it('describes no children', () => {
|
||||||
let indexCode;
|
const indexCode = createIndexCode([]);
|
||||||
|
|
||||||
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', () => {
|
||||||
let indexCode;
|
const indexCode = createIndexCode(['foo']);
|
||||||
|
|
||||||
indexCode = createIndexCode(['foo']);
|
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
'create index';
|
||||||
@@ -30,9 +26,7 @@ export foo from './foo';
|
|||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
it('describes multiple children', () => {
|
it('describes multiple children', () => {
|
||||||
let indexCode;
|
const indexCode = createIndexCode(['bar', 'foo']);
|
||||||
|
|
||||||
indexCode = createIndexCode(['bar', 'foo']);
|
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
'create index';
|
||||||
@@ -43,9 +37,7 @@ export foo from './foo';
|
|||||||
});
|
});
|
||||||
context('file with extension', () => {
|
context('file with extension', () => {
|
||||||
it('removes the extension from the export statement', () => {
|
it('removes the extension from the export statement', () => {
|
||||||
let indexCode;
|
const indexCode = createIndexCode(['foo.js']);
|
||||||
|
|
||||||
indexCode = createIndexCode(['foo.js']);
|
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
'create index';
|
||||||
@@ -56,9 +48,7 @@ export foo from './foo.js';
|
|||||||
});
|
});
|
||||||
context('multiple, unsorted', () => {
|
context('multiple, unsorted', () => {
|
||||||
it('sorts the files', () => {
|
it('sorts the files', () => {
|
||||||
let indexCode;
|
const indexCode = createIndexCode(['foo', 'bar']);
|
||||||
|
|
||||||
indexCode = createIndexCode(['foo', 'bar']);
|
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
'create index';
|
||||||
|
|||||||
+9
-27
@@ -10,81 +10,63 @@ const fixturesPath = path.resolve(__dirname, './../../fixtures/read-directory');
|
|||||||
describe('readDirectory()', () => {
|
describe('readDirectory()', () => {
|
||||||
context('target directory contains child directories', () => {
|
context('target directory contains child directories', () => {
|
||||||
it('gets names of the children directories', () => {
|
it('gets names of the children directories', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-directories'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['bar', 'foo']);
|
expect(names).to.deep.equal(['bar', 'foo']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains child directories that do not contain index', () => {
|
context('target directory contains child directories that do not contain index', () => {
|
||||||
it('gets names of the children directories', () => {
|
it('gets names of the children directories', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-without-index'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories-without-index'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['present.js']);
|
expect(names).to.deep.equal(['present.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains child directories (unsafe name)', () => {
|
context('target directory contains child directories (unsafe name)', () => {
|
||||||
it('gets names of the children directories', () => {
|
it('gets names of the children directories', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['present']);
|
expect(names).to.deep.equal(['present']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains ./index.js', () => {
|
context('target directory contains ./index.js', () => {
|
||||||
it('does not include ./index.js', () => {
|
it('does not include ./index.js', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-index'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-index'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['bar', 'foo']);
|
expect(names).to.deep.equal(['bar', 'foo']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains files', () => {
|
context('target directory contains files', () => {
|
||||||
it('refers to the files (with extension)', () => {
|
it('refers to the files (with extension)', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-files'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['bar.js', 'foo.js']);
|
expect(names).to.deep.equal(['bar.js', 'foo.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains dot files', () => {
|
context('target directory contains dot files', () => {
|
||||||
it('ignores files', () => {
|
it('ignores files', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-dot-files'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-dot-files'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['present.js']);
|
expect(names).to.deep.equal(['present.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains files with no extension', () => {
|
context('target directory contains files with no extension', () => {
|
||||||
it('ignores files', () => {
|
it('ignores files', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['present.js']);
|
expect(names).to.deep.equal(['present.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains files with multiple extensions', () => {
|
context('target directory contains files with multiple extensions', () => {
|
||||||
it('ignores files', () => {
|
it('ignores files', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-files-multiple-extensions'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-files-multiple-extensions'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['present.js']);
|
expect(names).to.deep.equal(['present.js']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('target directory contains directories and files with the same name', () => {
|
context('target directory contains directories and files with the same name', () => {
|
||||||
it('prefers file', () => {
|
it('prefers file', () => {
|
||||||
let names;
|
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-and-files'));
|
||||||
|
|
||||||
names = readDirectory(path.resolve(fixturesPath, 'children-directories-and-files'));
|
|
||||||
|
|
||||||
expect(names).to.deep.equal(['foo.js', 'present.js']);
|
expect(names).to.deep.equal(['foo.js', 'present.js']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,17 +5,14 @@ import sortByDepth from './../src/utilities/sortByDepth';
|
|||||||
|
|
||||||
describe('sortByDepth()', () => {
|
describe('sortByDepth()', () => {
|
||||||
it('sorts from deepest to the most shallow', () => {
|
it('sorts from deepest to the most shallow', () => {
|
||||||
let paths,
|
const paths = [
|
||||||
sortedPaths;
|
|
||||||
|
|
||||||
paths = [
|
|
||||||
'/b',
|
'/b',
|
||||||
'/a',
|
'/a',
|
||||||
'/a/b/c',
|
'/a/b/c',
|
||||||
'/a/b'
|
'/a/b'
|
||||||
];
|
];
|
||||||
|
|
||||||
sortedPaths = sortByDepth(paths);
|
const sortedPaths = sortByDepth(paths);
|
||||||
|
|
||||||
expect(sortedPaths).to.deep.equal(['/a/b/c', '/a/b', '/b', '/a']);
|
expect(sortedPaths).to.deep.equal(['/a/b/c', '/a/b', '/b', '/a']);
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-5
@@ -12,10 +12,7 @@ const fixturesPath = path.resolve(__dirname, './../../fixtures/write-index');
|
|||||||
|
|
||||||
describe('writeIndex()', () => {
|
describe('writeIndex()', () => {
|
||||||
it('creates index in target directory', () => {
|
it('creates index in target directory', () => {
|
||||||
let indexCode,
|
const indexFilePath = path.resolve(fixturesPath, './mixed/index.js');
|
||||||
indexFilePath;
|
|
||||||
|
|
||||||
indexFilePath = path.resolve(fixturesPath, './mixed/index.js');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.unlinkSync(indexFilePath);
|
fs.unlinkSync(indexFilePath);
|
||||||
@@ -27,7 +24,7 @@ describe('writeIndex()', () => {
|
|||||||
|
|
||||||
writeIndex([path.resolve(fixturesPath, './mixed')]);
|
writeIndex([path.resolve(fixturesPath, './mixed')]);
|
||||||
|
|
||||||
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';
|
||||||
|
|||||||
Reference in New Issue
Block a user