Skip to content

Commit ef9de61

Browse files
committed
feat: tymly add-model <model>
Initial implementation of the add-model command
1 parent d676d53 commit ef9de61

3 files changed

Lines changed: 105 additions & 19 deletions

File tree

lib/actions/add-model/index.js

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const c = require('ansi-colors')
2+
const { prompt } = require('enquirer')
23
const Scaffold = require('@wmfs/tymly-scaffold')
34

45
async function addModelAction (name, options) {
@@ -10,11 +11,102 @@ async function addModelAction (name, options) {
1011
}
1112

1213
const scaffold = new Scaffold()
13-
scaffold.setBlueprint(workingDirectory)
14+
scaffold.setBlueprintDir(workingDirectory)
1415

1516
console.log(c.bold(`Adding ${c.cyan(modelName)} model to blueprint ${c.cyan(scaffold.blueprintName)}`))
1617

18+
const modelDetails = await gatherModelDetails(modelName)
19+
20+
scaffold.addModel(modelDetails)
21+
1722
await scaffold.commit()
1823
} // addModelAction
1924

25+
async function gatherModelDetails (modelName) {
26+
const modelHeader = await gatherModelHeader(modelName)
27+
const modelProperties = await gatherModelProperties()
28+
29+
return {
30+
name: modelHeader.name,
31+
title: modelHeader.title,
32+
description: modelHeader.description,
33+
propertyHints: modelProperties
34+
}
35+
} // gatherModelDetails
36+
37+
async function gatherModelHeader (modelName) {
38+
let modelHeader = {
39+
name: modelName
40+
}
41+
42+
const questions = [
43+
{
44+
type: 'input',
45+
name: 'title',
46+
message: 'Title',
47+
initial: `${modelName[0].toUpperCase()}${modelName.substring(1)}`
48+
},
49+
{
50+
type: 'input',
51+
name: 'description',
52+
message: 'Description',
53+
initial: ''
54+
}
55+
]
56+
57+
for (const question of questions) {
58+
modelHeader = await prompt(question, {}, modelHeader)
59+
}
60+
61+
return modelHeader
62+
} // gatherModelHeader
63+
64+
async function gatherModelProperties () {
65+
const properties = []
66+
for await (const property of gatherModelProperty()) {
67+
properties.push(property)
68+
}
69+
return properties
70+
} // gatherModelProperties
71+
72+
async function * gatherModelProperty () {
73+
while (true) {
74+
let propertyDetails = {}
75+
76+
const questions = [
77+
{
78+
type: 'input',
79+
name: 'key',
80+
message: 'Property name'
81+
},
82+
{
83+
type: 'input',
84+
name: 'typeHint',
85+
message: 'Property type'
86+
},
87+
{
88+
type: 'confirm',
89+
name: 'primary',
90+
message: 'Primary Key',
91+
initial: false
92+
},
93+
{
94+
type: 'confirm',
95+
name: 'required',
96+
message: 'Required',
97+
initial: false
98+
}
99+
]
100+
101+
for (const question of questions) {
102+
propertyDetails = await prompt(question, {}, propertyDetails)
103+
104+
if (!propertyDetails.key) {
105+
return
106+
}
107+
}
108+
yield propertyDetails
109+
}
110+
} // gatherModelProperty
111+
20112
module.exports = addModelAction

lib/actions/new-blueprint/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,12 @@ async function newBlueprint (name, options) {
2222
}
2323

2424
const profile = loadProfile(options.profile)
25-
const blueprintInfo = await solicitDetails(profile)
25+
const blueprintInfo = await solicitDetails(blueprintName, profile)
2626

2727
const scaffold = new Scaffold({
2828
basePath: workingDirectory
2929
})
30-
scaffold.addBlueprint({
31-
name: blueprintName,
32-
description: blueprintInfo.description,
33-
author: blueprintInfo.author,
34-
organisation: blueprintInfo.organisation,
35-
license: blueprintInfo.license,
36-
gitHubOwner: blueprintInfo.gitHubOwner,
37-
npmOrg: blueprintInfo.npmOrg,
38-
semanticVersioning: blueprintInfo.semanticVersioning,
39-
ciProfile: blueprintInfo.ciProfile
40-
})
30+
scaffold.addBlueprint(blueprintInfo)
4131
await scaffold.commit()
4232
} // initAction
4333

@@ -50,8 +40,10 @@ function canCreate (blueprintPath) {
5040
}
5141
}
5242

53-
async function solicitDetails (profile) {
54-
let blueprintInfo = { }
43+
async function solicitDetails (blueprintName, profile) {
44+
let blueprintInfo = {
45+
name: blueprintName
46+
}
5547

5648
const questions = [
5749
{
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
2-
"title": "pizza",
2+
"$schema": "http://json-schema.org/draft-06/schema#",
3+
"title": "Pizza",
34
"description": "A pizza",
4-
"primaryKey": [
5-
"name"
6-
],
5+
"type": "object",
76
"properties": {
87
"name": {
98
"type": "string"
109
}
1110
},
1211
"required": [
1312
"name"
13+
],
14+
"primaryKey": [
15+
"name"
1416
]
1517
}

0 commit comments

Comments
 (0)