Skip to content

Commit a91bcc5

Browse files
committed
feat: add-editable supports --model-path option, so you can scaffold a form for a model in another b
1 parent a2c94e5 commit a91bcc5

27 files changed

Lines changed: 943 additions & 11 deletions

File tree

lib/actions/add-editable/index.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,65 @@ const c = require('ansi-colors')
22
const { prompt } = require('enquirer')
33
const Blueprint = require('../blueprint')
44
const validator = require('../../util/validators')
5+
const path = require('path')
6+
const fs = require('fs-extra')
57

68
async function addEditable (options) {
79
const blueprint = Blueprint.load(options)
810
if (!blueprint) return
911

10-
const models = blueprint.models()
11-
12-
if (models.length === 0) {
13-
console.log(c.bold.red('Blueprint has no models to scaffold a form against'))
12+
const model = await selectModel(blueprint, options.modelPath)
13+
if (!model) {
1414
return
1515
}
16-
const model = await selectModel(models)
16+
1717
const fields = await selectFields(model.fields)
1818
const filename = await chooseFilename(`${model.name}-editing-form`)
1919

2020
const scaffold = blueprint.scaffold
2121
scaffold.makeEditable({
2222
modelName: model.name,
23+
modelSchema: model.schema,
2324
fields: fields,
2425
filename: filename
2526
})
2627
await scaffold.commit()
27-
}
28+
} // addEditable
29+
30+
async function selectModel (blueprint, modelPath) {
31+
if (modelPath) {
32+
return loadExternalModel(modelPath)
33+
}
34+
35+
return selectInternalModel(blueprint)
36+
} // selectInternalModel
37+
38+
function loadExternalModel (modelPath) {
39+
if (!fs.pathExistsSync(modelPath)) {
40+
console.log(c.bold.red(`Can not find model at ${c.cyan(modelPath)}`))
41+
}
42+
43+
const name = path.basename(modelPath, path.extname(modelPath))
44+
const schema = fs.readJsonSync(modelPath)
45+
const fields = schema.properties
46+
console.log(c.bold(`Loading model ${c.cyan(name)}`))
47+
return {
48+
name,
49+
schema,
50+
fields
51+
}
52+
} // loadExternalModel
53+
54+
async function selectInternalModel (blueprint) {
55+
const models = blueprint.models()
56+
57+
if (models.length === 0) {
58+
console.log(c.bold.red('Blueprint has no models to scaffold a form against'))
59+
return
60+
}
2861

29-
async function selectModel (models) {
3062
if (models.length === 1) {
63+
console.log(c.bold(`Blueprint has one model - ${c.cyan(models[0].name)}`))
3164
return models[0]
3265
}
3366

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function tymlyCli (argv) {
3838
.command('add-editable')
3939
.description('Scaffold a new form')
4040
.option('-p, --path <path>', 'blueprint directory - defaults to "."')
41+
.option('-m, --model-path <path>', 'path to the model file, useful if you want to add a form for a model that is defined in another blueprint')
4142
.action((options) => {
4243
actions.addEditableAction(options)
4344
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"tymly": "bin/tymly-cli.js"
2424
},
2525
"dependencies": {
26-
"@wmfs/tymly-scaffold": "1.3.0",
26+
"@wmfs/tymly-scaffold": "1.4.0",
2727
"ansi-colors": "3.2.3",
2828
"commander": "2.19.0",
2929
"enquirer": "2.3.0",

test/add-editable-test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env mocha */
22

33
const helpers = require('./test-helpers')
4+
const path = require('path')
45

56
const addEditable = require('../lib/actions').addEditableAction
67

@@ -27,7 +28,16 @@ describe('tymly add-editable', () => {
2728
'set-file-name': [
2829
'Y',
2930
'the-pizza-form.json'
30-
]
31+
],
32+
'path-to-external-model': {
33+
user: [
34+
'Y',
35+
''
36+
],
37+
options: {
38+
'modelPath': path.join(__dirname, 'fixtures/additional-blueprint/models/drinks.json')
39+
}
40+
}
3141
}
3242

3343
const suiteName = 'add-editable'
@@ -37,12 +47,19 @@ describe('tymly add-editable', () => {
3747
})
3848

3949
for (const [name, inputs] of Object.entries(tests)) {
50+
let userInput = inputs
51+
let options = { }
52+
if (!Array.isArray(inputs)) {
53+
userInput = inputs.user
54+
options = inputs.options
55+
}
56+
4057
helpers.runTest(
4158
suiteName,
4259
name,
43-
inputs,
60+
userInput,
4461
addEditable,
45-
{ }
62+
options
4663
)
4764
}
4865
})
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
27+
*.sqlite
28+
29+
## Node ###
30+
~*
31+
.~*
32+
\#*
33+
34+
# Logs
35+
logs
36+
*.log
37+
npm-debug.log*
38+
39+
# Runtime data
40+
pids
41+
*.pid
42+
*.seed
43+
*.pid.lock
44+
45+
# Directory for instrumented libs generated by jscoverage/JSCover
46+
lib-cov
47+
48+
# Coverage directory used by tools like istanbul
49+
coverage
50+
51+
# nyc test coverage
52+
.nyc_output
53+
54+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
55+
.grunt
56+
57+
# node-waf configuration
58+
.lock-wscript
59+
60+
# Compiled binary addons (http://nodejs.org/api/addons.html)
61+
build/Release
62+
63+
# Dependency directories
64+
node_modules
65+
jspm_packages
66+
67+
# Optional npm cache directory
68+
.npm
69+
70+
# Optional eslint cache
71+
.eslintcache
72+
73+
# Optional REPL history
74+
.node_repl_history
75+
76+
# Output of 'npm pack'
77+
*.tgz
78+
79+
# Yarn Integrity file
80+
.yarn-integrity
81+
82+
83+
## IDE things ##
84+
85+
# Webstorm
86+
.idea
87+
88+
# Eclipse:
89+
90+
.project
91+
.metadata
92+
bin/
93+
tmp/
94+
*.tmp
95+
*.bak
96+
*.swp
97+
*~.nib
98+
local.properties
99+
.settings/
100+
.loadpath
101+
.recommenders
102+
103+
104+
scratch.txt
105+
package-lock.json
106+
107+
output/
108+
109+
# Ignore Rush temporary files
110+
/common/temp/**
111+
112+
package-deps.json
113+
114+
.yo-rc.json
115+
116+
lerna-debug.log
117+
118+
TEST-result.xml
119+
.gradle/
120+
.embedpostgresql/
121+
build/
122+
123+
# Windows thumbnail cache files
124+
Thumbs.db
125+
ehthumbs.db
126+
ehthumbs_vista.db
127+
128+
# Dump file
129+
*.stackdump
130+
131+
# Folder config file
132+
[Dd]esktop.ini
133+
134+
# Recycle Bin used on file shares
135+
$RECYCLE.BIN/
136+
137+
# Windows Installer files
138+
*.cab
139+
*.msi
140+
*.msix
141+
*.msm
142+
*.msp
143+
144+
# Windows shortcuts
145+
*.lnk
146+
147+
# OS generated files #
148+
######################
149+
.DS_Store
150+
.DS_Store?
151+
._*
152+
.Spotlight-V100
153+
.Trashes
154+
ehthumbs.db
155+
Thumbs.db
156+
.LSOverride
157+
158+
# Icon must end with two \r
159+
Icon
160+
161+
162+
# Thumbnails
163+
._*
164+
165+
# Files that might appear in the root of a volume
166+
.DocumentRevisions-V100
167+
.fseventsd
168+
.Spotlight-V100
169+
.TemporaryItems
170+
.Trashes
171+
.VolumeIcon.icns
172+
.com.apple.timemachine.donotpresent
173+
174+
# Directories potentially created on remote AFP share
175+
.AppleDB
176+
.AppleDesktop
177+
Network Trash Folder
178+
Temporary Items
179+
.apdisk
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"branch": "master",
3+
"analyzeCommits": {
4+
"preset": "angular",
5+
"releaseRules": [
6+
{
7+
"type": "build",
8+
"scope": "deps",
9+
"release": "minor"
10+
}
11+
]
12+
},
13+
"verifyConditions": [
14+
"@semantic-release/changelog",
15+
"@semantic-release/npm",
16+
"@semantic-release/git"
17+
],
18+
"generateNotes": {
19+
"preset": "metahub"
20+
},
21+
"prepare": [
22+
{
23+
"path": "@semantic-release/changelog",
24+
"changelogFile": "CHANGELOG.md"
25+
},
26+
{
27+
"path": "@semantic-release/git",
28+
"assets": [
29+
"CHANGELOG.md"
30+
]
31+
},
32+
"@semantic-release/npm"
33+
],
34+
"publish": [
35+
"@semantic-release/npm",
36+
"@semantic-release/github"
37+
],
38+
"success": [
39+
"@semantic-release/github"
40+
],
41+
"fail": [
42+
"@semantic-release/github"
43+
]
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: node_js
2+
notifications:
3+
email: false
4+
node_js:
5+
- lts/*
6+
sudo: false
7+
dist: trusty
8+
branches:
9+
only:
10+
- master
11+
env:
12+
global:
13+
- TIMEOUT=15000
14+
install:
15+
- travis_retry npm install
16+
test:
17+
- npm test
18+
before_deploy:
19+
- npm run coverage
20+
deploy:
21+
- provider: script
22+
skip_cleanup: true
23+
script: npx semantic-release
24+
on:
25+
branch: master

0 commit comments

Comments
 (0)