diff --git a/src/bin/create-index.js b/src/bin/create-index.js index 2848fee..e30586c 100644 --- a/src/bin/create-index.js +++ b/src/bin/create-index.js @@ -3,21 +3,21 @@ import yargs from 'yargs'; import { writeIndexCli -} from './../utilities'; +} from '../utilities'; const argv = yargs - .demand(1) - .options({ - 'update-index': { - 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.', - 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 --update-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.') - .argv; + .demand(1) + .options({ + 'update-index': { + 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.', + 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 --update-index ./src ./tests', 'Finds all create-index index files in the target directories and descending directories. Updates found index files.') + .argv; writeIndexCli(argv._, { - updateIndex: argv.updateIndex + updateIndex: argv.updateIndex }); diff --git a/src/index.js b/src/index.js index 498c0a2..47e3103 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ export { - findIndexFiles, - writeIndex + findIndexFiles, + writeIndex } from './utilities'; diff --git a/src/utilities/createIndexCode.js b/src/utilities/createIndexCode.js index f13fe53..2333230 100644 --- a/src/utilities/createIndexCode.js +++ b/src/utilities/createIndexCode.js @@ -1,37 +1,37 @@ import _ from 'lodash'; const safeVariableName = (fileName) => { - const indexOfDot = fileName.indexOf('.'); + const indexOfDot = fileName.indexOf('.'); - if (indexOfDot === -1) { - return fileName; - } else { - return fileName.slice(0, indexOfDot); - } + if (indexOfDot === -1) { + return fileName; + } else { + return fileName.slice(0, indexOfDot); + } }; const buildExportBlock = (files) => { - let importBlock; + let importBlock; - importBlock = _.map(files, (fileName) => { - return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';'; - }); + importBlock = _.map(files, (fileName) => { + return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';'; + }); - importBlock = importBlock.join('\n'); + importBlock = importBlock.join('\n'); - return importBlock; + return importBlock; }; export default (filePaths) => { - let code; + let code; - code = '\'create index\';\n\n'; + code = '\'create index\';\n\n'; - if (filePaths.length) { - const sortedFilePaths = filePaths.sort(); + if (filePaths.length) { + const sortedFilePaths = filePaths.sort(); - code += buildExportBlock(sortedFilePaths) + '\n\n'; - } + code += buildExportBlock(sortedFilePaths) + '\n\n'; + } - return code; + return code; }; diff --git a/src/utilities/findIndexFiles.js b/src/utilities/findIndexFiles.js index 4e276c2..3ca96f6 100644 --- a/src/utilities/findIndexFiles.js +++ b/src/utilities/findIndexFiles.js @@ -1,28 +1,28 @@ +import path from 'path'; import _ from 'lodash'; import glob from 'glob'; -import path from 'path'; import validateTargetDirectory from './validateTargetDirectory'; 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) => { - try { - validateTargetDirectory(path.dirname(targetDirectoryPath)); + targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => { + try { + validateTargetDirectory(path.dirname(targetDirectoryPath)); - return true; - /* eslint-disable no-empty */ - } catch (error) { + return true; - } - /* eslint-enable no-empty */ + // eslint-disable-next-line no-empty + } catch (error) { - return false; - }); + } - targetDirectories = _.map(targetDirectories, path.dirname); + return false; + }); - return targetDirectories; + targetDirectories = _.map(targetDirectories, path.dirname); + + return targetDirectories; }; diff --git a/src/utilities/log.js b/src/utilities/log.js index 3c9ffec..97b86d0 100644 --- a/src/utilities/log.js +++ b/src/utilities/log.js @@ -3,6 +3,7 @@ import moment from 'moment'; export default (...append) => { /* 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 */ }; diff --git a/src/utilities/readDirectory.js b/src/utilities/readDirectory.js index d19766c..caee3bc 100644 --- a/src/utilities/readDirectory.js +++ b/src/utilities/readDirectory.js @@ -1,81 +1,81 @@ import fs from 'fs'; -import _ from 'lodash'; import path from 'path'; +import _ from 'lodash'; import validateTargetDirectory from './validateTargetDirectory'; const hasIndex = (directoryPath) => { - const indexPath = path.resolve(directoryPath, 'index.js'); + const indexPath = path.resolve(directoryPath, 'index.js'); - try { - fs.statSync(indexPath); + try { + fs.statSync(indexPath); - return true; - } catch (error) { - return false; - } + return true; + } catch (error) { + return false; + } }; const hasNoExtension = (fileName) => { - const matches = fileName.match(/\./g); + const matches = fileName.match(/\./g); - return !matches; + return !matches; }; 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) => { - return /^[a-z][a-z0-9\._]+$/i.test(fileName); + return /^[a-z][a-z0-9._]+$/i.test(fileName); }; const removeDuplicates = (files) => { - return _.filter(files, (fileName) => { - return !_.includes(files, fileName + '.js'); - }); + return _.filter(files, (fileName) => { + return !_.includes(files, fileName + '.js'); + }); }; export default (directoryPath) => { - let children; + let children; - validateTargetDirectory(directoryPath); + validateTargetDirectory(directoryPath); - children = fs.readdirSync(directoryPath); + children = fs.readdirSync(directoryPath); - children = _.filter(children, (fileName) => { - const absolutePath = path.resolve(directoryPath, fileName); - const isDirectory = fs.statSync(absolutePath).isDirectory(); + children = _.filter(children, (fileName) => { + const absolutePath = path.resolve(directoryPath, fileName); + const isDirectory = fs.statSync(absolutePath).isDirectory(); - if (!isSafeName(fileName)) { - return false; - } + if (!isSafeName(fileName)) { + return false; + } - if (hasNoExtension(fileName) && !isDirectory) { - return false; - } + if (hasNoExtension(fileName) && !isDirectory) { + return false; + } - if (hasMultipleExtensions(fileName)) { - return false; - } + if (hasMultipleExtensions(fileName)) { + return false; + } - if (_.startsWith(fileName, 'index.js')) { - return false; - } + if (_.startsWith(fileName, 'index.js')) { + return false; + } - if (!isDirectory && !_.endsWith(fileName, '.js')) { - return false; - } + if (!isDirectory && !_.endsWith(fileName, '.js')) { + return false; + } - if (isDirectory && !hasIndex(absolutePath)) { - return false; - } + if (isDirectory && !hasIndex(absolutePath)) { + return false; + } - return true; - }); + return true; + }); - children = removeDuplicates(children); + children = removeDuplicates(children); - return children.sort(); + return children.sort(); }; diff --git a/src/utilities/sortByDepth.js b/src/utilities/sortByDepth.js index dc7b36e..5b1af92 100644 --- a/src/utilities/sortByDepth.js +++ b/src/utilities/sortByDepth.js @@ -1,7 +1,7 @@ import _ from 'lodash'; export default (paths) => { - return _.sortBy(paths, (path) => { - return -path.split('/').length; - }); + return _.sortBy(paths, (path) => { + return -path.split('/').length; + }); }; diff --git a/src/utilities/validateTargetDirectory.js b/src/utilities/validateTargetDirectory.js index f6c7d75..7304ad3 100644 --- a/src/utilities/validateTargetDirectory.js +++ b/src/utilities/validateTargetDirectory.js @@ -1,33 +1,33 @@ -import _ from 'lodash'; import fs from 'fs'; import path from 'path'; +import _ from 'lodash'; export default (targetDirectory) => { - let stats; + let stats; - try { - stats = fs.statSync(targetDirectory); - } catch (error) { - throw new Error('Directory "' + targetDirectory + '" does not exist.'); - } + try { + stats = fs.statSync(targetDirectory); + } catch (error) { + throw new Error('Directory "' + targetDirectory + '" does not exist.'); + } - if (!stats.isDirectory()) { - throw new Error('"' + targetDirectory + '" is not a directory.'); - } + if (!stats.isDirectory()) { + throw new Error('"' + targetDirectory + '" is not a directory.'); + } - 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.'); - } + 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.'); + } + + return true; }; diff --git a/src/utilities/writeIndex.js b/src/utilities/writeIndex.js index dec7ba2..9906a15 100644 --- a/src/utilities/writeIndex.js +++ b/src/utilities/writeIndex.js @@ -1,23 +1,23 @@ -import _ from 'lodash'; -import createIndexCode from './createIndexCode'; import fs from 'fs'; import path from 'path'; +import _ from 'lodash'; +import createIndexCode from './createIndexCode'; import validateTargetDirectory from './validateTargetDirectory'; import readDirectory from './readDirectory'; import sortByDepth from './sortByDepth'; export default (directoryPaths) => { - const sortedDirectoryPaths = sortByDepth(directoryPaths); + const sortedDirectoryPaths = sortByDepth(directoryPaths); - _.forEach(sortedDirectoryPaths, (directoryPath) => { - validateTargetDirectory(directoryPath); - }); + _.forEach(sortedDirectoryPaths, (directoryPath) => { + validateTargetDirectory(directoryPath); + }); - _.forEach(sortedDirectoryPaths, (directoryPath) => { - const siblings = readDirectory(directoryPath); - const indexCode = createIndexCode(siblings); - const indexFilePath = path.resolve(directoryPath, 'index.js'); + _.forEach(sortedDirectoryPaths, (directoryPath) => { + const siblings = readDirectory(directoryPath); + const indexCode = createIndexCode(siblings); + const indexFilePath = path.resolve(directoryPath, 'index.js'); - fs.writeFileSync(indexFilePath, indexCode); - }); + fs.writeFileSync(indexFilePath, indexCode); + }); }; diff --git a/src/utilities/writeIndexCli.js b/src/utilities/writeIndexCli.js index e9e4f95..be82404 100644 --- a/src/utilities/writeIndexCli.js +++ b/src/utilities/writeIndexCli.js @@ -1,62 +1,64 @@ -import _ from 'lodash'; -import createIndexCode from './createIndexCode'; import fs from 'fs'; import path from 'path'; +import _ from 'lodash'; +import chalk from 'chalk'; +import createIndexCode from './createIndexCode'; import validateTargetDirectory from './validateTargetDirectory'; import readDirectory from './readDirectory'; import sortByDepth from './sortByDepth'; import log from './log'; import findIndexFiles from './findIndexFiles'; -import chalk from 'chalk'; export default (directoryPaths, options = {}) => { - let sortedDirectoryPaths; + let sortedDirectoryPaths; - sortedDirectoryPaths = sortByDepth(directoryPaths); + sortedDirectoryPaths = sortByDepth(directoryPaths); - log('Target directories', sortedDirectoryPaths); - log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false')); + log('Target directories', sortedDirectoryPaths); + log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false')); - if (options.updateIndex) { - sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles); - sortedDirectoryPaths = _.flatten(sortedDirectoryPaths); - sortedDirectoryPaths = _.uniq(sortedDirectoryPaths); - sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths); + if (options.updateIndex) { + sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles); + sortedDirectoryPaths = _.flatten(sortedDirectoryPaths); + sortedDirectoryPaths = _.uniq(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 */ - fs.writeFileSync(indexFilePath, indexCode); + fs.writeFileSync(indexFilePath, indexCode); - if (existingIndexCode && existingIndexCode === indexCode) { - log(indexFilePath, chalk.yellow('[index have not changed]')); - } else if (existingIndexCode && existingIndexCode !== indexCode) { - log(indexFilePath, chalk.green('[updated index]')); - } else { - log(indexFilePath, chalk.green('[created index]')); - } - }); + if (existingIndexCode && existingIndexCode === indexCode) { + log(indexFilePath, chalk.yellow('[index have not changed]')); + } else if (existingIndexCode && existingIndexCode !== indexCode) { + log(indexFilePath, chalk.green('[updated index]')); + } else { + log(indexFilePath, chalk.green('[created index]')); + } + }); - log('Done'); + log('Done'); }; diff --git a/tests/codeExample.js b/tests/codeExample.js index c11c5aa..a548cd3 100644 --- a/tests/codeExample.js +++ b/tests/codeExample.js @@ -1,5 +1,5 @@ import _ from 'lodash'; export default (code) => { - return _.trim(code) + '\n\n'; + return _.trim(code) + '\n\n'; }; diff --git a/tests/createIndexCode.js b/tests/createIndexCode.js index 875e64c..f5f650d 100644 --- a/tests/createIndexCode.js +++ b/tests/createIndexCode.js @@ -3,59 +3,57 @@ import { expect } from 'chai'; - -import createIndexCode from './../src/utilities/createIndexCode'; - +import createIndexCode from '../src/utilities/createIndexCode'; import codeExample from './codeExample'; describe('createIndexCode()', () => { - it('describes no children', () => { - const indexCode = createIndexCode([]); + it('describes no children', () => { + const indexCode = createIndexCode([]); - expect(indexCode).to.equal(codeExample(` + expect(indexCode).to.equal(codeExample(` 'create index'; `)); - }); - it('describes a single child', () => { - const indexCode = createIndexCode(['foo']); + }); + it('describes a single child', () => { + const indexCode = createIndexCode(['foo']); - expect(indexCode).to.equal(codeExample(` + expect(indexCode).to.equal(codeExample(` 'create index'; export foo from './foo'; `)); - }); - it('describes multiple children', () => { - const indexCode = createIndexCode(['bar', 'foo']); + }); + it('describes multiple children', () => { + const indexCode = createIndexCode(['bar', 'foo']); - expect(indexCode).to.equal(codeExample(` + expect(indexCode).to.equal(codeExample(` 'create index'; export bar from './bar'; export foo from './foo'; `)); - }); - context('file with extension', () => { - it('removes the extension from the export statement', () => { - const indexCode = createIndexCode(['foo.js']); + }); + context('file with extension', () => { + it('removes the extension from the export statement', () => { + const indexCode = createIndexCode(['foo.js']); - expect(indexCode).to.equal(codeExample(` + expect(indexCode).to.equal(codeExample(` 'create index'; export foo from './foo.js'; `)); - }); }); - context('multiple, unsorted', () => { - it('sorts the files', () => { - const indexCode = createIndexCode(['foo', 'bar']); + }); + context('multiple, unsorted', () => { + it('sorts the files', () => { + const indexCode = createIndexCode(['foo', 'bar']); - expect(indexCode).to.equal(codeExample(` + expect(indexCode).to.equal(codeExample(` 'create index'; export bar from './bar'; export foo from './foo'; `)); - }); }); + }); }); diff --git a/tests/findIndexFiles.js b/tests/findIndexFiles.js index 56445aa..e07240c 100644 --- a/tests/findIndexFiles.js +++ b/tests/findIndexFiles.js @@ -1,20 +1,19 @@ +import path from 'path'; import { expect } from 'chai'; - -import path from 'path'; 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()', () => { - it('finds only the directories that have an existing valid index file', () => { - let names; + it('finds only the directories that have an existing valid index file', () => { + let names; - names = findIndexFiles(path.resolve(fixturesPath)); - names = names.sort(); + names = findIndexFiles(path.resolve(fixturesPath)); + 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-*'))); + }); }); diff --git a/tests/readDirectory.js b/tests/readDirectory.js index 6e4cb0f..fe05930 100644 --- a/tests/readDirectory.js +++ b/tests/readDirectory.js @@ -1,74 +1,73 @@ +import path from 'path'; import { expect } from 'chai'; +import readDirectory from '../src/utilities/readDirectory'; -import path from 'path'; -import readDirectory from './../src/utilities/readDirectory'; - -const fixturesPath = path.resolve(__dirname, './../../fixtures/read-directory'); +const fixturesPath = path.resolve(__dirname, '../fixtures/read-directory'); describe('readDirectory()', () => { - context('target directory contains child directories', () => { - it('gets names of the children directories', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-directories')); + context('target directory contains child directories', () => { + it('gets names of the 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-directories-without-index')); + }); + context('target directory contains child directories that do not contain 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-directories-unsafe-name')); + }); + context('target directory contains child 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-index')); + }); + context('target directory contains ./index.js', () => { + 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)', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-files')); + }); + context('target directory contains 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-dot-files')); + }); + context('target directory contains 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-files-no-extension')); + }); + context('target directory contains files with 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-files-multiple-extensions')); + }); + context('target directory contains files with 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', () => { - const names = readDirectory(path.resolve(fixturesPath, 'children-directories-and-files')); + }); + context('target directory contains directories and files with the same name', () => { + 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']); }); + }); }); diff --git a/tests/sortByDepth.js b/tests/sortByDepth.js index d67240a..64059a0 100644 --- a/tests/sortByDepth.js +++ b/tests/sortByDepth.js @@ -4,16 +4,16 @@ import { import sortByDepth from './../src/utilities/sortByDepth'; describe('sortByDepth()', () => { - it('sorts from deepest to the most shallow', () => { - const paths = [ - '/b', - '/a', - '/a/b/c', - '/a/b' - ]; + it('sorts from deepest to the most shallow', () => { + const paths = [ + '/b', + '/a', + '/a/b/c', + '/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']); + }); }); diff --git a/tests/validateTargetDirectory.js b/tests/validateTargetDirectory.js index c0687fe..8f09dc1 100644 --- a/tests/validateTargetDirectory.js +++ b/tests/validateTargetDirectory.js @@ -1,47 +1,47 @@ /* eslint-disable max-nested-callbacks */ +import path from 'path'; import { expect } 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('directory path', () => { - context('refers to a directory that does not exist', () => { - it('throws an error', () => { - expect(() => { - validateTargetDirectory(path.resolve(fixturesPath, '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('directory path', () => { + context('refers to a directory that does not exist', () => { + it('throws an error', () => { + expect(() => { + validateTargetDirectory(path.resolve(fixturesPath, 'does-not-exist')); + }).to.throw(Error, 'Directory "' + path.resolve(fixturesPath, 'does-not-exist') + '" does not exist.'); + }); }); - 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.'); - }); - }); + 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('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.'); + }); + }); + }); }); diff --git a/tests/writeIndex.js b/tests/writeIndex.js index e428ea8..45a6388 100644 --- a/tests/writeIndex.js +++ b/tests/writeIndex.js @@ -1,36 +1,36 @@ /* eslint-disable no-restricted-syntax */ +import fs from 'fs'; +import path from 'path'; import { expect } from 'chai'; -import fs from 'fs'; -import path from 'path'; +import writeIndex from '../src/utilities/writeIndex'; 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()', () => { - it('creates index in target directory', () => { - const indexFilePath = path.resolve(fixturesPath, './mixed/index.js'); + it('creates index in target directory', () => { + const indexFilePath = path.resolve(fixturesPath, './mixed/index.js'); - try { - fs.unlinkSync(indexFilePath); - /* eslint-disable no-empty */ - } catch (error) { + try { + fs.unlinkSync(indexFilePath); - } - /* eslint-enable no-empty */ + // eslint-disable-next-line 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'; export bar from './bar'; export foo from './foo.js'; `)); - }); + }); });