Skip to content

Commit

Permalink
feat: allow to extend config schema
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jan 6, 2019
1 parent 56ed2df commit d517135
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
7 changes: 7 additions & 0 deletions docs/generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ interface Opts {
cacheIdentifier?: string
/** npm registry */
registry?: string
extendConfigSchema?: (
schema: object,
struct: Struct,
superStruct: SuperStruct
) => void
}
```

Check out [validateSchema.js](https://github.com/saojs/kopy/blob/master/lib/validateConfig.js) for the underlying schema we use to validate the config.

## generator.test

- Type: `(answers: Answers) => Promise<void>`
Expand Down
17 changes: 9 additions & 8 deletions lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const valOrFunc = (val, ctx) =>
module.exports = class Generator {
constructor(config) {
this.opts = {}
this.config = validateConfig(config)
this.logger = logger
this.config = config
this.spinner = spinner
}

Expand Down Expand Up @@ -151,27 +151,28 @@ module.exports = class Generator {

async run(opts = {}) {
this.opts = opts

logger.setOptions({ logLevel: opts.logLevel || 3 })

if (this.config.prepare) {
await this.config.prepare.call(this, this)
const config = validateConfig(this.config, opts.extendConfigSchema)

if (config.prepare) {
await config.prepare.call(this, this)
}

const prompts = valOrFunc(this.config.prompts, this)
const prompts = valOrFunc(config.prompts, this)
if (prompts) {
this.answers = await runPrompts(prompts, this)
}

const actions = valOrFunc(this.config.actions, this)
const actions = valOrFunc(config.actions, this)
if (actions && actions.length > 0) {
for (const action of actions) {
await runAction(action, this)
}
}

if (this.config.completed && !opts.test) {
await this.config.completed.call(this, this)
if (config.completed && !opts.test) {
await config.completed.call(this, this)
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/validateConfig.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const { struct } = require('superstruct')
const { struct, superstruct } = require('superstruct')
const KopyError = require('./KopyError')

module.exports = config => {
module.exports = (config, extendConfigSchema) => {
// TODO: improve prompts and actions validation
const res = struct({
const schema = {
prepare: struct.optional('function'),
prompts: struct.optional(struct.union(['array', 'function'])),
actions: struct.optional(struct.union(['array', 'function'])),
completed: struct.optional('function')
})
}

if (typeof extendConfigSchema === 'function') {
extendConfigSchema(schema, struct, superstruct)
}

const res = struct(schema)

const [error, result] = res.validate(config)
if (error) {
Expand Down

0 comments on commit d517135

Please sign in to comment.