Last active
July 13, 2016 13:43
-
-
Save Guria/e8af1c7172f09fc598912356bc0e34c1 to your computer and use it in GitHub Desktop.
cerebral-view-inquirer :)
This file contains hidden or 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
import controller from './controller.js' | |
import {Computed} from 'cerebral' | |
import {set, copy} from 'cerebral/operators' | |
import inquirer from 'inquirer' | |
controller.addSignals({ | |
pageChanged: [ copy('input:page', 'state:page') ], | |
scaffoldSelected: [ | |
() => console.log('scaffold'), | |
() => { /* services.scaffold(options) */ }, | |
() => process.exit() | |
], | |
aborted: [ () => { process.exit() } ] | |
}) | |
function connect (paths, promptFactory) { | |
return function () { | |
return promptFactory(Object.keys(paths || {}).reduce(function (props, key) { | |
props[key] = paths[key].getDepsMap ? paths[key].get(controller.get()) : controller.get(paths[key]) | |
return props | |
}, { signals: controller.getSignals() })) | |
} | |
} | |
let getModules = Computed({ | |
modules: 'config.modules' | |
}, ({modules}) => { | |
return Object.keys(modules).filter(module => modules[module]) | |
}) | |
let list = connect({ | |
view: 'config.view', | |
model: 'config.model', | |
modules: getModules(), | |
}, function (props) { | |
return { | |
questions: [{ | |
type: 'list', | |
name: 'main', | |
message: 'Project configuration: ', | |
choices: [ | |
new inquirer.Separator(), | |
{ value: 'scaffold', name: 'Create project' }, | |
new inquirer.Separator(), | |
{ value: 'view', name: 'Change view: (' + props.view + ')' }, | |
{ value: 'model', name: 'Change model: (' + props.model + ')' }, | |
{ value: 'modules', name: 'Change modules: (' + props.modules.join(', ') + ')' } | |
] | |
}], | |
then: function (answers) { | |
if (answers.main === 'scaffold') { | |
props.signals.scaffoldSelected() | |
} else { | |
props.signals.pageChanged({ page: answers.main }) | |
} | |
} | |
} | |
}) | |
let app = connect({ | |
page: 'page' | |
}, ({page}) => { | |
if (page === 'start') return list() | |
return { | |
questions: [{ | |
type: 'confirm', | |
name: 'wtf', | |
message: 'Unknown question' | |
}], | |
then: function (answers) { | |
props.signals.aborted() | |
} | |
} | |
}) | |
function render (component) { | |
let { questions, then } = component() | |
inquirer.prompt(questions) | |
.then(then) | |
.then(() => new Promise((resolve) => controller.once('signalEnd', resolve))) | |
.then(() => render(component)) | |
} | |
render(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment