style: fix code style
This commit is contained in:
+13
-13
@@ -3,21 +3,21 @@
|
|||||||
import yargs from 'yargs';
|
import yargs from 'yargs';
|
||||||
import {
|
import {
|
||||||
writeIndexCli
|
writeIndexCli
|
||||||
} from './../utilities';
|
} from '../utilities';
|
||||||
|
|
||||||
const argv = yargs
|
const argv = yargs
|
||||||
.demand(1)
|
.demand(1)
|
||||||
.options({
|
.options({
|
||||||
'update-index': {
|
'update-index': {
|
||||||
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: '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.',
|
||||||
type: 'boolean'
|
type: 'boolean'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.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-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
|
.example('create-index --update-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.')
|
||||||
.argv;
|
.argv;
|
||||||
|
|
||||||
writeIndexCli(argv._, {
|
writeIndexCli(argv._, {
|
||||||
updateIndex: argv.updateIndex
|
updateIndex: argv.updateIndex
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
export {
|
export {
|
||||||
findIndexFiles,
|
findIndexFiles,
|
||||||
writeIndex
|
writeIndex
|
||||||
} from './utilities';
|
} from './utilities';
|
||||||
|
|||||||
@@ -1,37 +1,37 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
const safeVariableName = (fileName) => {
|
const safeVariableName = (fileName) => {
|
||||||
const indexOfDot = fileName.indexOf('.');
|
const indexOfDot = fileName.indexOf('.');
|
||||||
|
|
||||||
if (indexOfDot === -1) {
|
if (indexOfDot === -1) {
|
||||||
return fileName;
|
return fileName;
|
||||||
} else {
|
} else {
|
||||||
return fileName.slice(0, indexOfDot);
|
return fileName.slice(0, indexOfDot);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildExportBlock = (files) => {
|
const buildExportBlock = (files) => {
|
||||||
let importBlock;
|
let importBlock;
|
||||||
|
|
||||||
importBlock = _.map(files, (fileName) => {
|
importBlock = _.map(files, (fileName) => {
|
||||||
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
||||||
});
|
});
|
||||||
|
|
||||||
importBlock = importBlock.join('\n');
|
importBlock = importBlock.join('\n');
|
||||||
|
|
||||||
return importBlock;
|
return importBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (filePaths) => {
|
export default (filePaths) => {
|
||||||
let code;
|
let code;
|
||||||
|
|
||||||
code = '\'create index\';\n\n';
|
code = '\'create index\';\n\n';
|
||||||
|
|
||||||
if (filePaths.length) {
|
if (filePaths.length) {
|
||||||
const sortedFilePaths = filePaths.sort();
|
const sortedFilePaths = filePaths.sort();
|
||||||
|
|
||||||
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
|
import path from 'path';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
import path from 'path';
|
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
export default (directoryPath) => {
|
export default (directoryPath) => {
|
||||||
let targetDirectories;
|
let targetDirectories;
|
||||||
|
|
||||||
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
||||||
|
|
||||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||||
try {
|
try {
|
||||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
/* eslint-disable no-empty */
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
// eslint-disable-next-line no-empty
|
||||||
/* eslint-enable no-empty */
|
} catch (error) {
|
||||||
|
|
||||||
return false;
|
}
|
||||||
});
|
|
||||||
|
|
||||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
return targetDirectories;
|
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||||
|
|
||||||
|
return targetDirectories;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import moment from 'moment';
|
|||||||
|
|
||||||
export default (...append) => {
|
export default (...append) => {
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
|
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
|
||||||
|
|
||||||
/* eslint-enable no-console */
|
/* eslint-enable no-console */
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,81 +1,81 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import _ from 'lodash';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import _ from 'lodash';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
|
|
||||||
const hasIndex = (directoryPath) => {
|
const hasIndex = (directoryPath) => {
|
||||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.statSync(indexPath);
|
fs.statSync(indexPath);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasNoExtension = (fileName) => {
|
const hasNoExtension = (fileName) => {
|
||||||
const matches = fileName.match(/\./g);
|
const matches = fileName.match(/\./g);
|
||||||
|
|
||||||
return !matches;
|
return !matches;
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasMultipleExtensions = (fileName) => {
|
const hasMultipleExtensions = (fileName) => {
|
||||||
const matches = fileName.match(/\./g);
|
const matches = fileName.match(/\./g);
|
||||||
|
|
||||||
return matches && matches.length > 1;
|
return matches && matches.length > 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isSafeName = (fileName) => {
|
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 removeDuplicates = (files) => {
|
||||||
return _.filter(files, (fileName) => {
|
return _.filter(files, (fileName) => {
|
||||||
return !_.includes(files, fileName + '.js');
|
return !_.includes(files, fileName + '.js');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (directoryPath) => {
|
export default (directoryPath) => {
|
||||||
let children;
|
let children;
|
||||||
|
|
||||||
validateTargetDirectory(directoryPath);
|
validateTargetDirectory(directoryPath);
|
||||||
|
|
||||||
children = fs.readdirSync(directoryPath);
|
children = fs.readdirSync(directoryPath);
|
||||||
|
|
||||||
children = _.filter(children, (fileName) => {
|
children = _.filter(children, (fileName) => {
|
||||||
const absolutePath = path.resolve(directoryPath, fileName);
|
const absolutePath = path.resolve(directoryPath, fileName);
|
||||||
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||||
|
|
||||||
if (!isSafeName(fileName)) {
|
if (!isSafeName(fileName)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasNoExtension(fileName) && !isDirectory) {
|
if (hasNoExtension(fileName) && !isDirectory) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasMultipleExtensions(fileName)) {
|
if (hasMultipleExtensions(fileName)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_.startsWith(fileName, 'index.js')) {
|
if (_.startsWith(fileName, 'index.js')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDirectory && !hasIndex(absolutePath)) {
|
if (isDirectory && !hasIndex(absolutePath)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
children = removeDuplicates(children);
|
children = removeDuplicates(children);
|
||||||
|
|
||||||
return children.sort();
|
return children.sort();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
export default (paths) => {
|
export default (paths) => {
|
||||||
return _.sortBy(paths, (path) => {
|
return _.sortBy(paths, (path) => {
|
||||||
return -path.split('/').length;
|
return -path.split('/').length;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,33 +1,33 @@
|
|||||||
import _ from 'lodash';
|
|
||||||
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) => {
|
||||||
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.');
|
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
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');
|
||||||
|
|
||||||
try {
|
|
||||||
fs.statSync(indexFilePath);
|
|
||||||
} catch (error) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
|
||||||
|
|
||||||
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
|
||||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.statSync(indexFilePath);
|
||||||
|
} catch (error) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|
||||||
|
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
||||||
|
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|||||||
+12
-12
@@ -1,23 +1,23 @@
|
|||||||
import _ from 'lodash';
|
|
||||||
import createIndexCode from './createIndexCode';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import createIndexCode from './createIndexCode';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
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) => {
|
||||||
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
validateTargetDirectory(directoryPath);
|
validateTargetDirectory(directoryPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
const siblings = readDirectory(directoryPath);
|
const siblings = readDirectory(directoryPath);
|
||||||
const indexCode = createIndexCode(siblings);
|
const indexCode = createIndexCode(siblings);
|
||||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
fs.writeFileSync(indexFilePath, indexCode);
|
fs.writeFileSync(indexFilePath, indexCode);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,62 +1,64 @@
|
|||||||
import _ from 'lodash';
|
|
||||||
import createIndexCode from './createIndexCode';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
import createIndexCode from './createIndexCode';
|
||||||
import validateTargetDirectory from './validateTargetDirectory';
|
import validateTargetDirectory from './validateTargetDirectory';
|
||||||
import readDirectory from './readDirectory';
|
import readDirectory from './readDirectory';
|
||||||
import sortByDepth from './sortByDepth';
|
import sortByDepth from './sortByDepth';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
import findIndexFiles from './findIndexFiles';
|
import findIndexFiles from './findIndexFiles';
|
||||||
import chalk from 'chalk';
|
|
||||||
|
|
||||||
export default (directoryPaths, options = {}) => {
|
export default (directoryPaths, options = {}) => {
|
||||||
let sortedDirectoryPaths;
|
let sortedDirectoryPaths;
|
||||||
|
|
||||||
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'));
|
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||||
|
|
||||||
if (options.updateIndex) {
|
if (options.updateIndex) {
|
||||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
||||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||||
|
|
||||||
|
log('Found index file in:', sortedDirectoryPaths);
|
||||||
|
}
|
||||||
|
|
||||||
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
|
validateTargetDirectory(directoryPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||||
|
let existingIndexCode;
|
||||||
|
|
||||||
|
const siblings = readDirectory(directoryPath);
|
||||||
|
|
||||||
|
const indexCode = createIndexCode(siblings);
|
||||||
|
|
||||||
|
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||||
|
|
||||||
|
try {
|
||||||
|
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|
||||||
|
/* eslint-disable no-empty */
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
log('Found index file in:', sortedDirectoryPaths);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
|
||||||
validateTargetDirectory(directoryPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
|
||||||
let existingIndexCode;
|
|
||||||
|
|
||||||
const siblings = readDirectory(directoryPath);
|
|
||||||
|
|
||||||
const indexCode = createIndexCode(siblings);
|
|
||||||
|
|
||||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
|
||||||
|
|
||||||
try {
|
|
||||||
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
|
||||||
/* eslint-disable no-empty */
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
|
||||||
/* eslint-enable no-empty */
|
/* eslint-enable no-empty */
|
||||||
|
|
||||||
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 have 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 {
|
||||||
log(indexFilePath, chalk.green('[created index]'));
|
log(indexFilePath, chalk.green('[created index]'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
log('Done');
|
log('Done');
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
export default (code) => {
|
export default (code) => {
|
||||||
return _.trim(code) + '\n\n';
|
return _.trim(code) + '\n\n';
|
||||||
};
|
};
|
||||||
|
|||||||
+23
-25
@@ -3,59 +3,57 @@
|
|||||||
import {
|
import {
|
||||||
expect
|
expect
|
||||||
} from 'chai';
|
} from 'chai';
|
||||||
|
import createIndexCode from '../src/utilities/createIndexCode';
|
||||||
import createIndexCode from './../src/utilities/createIndexCode';
|
|
||||||
|
|
||||||
import codeExample from './codeExample';
|
import codeExample from './codeExample';
|
||||||
|
|
||||||
describe('createIndexCode()', () => {
|
describe('createIndexCode()', () => {
|
||||||
it('describes no children', () => {
|
it('describes no children', () => {
|
||||||
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 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 bar from './bar';
|
||||||
export foo from './foo';
|
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', () => {
|
||||||
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 foo from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
context('multiple, unsorted', () => {
|
});
|
||||||
it('sorts the files', () => {
|
context('multiple, unsorted', () => {
|
||||||
const indexCode = createIndexCode(['foo', 'bar']);
|
it('sorts the files', () => {
|
||||||
|
const indexCode = createIndexCode(['foo', 'bar']);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
'create index';
|
||||||
|
|
||||||
export bar from './bar';
|
export bar from './bar';
|
||||||
export foo from './foo';
|
export foo from './foo';
|
||||||
`));
|
`));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+9
-10
@@ -1,20 +1,19 @@
|
|||||||
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
expect
|
expect
|
||||||
} from 'chai';
|
} from 'chai';
|
||||||
|
|
||||||
import path from 'path';
|
|
||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
import findIndexFiles from './../src/utilities/findIndexFiles';
|
import findIndexFiles from '../src/utilities/findIndexFiles';
|
||||||
|
|
||||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/find-index-files');
|
const fixturesPath = path.resolve(__dirname, '../../fixtures/find-index-files');
|
||||||
|
|
||||||
describe('findIndexFiles()', () => {
|
describe('findIndexFiles()', () => {
|
||||||
it('finds only the directories that have an existing valid index file', () => {
|
it('finds only the directories that have an existing valid index file', () => {
|
||||||
let names;
|
let names;
|
||||||
|
|
||||||
names = findIndexFiles(path.resolve(fixturesPath));
|
names = findIndexFiles(path.resolve(fixturesPath));
|
||||||
names = names.sort();
|
names = names.sort();
|
||||||
|
|
||||||
expect(names).to.deep.equal(glob.sync(path.resolve(fixturesPath, './**/find-*')));
|
expect(names).to.deep.equal(glob.sync(path.resolve(fixturesPath, './**/find-*')));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+48
-49
@@ -1,74 +1,73 @@
|
|||||||
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
expect
|
expect
|
||||||
} from 'chai';
|
} from 'chai';
|
||||||
|
import readDirectory from '../src/utilities/readDirectory';
|
||||||
|
|
||||||
import path from 'path';
|
const fixturesPath = path.resolve(__dirname, '../fixtures/read-directory');
|
||||||
import readDirectory from './../src/utilities/readDirectory';
|
|
||||||
|
|
||||||
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', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories'));
|
const 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', () => {
|
});
|
||||||
it('gets names of the children directories', () => {
|
context('target directory contains child directories that do not contain index', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-without-index'));
|
it('gets names of the children directories', () => {
|
||||||
|
const 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)', () => {
|
});
|
||||||
it('gets names of the children directories', () => {
|
context('target directory contains child directories (unsafe name)', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name'));
|
it('gets names of the children directories', () => {
|
||||||
|
const 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', () => {
|
});
|
||||||
it('does not include ./index.js', () => {
|
context('target directory contains ./index.js', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-index'));
|
it('does not include ./index.js', () => {
|
||||||
|
const 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', () => {
|
});
|
||||||
it('refers to the files (with extension)', () => {
|
context('target directory contains files', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files'));
|
it('refers to the files (with extension)', () => {
|
||||||
|
const 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', () => {
|
});
|
||||||
it('ignores files', () => {
|
context('target directory contains dot files', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-dot-files'));
|
it('ignores files', () => {
|
||||||
|
const 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', () => {
|
});
|
||||||
it('ignores files', () => {
|
context('target directory contains files with no extension', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension'));
|
it('ignores files', () => {
|
||||||
|
const 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', () => {
|
});
|
||||||
it('ignores files', () => {
|
context('target directory contains files with multiple extensions', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-files-multiple-extensions'));
|
it('ignores files', () => {
|
||||||
|
const 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', () => {
|
});
|
||||||
it('prefers file', () => {
|
context('target directory contains directories and files with the same name', () => {
|
||||||
const names = readDirectory(path.resolve(fixturesPath, 'children-directories-and-files'));
|
it('prefers file', () => {
|
||||||
|
const 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']);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+10
-10
@@ -4,16 +4,16 @@ import {
|
|||||||
import sortByDepth from './../src/utilities/sortByDepth';
|
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', () => {
|
||||||
const paths = [
|
const paths = [
|
||||||
'/b',
|
'/b',
|
||||||
'/a',
|
'/a',
|
||||||
'/a/b/c',
|
'/a/b/c',
|
||||||
'/a/b'
|
'/a/b'
|
||||||
];
|
];
|
||||||
|
|
||||||
const 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']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,47 +1,47 @@
|
|||||||
/* eslint-disable max-nested-callbacks */
|
/* eslint-disable max-nested-callbacks */
|
||||||
|
|
||||||
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
expect
|
expect
|
||||||
} from 'chai';
|
} from 'chai';
|
||||||
import path from 'path';
|
import validateTargetDirectory from '../src/utilities/validateTargetDirectory';
|
||||||
import validateTargetDirectory from './../src/utilities/validateTargetDirectory';
|
|
||||||
|
|
||||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/validate-target-directory');
|
const fixturesPath = path.resolve(__dirname, '../fixtures/validate-target-directory');
|
||||||
|
|
||||||
describe('validateTargetDirectory()', () => {
|
describe('validateTargetDirectory()', () => {
|
||||||
describe('directory path', () => {
|
describe('directory path', () => {
|
||||||
context('refers to a directory that does not exist', () => {
|
context('refers to a directory that does not exist', () => {
|
||||||
it('throws an error', () => {
|
it('throws an error', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
validateTargetDirectory(path.resolve(fixturesPath, 'does-not-exist'));
|
validateTargetDirectory(path.resolve(fixturesPath, 'does-not-exist'));
|
||||||
}).to.throw(Error, 'Directory "' + path.resolve(fixturesPath, 'does-not-exist') + '" does not exist.');
|
}).to.throw(Error, 'Directory "' + path.resolve(fixturesPath, 'does-not-exist') + '" does not exist.');
|
||||||
});
|
});
|
||||||
});
|
|
||||||
context('refers to a file', () => {
|
|
||||||
it('throws an error', () => {
|
|
||||||
expect(() => {
|
|
||||||
validateTargetDirectory(path.resolve(fixturesPath, 'not-a-directory.js'));
|
|
||||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'not-a-directory.js') + '" is not a directory.');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
describe('target index', () => {
|
context('refers to a file', () => {
|
||||||
context('no index', () => {
|
it('throws an error', () => {
|
||||||
it('returns true', () => {
|
expect(() => {
|
||||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'no-index'))).to.equal(true);
|
validateTargetDirectory(path.resolve(fixturesPath, 'not-a-directory.js'));
|
||||||
});
|
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'not-a-directory.js') + '" is not a directory.');
|
||||||
});
|
});
|
||||||
context('safe', () => {
|
|
||||||
it('returns true', () => {
|
|
||||||
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
context('unsafe', () => {
|
|
||||||
it('throws an error', () => {
|
|
||||||
expect(() => {
|
|
||||||
validateTargetDirectory(path.resolve(fixturesPath, 'unsafe-index'));
|
|
||||||
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
describe('target index', () => {
|
||||||
|
context('no index', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'no-index'))).to.equal(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('safe', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
expect(validateTargetDirectory(path.resolve(fixturesPath, 'safe-index'))).to.equal(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
context('unsafe', () => {
|
||||||
|
it('throws an error', () => {
|
||||||
|
expect(() => {
|
||||||
|
validateTargetDirectory(path.resolve(fixturesPath, 'unsafe-index'));
|
||||||
|
}).to.throw(Error, '"' + path.resolve(fixturesPath, 'unsafe-index/index.js') + '" unsafe index.');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+16
-16
@@ -1,36 +1,36 @@
|
|||||||
/* eslint-disable no-restricted-syntax */
|
/* eslint-disable no-restricted-syntax */
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
expect
|
expect
|
||||||
} from 'chai';
|
} from 'chai';
|
||||||
import fs from 'fs';
|
import writeIndex from '../src/utilities/writeIndex';
|
||||||
import path from 'path';
|
|
||||||
import codeExample from './codeExample';
|
import codeExample from './codeExample';
|
||||||
import writeIndex from './../src/utilities/writeIndex';
|
|
||||||
|
|
||||||
const fixturesPath = path.resolve(__dirname, './../../fixtures/write-index');
|
const fixturesPath = path.resolve(__dirname, '../fixtures/write-index');
|
||||||
|
|
||||||
describe('writeIndex()', () => {
|
describe('writeIndex()', () => {
|
||||||
it('creates index in target directory', () => {
|
it('creates index in target directory', () => {
|
||||||
const indexFilePath = path.resolve(fixturesPath, './mixed/index.js');
|
const indexFilePath = path.resolve(fixturesPath, './mixed/index.js');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.unlinkSync(indexFilePath);
|
fs.unlinkSync(indexFilePath);
|
||||||
/* eslint-disable no-empty */
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
// eslint-disable-next-line no-empty
|
||||||
/* eslint-enable no-empty */
|
} catch (error) {
|
||||||
|
|
||||||
writeIndex([path.resolve(fixturesPath, './mixed')]);
|
}
|
||||||
|
|
||||||
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
writeIndex([path.resolve(fixturesPath, './mixed')]);
|
||||||
|
|
||||||
expect(indexCode).to.equal(codeExample(`
|
const indexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||||
|
|
||||||
|
expect(indexCode).to.equal(codeExample(`
|
||||||
'create index';
|
'create index';
|
||||||
|
|
||||||
export bar from './bar';
|
export bar from './bar';
|
||||||
export foo from './foo.js';
|
export foo from './foo.js';
|
||||||
`));
|
`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user