Skip to content

Commit 195d0d2

Browse files
committed
feat: Add-Editable solicits edit field title
Suggests title from model schema if available, otherwise generates it from field name
1 parent c0b17d6 commit 195d0d2

8 files changed

Lines changed: 55 additions & 22 deletions

File tree

lib/actions/add-editable/index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Blueprint = require('../blueprint')
44
const validator = require('../../util/validators')
55
const path = require('path')
66
const fs = require('fs-extra')
7+
const _ = require('lodash')
78

89
async function addEditable (options) {
910
const blueprint = Blueprint.load(options)
@@ -14,7 +15,7 @@ async function addEditable (options) {
1415
return
1516
}
1617

17-
const fields = await selectFields(model.fields)
18+
const fields = await selectFields(model.fields, model.schema.properties)
1819
const filename = await chooseFilename(`${model.name}-editing-form`)
1920

2021
const scaffold = blueprint.scaffold
@@ -28,11 +29,9 @@ async function addEditable (options) {
2829
} // addEditable
2930

3031
async function selectModel (blueprint, modelPath) {
31-
if (modelPath) {
32-
return loadExternalModel(modelPath)
33-
}
32+
const model = modelPath ? loadExternalModel(modelPath) : await selectInternalModel(blueprint)
3433

35-
return selectInternalModel(blueprint)
34+
return model
3635
} // selectInternalModel
3736

3837
function loadExternalModel (modelPath) {
@@ -42,8 +41,8 @@ function loadExternalModel (modelPath) {
4241

4342
const name = path.basename(modelPath, path.extname(modelPath))
4443
const schema = fs.readJsonSync(modelPath)
45-
const fields = schema.properties
46-
console.log(c.bold(`Loading model ${c.cyan(name)}`))
44+
const fields = Object.keys(schema.properties)
45+
console.log(c.bold(`Loading external model ${c.cyan(name)}`))
4746
return {
4847
name,
4948
schema,
@@ -74,7 +73,7 @@ async function selectInternalModel (blueprint) {
7473
return models.find(m => m.name === modelName)
7574
} // selectModel
7675

77-
async function selectFields (propertyNames) {
76+
async function selectFields (propertyNames, properties) {
7877
const allProperties = await ask({
7978
type: 'confirm',
8079
name: 'all',
@@ -83,6 +82,7 @@ async function selectFields (propertyNames) {
8382
})
8483

8584
if (allProperties) {
85+
Object.entries(properties).forEach(([p, v]) => v.title = suggestedTitle(p, v.title))
8686
return null
8787
}
8888

@@ -95,11 +95,23 @@ async function selectFields (propertyNames) {
9595
initial: true
9696
})) {
9797
fields.push(property)
98+
properties[property].title = await ask({
99+
type: 'input',
100+
name: 'field',
101+
message: 'Field title',
102+
initial: suggestedTitle(property, properties[property].title),
103+
validate: validator.notEmpty()
104+
})
98105
}
99106
}
100107
return fields
101108
} // selectFields
102109

110+
function suggestedTitle (name, title) {
111+
if (title) return title
112+
return _.upperFirst(_.lowerCase(name))
113+
}
114+
103115
async function chooseFilename (defaultFilename) {
104116
const suffix = '.json'
105117
const filenameQuestion = {

lib/actions/blueprint.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ class Blueprint {
3131
.map(f => f.replace('.json', ''))
3232
names.sort()
3333
return names.map(n => {
34+
const schema = this.modelProperties(n)
3435
return {
3536
name: n,
36-
fields: this.modelPropertyNames(n)
37+
fields: Object.keys(schema.properties),
38+
schema: schema
3739
}
3840
})
3941
}
4042

43+
modelProperties (modelName) {
44+
return this.scaffold.loadModel(modelName)
45+
}
46+
4147
modelPropertyNames (modelName) {
4248
return Object.keys(this.scaffold.loadModel(modelName).properties)
4349
}

test/add-editable-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ describe('tymly add-editable', () => {
2020
helpers.down,
2121
'N',
2222
'',
23+
'', // generated title
2324
'',
25+
'', // use title from model
2426
'N',
2527
'',
26-
''
28+
'Is it vegetarian?', // provide title
29+
'' // take selected filename
2730
],
2831
'set-file-name': [
2932
'Y',

test/fixtures/add-editable/expected/one-model/card-templates/pizza-editing-form.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
{
1212
"id": "code",
13-
"type": "Input.Text"
13+
"type": "Input.Text",
14+
"title": "Code"
1415
},
1516
{
1617
"id": "label",
@@ -19,11 +20,13 @@
1920
},
2021
{
2122
"id": "popularitySeq",
22-
"type": "Input.Number"
23+
"type": "Input.Number",
24+
"title": "Popularity seq"
2325
},
2426
{
2527
"id": "vegetarian",
26-
"type": "Input.Toggle"
28+
"type": "Input.Toggle",
29+
"title": "Vegetarian"
2730
}
2831
],
2932
"actions": [

test/fixtures/add-editable/expected/path-to-external-model/card-templates/drinks-editing-form.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
{
1212
"id": "code",
13-
"type": "Input.Text"
13+
"type": "Input.Text",
14+
"title": "Code"
1415
},
1516
{
1617
"id": "label",

test/fixtures/add-editable/expected/set-file-name/card-templates/the-pizza-form.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
{
1212
"id": "code",
13-
"type": "Input.Text"
13+
"type": "Input.Text",
14+
"title": "Code"
1415
},
1516
{
1617
"id": "label",
@@ -19,11 +20,13 @@
1920
},
2021
{
2122
"id": "popularitySeq",
22-
"type": "Input.Number"
23+
"type": "Input.Number",
24+
"title": "Popularity seq"
2325
},
2426
{
2527
"id": "vegetarian",
26-
"type": "Input.Toggle"
28+
"type": "Input.Toggle",
29+
"title": "Vegetarian"
2730
}
2831
],
2932
"actions": [

test/fixtures/add-editable/expected/two-models-choose-fields/card-templates/pizza-editing-form.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
{
1212
"id": "code",
13-
"type": "Input.Text"
13+
"type": "Input.Text",
14+
"title": "Code"
1415
},
1516
{
1617
"id": "label",
@@ -19,7 +20,8 @@
1920
},
2021
{
2122
"id": "vegetarian",
22-
"type": "Input.Toggle"
23+
"type": "Input.Toggle",
24+
"title": "Is it vegetarian?"
2325
}
2426
],
2527
"actions": [

test/fixtures/add-editable/expected/two-models/card-templates/pizza-editing-form.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
{
1212
"id": "code",
13-
"type": "Input.Text"
13+
"type": "Input.Text",
14+
"title": "Code"
1415
},
1516
{
1617
"id": "label",
@@ -19,11 +20,13 @@
1920
},
2021
{
2122
"id": "popularitySeq",
22-
"type": "Input.Number"
23+
"type": "Input.Number",
24+
"title": "Popularity seq"
2325
},
2426
{
2527
"id": "vegetarian",
26-
"type": "Input.Toggle"
28+
"type": "Input.Toggle",
29+
"title": "Vegetarian"
2730
}
2831
],
2932
"actions": [

0 commit comments

Comments
 (0)