forked from gajus/create-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- This commit add functionality to ignore files based on provided regexps. - in index.js config must be specified as below // @create-index {"ignore":["/foo.js"]}
- Loading branch information
Showing
19 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/; | ||
|
||
export {CREATE_INDEX_PATTERN}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
export default (directoryPath) => { | ||
const indexPath = path.resolve(directoryPath, 'index.js'); | ||
|
||
try { | ||
fs.statSync(indexPath); | ||
|
||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import hasIndex from './hasIndex'; | ||
import {CREATE_INDEX_PATTERN} from './constants'; | ||
|
||
const isString = (str) => { | ||
return typeof str === 'string'; | ||
}; | ||
|
||
const isEmptyString = (str) => { | ||
return str.length === 0; | ||
}; | ||
|
||
export default (directoryPath) => { | ||
if (!hasIndex(directoryPath)) { | ||
return {}; | ||
} | ||
|
||
const indexPath = path.resolve(directoryPath, 'index.js'); | ||
const indexContents = fs.readFileSync(indexPath, 'utf-8'); | ||
const found = indexContents.match(CREATE_INDEX_PATTERN); | ||
const configLine = isString(found[1]) ? found[1].trim() : ''; | ||
|
||
if (isEmptyString(configLine)) { | ||
return {}; | ||
} | ||
|
||
let config; | ||
|
||
try { | ||
config = JSON.parse(configLine); | ||
} catch (error) { | ||
throw new Error( | ||
'"' + indexPath + '" contains invalid configuration object.\n' + | ||
'Configuration object must be a valid JSON.' | ||
); | ||
} | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// @create-index {"ignore": ["/foo.js$/"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// @create-index {ignore: 'foo'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// @create-index |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @create-index {"ignore":["/bar.js$/"]} | ||
|
||
export { default as foo } from './foo.js'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import path from 'path'; | ||
import { | ||
expect | ||
} from 'chai'; | ||
import readIndexConfig from '../src/utilities/readIndexConfig'; | ||
|
||
const fixtures = { | ||
noIndex: path.resolve(__dirname, 'fixtures/read-index-config/no-index'), | ||
withConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-config'), | ||
withInvalidConfig: path.resolve(__dirname, 'fixtures/read-index-config/with-invalid-config'), | ||
withoutConfig: path.resolve(__dirname, 'fixtures/read-index-config/without-config') | ||
}; | ||
|
||
const expectedValues = { | ||
noIndex: {}, | ||
withConfig: { | ||
ignore: ['/foo.js$/'] | ||
}, | ||
withoutConfig: {} | ||
}; | ||
|
||
describe('readIndexConfig()', () => { | ||
context('When valid config is defined', () => { | ||
it('reads config object', () => { | ||
const config = readIndexConfig(fixtures.withConfig); | ||
|
||
expect(config).to.deep.equal(expectedValues.withConfig); | ||
}); | ||
}); | ||
|
||
context('When invalid config is defined', () => { | ||
it('should throw an error', () => { | ||
const wrappedReadIndexConfig = () => { | ||
readIndexConfig(fixtures.withInvalidConfig); | ||
}; | ||
|
||
expect(wrappedReadIndexConfig).to.throw(/Configuration object must be a valid JSON./); | ||
}); | ||
}); | ||
|
||
context('When config is NOT defined', () => { | ||
it('returns an empty object', () => { | ||
const config = readIndexConfig(fixtures.withoutConfig); | ||
|
||
expect(config).to.deep.equal(expectedValues.withoutConfig); | ||
}); | ||
}); | ||
|
||
context('When index file doesn\'t exist', () => { | ||
it('returns an empty object', () => { | ||
const config = readIndexConfig(fixtures.withoutConfig); | ||
|
||
expect(config).to.deep.equal(expectedValues.noIndex); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.