Skip to content

Commit c32d850

Browse files
committed
refactor: Moving towards add-search-docs
1 parent dec781f commit c32d850

8 files changed

Lines changed: 150 additions & 71 deletions

File tree

lib/actions/add-editable/index.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ const c = require('ansi-colors')
22
const Blueprint = require('../blueprint')
33
const validator = require('../util/validators')
44
const ask = require('../util/ask')
5-
const selectInternalModel = require('../util/select-model')
5+
const selectModel = require('../util/select-model')
66
const chooseFilename = require('../util/choose-filename')
7-
const path = require('path')
8-
const fs = require('fs-extra')
97
const _ = require('lodash')
108

119
async function addEditable (options) {
@@ -31,28 +29,6 @@ async function addEditable (options) {
3129
await scaffold.commit()
3230
} // addEditable
3331

34-
async function selectModel (blueprint, modelPath) {
35-
const model = modelPath ? loadExternalModel(modelPath) : await selectInternalModel(blueprint)
36-
37-
return model
38-
} // selectInternalModel
39-
40-
function loadExternalModel (modelPath) {
41-
if (!fs.pathExistsSync(modelPath)) {
42-
console.log(c.bold.red(`Can not find model at ${c.cyan(modelPath)}`))
43-
}
44-
45-
const name = path.basename(modelPath, path.extname(modelPath))
46-
const schema = fs.readJsonSync(modelPath)
47-
const fields = Object.keys(schema.properties)
48-
console.log(c.bold(`Loading external model ${c.cyan(name)}`))
49-
return {
50-
name,
51-
schema,
52-
fields
53-
}
54-
} // loadExternalModel
55-
5632
async function selectFields (modelName, propertyNames, properties) {
5733
console.log()
5834
const allProperties = await ask({
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1-
function addSearchDoc (options) {
1+
const c = require('ansi-colors')
2+
const Blueprint = require('../blueprint')
3+
const selectModel = require('../util/select-model')
4+
const chooseCategory = require('../util/choose-category')
5+
const chooseFilename = require('../util/choose-filename')
26

7+
async function addSearchDoc (options) {
8+
const blueprint = Blueprint.load(options)
9+
if (!blueprint) return
10+
11+
const model = await selectModel(blueprint, options.modelPath)
12+
13+
const category = await chooseCategory(blueprint, model.name)
14+
15+
const filename = await chooseFilename(
16+
'Search-doc filename',
17+
`${model.name}`
18+
)
19+
20+
const scaffold = blueprint.scaffold
21+
scaffold.makeSearchDoc({
22+
namespace: blueprint.namespace,
23+
model: model.name,
24+
primaryKeys: model.schema.primaryKey,
25+
title: 'washboard',
26+
description: ['phonic'],
27+
roles: ['$everyone'],
28+
sort: 'label',
29+
category: category,
30+
launches: 'boop',
31+
filename: filename
32+
})
33+
console.log(c.bold(`\nCreating search-doc ${c.cyan(filename)}`))
34+
await scaffold.commit()
335
} // addSearchDoc
436

537
module.exports = addSearchDoc

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const ask = require('../util/ask')
33
const Blueprint = require('../blueprint')
44
const Scaffold = require('@wmfs/tymly-scaffold')
55
const selectInternalModel = require('../util/select-model')
6+
const chooseCategories = require('../util/choose-categories')
67
const chooseFilename = require('../util/choose-filename')
78
const _ = require('lodash')
89

@@ -78,31 +79,4 @@ async function selectModel (form, blueprint) {
7879
})
7980
} // selectModel
8081

81-
async function chooseCategories (blueprint) {
82-
const categories = blueprint.categories()
83-
84-
if (categories.length === 0) {
85-
console.log(c.bold.red('Blueprint has no categories defined'))
86-
return ask({
87-
message: 'Please enter categories',
88-
hint: 'You can enter several categories, separated by commas',
89-
type: 'list'
90-
})
91-
}
92-
93-
if (categories.length === 1) {
94-
console.log(c.bold(`Blueprint has one category - ${c.cyan(categories[0])}`))
95-
return categories
96-
}
97-
98-
const cats = await ask({
99-
type: 'multiselect',
100-
message: 'Select categories',
101-
hint: '(Use <space> to select, <return> to submit)',
102-
choices: categories
103-
})
104-
105-
return cats
106-
} // chooseCategories
107-
10882
module.exports = addStateMachine
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const c = require('ansi-colors')
2+
const ask = require('./ask')
3+
4+
async function chooseCategories (blueprint) {
5+
const categories = blueprint.categories()
6+
7+
if (categories.length === 0) {
8+
console.log(c.bold.red('Blueprint has no categories defined'))
9+
return ask({
10+
message: 'Please enter categories',
11+
hint: 'You can enter several categories, separated by commas',
12+
type: 'list'
13+
})
14+
}
15+
16+
if (categories.length === 1) {
17+
console.log(c.bold(`Blueprint has one category - ${c.cyan(categories[0])}`))
18+
return categories
19+
}
20+
21+
const cats = await ask({
22+
type: 'multiselect',
23+
message: 'Select categories',
24+
hint: '(Use <space> to select, <return> to submit)',
25+
choices: categories
26+
})
27+
28+
return cats
29+
} // chooseCategories
30+
31+
module.exports = chooseCategories
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const c = require('ansi-colors')
2+
const ask = require('./ask')
3+
4+
async function chooseCategory (blueprint, suggestion) {
5+
const categories = blueprint.categories()
6+
7+
if (categories.length === 0) {
8+
console.log(c.bold.red('Blueprint has no categories defined'))
9+
return ask({
10+
message: 'Please a categories',
11+
initial: suggestion
12+
})
13+
}
14+
15+
if (categories.length === 1) {
16+
console.log(c.bold(`Blueprint has one category - ${c.cyan(categories[0])}`))
17+
return categories[0]
18+
}
19+
20+
const cat = await ask({
21+
type: 'select',
22+
message: 'Select a category',
23+
choices: categories
24+
})
25+
26+
return cat
27+
} // chooseCategories
28+
29+
module.exports = chooseCategory
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const ask = require('./ask')
2+
const c = require('ansi-colors')
3+
4+
async function selectInternalModel (blueprint) {
5+
const models = blueprint.models()
6+
7+
if (models.length === 0) {
8+
console.log(c.bold.red('Blueprint has no models to scaffold against'))
9+
return
10+
}
11+
12+
if (models.length === 1) {
13+
console.log(c.bold(`Blueprint has one model - ${c.cyan(models[0].name)}`))
14+
return models[0]
15+
}
16+
17+
const modelName = await ask({
18+
type: 'select',
19+
message: 'Model to scaffold against',
20+
choices: models.map(m => m.name)
21+
})
22+
23+
return models.find(m => m.name === modelName)
24+
} // selectModel
25+
26+
module.exports = selectInternalModel

lib/actions/util/select-model.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
const ask = require('./ask')
21
const c = require('ansi-colors')
2+
const path = require('path')
3+
const fs = require('fs-extra')
4+
const selectInternalModel = require('./select-internal-model')
35

4-
async function selectModel (blueprint) {
5-
const models = blueprint.models()
6+
async function selectModel (blueprint, modelPath) {
7+
const model = modelPath ? loadExternalModel(modelPath) : await selectInternalModel(blueprint)
68

7-
if (models.length === 0) {
8-
console.log(c.bold.red('Blueprint has no models to scaffold against'))
9-
return
10-
}
9+
return model
10+
} // selectInternalModel
1111

12-
if (models.length === 1) {
13-
console.log(c.bold(`Blueprint has one model - ${c.cyan(models[0].name)}`))
14-
return models[0]
12+
function loadExternalModel (modelPath) {
13+
if (!fs.pathExistsSync(modelPath)) {
14+
console.log(c.bold.red(`Can not find model at ${c.cyan(modelPath)}`))
1515
}
1616

17-
const modelName = await ask({
18-
type: 'select',
19-
message: 'Model to scaffold against',
20-
choices: models.map(m => m.name)
21-
})
22-
23-
return models.find(m => m.name === modelName)
24-
} // selectModel
17+
const name = path.basename(modelPath, path.extname(modelPath))
18+
const schema = fs.readJsonSync(modelPath)
19+
const fields = Object.keys(schema.properties)
20+
console.log(c.bold(`Loading external model ${c.cyan(name)}`))
21+
return {
22+
name,
23+
schema,
24+
fields
25+
}
26+
} // loadExternalModel
2527

2628
module.exports = selectModel

lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ function tymlyCli (argv) {
6767
actions.addStateMachine(options)
6868
})
6969

70+
program
71+
.command('add-search-doc')
72+
.description('Scaffold a new search document')
73+
.option('-p, --path <path>', 'blueprint directory - defaults to "."')
74+
.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')
75+
.action((options) => {
76+
actions.addSearchDoc(options)
77+
})
78+
7079
program
7180
.arguments('<command>')
7281
.action((cmd) => {

0 commit comments

Comments
 (0)