Skip to content

Commit db99b50

Browse files
committed
Fix remove fields
1 parent adeb558 commit db99b50

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

generators/entity/prompts.js

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -167,39 +167,42 @@ function askForFields() {
167167
function askForFieldsToRemove() {
168168
const context = this.context;
169169
// prompt only if data is imported from a file
170-
if (!context.useConfigurationFile || context.updateEntity !== 'remove' || context.fieldNameChoices.length === 0) {
171-
return;
170+
if (!context.useConfigurationFile || context.updateEntity !== 'remove' || this.entityConfig.fields.length === 0) {
171+
return undefined;
172172
}
173-
const done = this.async();
174-
173+
175174
const prompts = [
176-
{
177-
type: 'checkbox',
178-
name: 'fieldsToRemove',
179-
message: 'Please choose the fields you want to remove',
180-
choices: context.fieldNameChoices,
181-
},
182-
{
183-
when: response => response.fieldsToRemove.length !== 0,
184-
type: 'confirm',
185-
name: 'confirmRemove',
186-
message: 'Are you sure to remove these fields?',
187-
default: true,
188-
},
175+
{
176+
type: 'checkbox',
177+
name: 'fieldsToRemove',
178+
message: 'Please choose the fields you want to remove',
179+
choices: () =>
180+
this.entityConfig.fields.map(field => {
181+
return { name: field.fieldName, value: field.fieldName };
182+
}),
183+
},
184+
{
185+
when: response => response.fieldsToRemove.length !== 0,
186+
type: 'confirm',
187+
name: 'confirmRemove',
188+
message: 'Are you sure to remove these fields?',
189+
default: true,
190+
},
189191
];
190-
this.prompt(prompts).then(props => {
191-
if (props.confirmRemove) {
192-
this.log(chalk.red(`\nRemoving fields: ${props.fieldsToRemove}\n`));
193-
for (let i = this.entityConfig.fields.length - 1; i >= 0; i -= 1) {
194-
const field = this.entityConfig.fields[i];
195-
if (props.fieldsToRemove.filter(val => val === field.fieldName).length > 0) {
196-
this.entityConfig.fields.splice(i, 1);
197-
}
198-
}
192+
return this.prompt(prompts).then(props => {
193+
if (props.confirmRemove) {
194+
this.log(chalk.red(`\nRemoving fields: ${props.fieldsToRemove}\n`));
195+
const fields = this.entityConfig.fields;
196+
for (let i = fields.length - 1; i >= 0; i -= 1) {
197+
const field = this.entityConfig.fields[i];
198+
if (props.fieldsToRemove.filter(val => val === field.fieldName).length > 0) {
199+
fields.splice(i, 1);
200+
}
199201
}
200-
done();
202+
this.entityConfig.fields = fields;
203+
}
201204
});
202-
}
205+
}
203206

204207
function askForRelationships() {
205208
const context = this.context;
@@ -219,43 +222,45 @@ function askForRelationships() {
219222
function askForRelationsToRemove() {
220223
const context = this.context;
221224
// prompt only if data is imported from a file
222-
if (!context.useConfigurationFile || context.updateEntity !== 'remove' || context.relNameChoices.length === 0) {
223-
return;
225+
if (!context.useConfigurationFile || context.updateEntity !== 'remove' || this.entityConfig.relationships.length === 0) {
226+
return undefined;
224227
}
225-
/* if (context.databaseType === 'cassandra') {
226-
return;
227-
} */
228-
229-
const done = this.async();
230-
228+
231229
const prompts = [
232-
{
233-
type: 'checkbox',
234-
name: 'relsToRemove',
235-
message: 'Please choose the relationships you want to remove',
236-
choices: context.relNameChoices,
237-
},
238-
{
239-
when: response => response.relsToRemove.length !== 0,
240-
type: 'confirm',
241-
name: 'confirmRemove',
242-
message: 'Are you sure to remove these relationships?',
243-
default: true,
244-
},
230+
{
231+
type: 'checkbox',
232+
name: 'relsToRemove',
233+
message: 'Please choose the relationships you want to remove',
234+
choices: () =>
235+
this.entityConfig.relationships.map(rel => {
236+
return {
237+
name: `${rel.relationshipName}:${rel.relationshipType}`,
238+
value: `${rel.relationshipName}:${rel.relationshipType}`,
239+
};
240+
}),
241+
},
242+
{
243+
when: response => response.relsToRemove.length !== 0,
244+
type: 'confirm',
245+
name: 'confirmRemove',
246+
message: 'Are you sure to remove these relationships?',
247+
default: true,
248+
},
245249
];
246-
this.prompt(prompts).then(props => {
247-
if (props.confirmRemove) {
248-
this.log(chalk.red(`\nRemoving relationships: ${props.relsToRemove}\n`));
249-
for (let i = this.entityConfig.relationships.length - 1; i >= 0; i -= 1) {
250-
const rel = this.entityConfig.relationships[i];
251-
if (props.relsToRemove.filter(val => val === `${rel.relationshipName}:${rel.relationshipType}`).length > 0) {
252-
this.entityConfig.relationships.splice(i, 1);
253-
}
254-
}
250+
return this.prompt(prompts).then(props => {
251+
if (props.confirmRemove) {
252+
this.log(chalk.red(`\nRemoving relationships: ${props.relsToRemove}\n`));
253+
const relationships = this.entityConfig.relationships;
254+
for (let i = relationships.length - 1; i >= 0; i -= 1) {
255+
const rel = relationships[i];
256+
if (props.relsToRemove.filter(val => val === `${rel.relationshipName}:${rel.relationshipType}`).length > 0) {
257+
relationships.splice(i, 1);
258+
}
255259
}
256-
done();
260+
this.entityConfig.relationships = relationships;
261+
}
257262
});
258-
}
263+
}
259264

260265
function askForTableName() {
261266
const context = this.context;

0 commit comments

Comments
 (0)