-
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.
- Loading branch information
1 parent
32f0105
commit 036eb74
Showing
8 changed files
with
279 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import {requires, entry} from './utils'; | ||
|
||
export class Schema { | ||
constructor(params) { | ||
this.params = params; | ||
requires(params.require); | ||
this.entry = entry(params.entry); | ||
} | ||
|
||
generate() { | ||
return this.params; | ||
const documents = {}; | ||
this.entry(documents); | ||
return documents; | ||
} | ||
} |
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,30 @@ | ||
import {resolve} from 'path'; | ||
|
||
function resolveRequire(path) { | ||
if (path.startsWith('.')) { | ||
return resolve(path); | ||
} | ||
return path; | ||
} | ||
|
||
export function requires(paths, req = require) { | ||
if (paths) { | ||
if (typeof paths === 'string') { | ||
req(resolveRequire(paths)); | ||
} else { | ||
paths.forEach((path) => { | ||
req(resolveRequire(path)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export function entry(entry, req = require) { | ||
if (typeof entry === 'object') { | ||
if (entry.export) { | ||
return req(resolveRequire(entry.path))[entry.export]; | ||
} | ||
return req(resolveRequire(entry.path)); | ||
} | ||
return req(resolveRequire(entry)); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import chai from 'chai'; | ||
import sinonChai from 'sinon-chai'; | ||
chai.use(sinonChai); | ||
chai.should(); |
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,2 @@ | ||
export default (documents) => { | ||
}; |
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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import {Schema} from '../../src'; | ||
|
||
const CONFIG = {}; | ||
const CONFIG = { | ||
entry: { | ||
path: './test/scenarios/basic', | ||
export: 'default', | ||
}, | ||
require: [ | ||
'@babel/register', | ||
], | ||
}; | ||
|
||
describe('Schema', () => { | ||
it('should pass', () => { | ||
const schema = new Schema(CONFIG); | ||
schema.generate().should.eql(CONFIG); | ||
schema.generate().should.eql({}); | ||
}); | ||
}); |
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,106 @@ | ||
import _ from 'lodash'; | ||
import sinon from 'sinon'; | ||
import {resolve} from 'path'; | ||
|
||
import {entry, requires} from '../../src/utils'; | ||
|
||
const RELATIVE_PATH = './name'; | ||
const NODE_MODULES_PATH = 'name'; | ||
const EXPORT_NAME = 'export name'; | ||
const EXPORT = 'export'; | ||
const ENTRY = { | ||
[EXPORT_NAME]: EXPORT, | ||
}; | ||
|
||
describe('utils', () => { | ||
describe('entry', () => { | ||
const variations = { | ||
'with a string': { | ||
'should support a relative path': { | ||
entry: RELATIVE_PATH, | ||
ret: ENTRY, | ||
req: resolve(RELATIVE_PATH), | ||
}, | ||
'should support a node_modules path': { | ||
entry: NODE_MODULES_PATH, | ||
ret: ENTRY, | ||
req: NODE_MODULES_PATH, | ||
}, | ||
}, | ||
'with an object': { | ||
'should support a relative path': { | ||
entry: {path: RELATIVE_PATH}, | ||
ret: ENTRY, | ||
req: resolve(RELATIVE_PATH), | ||
}, | ||
'should support a node_modules path': { | ||
entry: {path: NODE_MODULES_PATH}, | ||
ret: ENTRY, | ||
req: NODE_MODULES_PATH, | ||
}, | ||
}, | ||
'with an object and export': { | ||
'should support a relative path': { | ||
entry: {path: RELATIVE_PATH, export: EXPORT_NAME}, | ||
ret: EXPORT, | ||
req: resolve(RELATIVE_PATH), | ||
}, | ||
'should support a node_modules path': { | ||
entry: {path: NODE_MODULES_PATH, export: EXPORT_NAME}, | ||
ret: EXPORT, | ||
req: NODE_MODULES_PATH, | ||
}, | ||
}, | ||
}; | ||
|
||
_.forEach(variations, (variation, name) => { | ||
describe(name, () => { | ||
_.forEach(variation, (test, name) => { | ||
it(name, () => { | ||
const req = sinon.spy(() => ENTRY); | ||
const ret = entry(test.entry, req); | ||
req.should.have.been.calledOnce; | ||
req.should.have.been.calledWith(test.req); | ||
ret.should.eql(test.ret); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('requires', () => { | ||
describe('when undefined', () => { | ||
it('should not require anything', () => { | ||
const req = sinon.spy(); | ||
requires(undefined, req); | ||
req.should.not.have.been.called; | ||
}); | ||
}); | ||
|
||
describe('when a string', () => { | ||
it('should support a relative path', () => { | ||
const req = sinon.spy(); | ||
requires(RELATIVE_PATH, req); | ||
req.should.have.been.calledOnce; | ||
req.should.have.been.calledWith(resolve(RELATIVE_PATH)); | ||
}); | ||
|
||
it('should support a node_modules path', () => { | ||
const req = sinon.spy(); | ||
requires(NODE_MODULES_PATH, req); | ||
req.should.have.been.calledOnce; | ||
req.should.have.been.calledWith(NODE_MODULES_PATH); | ||
}); | ||
}); | ||
|
||
describe('when an array', () => { | ||
it('should require all', () => { | ||
const req = sinon.spy(); | ||
requires([RELATIVE_PATH, NODE_MODULES_PATH], req); | ||
req.should.have.been.calledTwice; | ||
req.should.have.been.calledWith(resolve(RELATIVE_PATH)); | ||
req.should.have.been.calledWith(NODE_MODULES_PATH); | ||
}); | ||
}); | ||
}); | ||
}); |