Skip to content

Commit fe78d26

Browse files
committed
feat: add-state-machine solicits form to use with the machine
1 parent b7e4bff commit fe78d26

2 files changed

Lines changed: 55 additions & 17 deletions

File tree

lib/actions/add-state-machine/index.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ async function addStateMachine (options) {
1212
if (!blueprint) return
1313

1414
const stateMachine = await selectMachine()
15+
if (!stateMachine) return
1516
const form = await selectForm(blueprint)
17+
if (!form) return
1618
const model = await selectModel(form, blueprint)
19+
if (!model) return
1720

1821
const scaffold = blueprint.scaffold
1922
const filename = scaffold.makeStateMachine({
2023
stateMachine: stateMachine,
2124
namespace: blueprint.namespace,
2225
modelName: model,
23-
formName: form
26+
formName: form.name
2427
})
25-
console.log(c.bold(`\nCreating ${c.cyan(filename)}`))
28+
console.log(c.bold(`\nCreating ${c.cyan(filename)} state machine`))
2629
await scaffold.commit()
2730
} // addStateMachine
2831

@@ -33,14 +36,34 @@ async function selectMachine () {
3336
choices: Scaffold.StateMachines()
3437
})
3538
return machine
36-
}
39+
} // selectMachine
3740

38-
function selectForm (blueprint) {
39-
return 'pizza-editing-form'
40-
}
41+
async function selectForm (blueprint) {
42+
const forms = blueprint.forms()
43+
44+
if (forms.length === 0) {
45+
console.log(c.bold.red('Blueprint has no models to scaffold a form against'))
46+
return
47+
}
48+
49+
if (forms.length === 1) {
50+
console.log(c.bold(`Blueprint has one form - ${c.cyan(forms[0].name)}`))
51+
return forms[0]
52+
}
53+
54+
const formName = await ask({
55+
type: 'select',
56+
message: 'Update form',
57+
choices: forms.map(m => m.name)
58+
})
59+
60+
return forms.find(m => m.name === formName)
61+
} // selectForm
4162

4263
function selectModel (form, blueprint) {
43-
return 'pizza'
64+
if (form.scaffold && form.scaffold.model) {
65+
return form.scaffold.model
66+
}
4467
}
4568

4669
module.exports = addStateMachine

lib/actions/blueprint.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ class Blueprint {
2727
get namespace () { return this.blueprint.namespace }
2828

2929
models () {
30-
const names = fs.readdirSync(path.join(this.path, 'models'))
31-
.filter(f => f.endsWith('.json'))
32-
.filter(f => this.isFile('models', f))
33-
.map(f => f.replace('.json', ''))
34-
names.sort()
30+
const names = listJsonFiles(this.path, 'models')
3531
return names.map(n => {
3632
const schema = this.modelProperties(n)
3733
return {
@@ -42,6 +38,16 @@ class Blueprint {
4238
})
4339
}
4440

41+
forms () {
42+
const names = listJsonFiles(this.path, 'card-templates')
43+
return names.map(n => {
44+
return {
45+
name: n,
46+
scaffold: fs.readJsonSync(path.join(this.path, 'card-templates', `${n}.json`)).tymlyScaffold
47+
}
48+
})
49+
} // forms
50+
4551
modelProperties (modelName) {
4652
return this.scaffold.loadModel(modelName)
4753
}
@@ -50,13 +56,22 @@ class Blueprint {
5056
return Object.keys(this.scaffold.loadModel(modelName).properties)
5157
}
5258

53-
isFile (...pathElements) {
54-
const fullName = path.join(this.path, ...pathElements)
55-
return fs.statSync(fullName).isFile()
56-
}
57-
5859
get name () { return this.blueprint.name }
5960
get scaffold () { return this.scaffold_ }
6061
}
6162

63+
function listJsonFiles (basepath, directory) {
64+
const names = fs.readdirSync(path.join(basepath, directory))
65+
.filter(f => f.endsWith('.json'))
66+
.filter(f => isFile(basepath, directory, f))
67+
.map(f => f.replace('.json', ''))
68+
names.sort()
69+
return names
70+
} // listJsonFiles
71+
72+
function isFile (...pathElements) {
73+
const fullName = path.join(...pathElements)
74+
return fs.statSync(fullName).isFile()
75+
} // isFile
76+
6277
module.exports = Blueprint

0 commit comments

Comments
 (0)