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